Skip to content

Commit 077b383

Browse files
authored
Merge pull request #16 from puppetlabs/(CONT-234)_Create-MVP_fix-failing-parameterised-commmand-test
2 parents 27849d9 + 5ae0952 commit 077b383

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

lib/puppet-lint/plugins/check_unsafe_interpolations.rb

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,51 @@ def check
1111
exec_resources.each do |command_resources|
1212
# Iterate over each command in execs and check for unsafe interpolations
1313
command_resources[:tokens].each do |token|
14-
# Check if any tokens in command are a varibale
15-
if token.type == :VARIABLE
16-
warning_message = "unsafe interpolation of variable '#{token.value}' in exec command"
17-
notify_warning(token, warning_message)
14+
# We are only interested in tokens from command onwards
15+
next unless token.type == :NAME
16+
# Don't check the command if it is parameterised
17+
next if parameterised?(token)
18+
19+
check_command(token).each do |t|
20+
notify_warning(t)
1821
end
1922
end
2023
end
2124
end
2225

2326
# Raises a warning given a token and message
24-
def notify_warning(token, message)
27+
def notify_warning(token)
2528
notify :warning,
26-
message: message,
29+
message: "unsafe interpolation of variable '#{token.value}' in exec command",
2730
line: token.line,
2831
column: token.column
2932
end
33+
34+
# Iterates over the tokens in a command and adds it to an array of violations if it is an input variable
35+
def check_command(token)
36+
# Initialise variables needed in while loop
37+
rule_violations = []
38+
current_token = token
39+
40+
# Iterate through tokens in command
41+
while current_token.type != :NEWLINE
42+
# Check if token is a varibale and if it is parameterised
43+
if current_token.type == :VARIABLE
44+
rule_violations.append(current_token)
45+
end
46+
current_token = current_token.next_token
47+
end
48+
49+
rule_violations
50+
end
51+
52+
# A command is parameterised if its args are placed in an array
53+
# This function checks if the current token is a :FARROW and if so, if it is followed by an LBRACK
54+
def parameterised?(token)
55+
current_token = token
56+
while current_token.type != :NEWLINE
57+
return true if current_token.type == :FARROW && current_token.next_token.next_token.type == :LBRACK
58+
current_token = current_token.next_token
59+
end
60+
end
3061
end

spec/puppet-lint/plugins/check_unsafe_interpolations_spec.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,11 @@ class foo {
9797
exec { 'bar':
9898
command => ['echo', $foo],
9999
}
100-
101100
}
102101
PUPPET
103102
end
104103

105104
it 'detects zero problems' do
106-
pending('not implemented yet')
107105
expect(problems).to have(0).problems
108106
end
109107
end

0 commit comments

Comments
 (0)