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.



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

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



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

def run_services_tests
  tests = ['services_have_accepted_keys',
           '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



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

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



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

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



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

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, property|
        if loaded['encrypted'][environment][property] != nil
          loaded['encrypted'][environment][property].each do |ssm, keys|
            unless loaded['encrypted'][environment][property]['$ssm'][keys].keys == accepted_keys
              services_with_unacceptable_keys << {path => {environment => property}}
            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 keys other than 'region' and 'encrypted'."
  end
  status
end

#service_encrypted_region_field_is_acceptedObject



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
# File 'lib/linter/services_linter.rb', line 156

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_have_no_hashes_as_valuesObject



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 87

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|
        properties.each do |key, value|
          if value.class == Hash
            services_with_hashes_in_environments << {path => {environments => key}}
          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



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

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



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

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