2015-03-19 201 views
3

我有使用logger模拟一些“假”登录脚本:saltstack - 传递变量包含单引号,双引号,空格...到cmd.script?

#!/bin/bash 

{%- set maxretry = salt['pillar.get']('fail2ban:maxretry', 3) %} 

tag="$1" 
message="$2" 

logger -p auth.info "The {{ maxretry + 1 }} \"$tag\" below lines are generated by logger to test Fail2ban" 

for i in $(seq {{ maxretry + 1 }}); do 
    logger -p auth.warning -t "$tag" "$message" 
done 

这就是所谓的宏:

fake_{{ formula }}_login: 
    cmd: 
    - script 
    - source: salt://fail2ban/fake_login.jinja2 
    - template: jinja 
    - args: "{{ tag|default(formula) }} '{{ message }}'" 
    - require: 
     - sls: bash 
     - sls: fail2ban 

事情是{{消息}}可以包含单/双引号,空格,方括号,... 根据cmd.script文档,要传递一个包含空格的字符串,我们必须将其双引号。 但是,如果我有这样的事情:

{{ fail2ban_regex_test('mysql', tag='mysqld', message="150114 3:40:50 [Warning] Access denied for user 'root'@'5.6.7.8' (using password: YES)") }} 

将被记录到系统日志而没有围绕用户/主机的单引号,只是:

mysqld: 150114 3:40:50 [Warning] Access denied for user [email protected] (using password: YES) 

,使的fail2ban未能确认为它与过滤器正则表达式不匹配。

我可以单引号改为双引号,并使用反斜线逃避:

fake_{{ formula }}_login: 
    cmd: 
    - script 
    - source: salt://fail2ban/fake_login.jinja2 
    - template: 
jinja 
    - args: "{{ tag|default(formula) }} \"{{ message|safe }}\"" 

    - require: 
     - sls: bash 
     - sls: fail2ban 

它处理上述情况时,消息只包含单引号。

但是,如果它包含双引号:

{{ fail2ban_regex_test('postfix', tag='postfix/smtpd[20228]', message="NOQUEUE: reject: RCPT from sender.com["5.6.7.8"]: 554 5.7.1 <[email protected]>: Recipient address rejected: Access denied; from=<[email protected]> to=<[email protected]> proto=ESMTP helo=<mg01d1.sender.com>") }} 

我得到这个错误:

local: 
    Data failed to compile: 
---------- 
    Rendering SLS "base:postfix.test" failed: Jinja syntax error: expected token ',', got 'float'; line 29 


--- 
[...] 
- sls: openldap 
- sls: openldap.diamond 
- sls: openldap.nrpe 
{%- endcall %} 


{{ fail2ban_regex_test('postfix', tag='postfix/smtpd[20228]', message="NOQUEUE: reject: RCPT from sender.com["5.6.7.8"]: 554 5.7.1 <[email protected]>: Recipie 
nt address rejected: Access denied; from=<[email protected]> to=<[email protected]> proto=ESMTP helo=<mg01d1.sender.com>") }} <====================== 

如果我试图逃跑用反斜杠双引号:

... message="NOQUEUE: reject: RCPT from sender.com[\"5.6.7.8\"] ... 

那么我得到了另一个错误:

local: 
    Data failed to compile: 
---------- 
    Rendering SLS postfix.test failed, render error: while parsing a block mapping 
    in "<unicode string>", line 84, column 7: 
     - args: "postfix/smtpd[20228] \"NO ... 
     ^
expected <block end>, but found '<scalar>' 
    in "<unicode string>", line 84, column 76: 
    ... : reject: RCPT from sender.com["5.6.7.8"]: 554 5.7.1 <[email protected] ... 

如何处理这两种情况?

+1

如何传递一个串过'stdin'及使用'read'来把它读入一个变量。然后在双引号内使用该变量... – anishsane 2015-03-19 04:31:11

+0

全部自动化。这里没有涉及到stdin。 – quanta 2015-03-19 04:46:38

+0

我的理解是这样的:你有你的框架中的字符串数据,使用它,你想触发另一个脚本。您正在使用一些字符串连接逻辑来完成此操作。取而代之的是,改变'另一个脚本'以从标准输入读取数据。然后,不要使用字符串连接,而是将字符串传递给脚本。 – anishsane 2015-03-19 09:29:49

回答

3

saltstack扩展jinja内置过滤器与一些custom filters

  • yaml_dquote:序列化字符串转换成适当的转义YAML双引号字符串。
  • yaml_encode:将单个对象序列化为YAML标量,并使用任何必要的处理来转义特殊字符。

喜欢的东西:

{%- set foo = 7.7 %} 
{%- set bar = none %} 
{%- set baz = true %} 
{%- set zap = 'The word of the day is "salty".' %} 
{%- set zip = '"The quick brown fox . . ."' %} 

foo: {{ foo|yaml_encode }} 
bar: {{ bar|yaml_encode }} 
baz: {{ baz|yaml_encode }} 
zap: {{ zap|yaml_encode }} 
zip: {{ zip|yaml_dquote }} 

给你:

foo: 7.7 
bar: null 
baz: true 
zap: "The word of the day is \"salty\"." 
zip: "\"The quick brown fox . . .\"" 

任意字符串,甚至{{ var|yaml_encode|yaml_decode }}可能无法正常工作。如果你可以对字符串进行编码,然后用脚本进行解码,那就更好了。

+0

'yaml_dquote'只是转换为双引号,并用反斜杠转义字符串内的双引号。这与我所尝试的相同,实际上,我得到了同样的错误。 – quanta 2015-03-25 14:53:39

+0

thankyouthankyouthankyou,你让我的星期五:) – Rumbles 2016-04-29 19:13:10

0

你有消息变量,它可以包含特殊字符。

- args: "{{ tag|default(formula) }} '{{ message }}'" 

我的理解是,你的bash脚本需要两个参数:

#!/bin/bash 
tag=$1 
message=$2 
echo "$message" #some use of message 

要调用与cmd.script脚本。

取而代之的是,你也许可以改变你的脚本如下:

- args: "{{ tag|default(formula) }}" 
- stdin: "{{ message }}\n" 

& bash脚本到:

#!/bin/bash 
tag=$1 
read message 
echo "$message" #some use of message