Ruby AWS SDK secretsmanager 'undefined method `[]' for nil:NilClass (NoMethodError)'

96 Views Asked by At

I'm trying to use the AWS SecretsManager SDK in Ruby and I'm getting the following error:

.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/aws-sdk-core-3.186.0/lib/aws-sdk-core/ini_parser.rb:28:in `block in ini_parse': undefined method `[]' for nil:NilClass (NoMethodError)

              if current_prefix.nil? && previous_item[2].strip.empty?

The code is the standard suggested by AWS when you upload secrets into the secrets manager:

require 'aws-sdk-secretsmanager'

# Retrieves secrets from AWS Secrets Manager
module SecretsManager
  def aws_secret(secret)
    client = Aws::SecretsManager::Client.new(region: 'eu-central-1')

    begin
      get_secret_value_response = client.get_secret_value(secret_id: secret)
    rescue StandardError => e
      raise e
    end

    get_secret_value_response.secret_string
  end

  def secrets
    secrets ||= aws_secret('test')
    JSON.parse(secrets)
  end
end

My guess is the SDK is failing trying to parse the ~/.aws/config file but there is nothing wrong with that file as far as I can tell and I can log in into AWS and use the SecretsManager with CLI to retrieve secrets successfully.

Any ideas? Thanks!

1

There are 1 best solutions below

0
On

I managed to reproduce the issue on my machine.

This was the ~/.aws/config that made the error happen. Notice the empty line between aws_access_key_id and aws_secret_access_key and the spaces just before the aws_secret_access_key.

[default]
region = ap-northeast-1
aws_access_key_id = AKIABLAHBLAHBLAH

 aws_secret_access_key = secretSECRET+secret/SECRET
/Users/xxxx/.rvm/gems/ruby-3.2.2/gems/aws-sdk-core-3.186.0/lib/aws-sdk-core/ini_parser.rb:28:in `block in ini_parse': undefined method `[]' for nil:NilClass (NoMethodError)

              if current_prefix.nil? && previous_item[2].strip.empty?

By removing the empty line and unnecessary spaces, the SDK could parse my configuration without issue.

[default]
region = ap-northeast-1
aws_access_key_id = AKIABLAHBLAHBLAH
aws_secret_access_key = secretSECRET+secret/SECRET