-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cf12113
commit aec9457
Showing
9 changed files
with
246 additions
and
53 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Unreleased Changes | ||
------------------ | ||
|
||
* Feature - Add an `AuthTokenGenerator` to generate auth tokens for `DbConnect` and `DbConnectAdmin` actions. |
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,22 @@ | ||
# frozen_string_literal: true | ||
|
||
# WARNING ABOUT GENERATED CODE | ||
# | ||
# This file is generated. See the contributing guide for more information: | ||
# https://github.com/aws/aws-sdk-ruby/blob/version-3/CONTRIBUTING.md | ||
# | ||
# WARNING ABOUT GENERATED CODE | ||
|
||
|
||
require 'aws-sdk-core' | ||
require 'aws-sigv4' | ||
|
||
Aws::Plugins::GlobalConfiguration.add_identifier(:dsql) | ||
|
||
module Aws::DSQL | ||
# this should get replaced by build | ||
|
||
GEM_VERSION = '1.0.0' | ||
end | ||
|
||
require_relative 'aws-sdk-dsql/customizations' |
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,3 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'aws-sdk-dsql/customizations/auth_token_generator' |
70 changes: 70 additions & 0 deletions
70
gems/aws-sdk-dsql/lib/aws-sdk-dsql/customizations/auth_token_generator.rb
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,70 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'aws-sigv4' | ||
|
||
module Aws | ||
module DSQL | ||
# A utility class that generates an auth token that supports database | ||
# logins for DSQL clusters. IAM credentials are used for authentication | ||
# instead of the database password. | ||
class AuthTokenGenerator | ||
# @option options [Credentials] :credentials An object that | ||
# responds to `#credentials` returning another object that responds to | ||
# `#access_key_id`, `#secret_access_key`, and `#session_token`. | ||
def initialize(options = {}) | ||
@credentials = options.fetch(:credentials) | ||
end | ||
|
||
# Generates an auth token for the DbConnect action. | ||
# | ||
# @param [Hash] options | ||
# @option options [String] :region The AWS region where the DSQL Cluster | ||
# is hosted. Defaults to the region of the client. | ||
# @option options [String] :endpoint The DSQL endpoint host name. | ||
# @option options [Integer] :expires_in (900) The number of seconds the | ||
# presigned URL is valid for. | ||
# @return [String] | ||
def generate_db_connect_auth_token(options = {}) | ||
presigned_token(options, 'DbConnect') | ||
end | ||
|
||
# Generates an auth token for the DbConnectAdmin action. | ||
# | ||
# @param [Hash] options | ||
# @option options [String] :region The AWS region where the DSQL Cluster | ||
# is hosted. Defaults to the region of the client. | ||
# @option options [String] :endpoint The DSQL endpoint host name. | ||
# @option options [Integer] :expires_in (900) The number of seconds the | ||
# token is valid for. | ||
# @return [String] | ||
def generate_db_connect_admin_auth_token(options = {}) | ||
presigned_token(options, 'DbConnectAdmin') | ||
end | ||
|
||
private | ||
|
||
def presigned_token(options, action) | ||
region = options.fetch(:region) | ||
endpoint = options.fetch(:endpoint) | ||
|
||
param_list = Aws::Query::ParamList.new | ||
param_list.set('Action', action) | ||
|
||
signer = Aws::Sigv4::Signer.new( | ||
service: 'dsql', | ||
region: region, | ||
credentials_provider: @credentials | ||
) | ||
|
||
presigned_url = signer.presign_url( | ||
http_method: 'GET', | ||
url: "https://#{endpoint}/?#{param_list}", | ||
body: '', | ||
expires_in: options[:expires_in] | ||
).to_s | ||
# Remove extra scheme for token | ||
presigned_url[8..-1] | ||
end | ||
end | ||
end | ||
end |
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,81 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative 'spec_helper' | ||
|
||
module Aws | ||
module DSQL | ||
describe AuthTokenGenerator do | ||
let(:generator) do | ||
AuthTokenGenerator.new( | ||
credentials: Credentials.new('akid', 'skid') | ||
) | ||
end | ||
|
||
describe 'initialize' do | ||
it 'requires :credentials' do | ||
expect { AuthTokenGenerator.new }.to raise_error(KeyError) | ||
end | ||
end | ||
|
||
describe 'generate_db_connect_auth_token' do | ||
it 'requires region and endpoint' do | ||
expect do | ||
generator.generate_db_connect_auth_token(region: 'us-west-2') | ||
end.to raise_error(KeyError) | ||
expect do | ||
generator.generate_db_connect_auth_token( | ||
endpoint: 'peccy.dsql.us-east-1.on.aws' | ||
) | ||
end.to raise_error(KeyError) | ||
end | ||
|
||
it 'generates a valid token' do | ||
now = Time.parse('20240827T000000Z') | ||
allow(Time).to receive(:now).and_return(now) | ||
|
||
region = 'us-east-1' | ||
endpoint = 'peccy.dsql.us-east-1.on.aws' | ||
token = generator.generate_db_connect_auth_token( | ||
region: region, | ||
endpoint: endpoint, | ||
expires_in: 450 | ||
) | ||
expect(token).to match(/#{endpoint}\/\?Action=DbConnect/) | ||
expect(token).to match(/X-Amz-Credential=akid%2F#{now.utc.strftime('%Y%m%d')}%2F#{region}%2Fdsql%2Faws4_request/) | ||
expect(token).to match(/X-Amz-Expires=450/) | ||
expect(token).not_to match(/http[s?]:\/\//) | ||
end | ||
end | ||
|
||
describe 'db_connect_admin_auth_token' do | ||
it 'requires region and endpoint' do | ||
expect do | ||
generator.generate_db_connect_admin_auth_token(region: 'us-west-2') | ||
end.to raise_error(KeyError) | ||
expect do | ||
generator.generate_db_connect_admin_auth_token( | ||
endpoint: 'peccy.dsql.us-east-1.on.aws' | ||
) | ||
end.to raise_error(KeyError) | ||
end | ||
|
||
it 'generates a valid token' do | ||
now = Time.parse('20240827T000000Z') | ||
allow(Time).to receive(:now).and_return(now) | ||
|
||
region = 'us-east-1' | ||
endpoint = 'peccy.dsql.us-east-1.on.aws' | ||
token = generator.generate_db_connect_admin_auth_token( | ||
region: region, | ||
endpoint: endpoint, | ||
expires_in: 450 | ||
) | ||
expect(token).to match(/#{endpoint}\/\?Action=DbConnectAdmin/) | ||
expect(token).to match(/X-Amz-Credential=akid%2F#{now.utc.strftime('%Y%m%d')}%2F#{region}%2Fdsql%2Faws4_request/) | ||
expect(token).to match(/X-Amz-Expires=450/) | ||
expect(token).not_to match(/http[s?]:\/\//) | ||
end | ||
end | ||
end | ||
end | ||
end |
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,18 @@ | ||
# frozen_string_literal: true | ||
|
||
# WARNING ABOUT GENERATED CODE | ||
# | ||
# This file is generated. See the contributing guide for more information: | ||
# https://github.com/aws/aws-sdk-ruby/blob/version-3/CONTRIBUTING.md | ||
# | ||
# WARNING ABOUT GENERATED CODE | ||
|
||
require_relative '../../aws-sdk-core/spec/shared_spec_helper' | ||
|
||
$:.unshift(File.expand_path('../../lib', __FILE__)) | ||
$:.unshift(File.expand_path('../../../aws-sdk-core/lib', __FILE__)) | ||
$:.unshift(File.expand_path('../../../aws-sigv4/lib', __FILE__)) | ||
|
||
require 'rspec' | ||
require 'webmock/rspec' | ||
require 'aws-sdk-dsql' |
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