Class: PropertyGenerator::ServicesLinter
- Inherits:
-
Object
- Object
- PropertyGenerator::ServicesLinter
- Defined in:
- lib/linter/services_linter.rb
Constant Summary collapse
- TESTS =
[ 'services_have_accepted_keys', 'service_environments_are_not_empty', 'service_environments_match_config_environments', 'service_encrypted_environments_match_config_environments', 'service_encrypted_fields_are_correct', 'service_encrypted_region_field_is_accepted' ].freeze
Instance Method Summary collapse
-
#initialize(path, configs, ignored_tests) ⇒ ServicesLinter
constructor
A new instance of ServicesLinter.
- #recursive_find_keys(obj, key) ⇒ Object
- #run_services_tests ⇒ Object
- #service_encrypted_environments_match_config_environments ⇒ Object
- #service_encrypted_fields_are_correct ⇒ Object
- #service_encrypted_region_field_is_accepted ⇒ Object
- #service_environments_are_not_empty ⇒ Object
- #service_environments_match_config_environments ⇒ Object
- #services_have_accepted_keys ⇒ Object
Constructor Details
#initialize(path, configs, ignored_tests) ⇒ ServicesLinter
Returns a new instance of ServicesLinter.
14 15 16 17 18 19 20 21 22 |
# File 'lib/linter/services_linter.rb', line 14 def initialize(path, configs, ignored_tests) @configs = configs @services = {} @ignored_tests = ignored_tests 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
#recursive_find_keys(obj, key) ⇒ Object
115 116 117 118 119 120 121 122 123 |
# File 'lib/linter/services_linter.rb', line 115 def recursive_find_keys(obj, key) if obj.respond_to?(:key?) && obj.key?(key) obj[key] elsif obj.is_a?(Hash) or obj.is_a?(Array) r = nil obj.find{ |*a| r = recursive_find_keys(a.last,key) } r end end |
#run_services_tests ⇒ Object
24 25 26 27 28 |
# File 'lib/linter/services_linter.rb', line 24 def run_services_tests tests = TESTS - @ignored_tests PropertyGenerator.test_runner(self, tests) end |
#service_encrypted_environments_match_config_environments ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/linter/services_linter.rb', line 91 def service_encrypted_environments_match_config_environments status = { status: 'pass', error: '' } missmatched_environments = [] @services.each do |path, loaded| next if loaded['encrypted'].nil? loaded['encrypted'].keys.each do |environment| if @configs['environments'].nil? status[:status] = 'warn' status[:error] = 'Environments list in config file is missing.' else unless @configs['environments'].include?(environment) missmatched_environments << { path => environment } 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_correct ⇒ Object
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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/linter/services_linter.rb', line 125 def service_encrypted_fields_are_correct status = { status: 'pass', error: '' } accepted_keys = ['region', 'encrypted', 'service', 'label'] services_with_unacceptable_keys = [] @services.each do |path, loaded| next 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 } } next end s = recursive_find_keys(value, '$ssm') k = recursive_find_keys(value, '$kms') if s.nil? && k.nil? services_with_unacceptable_keys << { path => { environment => property } } next end unless s.nil? s.keys.each do |key| unless accepted_keys.include?(key) services_with_unacceptable_keys << { path => { environment => property } } next end end end unless k.nil? if (k.keys & ['region', 'encrypted']).count != 2 services_with_unacceptable_keys << { path => { environment => property } } end end end end end unless services_with_unacceptable_keys.empty? status[:status] = 'fail' status[:error] = "Service files: #{services_with_unacceptable_keys} has an encrypted block without " + "properties, encrypted properties without $kms or $ssm blocks, or encrypted blocks with either bad " + "indentation or incorrect keys." end status end |
#service_encrypted_region_field_is_accepted ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 184 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 212 |
# File 'lib/linter/services_linter.rb', line 172 def service_encrypted_region_field_is_accepted status = { status: 'pass', error: '' } services_with_unacceptable_keys = [] @services.each do |path, loaded| next if loaded['encrypted'].nil? loaded['encrypted'].each do |environment, properties| if loaded['encrypted'][environment].nil? status[:status] = 'error' status[:error] = 'Encrypted properties are missing from the encrypted environment' return status end encrypted_env = loaded['encrypted'][environment] # next if encrypted_env[property].nil? encrypted_env.each do |property, encrypted_values| if @configs['environments'].nil? status[:status] = 'warn' status[:error] = 'Environments list in config file is missing.' break end %w[$ssm $kms].each do |encryption_type| values = recursive_find_keys(encrypted_values, encryption_type) next if values.nil? unless @configs['environments'].include?(values['region']) services_with_unacceptable_keys << { path => { environment => property } } end end end end end if !services_with_unacceptable_keys.empty? && status[:status] == 'pass' 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_empty ⇒ Object
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_match_config_environments ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/linter/services_linter.rb', line 67 def service_environments_match_config_environments status = { status: 'pass', error: '' } missmatched_environments = [] @services.each do |path, loaded| next if loaded['environments'].nil? loaded['environments'].keys.each do |environment| if @configs['environments'].nil? status[:status] = 'warn' status[:error] = 'Environments list in config file is missing.' else unless @configs['environments'].include?(environment) missmatched_environments << { path => environment } 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_keys ⇒ Object
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', 'configname', 'stringdata', 'configlabels', 'secretlabels', 'label'] 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', 'encrypted', 'configname', 'stringdata', 'configlabels', 'secretlabels' or 'label'" end status end |