Class: PuppetLint::CheckPlugin
- Inherits:
-
Object
- Object
- PuppetLint::CheckPlugin
- Defined in:
- lib/puppet-lint/security.rb
Overview
Defines helper methods for check plugins
Constant Summary collapse
- VALID_CONTENT_TOKENS =
This types represent valid values for variables and parameters
[:NAME,:SSTRING,:STRING,:NUMBER,:TRUE,:FALSE,:DQPRE,:DQMID,:DQPOST,:VARIABLE]
Instance Method Summary collapse
-
#bulk_notify(hash) ⇒ Object
Warps puppet-lint notify and generates notifies for array of problems.
-
#check_resource_index(hash) ⇒ Object
Wrapper to check logic.
-
#get_argument_token_for_function(tokens, function) ⇒ Array
Get array of tokens with arguments of a puppet-function.
-
#get_array_tokens_for_parameter(tokens, parameter) ⇒ Array
Get array of tokens for given parameter out of puppet resource definition.
-
#get_hash_tokens_for_parameter(tokens, parameter) ⇒ Array
Get array of tokens with given parameter out of a hash.
-
#get_resource_block_for(resource_title, token) ⇒ Array
Get resource block for given puppet resource name.
-
#get_resource_title_for(rule) ⇒ String
Get resource title for rule.
-
#get_tokens_between(tokens, type, parameter) ⇒ Array
Get array of tokens with given parameter out of any puppet block.
-
#get_value_token_for_parameter(tokens, parameter) ⇒ Array
Get array of tokens with values of given parameter.
-
#get_variable_value_for(token) ⇒ PuppetLint::Lexer::Token
Get first token with begin of value to a given parameter token.
-
#resource_in_class_or_define?(resource, class_or_define) ⇒ Boolean
Checks if given resource is defined in given class or define.
-
#title_tokens_with_block ⇒ Array
Get Hash of titles and tokens of puppet manifest.
-
#value_is_array?(tokens, parameter) ⇒ Boolean
Checks if given parameter of a puppet resource is an array.
Instance Method Details
#bulk_notify(hash) ⇒ Object
Warps puppet-lint notify and generates notifies for array of problems
195 196 197 198 199 200 201 202 203 |
# File 'lib/puppet-lint/security.rb', line 195 def bulk_notify(hash) hash[:result].each do |v| notify hash[:severity], { :message => hash[:message], :line => v.line, :column => v.column, } end end |
#check_resource_index(hash) ⇒ Object
Wrapper to check logic. Searches tokens matching given puppet resource or class.
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/puppet-lint/security.rb', line 213 def check_resource_index(hash) # Operate on arrays unless hash[:resource_type].is_a? Array resource_types=[hash[:resource_type]] else resource_types=hash[:resource_type] end rules=resource_indexes.find_all do |resource| # Need to search resource titles, when not a predefined puppet resource if resource[:type].type == :CLASS resource_titles = title_tokens.map { |t| t.value } diff_titles = (resource_types - resource_titles) # Difference is smaler then original # so some elements where substracted diff_titles.count < resource_types.count else resource_types.include? resource[:type].value end end result=rules.map do |rule| yield rule end.flatten.compact bulk_notify(hash.merge(:result => result)) end |
#get_argument_token_for_function(tokens, function) ⇒ Array
Get array of tokens with arguments of a puppet-function
120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/puppet-lint/security.rb', line 120 def get_argument_token_for_function(tokens,function) lparen=tokens.find do |token| token.type == :LPAREN and token.prev_code_token.type == :NAME and token.prev_code_token.value == function end if lparen.nil? return [] else return get_block_between(:PAREN,lparen) end end |
#get_array_tokens_for_parameter(tokens, parameter) ⇒ Array
Get array of tokens for given parameter out of puppet resource definition
37 38 39 40 41 |
# File 'lib/puppet-lint/security.rb', line 37 def get_array_tokens_for_parameter(tokens,parameter) get_tokens_between(tokens,:BRACK,parameter).reject do |token| token.type == :COMMA end end |
#get_hash_tokens_for_parameter(tokens, parameter) ⇒ Array
Get array of tokens with given parameter out of a hash
48 49 50 |
# File 'lib/puppet-lint/security.rb', line 48 def get_hash_tokens_for_parameter(tokens,parameter) get_tokens_between(tokens,:BRACE,parameter) end |
#get_resource_block_for(resource_title, token) ⇒ Array
Get resource block for given puppet resource name. Resource type is irrelevant
79 80 81 82 83 84 85 86 |
# File 'lib/puppet-lint/security.rb', line 79 def get_resource_block_for(resource_title,token) titles=title_tokens_with_block titles.find_all do | hash | hash[:title].value == resource_title end.first.values.flatten end |
#get_resource_title_for(rule) ⇒ String
Get resource title for rule
179 180 181 182 183 184 |
# File 'lib/puppet-lint/security.rb', line 179 def get_resource_title_for(rule) title_token=title_tokens_with_block.find do |h| h[:tokens].first == rule[:tokens].first end title_token[:title] unless title_token.nil? end |
#get_tokens_between(tokens, type, parameter) ⇒ Array
Get array of tokens with given parameter out of any puppet block
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/puppet-lint/security.rb', line 57 def get_tokens_between(tokens,type,parameter) brace_open=tokens.find do |token| token.type == left(type) and # Vorher kommt ein Pfeil, somit ist es der Wert eines Parmeters token.prev_code_token.type == :FARROW and # Vor dem Pfeil kommt der gesuchte Parameter token.prev_code_token.prev_code_token.value == parameter end if brace_open.nil? return [] else return get_block_between(type,brace_open) end end |
#get_value_token_for_parameter(tokens, parameter) ⇒ Array
Get array of tokens with values of given parameter
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/puppet-lint/security.rb', line 93 def get_value_token_for_parameter(tokens,parameter) value_starts_tokens=tokens.find_all do |token| VALID_CONTENT_TOKENS.include? token.type and # An arrow first indicates the value of a parameter token.prev_code_token.type == :FARROW and # The given parameter comes first, then the arrow token.prev_code_token.prev_code_token.value == parameter end value_starts_tokens.map do |token| if token.type==:DQPRE t=[] until token.type == :DQPOST t << token token=token.next_code_token end t else token end end.flatten end |
#get_variable_value_for(token) ⇒ PuppetLint::Lexer::Token
Get first token with begin of value to a given parameter token
138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/puppet-lint/security.rb', line 138 def get_variable_value_for(token) if token.type == :VARIABLE and token.next_code_token.type == :EQUALS token=token.next_code_token until token.next_code_token.nil? or VALID_CONTENT_TOKENS.include? token.type or token.type == :VARIABLE token=token.next_code_token end return token else return nil end end |
#resource_in_class_or_define?(resource, class_or_define) ⇒ Boolean
Checks if given resource is defined in given class or define
12 13 14 15 |
# File 'lib/puppet-lint/security.rb', line 12 def resource_in_class_or_define?(resource,class_or_define) resource[:start] > class_or_define[:start] and resource[:end] < class_or_define[:end] end |
#title_tokens_with_block ⇒ Array
Get Hash of titles and tokens of puppet manifest
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/puppet-lint/security.rb', line 154 def title_tokens_with_block # Get all token blocks having between colon and semic or rbrace title_tokens.map do |block_starter| token_array=[] t = block_starter.next_token until [:SEMIC,:RBRACE].include? t.type token_array << t unless t.type == :COLON t = t.next_token end { :title => block_starter, :tokens => token_array } end end |
#value_is_array?(tokens, parameter) ⇒ Boolean
Checks if given parameter of a puppet resource is an array
22 23 24 25 26 27 28 29 30 |
# File 'lib/puppet-lint/security.rb', line 22 def value_is_array?(tokens,parameter) values_with_lbracks=tokens.find_all do |token| # match 'parameter => [' token.type == :LBRACK and token.prev_code_token.type == :FARROW and token.prev_code_token.prev_code_token.value == parameter end not values_with_lbracks.empty? end |