Module: Awsecrets
- Defined in:
- lib/awsecrets.rb,
lib/awsecrets/version.rb
Constant Summary collapse
- VERSION =
'1.1.0'
Class Method Summary collapse
- .load(profile: nil, region: nil, secrets_path: 'secrets.yml') ⇒ Object
- .load_config ⇒ Object
- .load_creds ⇒ Object
- .load_env ⇒ Object
- .load_method_args ⇒ Object
- .load_options ⇒ Object
- .load_yaml ⇒ Object
Class Method Details
.load(profile: nil, region: nil, secrets_path: 'secrets.yml') ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/awsecrets.rb', line 8 def self.load(profile: nil, region: nil, secrets_path: 'secrets.yml') @profile = profile @secrets_path = secrets_path @region = region @credentials = nil # 1. Command Line Options if load_method_args # 2. Environment Variables load_env # 3. YAML file (secrets.yml) load_yaml # 4. The AWS credentials file load_creds # 5. The CLI configuration file load_config Aws.config[:region] = @region Aws.config[:credentials] = @credentials end |
.load_config ⇒ Object
83 84 85 86 87 88 |
# File 'lib/awsecrets.rb', line 83 def self.load_config return unless @region.nil? aws_config = AWSConfig.profiles[@profile] aws_config = AWSConfig.profiles['default'] unless aws_config @region = aws_config.config_hash[:region] if aws_config end |
.load_creds ⇒ Object
78 79 80 81 |
# File 'lib/awsecrets.rb', line 78 def self.load_creds return unless @credentials.nil? @credentials = Aws::SharedCredentials.new(profile_name: nil) end |
.load_env ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/awsecrets.rb', line 52 def self.load_env @region = ENV['AWS_REGION'] unless @region @region = ENV['AWS_DEFAULT_REGION'] unless @region if @credentials.nil? && ENV['AWS_PROFILE'] @credentials = Aws::SharedCredentials.new(profile_name: ENV['AWS_PROFILE']) @profile = ENV['AWS_PROFILE'] end if @credentials.nil? && ENV['AWS_ACCESS_KEY_ID'] && ENV['AWS_SECRET_ACCESS_KEY'] @credentials = @credentials = Aws::Credentials.new( ENV['AWS_ACCESS_KEY_ID'], ENV['AWS_SECRET_ACCESS_KEY']) end end |
.load_method_args ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/awsecrets.rb', line 29 def self.load_method_args return false unless @profile aws_config = AWSConfig.profiles[@profile] @region = aws_config.config_hash[:region] if aws_config && @region.nil? @credentials = Aws::SharedCredentials.new(profile_name: @profile) true end |
.load_options ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/awsecrets.rb', line 37 def self. opt = OptionParser.new opt.on('--profile PROFILE') { |v| @profile = v } unless @profile opt.on('--region REGION') { |v| @region = v } unless @region opt.on('--secrets_path SECRETS_PATH') { |v| @secrets_path = v } unless @secrets_path begin opt.parse!(ARGV) rescue OptionParser::InvalidOption end return unless @profile aws_config = AWSConfig.profiles[@profile] @region = aws_config.config_hash[:region] if aws_config && @region.nil? @credentials = Aws::SharedCredentials.new(profile_name: @profile) end |
.load_yaml ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/awsecrets.rb', line 66 def self.load_yaml creds = YAML.load_file(@secrets_path) if File.exist?(@secrets_path) if @region.nil? && creds @region = creds['region'] if creds.include?('region') end if @credentials.nil? && creds && creds.include?('aws_access_key_id') && creds.include?('aws_secret_access_key') @credentials = Aws::Credentials.new( creds['aws_access_key_id'], creds['aws_secret_access_key']) end end |