Skip to content

Commit be36879

Browse files
author
Matt Muller
committed
PR feedback #1
1 parent 743d602 commit be36879

5 files changed

Lines changed: 53 additions & 10 deletions

File tree

gems/aws-sdk-core/lib/aws-sdk-core/lru_cache.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ module Aws
44
# @api private
55
# A simple thread safe LRU cache
66
class LRUCache
7+
# @param [Hash] options
8+
# @option options [Integer] :max_entries (100) Maximum number of entries
9+
# @option options [Integer] :expiration (nil) Expiration time in seconds
710
def initialize(options = {})
811
@max_entries = options[:max_entries] || 100
912
@expiration = options[:expiration]

gems/aws-sdk-s3/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Unreleased Changes
22
------------------
33

4-
* Feature - Support S3 Access Grants authentication. Access Grants can be enabled with the `s3_access_grants` option, and custom options can be passed into the `access_grants_credentials_provider` option. This feature is enabled if `aws-sdk-s3control` is installed.
4+
* Feature - Support S3 Access Grants authentication. Access Grants can be enabled with the `access_grants` option, and custom options can be passed into the `access_grants_credentials_provider` option. This feature requires `aws-sdk-s3control` to be installed.
55

66
1.148.0 (2024-04-25)
77
------------------

gems/aws-sdk-s3/lib/aws-sdk-s3/access_grants_credentials_provider.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
module Aws
44
module S3
55
# @api private
6-
ACCESS_GRANTS_CREDENTIALS_CACHE = LRUCache.new
6+
ACCESS_GRANTS_CREDENTIALS_CACHE = LRUCache.new(max_entries: 100)
77
# @api private
8-
ACCESS_GRANTS_ACCOUNT_ID_CACHE = LRUCache.new(expiration: 60 * 10)
8+
ACCESS_GRANTS_ACCOUNT_ID_CACHE = LRUCache.new(
9+
max_entries: 100,
10+
expiration: 60 * 10
11+
)
912

1013
# Returns Credentials class for S3 Access Grants. Accepts GetDataAccess
1114
# params and other configuration as options. See

gems/aws-sdk-s3/lib/aws-sdk-s3/express_credentials_provider.rb

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
module Aws
44
module S3
55
# @api private
6-
EXPRESS_CREDENTIALS_CACHE = LRUCache.new
6+
EXPRESS_CREDENTIALS_CACHE = LRUCache.new(max_entries: 100)
77

88
# Returns Credentials class for S3 Express. Accepts CreateSession
99
# params as options. See {Client#create_session} for details.
@@ -18,21 +18,33 @@ class ExpressCredentialsProvider
1818
# credentials are refreshed.
1919
def initialize(options = {})
2020
@client = options.delete(:client)
21-
@caching = options.delete(:caching) || true
21+
@caching = options.delete(:caching) != false
2222
@options = options
2323
@cache = EXPRESS_CREDENTIALS_CACHE
2424
end
2525

2626
def express_credentials_for(bucket)
27-
@cache[bucket] || new_credentials_for(bucket)
27+
if @caching
28+
cached_credentials_for(bucket)
29+
else
30+
new_credentials_for(bucket)
31+
end
2832
end
2933

3034
attr_accessor :client
3135

3236
private
3337

38+
def cached_credentials_for(bucket)
39+
if @cache.key?(bucket)
40+
@cache[bucket]
41+
else
42+
@cache[bucket] = new_credentials_for(bucket)
43+
end
44+
end
45+
3446
def new_credentials_for(bucket)
35-
@cache[bucket] = ExpressCredentials.new(
47+
ExpressCredentials.new(
3648
bucket: bucket,
3749
client: @client,
3850
**@options

gems/aws-sdk-s3/spec/express_credentials_provider_spec.rb

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,48 @@ module S3
1616
end
1717

1818
describe '#express_credentials_for' do
19-
before(:each) do
19+
after do
2020
EXPRESS_CREDENTIALS_CACHE.clear
21+
end
22+
23+
it 'returns a new set of credentials for the bucket' do
2124
expect(ExpressCredentials).to receive(:new)
2225
.with(bucket: 'bucket', client: client, session_mode: 'ReadWrite')
2326
.and_return(express_credentials)
24-
end
2527

26-
it 'returns a new set of credentials for the bucket' do
2728
expect(subject.express_credentials_for('bucket'))
2829
.to eq(express_credentials)
2930
end
3031

3132
it 'returns the same set of credentials for the bucket' do
33+
expect(ExpressCredentials).to receive(:new)
34+
.with(bucket: 'bucket', client: client, session_mode: 'ReadWrite')
35+
.and_return(express_credentials)
36+
3237
credentials = subject.express_credentials_for('bucket')
3338
expect(subject.express_credentials_for('bucket'))
3439
.to be(credentials)
3540
end
41+
42+
context 'caching disabled' do
43+
subject do
44+
ExpressCredentialsProvider.new(
45+
client: client,
46+
session_mode: 'ReadWrite',
47+
caching: false
48+
)
49+
end
50+
51+
it 'returns a different set of credentials for the bucket' do
52+
expect(ExpressCredentials).to receive(:new)
53+
.with(bucket: 'bucket', client: client, session_mode: 'ReadWrite')
54+
.and_return(express_credentials).twice
55+
56+
credentials = subject.express_credentials_for('bucket')
57+
expect(subject.express_credentials_for('bucket'))
58+
.to be(credentials)
59+
end
60+
end
3661
end
3762

3863
describe '#client' do

0 commit comments

Comments
 (0)