Skip to content

Commit d833a2d

Browse files
Merge pull request #97 from patterninc/remove-aws-sts-signature
Remove AWS STS Signature
2 parents 66f1d19 + 9f23933 commit d833a2d

File tree

5 files changed

+23
-52
lines changed

5 files changed

+23
-52
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 2.4.13
2+
3+
- Remove AWS STS Signature in requests [#97](https://github.com/patterninc/muffin_man/pull/97)
4+
15
# 2.4.12
26

37
- [#95](https://github.com/patterninc/muffin_man/pull/95)

README.md

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,7 @@ credentials = {
6666
refresh_token: LWA_REFRESH_TOKEN,
6767
client_id: CLIENT_ID,
6868
client_secret: CLIENT_SECRET,
69-
aws_access_key_id: AWS_ACCESS_KEY_ID,
70-
aws_secret_access_key: AWS_SECRET_ACCESS_KEY,
7169
region: REGION, # This can be one of ['na', 'eu', 'fe'] and defaults to 'na'
72-
sts_iam_role_arn: STS_IAM_ROLE_ARN, # Optional
7370
access_token_cache_key: SELLING_PARTNER_ID, # Optional if you want access token caching
7471
}
7572
client = MuffinMan::Solicitations::V1.new(credentials)
@@ -115,9 +112,6 @@ To retrieve the refresh token from an LWA Website authorization workflow, you ca
115112
credentials = {
116113
client_id: CLIENT_ID,
117114
client_secret: CLIENT_SECRET,
118-
aws_access_key_id: AWS_ACCESS_KEY_ID,
119-
aws_secret_access_key: AWS_SECRET_ACCESS_KEY,
120-
sts_iam_role_arn: STS_IAM_ROLE_ARN, # Optional
121115
scope: 'sellingpartnerapi::migration' # Grantless scope for MWS migration
122116
}
123117
client = MuffinMan::Authorization::V1.new(credentials)

lib/muffin_man/sp_api_client.rb

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66

77
module MuffinMan
88
class SpApiClient
9-
attr_reader :refresh_token, :client_id, :client_secret, :aws_access_key_id,
10-
:aws_secret_access_key, :sts_iam_role_arn, :sandbox, :config,
9+
attr_reader :refresh_token, :client_id, :client_secret, :sandbox, :config,
1110
:region, :request_type, :local_var_path, :query_params,
1211
:request_body, :scope, :access_token_cache_key, :credentials,
1312
:pii_data_elements
@@ -26,9 +25,6 @@ def initialize(credentials, sandbox = false)
2625
@refresh_token = credentials[:refresh_token]
2726
@client_id = credentials[:client_id]
2827
@client_secret = credentials[:client_secret]
29-
@aws_access_key_id = credentials[:aws_access_key_id]
30-
@aws_secret_access_key = credentials[:aws_secret_access_key]
31-
@sts_iam_role_arn = credentials[:sts_iam_role_arn]
3228
@region = credentials[:region] || "na"
3329
@scope = credentials[:scope]
3430
@access_token_cache_key = credentials[:access_token_cache_key]
@@ -135,43 +131,17 @@ def request_grantless_access_token
135131
JSON.parse(response.body)
136132
end
137133

138-
def request_sts_token
139-
client = Aws::STS::Client.new(
140-
region: derive_aws_region,
141-
credentials: Aws::Credentials.new(aws_access_key_id, aws_secret_access_key),
142-
http_wire_trace: ENV.fetch("AWS_DEBUG", nil) == "true" || false
143-
)
144-
client.assume_role(role_arn: sts_iam_role_arn, role_session_name: SecureRandom.uuid)
145-
end
146-
147-
def signed_request
148-
request_config = {
149-
service: SERVICE_NAME,
150-
region: derive_aws_region,
151-
endpoint: sp_api_host
152-
}
153-
if sts_iam_role_arn.nil?
154-
request_config[:access_key_id] = aws_access_key_id
155-
request_config[:secret_access_key] = aws_secret_access_key
156-
else
157-
request_config[:credentials_provider] = request_sts_token
158-
end
159-
signer = Aws::Sigv4::Signer.new(request_config)
160-
signer.sign_request(http_method: request_type, url: request.url, body: request_body&.to_json)
161-
end
162-
163134
def headers
164135
if requires_rdt_token_for_pii?
165136
access_token = retrieve_rdt_access_token || retrieve_lwa_access_token
166137
else
167138
access_token = scope ? retrieve_grantless_access_token : retrieve_lwa_access_token
168139
end
169-
headers = {
140+
{
170141
"x-amz-access-token" => access_token,
171142
"user-agent" => "MuffinMan/#{VERSION} (Language=Ruby)",
172143
"content-type" => "application/json"
173144
}
174-
signed_request.headers.merge(headers)
175145
end
176146

177147
def requires_rdt_token_for_pii?

spec/muffin_man/sp_api_client_spec.rb

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
refresh_token: "a-refresh-token",
66
client_id: "a-client-id",
77
client_secret: "a-client-secret",
8-
aws_access_key_id: "an-aws-access-key-id",
9-
aws_secret_access_key: "an-aws-secret-access-key",
108
access_token_cache_key: "a-selling_partner_id"
119
}
1210
end
@@ -20,8 +18,6 @@
2018
expect(client.refresh_token).not_to be_nil
2119
expect(client.client_id).not_to be_nil
2220
expect(client.client_secret).not_to be_nil
23-
expect(client.aws_access_key_id).not_to be_nil
24-
expect(client.aws_secret_access_key).not_to be_nil
2521
end
2622

2723
it "sets the Typhoeus user agent to an empty string" do
@@ -52,8 +48,10 @@ def make_a_request(region = "na", query_params: {})
5248

5349
it "gets an access token and signs the headers" do
5450
expect(Typhoeus).to receive(:get)
55-
.with("https://#{hostname}/some_path", headers: hash_including("x-amz-access-token" => fake_lwa_access_token,
56-
"authorization" => a_string_including("SignedHeaders=host;x-amz-content-sha256;x-amz-date")))
51+
.with("https://#{hostname}/some_path",
52+
hash_including(
53+
headers: hash_including("x-amz-access-token" => fake_lwa_access_token)
54+
))
5755
client.make_a_request
5856
end
5957

@@ -83,7 +81,10 @@ def make_a_request(region = "na", query_params: {})
8381
expect_any_instance_of(MockRedis).to receive(:set).with("SP-TOKEN-#{credentials[:access_token_cache_key]}",
8482
fake_lwa_access_token)
8583
expect(Typhoeus).to receive(:get)
86-
.with("https://#{hostname}/some_path", headers: hash_including("x-amz-access-token" => fake_lwa_access_token))
84+
.with("https://#{hostname}/some_path",
85+
hash_including(
86+
headers: hash_including("x-amz-access-token" => fake_lwa_access_token)
87+
))
8788
client.make_a_request
8889
end
8990
end
@@ -97,26 +98,30 @@ def make_a_request(region = "na", query_params: {})
9798
it "uses the stored token" do
9899
expect_any_instance_of(MockRedis).to receive(:get).with("SP-TOKEN-#{credentials[:access_token_cache_key]}").and_return(another_fake_lwa_access_token)
99100
expect(Typhoeus).to receive(:get)
100-
.with("https://#{hostname}/some_path", headers: hash_including("x-amz-access-token" => another_fake_lwa_access_token))
101+
.with("https://#{hostname}/some_path",
102+
hash_including(
103+
headers: hash_including("x-amz-access-token" => another_fake_lwa_access_token)
104+
))
101105
client.make_a_request
102106
end
103107
end
104108

105109
context "when using the sandbox environment" do
106110
let(:sandbox) { true }
107111
it "correctly builds the canonical api hostname" do
108-
expect(Typhoeus).to receive(:get).with("https://sandbox.#{hostname}/some_path", headers: hash_including({}))
112+
expect(Typhoeus).to receive(:get).with("https://sandbox.#{hostname}/some_path",
113+
hash_including(headers: hash_including({})))
109114
client.make_a_request
110115
end
111116
end
112117

113118
context "multiple requests with the same client instance" do
114119
it "uses correct query params for each new request" do
115120
expect(Typhoeus).to receive(:get).with("https://#{hostname}/some_path?flavor=blueberry",
116-
headers: hash_including({}))
121+
hash_including(headers: hash_including({})))
117122
client.make_a_request(query_params: { "flavor" => "blueberry" })
118123
expect(Typhoeus).to receive(:get).with("https://#{hostname}/some_path?flavor=chocolate",
119-
headers: hash_including({}))
124+
hash_including(headers: hash_including({})))
120125
client.make_a_request(query_params: { "flavor" => "chocolate" })
121126
end
122127
end

spec/support/sp_api_helpers.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -523,9 +523,7 @@ def credentials
523523
{
524524
refresh_token: "a-refresh-token",
525525
client_id: "a-client-id",
526-
client_secret: "a-client-secret",
527-
aws_access_key_id: "an-aws-access-key-id",
528-
aws_secret_access_key: "an-aws-secret-access-key"
526+
client_secret: "a-client-secret"
529527
}
530528
end
531529

0 commit comments

Comments
 (0)