Class: JwtAuthCognito::SSMService

Inherits:
Object
  • Object
show all
Defined in:
lib/jwt_auth_cognito/ssm_service.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.certificate_cacheObject

Returns the value of attribute certificate_cache.



11
12
13
# File 'lib/jwt_auth_cognito/ssm_service.rb', line 11

def certificate_cache
  @certificate_cache
end

.clientObject

Returns the value of attribute client.



11
12
13
# File 'lib/jwt_auth_cognito/ssm_service.rb', line 11

def client
  @client
end

Class Method Details

.cache_statsObject

Gets cache stats



123
124
125
126
127
128
# File 'lib/jwt_auth_cognito/ssm_service.rb', line 123

def self.cache_stats
  {
    size: @certificate_cache.size,
    keys: @certificate_cache.keys
  }
end

.clear_cacheObject

Clears the certificate cache



118
119
120
# File 'lib/jwt_auth_cognito/ssm_service.rb', line 118

def self.clear_cache
  @certificate_cache.clear
end

.get_ca_certificate(cert_path, cert_name) ⇒ Object

Gets a certificate from AWS Parameter Store (compatible with auth-service) Uses the same path pattern: /$cert_path/$cert_name



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/jwt_auth_cognito/ssm_service.rb', line 47

def self.get_ca_certificate(cert_path, cert_name)
  full_path = "/#{cert_path}/#{cert_name}"

  # Check cache first
  if @certificate_cache.key?(full_path)
    puts '📋 Using cached certificate from SSM'
    return @certificate_cache[full_path]
  end

  begin
    region = ENV['AWS_REGION'] || ENV['AWS_DEFAULT_REGION'] || 'us-east-1'
    has_credentials = !(ENV.fetch('AWS_ACCESS_KEY_ID', nil) && ENV.fetch('AWS_SECRET_ACCESS_KEY', nil)).nil?

    puts "📡 Getting certificate from Parameter Store: #{full_path}"
    puts "🌍 AWS Region: #{region}"
    puts "🔑 Credentials configured: #{has_credentials ? 'Yes' : 'No (using IAM role/profile)'}"

    client = get_client
    response = client.get_parameter({
                                      name: full_path,
                                      with_decryption: true
                                    })

    raise ConfigurationError, "Certificate parameter not found or invalid: #{full_path}" unless response.parameter&.value

    # Cache the certificate
    @certificate_cache[full_path] = response.parameter.value
    puts '✅ Certificate obtained from SSM and cached'

    response.parameter.value
  rescue Aws::SSM::Errors::ParameterNotFound
    raise ConfigurationError, "Certificate parameter not found: #{full_path}"
  rescue Aws::SSM::Errors::ServiceError => e
    puts "❌ Error getting certificate from SSM (#{full_path}): #{e.message}"
    raise ConfigurationError, "Error accessing SSM: #{e.message}"
  rescue StandardError => e
    puts "❌ Error getting certificate from SSM (#{full_path}): #{e.message}"
    raise e
  end
end

.get_clientObject

Initialize the SSM client with comprehensive AWS configuration



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/jwt_auth_cognito/ssm_service.rb', line 18

def self.get_client
  @client ||= begin
    require 'aws-sdk-ssm'

    client_config = {
      region: ENV['AWS_REGION'] || ENV['AWS_DEFAULT_REGION'] || 'us-east-1'
    }

    # Add credentials if provided
    if ENV['AWS_ACCESS_KEY_ID'] && ENV['AWS_SECRET_ACCESS_KEY']
      client_config[:credentials] = Aws::Credentials.new(
        ENV['AWS_ACCESS_KEY_ID'],
        ENV['AWS_SECRET_ACCESS_KEY'],
        ENV.fetch('AWS_SESSION_TOKEN', nil)
      )
    end

    # Add endpoint if provided (for custom endpoints)
    client_config[:endpoint] = ENV['AWS_SSM_ENDPOINT'] if ENV['AWS_SSM_ENDPOINT']

    Aws::SSM::Client.new(client_config)
  end
rescue LoadError
  raise ConfigurationError,
        "aws-sdk-ssm gem is required for SSM functionality. Add 'gem \"aws-sdk-ssm\"' to your Gemfile"
end

.get_parameter(parameter_name, with_decryption = true) ⇒ Object

Gets a parameter from AWS Parameter Store



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/jwt_auth_cognito/ssm_service.rb', line 89

def self.get_parameter(parameter_name, with_decryption = true)
  # Check cache first
  return @certificate_cache[parameter_name] if @certificate_cache.key?(parameter_name)

  begin
    client = get_client
    response = client.get_parameter({
                                      name: parameter_name,
                                      with_decryption: with_decryption
                                    })

    raise ConfigurationError, "Parameter not found or invalid: #{parameter_name}" unless response.parameter&.value

    # Cache the parameter
    @certificate_cache[parameter_name] = response.parameter.value

    response.parameter.value
  rescue Aws::SSM::Errors::ParameterNotFound
    raise ConfigurationError, "Parameter not found: #{parameter_name}"
  rescue Aws::SSM::Errors::ServiceError => e
    puts "❌ Error getting parameter from SSM (#{parameter_name}): #{e.message}"
    raise ConfigurationError, "Error accessing SSM: #{e.message}"
  rescue StandardError => e
    puts "❌ Error getting parameter from SSM (#{parameter_name}): #{e.message}"
    raise e
  end
end