Skip to content

Commit bb6d655

Browse files
committed
Remove security token param if nil
1 parent efbe3c9 commit bb6d655

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

lib/kafka/sasl/awsmskiam.rb

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -63,39 +63,41 @@ def digest
6363

6464
def authentication_payload(host:, time_now:)
6565
{
66-
'version': "2020_10_22",
67-
'host': host,
68-
'user-agent': "ruby-kafka",
69-
'action': "kafka-cluster:Connect",
70-
'x-amz-algorithm': "AWS4-HMAC-SHA256",
71-
'x-amz-credential': @access_key_id + "/" + time_now.strftime("%Y%m%d") + "/" + @aws_region + "/kafka-cluster/aws4_request",
72-
'x-amz-date': time_now.strftime("%Y%m%dT%H%M%SZ"),
73-
'x-amz-signedheaders': "host",
74-
'x-amz-expires': "900",
75-
'x-amz-security-token': @session_token,
76-
'x-amz-signature': signature(host: host, time_now: time_now)
77-
}.to_json
66+
'version' => "2020_10_22",
67+
'host' => host,
68+
'user-agent' => "ruby-kafka",
69+
'action' => "kafka-cluster:Connect",
70+
'x-amz-algorithm' => "AWS4-HMAC-SHA256",
71+
'x-amz-credential' => @access_key_id + "/" + time_now.strftime("%Y%m%d") + "/" + @aws_region + "/kafka-cluster/aws4_request",
72+
'x-amz-date' => time_now.strftime("%Y%m%dT%H%M%SZ"),
73+
'x-amz-signedheaders' => "host",
74+
'x-amz-expires' => "900",
75+
'x-amz-security-token' => @session_token,
76+
'x-amz-signature' => signature(host: host, time_now: time_now)
77+
}.delete_if { |_, v| v.nil? }.to_json
7878
end
7979

8080
def canonical_request(host:, time_now:)
8181
"GET\n" +
82-
"/\n" +
83-
canonical_query_string(time_now: time_now) + "\n" +
84-
canonical_headers(host: host) + "\n" +
85-
signed_headers + "\n" +
86-
hashed_payload
82+
"/\n" +
83+
canonical_query_string(time_now: time_now) + "\n" +
84+
canonical_headers(host: host) + "\n" +
85+
signed_headers + "\n" +
86+
hashed_payload
8787
end
8888

8989
def canonical_query_string(time_now:)
90-
URI.encode_www_form(
90+
params = {
9191
"Action" => "kafka-cluster:Connect",
9292
"X-Amz-Algorithm" => "AWS4-HMAC-SHA256",
9393
"X-Amz-Credential" => @access_key_id + "/" + time_now.strftime("%Y%m%d") + "/" + @aws_region + "/kafka-cluster/aws4_request",
9494
"X-Amz-Date" => time_now.strftime("%Y%m%dT%H%M%SZ"),
9595
"X-Amz-Expires" => "900",
9696
"X-Amz-Security-Token" => @session_token,
9797
"X-Amz-SignedHeaders" => "host"
98-
)
98+
}.delete_if { |_, v| v.nil? }
99+
100+
URI.encode_www_form(params)
99101
end
100102

101103
def canonical_headers(host:)
@@ -112,9 +114,9 @@ def hashed_payload
112114

113115
def string_to_sign(host:, time_now:)
114116
"AWS4-HMAC-SHA256" + "\n" +
115-
time_now.strftime("%Y%m%dT%H%M%SZ") + "\n" +
116-
time_now.strftime("%Y%m%d") + "/" + @aws_region + "/kafka-cluster/aws4_request" + "\n" +
117-
bin_to_hex(digest.digest(canonical_request(host: host, time_now: time_now)))
117+
time_now.strftime("%Y%m%dT%H%M%SZ") + "\n" +
118+
time_now.strftime("%Y%m%d") + "/" + @aws_region + "/kafka-cluster/aws4_request" + "\n" +
119+
bin_to_hex(digest.digest(canonical_request(host: host, time_now: time_now)))
118120
end
119121

120122
def signature(host:, time_now:)

0 commit comments

Comments
 (0)