Skip to content

Commit

Permalink
Merge pull request #20 from wjohnstondrip/configurable-aws-clients
Browse files Browse the repository at this point in the history
Allow the AWS API clients to be passed in when configuring
  • Loading branch information
adorechic authored Apr 5, 2024
2 parents 88e8208 + 5e8f36a commit 7dd1fb6
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 4 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ RCredStash uses [aws-sdk v2](https://github.com/aws/aws-sdk-ruby), so configurat
```ruby
CredStash.configure do |config|
config.table_name = 'your_dynamodb_table_name'

# Optional, if you want to modify them, like for Localstack.
config.dynamo_client = Aws::DynamoDB::Client.new
config.kms_client = Aws::KMS::Client.new
end
```

Expand Down
4 changes: 2 additions & 2 deletions lib/cred_stash/cipher_key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class CredStash::CipherKey

attr_reader :data_key, :hmac_key, :wrapped_key

def self.generate(client: Aws::KMS::Client.new, kms_key_id: nil,
def self.generate(client: CredStash.config.kms_client, kms_key_id: nil,
context: {})
res = client.generate_data_key(
key_id: kms_key_id || DEFAULT_KMS_KEY_ID,
Expand All @@ -19,7 +19,7 @@ def self.generate(client: Aws::KMS::Client.new, kms_key_id: nil,
)
end

def self.decrypt(wrapped_key, client: Aws::KMS::Client.new, context: {})
def self.decrypt(wrapped_key, client: CredStash.config.kms_client, context: {})
res = client.decrypt(ciphertext_blob: wrapped_key, encryption_context: context)
new(
data_key: res.plaintext[0...32],
Expand Down
4 changes: 3 additions & 1 deletion lib/cred_stash/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def config
end

class Config
attr_accessor :table_name, :storage
attr_accessor :table_name, :storage, :kms_client, :dynamo_client

def initialize
reset!
Expand All @@ -19,6 +19,8 @@ def initialize
def reset!
@table_name = 'credential-store'
@storage = :dynamodb
@kms_client = Aws::KMS::Client.new
@dynamo_client = Aws::DynamoDB::Client.new
end
end
end
2 changes: 1 addition & 1 deletion lib/cred_stash/repository/dynamo_db.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module CredStash::Repository
class DynamoDB
def initialize(client: nil)
@client = client || Aws::DynamoDB::Client.new
@client = client || CredStash.config.dynamo_client
end

def get(name, version: nil)
Expand Down

0 comments on commit 7dd1fb6

Please sign in to comment.