11package kms
22
33import (
4+ "context"
45 "crypto/ecdsa"
56 "errors"
67 "time"
78
8- "github.com/aws/aws-sdk-go/aws"
9- "github.com/aws/aws-sdk-go/aws/awserr"
10- kmslib "github.com/aws/aws-sdk-go/service/kms"
9+ "github.com/aws/aws-sdk-go-v2/service/kms"
10+ kmstypes "github.com/aws/aws-sdk-go-v2/service/kms/types"
1111 "github.com/ethereum/go-ethereum/crypto"
1212)
1313
@@ -28,25 +28,31 @@ func NewFakeKMSClient(keys []Key) (*FakeKMSClient, error) {
2828 }, nil
2929}
3030
31- func (m * FakeKMSClient ) GetPublicKey (input * kmslib.GetPublicKeyInput ) (* kmslib.GetPublicKeyOutput , error ) {
31+ func (m * FakeKMSClient ) GetPublicKey (ctx context.Context , input * kms.GetPublicKeyInput , opts ... func (* kms.Options )) (* kms.GetPublicKeyOutput , error ) {
32+ if input .KeyId == nil {
33+ return nil , errors .New ("key ID is required" )
34+ }
3235 for _ , key := range m .keys {
33- if aws . StringValue ( input .KeyId ) == key .KeyID {
36+ if * input .KeyId == key .KeyID {
3437 asn1PubKey , err := SEC1ToASN1PublicKey (crypto .FromECDSAPub (& key .PrivateKey .PublicKey ))
3538 if err != nil {
3639 return nil , err
3740 }
38- return & kmslib .GetPublicKeyOutput {
39- KeyId : aws . String ( key .KeyID ) ,
41+ return & kms .GetPublicKeyOutput {
42+ KeyId : & key .KeyID ,
4043 PublicKey : asn1PubKey ,
4144 }, nil
4245 }
4346 }
44- return nil , awserr . New ( kmslib . ErrCodeNotFoundException , "key not found" , errors .New ("key not found" ) )
47+ return nil , errors .New ("key not found" )
4548}
4649
47- func (m * FakeKMSClient ) Sign (input * kmslib.SignInput ) (* kmslib.SignOutput , error ) {
50+ func (m * FakeKMSClient ) Sign (ctx context.Context , input * kms.SignInput , opts ... func (* kms.Options )) (* kms.SignOutput , error ) {
51+ if input .KeyId == nil {
52+ return nil , errors .New ("key ID is required" )
53+ }
4854 for _ , key := range m .keys {
49- if aws . StringValue ( input .KeyId ) == key .KeyID {
55+ if * input .KeyId == key .KeyID {
5056 sig , err := crypto .Sign (input .Message , key .PrivateKey )
5157 if err != nil {
5258 return nil , err
@@ -55,40 +61,45 @@ func (m *FakeKMSClient) Sign(input *kmslib.SignInput) (*kmslib.SignOutput, error
5561 if err != nil {
5662 return nil , err
5763 }
58- return & kmslib .SignOutput {
59- KeyId : aws . String ( key .KeyID ) ,
64+ return & kms .SignOutput {
65+ KeyId : & key .KeyID ,
6066 Signature : asn1Sig ,
6167 }, nil
6268 }
6369 }
64- return nil , awserr . New ( kmslib . ErrCodeNotFoundException , "key not found" , errors .New ("key not found" ) )
70+ return nil , errors .New ("key not found" )
6571}
6672
6773// DescribeKey returns metadata about the key.
68- func (m * FakeKMSClient ) DescribeKey (input * kmslib.DescribeKeyInput ) (* kmslib.DescribeKeyOutput , error ) {
74+ func (m * FakeKMSClient ) DescribeKey (ctx context.Context , input * kms.DescribeKeyInput , opts ... func (* kms.Options )) (* kms.DescribeKeyOutput , error ) {
75+ if input .KeyId == nil {
76+ return nil , errors .New ("key ID is required" )
77+ }
6978 for _ , key := range m .keys {
70- if aws .StringValue (input .KeyId ) == key .KeyID {
71- return & kmslib.DescribeKeyOutput {
72- KeyMetadata : & kmslib.KeyMetadata {
73- KeyId : aws .String (key .KeyID ),
74- KeySpec : aws .String (kmslib .KeySpecEccSecgP256k1 ),
75- CreationDate : aws .Time (m .createdAt ),
79+ if * input .KeyId == key .KeyID {
80+ keySpec := kmstypes .KeySpecEccSecgP256k1
81+ return & kms.DescribeKeyOutput {
82+ KeyMetadata : & kmstypes.KeyMetadata {
83+ KeyId : & key .KeyID ,
84+ KeySpec : keySpec ,
85+ CreationDate : & m .createdAt ,
7686 },
7787 }, nil
7888 }
7989 }
80- return nil , awserr . New ( kmslib . ErrCodeNotFoundException , "key not found" , errors .New ("key not found" ) )
90+ return nil , errors .New ("key not found" )
8191}
8292
8393// ListKeys returns a list of key IDs.
84- func (m * FakeKMSClient ) ListKeys (_ * kmslib .ListKeysInput ) (* kmslib .ListKeysOutput , error ) {
85- keys := make ([]* kmslib .KeyListEntry , 0 , len (m .keys ))
94+ func (m * FakeKMSClient ) ListKeys (ctx context. Context , input * kms .ListKeysInput , opts ... func ( * kms. Options )) (* kms .ListKeysOutput , error ) {
95+ keys := make ([]kmstypes .KeyListEntry , 0 , len (m .keys ))
8696 for _ , key := range m .keys {
87- keys = append (keys , & kmslib.KeyListEntry {
88- KeyId : aws .String (key .KeyID ),
97+ keyID := key .KeyID
98+ keys = append (keys , kmstypes.KeyListEntry {
99+ KeyId : & keyID ,
89100 })
90101 }
91- return & kmslib .ListKeysOutput {
102+ return & kms .ListKeysOutput {
92103 Keys : keys ,
93104 }, nil
94105}
0 commit comments