Class: PropertyGenerator::ServicesLinter

Inherits:
Object
  • Object
show all
Defined in:
lib/linter/services_linter.rb

Instance Method Summary collapse

Constructor Details

#initialize(path, configs) ⇒ ServicesLinter



6
7
8
9
10
11
12
13
# File 'lib/linter/services_linter.rb', line 6

def initialize(path, configs)
  @configs = configs
  @services = {}
  valid_paths = PropertyGenerator.valid_paths(path)
  valid_paths.each do |file_path|
    @services[file_path] = YAML.load_file(file_path)
  end
end

Instance Method Details

#run_services_testsObject



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/linter/services_linter.rb', line 15

def run_services_tests
  tests = ['services_have_accepted_keys',
           'service_environments_are_not_empty',
           'service_defaults_have_no_hashes_as_values',
           'service_environments_match_config_environments',
           'service_environments_have_no_hashes_as_values',
           'service_encrypted_environments_match_config_environments',
           'service_encrypted_fields_are_correct',
           'service_encrypted_region_field_is_accepted']
  results = PropertyGenerator.test_runner(self, tests)
  results
end

#service_defaults_have_no_hashes_as_valuesObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/linter/services_linter.rb', line 65

def service_defaults_have_no_hashes_as_values
  status = {status: 'pass', error: ''}
  services_with_hashes_in_defaults = []
  @services.each do |path, loaded|
    unless loaded['default'] == nil
      loaded['default'].each do |defaultkey, defaultvalue|
        if defaultvalue.class == Hash
          services_with_hashes_in_defaults << {path => defaultkey}
        end
      end
    end
  end
  if services_with_hashes_in_defaults != []
    status[:status] = 'fail'
    status[:error] = "Service files: #{services_with_hashes_in_defaults} have default properties with values as hashes."
  end
  status
end

#service_encrypted_environments_match_config_environmentsObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/linter/services_linter.rb', line 131

def service_encrypted_environments_match_config_environments
  status = {status: 'pass', error: ''}
  missmatched_environments = []
  @services.each do |path, loaded|
    if loaded['encrypted'] != nil
      loaded['encrypted'].keys.each do |environment|
        if @configs['environments'] != nil
          unless @configs['environments'].include?(environment)
            missmatched_environments << {path => environment}
          end
        else
          status[:status] = 'warn'
          status[:error] = "Environments list in config file is missing."
        end
      end
    end
  end
  if missmatched_environments != []
    status[:status] = 'warn'
    status[:error] = "Service files: #{missmatched_environments} have encrypted environments not matching config list."
  end
  status
end

#service_encrypted_fields_are_correctObject



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/linter/services_linter.rb', line 155

def service_encrypted_fields_are_correct
  status = {status: 'pass', error: ''}
  accepted_keys = ['region', 'encrypted']
  services_with_unacceptable_keys = []
  @services.each do |path, loaded|
    if loaded['encrypted'] != nil
      loaded['encrypted'].each do |environment, properties|
        properties.each do |property, value|
          if value == nil
            services_with_unacceptable_keys << {path => {environment => property}}
          else
            if value['$ssm'] != nil
              value['$ssm'].keys.each do |key|
                unless accepted_keys.include?(key)
                  services_with_unacceptable_keys << {path => {environment => property}}
                end
              end
            end
          end
        end
      end
    end
  end
  if services_with_unacceptable_keys != []
    status[:status] = 'fail'
    status[:error] = "Service files: #{services_with_unacceptable_keys} have encrypted properties with bad indentation or keys other than 'region' and 'encrypted'."
  end
  status
end

#service_encrypted_region_field_is_acceptedObject



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/linter/services_linter.rb', line 185

def service_encrypted_region_field_is_accepted
  status = {status: 'pass', error: ''}
  services_with_unacceptable_keys = []
  @services.each do |path, loaded|
    if loaded['encrypted'] != nil
      loaded['encrypted'].each do |environment, property|
        if loaded['encrypted'][environment][property] != nil
          loaded['encrypted'][environment][property].each do |ssm, keys|
            if @configs['environments'] != nil
              unless @configs['environments'].include?(loaded['encrypted'][environment][property]['$ssm'][keys]['region'])
                services_with_unacceptable_keys << {path => {environment => property}}
              end
            else
              status[:status] = 'warn'
              status[:error] = "Environments list in config file is missing."
            end
          end
        end
      end
    end
  end
  if services_with_unacceptable_keys != []
    status[:status] = 'warn'
    status[:error] = "Service files: #{services_with_unacceptable_keys} have encrypted properties a region field not matching a declared environment in the configs."
  end
  status
end

#service_environments_are_not_emptyObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/linter/services_linter.rb', line 28

def service_environments_are_not_empty
  status = {status: 'pass', error: ''}
  services_empty_environments = []
  @services.each do |path, loaded|
    unless loaded['environments'] == nil
      loaded['environments'].each do |environments, properties|
        if properties == nil
          services_empty_environments << {path => environments}
        end
      end
    end
  end
  if services_empty_environments != []
    status[:status] = 'fail'
    status[:error] = "Service files #{services_empty_environments} have empty environments, these should be omitted."
  end
  status
end

#service_environments_have_no_hashes_as_valuesObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/linter/services_linter.rb', line 108

def service_environments_have_no_hashes_as_values
  status = {status: 'pass', error: ''}
  services_with_hashes_in_environments = []
  @services.each do |path, loaded|
    unless loaded['environments'] == nil
      loaded['environments'].each do |environments, properties|
        unless properties == nil
          properties.each do |key, value|
            if value.class == Hash
              services_with_hashes_in_environments << {path => {environments => key}}
            end
          end
        end
      end
    end
  end
  if services_with_hashes_in_environments != []
    status[:status] = 'fail'
    status[:error] = "Service files #{services_with_hashes_in_environments} have environment properties with values as hashes."
  end
  status
end

#service_environments_match_config_environmentsObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/linter/services_linter.rb', line 84

def service_environments_match_config_environments
  status = {status: 'pass', error: ''}
  missmatched_environments = []
  @services.each do |path, loaded|
    if loaded['environments'] != nil
      loaded['environments'].keys.each do |environment|
        if @configs['environments'] != nil
          unless @configs['environments'].include?(environment)
            missmatched_environments << {path => environment}
          end
        else
          status[:status] = 'warn'
          status[:error] = "Environments list in config file is missing."
        end
      end
    end
  end
  if missmatched_environments != []
    status[:status] = 'warn'
    status[:error] = "Service files: #{missmatched_environments} have environments not matching config list."
  end
  status
end

#services_have_accepted_keysObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/linter/services_linter.rb', line 47

def services_have_accepted_keys
  status = {status: 'pass', error: ''}
  accepted_keys = ['default', 'environments', 'encrypted']
  services_with_unacceptable_keys = []
  @services.each do |path, loaded|
    loaded.keys.each do |service_key|
      unless accepted_keys.include?(service_key)
        services_with_unacceptable_keys << {path => service_key}
      end
    end
  end
  if services_with_unacceptable_keys != []
    status[:status] = 'fail'
    status[:error] = "Service files: #{services_with_unacceptable_keys} have keys other than 'default', 'environments', or 'encrypted'."
  end
  status
end