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

Returns a new instance of 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
27
28
# 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



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

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



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

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



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
184
185
186
187
# File 'lib/linter/services_linter.rb', line 157

def service_encrypted_fields_are_correct
  status = {status: 'pass', error: ''}
  accepted_keys = ['region', 'encrypted', 'service']
  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}}
          elsif value['$ssm'] == 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



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

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



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

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



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

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



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

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



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

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