Skip to content

Commit

Permalink
GenerateDataKeyWithoutPlaintext does not send KeySpec when sending Nu…
Browse files Browse the repository at this point in the history
…mberOfBytes (#15)

This behavior is now consistent with GenerateDataKey and fixes `GenerateDataKeyWithoutPlaintext` with a `NumberOfBytes` specified causing KMS to return `{"ValidationException", "Please specify either number of bytes or key spec."}`.
  • Loading branch information
stevegrossi authored Aug 17, 2024
1 parent 3a072ca commit bdbc084
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lib/ex_aws/kms.ex
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,14 @@ defmodule ExAws.KMS do
|> Map.merge(%{
"Action" => "GenerateDataKeyWithoutPlaintext",
"Version" => @version,
"KeyId" => key_id,
"KeySpec" => opts[:key_spec] || "AES_256"
"KeyId" => key_id
})

request(:generate_data_key_without_plaintext, query_params)
if !Map.has_key?(query_params, "KeySpec") and !Map.has_key?(query_params, "NumberOfBytes") do
request(:generate_data_key_without_plaintext, Map.put(query_params, "KeySpec", "AES_256"))
else
request(:generate_data_key_without_plaintext, query_params)
end
end

@doc "Generates an unpredictable byte string"
Expand Down
30 changes: 30 additions & 0 deletions test/lib/kms_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,36 @@ defmodule ExAws.KMSTest do
key_spec: "AES_128",
number_of_bytes: 16
)

operation_with_number_of_bytes =
ExAws.KMS.generate_data_key_without_plaintext("key-id",
encryption_context: %{"key" => "value"},
grant_tokens: ["token"],
number_of_bytes: 32
)

assert %ExAws.Operation.JSON{
before_request: nil,
data: %{
"Action" => "GenerateDataKeyWithoutPlaintext",
"Version" => @version,
"KeyId" => "key-id",
"EncryptionContext" => %{"key" => "value"},
"GrantTokens" => ["token"],
"NumberOfBytes" => 32
},
headers: [
{"x-amz-target", "TrentService.GenerateDataKeyWithoutPlaintext"},
{"content-type", "application/x-amz-json-1.0"}
],
http_method: :post,
parser: _,
path: "/",
service: :kms,
stream_builder: nil
} = operation_with_number_of_bytes

refute operation_with_number_of_bytes.data["KeySpec"]
end

test "GenerateRandom" do
Expand Down

0 comments on commit bdbc084

Please sign in to comment.