-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
We are going to move away from HashiCorp Vault to use AWS SSM. In thi…
…s commit I have updated the configuration so that will import the environment variables from AWS SSM. If something goes wrong, an error will be logged.
- Loading branch information
Showing
4 changed files
with
67 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
require 'rollbar' | ||
|
||
IDAM_SSM_SERVICE_NAME_REGEX = /pmp-idam-[a-z]+-ssm-service/.freeze | ||
|
||
def config_aws_ssm | ||
ssm_client = setup_ssm_client | ||
|
||
pmp_idam_variables = find_pmp_idam_variables(ssm_client) | ||
|
||
pmp_idam_variables.each do |pmp_idam_key, pmp_idam_value| | ||
ENV[pmp_idam_key] = pmp_idam_value | ||
end | ||
|
||
Rails.logger.info('SUCCESS: All variables imported from AWS SSM') | ||
rescue Aws::SSM::Errors::StandardError => e | ||
Rails.logger.error('ERROR: Failed to import variables from AWS SSM') | ||
Rails.logger.error(e.message) | ||
end | ||
|
||
def setup_ssm_client | ||
vcap_services = JSON.parse(ENV.fetch('VCAP_SERVICES', nil), symbolize_names: true) | ||
|
||
aws_credentials = vcap_services[:'user-provided'].find { |service| service[:name].match(IDAM_SSM_SERVICE_NAME_REGEX) }[:credentials] | ||
|
||
Aws::SSM::Client.new( | ||
region: aws_credentials[:region], | ||
access_key_id: aws_credentials[:aws_access_key_id], | ||
secret_access_key: aws_credentials[:aws_secret_access_key] | ||
) | ||
end | ||
|
||
def find_pmp_idam_variables(ssm_client) | ||
pmp_idam_variables = [] | ||
|
||
next_token = nil | ||
|
||
loop do | ||
resp = ssm_client.get_parameters_by_path( | ||
{ | ||
path: '/pmp-idam/', | ||
with_decryption: true, | ||
max_results: 10, | ||
next_token: next_token | ||
} | ||
) | ||
|
||
pmp_idam_variables += resp.parameters.map do |parameter| | ||
[ | ||
parameter.name.split('/').last.upcase.to_s, | ||
parameter.value.to_s | ||
] | ||
end | ||
|
||
next_token = resp.next_token | ||
|
||
break unless next_token | ||
end | ||
|
||
pmp_idam_variables | ||
end | ||
|
||
config_aws_ssm if ENV['SERVER_ENV_NAME'].present? |
This file was deleted.
Oops, something went wrong.