Skip to content

Commit 76999f9

Browse files
committed
first pass at get_public_key requests and mocks
1 parent 6e0a714 commit 76999f9

File tree

7 files changed

+94
-4
lines changed

7 files changed

+94
-4
lines changed

lib/fog/aws/kms.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class KMS < Fog::Service
2020
request :list_keys
2121
request :create_key
2222
request :describe_key
23+
request :get_public_key
2324
request :schedule_key_deletion
2425

2526
model_path 'fog/aws/models/kms'
@@ -31,7 +32,8 @@ def self.data
3132
@data ||= Hash.new do |hash, region|
3233
hash[region] = Hash.new do |region_hash, access_key|
3334
region_hash[access_key] = {
34-
:keys => {},
35+
keys: {},
36+
pkeys: {}
3537
}
3638
end
3739
end
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
module Fog
2+
module Parsers
3+
module AWS
4+
module KMS
5+
class GetPublicKey < Fog::Parsers::Base
6+
def reset
7+
@response = {}
8+
end
9+
10+
def start_element(name, attrs = [])
11+
super
12+
case name
13+
when 'EncryptionAlgorithms', 'KeyAgreementAlgorithms', 'SigningAlgorithms'
14+
@response[name] = []
15+
end
16+
end
17+
18+
def end_element(name)
19+
case name
20+
when 'KeyId', 'KeySpec', 'KeyUsage', 'PublicKey'
21+
@response[name] = value
22+
when 'EncryptionAlgorithms', 'KeyAgreementAlgorithms', 'SigningAlgorithms'
23+
@response[name] << value
24+
end
25+
end
26+
end
27+
end
28+
end
29+
end
30+
end

lib/fog/aws/requests/kms/create_key.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ class Real
1919
# https://docs.aws.amazon.com/kms/latest/APIReference/API_CreateKey.html
2020
def create_key(*args)
2121
options = Fog::AWS::KMS.parse_create_key_args(args)
22-
request(
22+
request({
2323
'Action' => 'CreateKey',
2424
:parser => Fog::Parsers::AWS::KMS::DescribeKey.new
25-
).merge!(options)
25+
}.merge!(options))
2626
end
2727
end
2828

@@ -52,6 +52,14 @@ def create_key(*args)
5252

5353
self.data[:keys][key_id] = key
5454

55+
spec = key['KeySpec']
56+
size = spec.split('_').last
57+
spec = spec.split("_#{size}").first
58+
case spec
59+
when 'RSA'
60+
self.data[:pkeys][key_id] = OpenSSL::PKey::RSA.generate(size)
61+
end
62+
5563
response.body = { 'KeyMetadata' => key }
5664
response
5765
end
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
module Fog
2+
module AWS
3+
class KMS
4+
class Real
5+
require 'fog/aws/parsers/kms/get_public_key'
6+
7+
def get_public_key(identifier, grant_tokens = nil)
8+
request(
9+
'Action' => 'GetPublicKey',
10+
'GrantTokens' => grant_tokens,
11+
'KeyId' => identifier,
12+
:parser => Fog::Parsers::AWS::KMS::GetPublicKey.new
13+
)
14+
end
15+
end
16+
17+
class Mock
18+
def get_public_key(identifier, grant_tokens = [])
19+
response = Excon::Response.new
20+
key = self.data[:keys][identifier]
21+
pkey = self.data[:pkeys][identifier]
22+
23+
response.body = {
24+
'KeyId' => key['KeyId'],
25+
'KeyUsage' => key['KeyUsage'],
26+
'KeySpec' => key['KeySpec'],
27+
'PublicKey' => Base64.strict_encode64(pkey.public_to_der),
28+
'SigningAlgorithms' => key['SigningAlgorithms']
29+
}
30+
response
31+
end
32+
end
33+
end
34+
end
35+
end

lib/fog/aws/requests/kms/list_keys.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ module Fog
22
module AWS
33
class KMS
44
class Real
5-
65
require 'fog/aws/parsers/kms/list_keys'
76

87
def list_keys(options={})

tests/requests/kms/helper.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ module Formats
2020
}
2121
}.freeze
2222

23+
GET_PUBLIC_KEY = {
24+
'EncryptionAlgorithms' => Fog::Nullable::Array,
25+
'KeyAgreementAlgorithms' => Fog::Nullable::Array,
26+
'KeyId' => String,
27+
'KeySpec' => String,
28+
'KeyUsage' => String,
29+
'PublicKey' => String,
30+
'SigningAlgorithms' => Fog::Nullable::Array
31+
}.freeze
32+
2333
LIST_KEYS = {
2434
'Keys' => [{ 'KeyArn' => String, 'KeyId' => String }],
2535
'Marker' => Fog::Nullable::String,

tests/requests/kms/key_tests.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616
result
1717
end
1818

19+
tests('#get_public_key').data_matches_schema(AWS::KMS::Formats::GET_PUBLIC_KEY) do
20+
result = Fog::AWS[:kms].get_public_key(key_id).body
21+
returns(key_id) { result['KeyId'] }
22+
result
23+
end
24+
1925
tests('#list_keys').data_matches_schema(AWS::KMS::Formats::LIST_KEYS) do
2026
Fog::AWS[:kms].list_keys.body
2127
end

0 commit comments

Comments
 (0)