Class: PuppetLint::CheckPlugin

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet-lint/linter.rb

Instance Method Summary collapse

Instance Method Details

#filter_credentials(tokens) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/puppet-lint/linter.rb', line 121

def filter_credentials(tokens)
   credentials = {}  
   tokens.each do |token|         
      next if token.next_code_token.nil?
      next if token.prev_code_token.nil?
      # accepts (<VARIABLE>|<NAME>) =~ SECRET (<EQUALS>|<FARROW>) !(<STRING>|<SSTRING>|<NAME>) =~ (NONSECRET AND PLACHOLDER)
      if [:VARIABLE, :NAME].include? token.prev_code_token.type and [:EQUALS, :FARROW].include? token.type and token.prev_code_token.value.downcase =~ Rules.secret and !(token.next_code_token.value.downcase =~ Rules.nonsecret) and !(token.next_code_token.value.downcase =~ Rules.placeholder)
         # check if username
         left_side_value = token.prev_code_token.value.downcase
         is_username = left_side_value[Rules.username]
         is_password = left_side_value[Rules.password]
         if !is_username.nil?
            puts is_username
            context = left_side_value.gsub(is_username, '')
            if context.length > 0
               puts "CONTEXT", context
            end
            credentials.merge!(context => {:username => left_side_value }) if context.length > 0
         end
         if !is_password.nil?
            puts is_password
            context = left_side_value.gsub(is_password, '')
            if context.length > 0
               puts "CONTEXT", context
            end
            credentials.merge!(context => {:password => left_side_value }) if context.length > 0
         end
      end
   end
   puts credentials
end

#filter_resources(tokens, resources) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/puppet-lint/linter.rb', line 75

def filter_resources(tokens, resources)
   is_resource = false  
   brackets = 0
   ftokens=tokens.find_all do |hash|
      
      if resources.include? hash.value.downcase
         is_resource = true
      elsif is_resource and hash.type == :LBRACE
         brackets += 1
      elsif is_resource and hash.type == :RBRACE
         brackets -=1
      end

      if is_resource and hash.type == :RBRACE and brackets == 0
         is_resource = false
      end

      if !is_resource
         [:NAME, :VARIABLE, :SSTRING, :STRING].include? hash.type
      end
   end
   return ftokens
end

#filter_tokens(tokens) ⇒ Object



114
115
116
117
118
119
# File 'lib/puppet-lint/linter.rb', line 114

def filter_tokens(tokens)
   ftokens=tokens.find_all do |hash|
      [:NAME, :VARIABLE, :SSTRING, :STRING].include? hash.type
   end
   return ftokens
end

#filter_tokens_per_value(tokens, token) ⇒ Object



107
108
109
110
111
112
# File 'lib/puppet-lint/linter.rb', line 107

def filter_tokens_per_value(tokens, token)
   ftokens=tokens.find_all do |hash|
      [:NAME, :SSTRING, :STRING].include? hash.type and !hash.value.downcase.include? token
   end
   return ftokens
end

#filter_variables(tokens, keywords) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
# File 'lib/puppet-lint/linter.rb', line 153

def filter_variables(tokens, keywords)
   line = -1
   kw_regex = Regexp.new keywords.join("|")
   ftokens=tokens.find_all do |hash|
      if [:NAME, :VARIABLE].include? hash.type and hash.value.downcase =~ kw_regex
         line = hash.line
      elsif hash.line != line
         hash
      end
   end
end

#filter_whitelist(tokens) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/puppet-lint/linter.rb', line 99

def filter_whitelist(tokens)
   whitelist=Config.regex.whitelist 
   ftokens=tokens.find_all do |hash|
      !(whitelist =~ hash.value.downcase)
   end
   return ftokens
end

#get_comments(tokens) ⇒ Object



68
69
70
71
72
73
# File 'lib/puppet-lint/linter.rb', line 68

def get_comments(tokens)
   ftokens=tokens.find_all do |hash|
      [:COMMENT, :MLCOMMENT, :SLASH_COMMENT].include? hash.type
   end
   return ftokens
end

#get_dependencies(tokens) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/puppet-lint/linter.rb', line 11

def get_dependencies(tokens)
   is_resource = false
   ftokens = []
   dependency = ''
   tokens.each do |token|

      is_next_brace = (not token.next_code_token.nil? and token.next_code_token.type == :LBRACE)
      is_prev_brace = (not token.prev_code_token.nil? and token.prev_code_token.type == :LBRACE)

      if (token.value.downcase[Config.regex.dependencies] and token.type == :NAME and is_next_brace) or (token.value.downcase[Config.regex.dependencies] and token.type == :SSTRING and is_prev_brace)
         is_resource = true
         dependency = token.value.downcase[Config.regex.dependencies]
      end 

      if is_resource and token.type == :RBRACE
         is_resource = false
      end
  
      if not is_resource and not token.next_code_token.nil? 
         if token.value.downcase[Config.regex.dependencies] and token.value.downcase[/_version/]
            dependency = token.value.downcase[Config.regex.dependencies]
            variable_name = "#{dependency}_version"
            if token.value.downcase == variable_name and [:EQUALS, :FARROW].include? token.next_code_token.type
               ftokens += [{"token": token.next_code_token, "dependency": dependency}]
            end
         end
      end
      
      is_assign = (not token.prev_code_token.nil? and not token.next_code_token.nil?)
      is_version = (is_assign and token.prev_code_token.value.downcase =~ /version/)
      
      if is_resource and is_version and [:EQUALS, :FARROW].include? token.type and ![:VARIABLE, :NAME].include? token.next_code_token.type
         if !token.prev_code_token.value.downcase[Config.regex.dependencies].eql? dependency and token.prev_code_token.value.downcase[Config.regex.dependencies]
            ftokens += [{"token": token, "dependency": token.prev_code_token.value.downcase[Config.regex.dependencies]}]
         else
            ftokens += [{"token": token, "dependency": dependency}]
         end
      end

   end
   return ftokens
end

#get_malicious_cves(dependency, version) ⇒ Object



3
4
5
6
7
8
9
# File 'lib/puppet-lint/linter.rb', line 3

def get_malicious_cves(dependency, version)
   path = "#{Config.dpath}#{dependency}.json"
   cves = JSON.parse(File.read(path))
   if !cves[version].nil?
      return cves[version]
   end
end

#get_string_tokens(tokens, token) ⇒ Object



54
55
56
57
58
59
# File 'lib/puppet-lint/linter.rb', line 54

def get_string_tokens(tokens, token)
   ftokens=tokens.find_all do |hash|
      [:SSTRING, :STRING].include? hash.type and hash.value.downcase.include? token
   end
   return ftokens
end

#get_tokens(tokens, token) ⇒ Object



61
62
63
64
65
66
# File 'lib/puppet-lint/linter.rb', line 61

def get_tokens(tokens, token)
   ftokens=tokens.find_all do |hash|
      [:NAME, :VARIABLE, :SSTRING, :STRING].include? hash.type and hash.value.downcase.include? token
   end
   return ftokens
end