Skip to content

Commit

Permalink
[kms] convert create_key to options hash instead of positional arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
geemus committed Dec 10, 2024
1 parent 450979b commit dd2f554
Showing 1 changed file with 46 additions and 9 deletions.
55 changes: 46 additions & 9 deletions lib/fog/aws/requests/kms/create_key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,32 @@ class KMS
class Real
require 'fog/aws/parsers/kms/describe_key'

def create_key(policy = nil, description = nil, usage = 'ENCRYPT_DECRYPT')
# Create Key
#
# ==== Parameters
# * options<~Hash>:
# * 'Description'<~String>:
# * 'KeyUsage'<~String>:
# * 'Policy'<~String>:
# * ... (see docs from see also)
#
# === Returns
#
# ==== See Also
# https://docs.aws.amazon.com/kms/latest/APIReference/API_CreateKey.html
def create_key(*args)
options = Fog::AWS::KMS.parse_create_key_args(args)
request(
'Action' => 'CreateKey',
'Description' => description,
'KeyUsage' => usage,
'Policy' => policy,
:parser => Fog::Parsers::AWS::KMS::DescribeKey.new
)
).merge!(options)
end
end

class Mock
def create_key(_policy = nil, description = nil, usage = 'ENCRYPT_DECRYPT')
def create_key(*args)
options = Fog::AWS::KMS.parse_create_key_args(args)

response = Excon::Response.new
key_id = UUID.uuid
key_arn = Fog::AWS::Mock.arn("kms", self.account_id, "key/#{key_id}", @region)
Expand All @@ -26,12 +39,10 @@ def create_key(_policy = nil, description = nil, usage = 'ENCRYPT_DECRYPT')
'AWSAccountId' => self.account_id,
'CreationDate' => Time.now.utc,
'DeletionDate' => nil,
'Description' => description,
'Enabled' => true,
'KeyId' => key_id,
'KeyState' => 'Enabled',
'KeyUsage' => usage
}
}.merge!(options)

# @todo use default policy

Expand All @@ -41,6 +52,32 @@ def create_key(_policy = nil, description = nil, usage = 'ENCRYPT_DECRYPT')
response
end
end

# previous args (policy, description, usage) was deprecated in favor of a hash of options
def self.parse_create_key_args(args)
case args.size
when 0
{}
when 1
if args[0].is_a?(Hash)
args[0]
else
Fog::Logger.deprecation("create_key with distinct arguments is deprecated, use options hash instead [light_black](#{caller.first})[/]")
{
'Policy' => args[0]
}
end
when 2, 3
Fog::Logger.deprecation("create_key with distinct arguments is deprecated, use options hash instead [light_black](#{caller.first})[/]")
{
'Policy' => args[0],
'Description' => args[1],
'KeyUsage' => args[2] || 'ENCRYPT_DECRYPT'
}
else
raise "Unknown argument style: #{args.inspect}, use options hash instead."
end
end
end
end
end

0 comments on commit dd2f554

Please sign in to comment.