Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CredentialProvider class for library use. #85

Merged
merged 2 commits into from
Sep 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/awskeyring.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def self.init_keychain(awskeyring:)
prefs = {
awskeyring: awskeyring,
keyage: DEFAULT_KEY_AGE,
browser: DEFAULT_BROWSER_LIST,
console: DEFAULT_CONSOLE_LIST
}
File.new(Awskeyring::PREFS_FILE, 'w').write JSON.dump(prefs)
Expand Down
29 changes: 29 additions & 0 deletions lib/awskeyring/credential_provider.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

require 'aws-sdk-core'
require 'awskeyring'

module Awskeyring
# Provide a credential provider for use as a library, eg.
# require 'awskeyring/credential_provider'
# client = Aws::STS::Client.new(
# credentials: Awskeyring::CredentialProvider.new("company-acc")
# )
class CredentialProvider
include Aws::CredentialProvider

attr_accessor :account

def initialize(account)
@account = account
end

# returns a new Aws::Credentials object
def credentials
cred = Awskeyring.get_valid_creds(account: account)
Aws::Credentials.new(cred[:key],
cred[:secret],
cred[:token])
end
end
end
25 changes: 25 additions & 0 deletions spec/lib/awskeyring/credential_provider_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

require 'spec_helper'
require_relative '../../../lib/awskeyring/credential_provider'

describe Awskeyring::CredentialProvider do
subject(:credential_provider) { described_class }

context 'when getting credentials' do
before do
allow(Awskeyring).to receive(:get_valid_creds).with(account: 'test').and_return(
account: 'test',
key: 'AKIATESTTEST',
secret: 'biglongbase64',
token: nil,
updated: Time.parse('2011-08-01T22:20:01Z')
)
end

it 'gets a credential_provider object' do
expect(credential_provider.new('test').credentials)
.to be_a Aws::Credentials
end
end
end
1 change: 1 addition & 0 deletions spec/lib/awskeyring_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@

it 'creates a new file' do
expect(awskeyring.init_keychain(awskeyring: 'awskeyringtest')).to eq(write_success)
expect(prefs_file).to have_received(:write).with(/browser/)
end
end

Expand Down