ssm_params_loader 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d8a68deb19396ec5e6408a733d2a279d499b9c71adba02fa75f7f3b4d812f4ce
4
+ data.tar.gz: 023eb7cce06eb9de920f90e0ea504fd66a764bc8a55c6f0bf92c4513588e95d9
5
+ SHA512:
6
+ metadata.gz: a5e8eb20335f4443fcf1410ab51679c67a0b1f3f63f6d26ccfe74f2f2cda33439a010983462922fdc75529a660dc6f41479cfc65d6ad171cb096453f05477ac8
7
+ data.tar.gz: 1202103d4d03b52059df4c126d48f0c87d5f3119d9696a6f461d542202903b75e220745bff551b0ef2c4e9687a6526dcb92f92699baa0a30528e047ceec422ac
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2006-2024 Olseksii Samoliuk
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # In a future, maybe :)
4
+ puts "hi :)"
@@ -0,0 +1,25 @@
1
+ module SsmParamsLoader
2
+ module Generators
3
+ class InstallGenerator < ::Rails::Generators::Base
4
+ desc "Generates the SSM Params Loader config file."
5
+
6
+ def self.source_root
7
+ @_config_source_root ||= File.expand_path("../templates", __FILE__)
8
+ end
9
+
10
+ def copy_settings
11
+ template "config.yml", "config/ssm_params_loader.yml"
12
+ end
13
+
14
+ def modify_gitignore
15
+ create_file '.gitignore' unless File.exist? '.gitignore'
16
+
17
+ append_to_file '.gitignore' do
18
+ "\n" +
19
+ "# Ignore SSM Params Loader config file\n" +
20
+ "config/ssm_params_loader.yml\n"
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,9 @@
1
+ # Path to a SSM parameters
2
+ ssm_store_path: '' # Ex: /MyApp/production
3
+
4
+ # Additional static secrets list
5
+ # If a name is equal to a name obtained from the SSM, the value from the SSM
6
+ # is overwritten by a value from this list.
7
+ additional_vars: [ ]
8
+ # - name: secrets_attachment_scan_host
9
+ # value: zzzz
@@ -0,0 +1,74 @@
1
+ require 'aws-sdk-ssm'
2
+
3
+ module SsmParamsLoader
4
+ def self.fetch_params(config_file = nil)
5
+
6
+ config_file = config_file || default_config_file
7
+
8
+ unless File.exist?(config_file)
9
+ # puts "SSM Params Loader config file #{config_file} not found"
10
+ return
11
+ end
12
+
13
+ config = YAML.safe_load_file(config_file).with_indifferent_access
14
+
15
+ ssm_path = config[:ssm_store_path] || nil
16
+ additional = config[:additional_vars] || nil
17
+
18
+ # Get secrets and set environment variables
19
+ environments = load_secrets(ssm_path, additional)
20
+ environments.each { |secret| ENV["SSM_#{secret[:name].gsub('-', '_').upcase}"] = secret[:value] }
21
+ end
22
+
23
+ private
24
+
25
+ # Setup the default path to the config file
26
+ def self.default_config_file
27
+ ::Rails.root.join('config/ssm_params_loader.yml')
28
+ end
29
+
30
+ # Get a hash array of secrets
31
+ def self.load_secrets(ssm_path, additional)
32
+ secrets = []
33
+
34
+ unless ssm_path.nil?
35
+ secrets += from_ssm(ssm_path)
36
+ end
37
+
38
+ unless additional.nil?
39
+ secrets += additional_variables(additional)
40
+ end
41
+
42
+ secrets
43
+ end
44
+
45
+ # Get a hash array of secrets from the SSM
46
+ def self.from_ssm(ssm_path)
47
+ ssm_data = []
48
+ ssm = Aws::SSM::Client.new
49
+ ssm.get_parameters_by_path(
50
+ path: ssm_path,
51
+ with_decryption: true
52
+ ).each do |response|
53
+ ssm_data.push(*response['parameters'])
54
+ end
55
+
56
+ variables = []
57
+
58
+ ssm_data.each do |secret|
59
+ variables << { :name => secret.name.split('/')[-1], :value => secret.value }
60
+ end
61
+
62
+ variables
63
+ end
64
+
65
+ # Get a hash array of secrets from the config file (the `additional` section)
66
+ def self.additional_variables(additional_variables)
67
+ variables = []
68
+ additional_variables.each do |var|
69
+ variables << { :name => var['name'], :value => var['value'] }
70
+ end
71
+
72
+ variables
73
+ end
74
+ end
@@ -0,0 +1,3 @@
1
+ module SsmParamsLoader
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,13 @@
1
+ require_relative 'ssm_params_loader/load'
2
+
3
+ module SsmParamsLoader
4
+ module Rails
5
+ class Railtie < ::Rails::Railtie
6
+ def setup_environment_variables
7
+ SsmParamsLoader.fetch_params
8
+ end
9
+
10
+ config.before_initialize { setup_environment_variables }
11
+ end
12
+ end
13
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ssm_params_loader
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Oleksii Samoliuk
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-03-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: aws-sdk-ssm
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 1.165.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 1.165.0
27
+ description: Preloads AWS SSM parameters as an environment variables
28
+ email:
29
+ - work@yousysadmin.com
30
+ executables:
31
+ - ssm_params_loader
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - LICENSE.txt
36
+ - bin/ssm_params_loader
37
+ - lib/generators/ssm_params_loader/install_generator.rb
38
+ - lib/generators/ssm_params_loader/templates/config.yml
39
+ - lib/ssm_params_loader.rb
40
+ - lib/ssm_params_loader/load.rb
41
+ - lib/ssm_params_loader/version.rb
42
+ homepage: https://github.com/yousysadmin/rails_ssm_params_loader
43
+ licenses:
44
+ - MIT
45
+ metadata:
46
+ rubygems_mfa_required: 'false'
47
+ github_repo: https://github.com/yousysadmin/rails_ssm_params_loader
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: 3.0.0
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubygems_version: 3.3.5
64
+ signing_key:
65
+ specification_version: 4
66
+ summary: Rails application parameters preloader
67
+ test_files: []