diff --git a/design/design.go b/design/design.go index 55926f7db256e33836cfa625e39d80da76b0ddeb..cbba82903578276998fad75e7ed7ab8bf54597e4 100644 --- a/design/design.go +++ b/design/design.go @@ -45,7 +45,7 @@ var _ = Service("signer", func() { }) Method("GetKey", func() { - Description("GetKey returns a key from Vault or OCM.") + Description("GetKey returns key information from Vault or OCM.") Payload(GetKeyRequest) Result(Any) HTTP(func() { diff --git a/gen/http/cli/signer/cli.go b/gen/http/cli/signer/cli.go index 1ce301b311933db9dae52a566d75df03c61c6192..a9a30f25c84e3ca32a5c2a05c46ee1beaa72c631 100644 --- a/gen/http/cli/signer/cli.go +++ b/gen/http/cli/signer/cli.go @@ -226,7 +226,7 @@ Usage: COMMAND: credential-proof: CredentialProof adds a proof to a given Verifiable Credential. presentation-proof: PresentationProof adds a proof to a given Verifiable Presentation. - get-key: GetKey returns a key from Vault or OCM. + get-key: GetKey returns key information from Vault or OCM. Additional help: %[1]s signer COMMAND --help @@ -259,7 +259,7 @@ Example: func signerGetKeyUsage() { fmt.Fprintf(os.Stderr, `%[1]s [flags] signer get-key -key STRING -GetKey returns a key from Vault or OCM. +GetKey returns key information from Vault or OCM. -key STRING: Name of requested key. Example: diff --git a/gen/http/openapi.json b/gen/http/openapi.json index 76042bc2e515dae20dd64fb730d61a83152ad51c..bf84e18726318d30083d75e838b80894ced7d5eb 100644 --- a/gen/http/openapi.json +++ b/gen/http/openapi.json @@ -1 +1 @@ -{"swagger":"2.0","info":{"title":"Signer Service","description":"The signer service exposes HTTP API for creating and verifying digital signatures.","version":""},"host":"localhost:8085","consumes":["application/json","application/xml","application/gob"],"produces":["application/json","application/xml","application/gob"],"paths":{"/liveness":{"get":{"tags":["health"],"summary":"Liveness health","operationId":"health#Liveness","responses":{"200":{"description":"OK response."}},"schemes":["http"]}},"/readiness":{"get":{"tags":["health"],"summary":"Readiness health","operationId":"health#Readiness","responses":{"200":{"description":"OK response."}},"schemes":["http"]}},"/v1/credential/proof":{"post":{"tags":["signer"],"summary":"CredentialProof signer","description":"CredentialProof adds a proof to a given Verifiable Credential.","operationId":"signer#CredentialProof","parameters":[{"name":"key","in":"query","description":"Key to use for the proof signature (optional).","required":false,"type":"string"},{"name":"bytes","in":"body","description":"Verifiable Credential in JSON format.","required":true,"schema":{"type":"string","format":"byte"}}],"responses":{"200":{"description":"OK response.","schema":{"type":"string","format":"binary"}}},"schemes":["http"]}},"/v1/keys/{key}":{"get":{"tags":["signer"],"summary":"GetKey signer","description":"GetKey returns a key from Vault or OCM.","operationId":"signer#GetKey","parameters":[{"name":"key","in":"path","description":"Name of requested key.","required":true,"type":"string"}],"responses":{"200":{"description":"OK response.","schema":{"type":"string","format":"binary"}}},"schemes":["http"]}},"/v1/presentation/proof":{"post":{"tags":["signer"],"summary":"PresentationProof signer","description":"PresentationProof adds a proof to a given Verifiable Presentation.","operationId":"signer#PresentationProof","parameters":[{"name":"key","in":"query","description":"Key to use for the proof signature (optional).","required":false,"type":"string"},{"name":"bytes","in":"body","description":"Verifiable Presentation in JSON format.","required":true,"schema":{"type":"string","format":"byte"}}],"responses":{"200":{"description":"OK response.","schema":{"type":"string","format":"binary"}}},"schemes":["http"]}}}} \ No newline at end of file +{"swagger":"2.0","info":{"title":"Signer Service","description":"The signer service exposes HTTP API for creating and verifying digital signatures.","version":""},"host":"localhost:8085","consumes":["application/json","application/xml","application/gob"],"produces":["application/json","application/xml","application/gob"],"paths":{"/liveness":{"get":{"tags":["health"],"summary":"Liveness health","operationId":"health#Liveness","responses":{"200":{"description":"OK response."}},"schemes":["http"]}},"/readiness":{"get":{"tags":["health"],"summary":"Readiness health","operationId":"health#Readiness","responses":{"200":{"description":"OK response."}},"schemes":["http"]}},"/v1/credential/proof":{"post":{"tags":["signer"],"summary":"CredentialProof signer","description":"CredentialProof adds a proof to a given Verifiable Credential.","operationId":"signer#CredentialProof","parameters":[{"name":"key","in":"query","description":"Key to use for the proof signature (optional).","required":false,"type":"string"},{"name":"bytes","in":"body","description":"Verifiable Credential in JSON format.","required":true,"schema":{"type":"string","format":"byte"}}],"responses":{"200":{"description":"OK response.","schema":{"type":"string","format":"binary"}}},"schemes":["http"]}},"/v1/keys/{key}":{"get":{"tags":["signer"],"summary":"GetKey signer","description":"GetKey returns key information from Vault or OCM.","operationId":"signer#GetKey","parameters":[{"name":"key","in":"path","description":"Name of requested key.","required":true,"type":"string"}],"responses":{"200":{"description":"OK response.","schema":{"type":"string","format":"binary"}}},"schemes":["http"]}},"/v1/presentation/proof":{"post":{"tags":["signer"],"summary":"PresentationProof signer","description":"PresentationProof adds a proof to a given Verifiable Presentation.","operationId":"signer#PresentationProof","parameters":[{"name":"key","in":"query","description":"Key to use for the proof signature (optional).","required":false,"type":"string"},{"name":"bytes","in":"body","description":"Verifiable Presentation in JSON format.","required":true,"schema":{"type":"string","format":"byte"}}],"responses":{"200":{"description":"OK response.","schema":{"type":"string","format":"binary"}}},"schemes":["http"]}}}} \ No newline at end of file diff --git a/gen/http/openapi.yaml b/gen/http/openapi.yaml index a1b7da786d72b910c7f7afed1a2e99659f6dd878..8ed1f1d13636f5c6fe9e852df5043ef65c6c557d 100644 --- a/gen/http/openapi.yaml +++ b/gen/http/openapi.yaml @@ -68,7 +68,7 @@ paths: tags: - signer summary: GetKey signer - description: GetKey returns a key from Vault or OCM. + description: GetKey returns key information from Vault or OCM. operationId: signer#GetKey parameters: - name: key diff --git a/gen/http/openapi3.json b/gen/http/openapi3.json index 51bddf7de3bee3dbb549103547504a49126310f2..8252c78508ef777bdbce49edf61142f9e337c7ad 100644 --- a/gen/http/openapi3.json +++ b/gen/http/openapi3.json @@ -1 +1 @@ -{"openapi":"3.0.3","info":{"title":"Signer Service","description":"The signer service exposes HTTP API for creating and verifying digital signatures.","version":"1.0"},"servers":[{"url":"http://localhost:8085","description":"Signer Server"}],"paths":{"/liveness":{"get":{"tags":["health"],"summary":"Liveness health","operationId":"health#Liveness","responses":{"200":{"description":"OK response."}}}},"/readiness":{"get":{"tags":["health"],"summary":"Readiness health","operationId":"health#Readiness","responses":{"200":{"description":"OK response."}}}},"/v1/credential/proof":{"post":{"tags":["signer"],"summary":"CredentialProof signer","description":"CredentialProof adds a proof to a given Verifiable Credential.","operationId":"signer#CredentialProof","parameters":[{"name":"key","in":"query","description":"Key to use for the proof signature (optional).","allowEmptyValue":true,"schema":{"type":"string","description":"Key to use for the proof signature (optional).","example":"key1"},"example":"key1"}],"requestBody":{"description":"Verifiable Credential in JSON format.","required":true,"content":{"application/json":{"schema":{"type":"string","description":"Verifiable Credential in JSON format.","example":"RGVsZW5pdGkgaXBzYS4=","format":"binary"},"example":"RW5pbSBkZXNlcnVudC4="}}},"responses":{"200":{"description":"OK response.","content":{"application/json":{"schema":{"type":"string","example":"Itaque adipisci voluptas.","format":"binary"},"example":"Cupiditate et aliquid reiciendis pariatur."}}}}}},"/v1/keys/{key}":{"get":{"tags":["signer"],"summary":"GetKey signer","description":"GetKey returns a key from Vault or OCM.","operationId":"signer#GetKey","parameters":[{"name":"key","in":"path","description":"Name of requested key.","required":true,"schema":{"type":"string","description":"Name of requested key.","example":"key1"},"example":"key1"}],"responses":{"200":{"description":"OK response.","content":{"application/json":{"schema":{"type":"string","example":"Laudantium exercitationem quis sunt eos.","format":"binary"},"example":"Eum molestiae."}}}}}},"/v1/presentation/proof":{"post":{"tags":["signer"],"summary":"PresentationProof signer","description":"PresentationProof adds a proof to a given Verifiable Presentation.","operationId":"signer#PresentationProof","parameters":[{"name":"key","in":"query","description":"Key to use for the proof signature (optional).","allowEmptyValue":true,"schema":{"type":"string","description":"Key to use for the proof signature (optional).","example":"key1"},"example":"key1"}],"requestBody":{"description":"Verifiable Presentation in JSON format.","required":true,"content":{"application/json":{"schema":{"type":"string","description":"Verifiable Presentation in JSON format.","example":"QXNwZXJpb3JlcyBtb2xlc3RpYXMgcXVpLg==","format":"binary"},"example":"TW9sZXN0aWFlIHZlbGl0IG1haW9yZXMgZXQgcXVpYS4="}}},"responses":{"200":{"description":"OK response.","content":{"application/json":{"schema":{"type":"string","example":"Ipsa vel in repudiandae repellat.","format":"binary"},"example":"Voluptatem consectetur."}}}}}}},"components":{},"tags":[{"name":"health","description":"Health service provides health check endpoints."},{"name":"signer","description":"Sign service provides endpoints for making digital signatures and proofs for verifiable credentials and presentations."}]} \ No newline at end of file +{"openapi":"3.0.3","info":{"title":"Signer Service","description":"The signer service exposes HTTP API for creating and verifying digital signatures.","version":"1.0"},"servers":[{"url":"http://localhost:8085","description":"Signer Server"}],"paths":{"/liveness":{"get":{"tags":["health"],"summary":"Liveness health","operationId":"health#Liveness","responses":{"200":{"description":"OK response."}}}},"/readiness":{"get":{"tags":["health"],"summary":"Readiness health","operationId":"health#Readiness","responses":{"200":{"description":"OK response."}}}},"/v1/credential/proof":{"post":{"tags":["signer"],"summary":"CredentialProof signer","description":"CredentialProof adds a proof to a given Verifiable Credential.","operationId":"signer#CredentialProof","parameters":[{"name":"key","in":"query","description":"Key to use for the proof signature (optional).","allowEmptyValue":true,"schema":{"type":"string","description":"Key to use for the proof signature (optional).","example":"key1"},"example":"key1"}],"requestBody":{"description":"Verifiable Credential in JSON format.","required":true,"content":{"application/json":{"schema":{"type":"string","description":"Verifiable Credential in JSON format.","example":"RGVsZW5pdGkgaXBzYS4=","format":"binary"},"example":"RW5pbSBkZXNlcnVudC4="}}},"responses":{"200":{"description":"OK response.","content":{"application/json":{"schema":{"type":"string","example":"Itaque adipisci voluptas.","format":"binary"},"example":"Cupiditate et aliquid reiciendis pariatur."}}}}}},"/v1/keys/{key}":{"get":{"tags":["signer"],"summary":"GetKey signer","description":"GetKey returns key information from Vault or OCM.","operationId":"signer#GetKey","parameters":[{"name":"key","in":"path","description":"Name of requested key.","required":true,"schema":{"type":"string","description":"Name of requested key.","example":"key1"},"example":"key1"}],"responses":{"200":{"description":"OK response.","content":{"application/json":{"schema":{"type":"string","example":"Laudantium exercitationem quis sunt eos.","format":"binary"},"example":"Eum molestiae."}}}}}},"/v1/presentation/proof":{"post":{"tags":["signer"],"summary":"PresentationProof signer","description":"PresentationProof adds a proof to a given Verifiable Presentation.","operationId":"signer#PresentationProof","parameters":[{"name":"key","in":"query","description":"Key to use for the proof signature (optional).","allowEmptyValue":true,"schema":{"type":"string","description":"Key to use for the proof signature (optional).","example":"key1"},"example":"key1"}],"requestBody":{"description":"Verifiable Presentation in JSON format.","required":true,"content":{"application/json":{"schema":{"type":"string","description":"Verifiable Presentation in JSON format.","example":"QXNwZXJpb3JlcyBtb2xlc3RpYXMgcXVpLg==","format":"binary"},"example":"TW9sZXN0aWFlIHZlbGl0IG1haW9yZXMgZXQgcXVpYS4="}}},"responses":{"200":{"description":"OK response.","content":{"application/json":{"schema":{"type":"string","example":"Ipsa vel in repudiandae repellat.","format":"binary"},"example":"Voluptatem consectetur."}}}}}}},"components":{},"tags":[{"name":"health","description":"Health service provides health check endpoints."},{"name":"signer","description":"Sign service provides endpoints for making digital signatures and proofs for verifiable credentials and presentations."}]} \ No newline at end of file diff --git a/gen/http/openapi3.yaml b/gen/http/openapi3.yaml index 711249dc0b897cb8aeff2c829c21f3ddbce09f8d..a0b57111324702aa270e67a22ab4d4e41ce1d5bf 100644 --- a/gen/http/openapi3.yaml +++ b/gen/http/openapi3.yaml @@ -96,7 +96,7 @@ paths: tags: - signer summary: GetKey signer - description: GetKey returns a key from Vault or OCM. + description: GetKey returns key information from Vault or OCM. operationId: signer#GetKey parameters: - name: key diff --git a/gen/signer/service.go b/gen/signer/service.go index 911b0363057e35dca63ff8299624403af0020374..247f2c9d8b178cc203a9fcaf0e6b2f34776105ac 100644 --- a/gen/signer/service.go +++ b/gen/signer/service.go @@ -18,7 +18,7 @@ type Service interface { CredentialProof(context.Context, *CredentialProofRequest) (res interface{}, err error) // PresentationProof adds a proof to a given Verifiable Presentation. PresentationProof(context.Context, *PresentationProofRequest) (res interface{}, err error) - // GetKey returns a key from Vault or OCM. + // GetKey returns key information from Vault or OCM. GetKey(context.Context, *GetKeyRequest) (res interface{}, err error) } diff --git a/internal/clients/vault/client.go b/internal/clients/vault/client.go index d5e199372cbb10d8a5091175a826c946c16b2e6b..bf2b160f18c56426b90619508ab33903c6710f11 100644 --- a/internal/clients/vault/client.go +++ b/internal/clients/vault/client.go @@ -1,6 +1,7 @@ package vault import ( + "context" "encoding/base64" "encoding/json" "fmt" @@ -58,27 +59,23 @@ func (c *Client) WithKey(key string) signer.Signer { } // Key tries to fetch a key with the given name from the Vault. -func (c *Client) Key(key string) (*signer.SignKey, error) { +func (c *Client) Key(ctx context.Context, key string) (*signer.SignKey, error) { req := c.client.NewRequest(http.MethodGet, pathKeys+key) - res, err := c.client.RawRequest(req) + res, err := c.client.RawRequestWithContext(ctx, req) if err != nil { return nil, errors.New(errors.GetKind(res.StatusCode), err) } defer res.Body.Close() - var response struct { - Data struct { - Name string `json:"name"` - Type string `json:"type"` - } `json:"data"` - } + var response getKeyResponse if err := json.NewDecoder(res.Body).Decode(&response); err != nil { return nil, err } return &signer.SignKey{ - Name: response.Data.Name, - Type: response.Data.Type, + Name: response.Data.Name, + Type: response.Data.Type, + PublicKey: response.lastPublicKeyVersion(), }, nil } diff --git a/internal/clients/vault/client_test.go b/internal/clients/vault/client_test.go index 66258b01eaa6589094ecec26cf9b56358f5a8919..d0fc41e473dab450c53000374c766dd1810777bf 100644 --- a/internal/clients/vault/client_test.go +++ b/internal/clients/vault/client_test.go @@ -1,6 +1,7 @@ package vault_test import ( + "context" "net/http" "net/http/httptest" "testing" @@ -68,7 +69,7 @@ func TestClient_Key(t *testing.T) { client, err := vault.New(vaultsrv.URL, "token", false, http.DefaultClient) assert.NoError(t, err) - res, err := client.Key(test.key) + res, err := client.Key(context.Background(), test.key) if err != nil { assert.Nil(t, res) diff --git a/internal/clients/vault/transport.go b/internal/clients/vault/transport.go new file mode 100644 index 0000000000000000000000000000000000000000..612f13e433120afbb3b05c15ba28e09502d89988 --- /dev/null +++ b/internal/clients/vault/transport.go @@ -0,0 +1,33 @@ +package vault + +import "strconv" + +type getKeyResponse struct { + Data struct { + Name string `json:"name"` + Type string `json:"type"` + Keys map[string]struct { + PublicKey string `json:"public_key"` + } `json:"keys"` + } `json:"data"` +} + +// lastPublicKeyVersion iterates the map with key versions and +// returns the latest public key. +func (r *getKeyResponse) lastPublicKeyVersion() string { + var lastVerString string + var lastVerInt int + for ver := range r.Data.Keys { + verInt, err := strconv.Atoi(ver) + if err != nil { + continue + } + + if verInt > lastVerInt { + lastVerInt = verInt + lastVerString = ver + } + } + + return r.Data.Keys[lastVerString].PublicKey +} diff --git a/internal/clients/vault/transport_test.go b/internal/clients/vault/transport_test.go new file mode 100644 index 0000000000000000000000000000000000000000..8b5a82e64ceaba9d2a89d74dc1ca7e28b4d4756b --- /dev/null +++ b/internal/clients/vault/transport_test.go @@ -0,0 +1,72 @@ +package vault + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func Test_lastPublicKeyVersion(t *testing.T) { + tests := []struct { + name string + keys map[string]struct { + PublicKey string `json:"public_key"` + } + key string + }{ + { + name: "no keys in response", + }, + { + name: "one key in response", + keys: map[string]struct { + PublicKey string `json:"public_key"` + }{ + "1": {PublicKey: "key1"}, + }, + key: "key1", + }, + { + name: "two keys in response", + keys: map[string]struct { + PublicKey string `json:"public_key"` + }{ + "2": {PublicKey: "key2"}, + "1": {PublicKey: "key1"}, + }, + key: "key2", + }, + { + name: "three keys in response", + keys: map[string]struct { + PublicKey string `json:"public_key"` + }{ + "2": {PublicKey: "key2"}, + "1": {PublicKey: "key1"}, + "4": {PublicKey: "key4"}, + }, + key: "key4", + }, + { + name: "four keys in response", + keys: map[string]struct { + PublicKey string `json:"public_key"` + }{ + "2": {PublicKey: "key2"}, + "8": {PublicKey: "key8"}, + "1": {PublicKey: "key1"}, + "4": {PublicKey: "key4"}, + }, + key: "key8", + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + response := &getKeyResponse{} + response.Data.Keys = test.keys + key := response.lastPublicKeyVersion() + assert.Equal(t, test.key, key) + }) + } +} diff --git a/internal/service/signer/service.go b/internal/service/signer/service.go index 06755754577a21cf4db4c77c181f216d651783c6..6e19f0b1b22b1efb3a4fc9f89b4cdf2ffdb3777c 100644 --- a/internal/service/signer/service.go +++ b/internal/service/signer/service.go @@ -21,12 +21,13 @@ import ( //go:generate counterfeiter . Signer type SignKey struct { - Name string - Type string + Name string `json:"name"` + Type string `json:"type"` + PublicKey string `json:"public_key,omitempty"` } type Signer interface { - Key(key string) (*SignKey, error) + Key(ctx context.Context, key string) (*SignKey, error) Sign(data []byte) ([]byte, error) WithKey(key string) Signer } @@ -48,6 +49,17 @@ func New(signer Signer, defaultKey string, httpClient *http.Client, logger *zap. } } +// GetKey returns a key from Vault or OCM. +func (s *Service) GetKey(ctx context.Context, req *signer.GetKeyRequest) (interface{}, error) { + key, err := s.signer.Key(ctx, req.Key) + if err != nil { + s.logger.Error("error getting key", zap.Error(err)) + return nil, err + } + + return key, nil +} + // CredentialProof adds a proof to a given Verifiable Credential. func (s *Service) CredentialProof(ctx context.Context, req *signer.CredentialProofRequest) (interface{}, error) { logger := s.logger.With(zap.String("operation", "credentialProof")) @@ -63,7 +75,7 @@ func (s *Service) CredentialProof(ctx context.Context, req *signer.CredentialPro keyname = *req.Key } - key, err := s.signer.Key(keyname) + key, err := s.signer.Key(ctx, keyname) if err != nil { logger.Error("error getting signing key", zap.String("key", keyname), zap.Error(err)) return nil, errors.New("error getting signing key", err) @@ -98,7 +110,7 @@ func (s *Service) PresentationProof(ctx context.Context, req *signer.Presentatio keyname = *req.Key } - key, err := s.signer.Key(keyname) + key, err := s.signer.Key(ctx, keyname) if err != nil { logger.Error("error getting signing key", zap.String("key", keyname), zap.Error(err)) return nil, errors.New("error getting signing key", err) diff --git a/internal/service/signer/service_test.go b/internal/service/signer/service_test.go index 9a44fb3b27cf0dc41b44aafb7340c34a9693d682..3c34e982648ec97a1086d226415b7d234e130931 100644 --- a/internal/service/signer/service_test.go +++ b/internal/service/signer/service_test.go @@ -17,6 +17,46 @@ import ( "code.vereign.com/gaiax/tsa/signer/internal/service/signer/signerfakes" ) +func TestService_GetKey(t *testing.T) { + t.Run("signer returns error when getting key", func(t *testing.T) { + signerError := &signerfakes.FakeSigner{ + KeyStub: func(ctx context.Context, key string) (*signer.SignKey, error) { + return nil, errors.New(errors.NotFound, "key not found") + }, + } + + svc := signer.New(signerError, "default key", http.DefaultClient, zap.NewNop()) + result, err := svc.GetKey(context.Background(), &goasigner.GetKeyRequest{Key: "key1"}) + assert.Nil(t, result) + assert.Error(t, err) + e, ok := err.(*errors.Error) + assert.True(t, ok) + assert.Equal(t, errors.NotFound, e.Kind) + }) + + t.Run("signer returns key successfully", func(t *testing.T) { + signerOK := &signerfakes.FakeSigner{ + KeyStub: func(ctx context.Context, key string) (*signer.SignKey, error) { + return &signer.SignKey{ + Name: "keyname", + Type: "ed25519", + PublicKey: "public key", + }, nil + }, + } + + svc := signer.New(signerOK, "default key", http.DefaultClient, zap.NewNop()) + result, err := svc.GetKey(context.Background(), &goasigner.GetKeyRequest{Key: "key1"}) + assert.NotNil(t, result) + assert.NoError(t, err) + assert.Equal(t, &signer.SignKey{ + Name: "keyname", + Type: "ed25519", + PublicKey: "public key", + }, result) + }) +} + func TestService_CredentialProof(t *testing.T) { tests := []struct { name string @@ -68,7 +108,7 @@ func TestService_CredentialProof(t *testing.T) { Credential: []byte(validCredential), }, signer: &signerfakes.FakeSigner{ - KeyStub: func(key string) (*signer.SignKey, error) { + KeyStub: func(ctx context.Context, key string) (*signer.SignKey, error) { return nil, errors.New(errors.NotFound) }, }, @@ -83,7 +123,7 @@ func TestService_CredentialProof(t *testing.T) { Credential: []byte(validCredential), }, signer: &signerfakes.FakeSigner{ - KeyStub: func(key string) (*signer.SignKey, error) { + KeyStub: func(ctx context.Context, key string) (*signer.SignKey, error) { return nil, errors.New(errors.Internal) }, }, @@ -98,7 +138,7 @@ func TestService_CredentialProof(t *testing.T) { Credential: []byte(validCredential), }, signer: &signerfakes.FakeSigner{ - KeyStub: func(key string) (*signer.SignKey, error) { + KeyStub: func(ctx context.Context, key string) (*signer.SignKey, error) { return nil, errors.New(errors.Internal) }, }, @@ -113,7 +153,7 @@ func TestService_CredentialProof(t *testing.T) { Credential: []byte(validCredential), }, signer: &signerfakes.FakeSigner{ - KeyStub: func(key string) (*signer.SignKey, error) { + KeyStub: func(ctx context.Context, key string) (*signer.SignKey, error) { return &signer.SignKey{ Name: "key23", Type: "rsa4096", @@ -131,7 +171,7 @@ func TestService_CredentialProof(t *testing.T) { Credential: []byte(validCredential), }, signer: &signerfakes.FakeSigner{ - KeyStub: func(key string) (*signer.SignKey, error) { + KeyStub: func(ctx context.Context, key string) (*signer.SignKey, error) { return &signer.SignKey{ Name: "key123", Type: "ed25519", @@ -164,7 +204,7 @@ func TestService_CredentialProof(t *testing.T) { Credential: []byte(validCredential), }, signer: &signerfakes.FakeSigner{ - KeyStub: func(key string) (*signer.SignKey, error) { + KeyStub: func(ctx context.Context, key string) (*signer.SignKey, error) { return &signer.SignKey{ Name: "key123", Type: "ecdsa-p256", @@ -273,7 +313,7 @@ func TestService_PresentationProof(t *testing.T) { Presentation: []byte(validPresentation), }, signer: &signerfakes.FakeSigner{ - KeyStub: func(key string) (*signer.SignKey, error) { + KeyStub: func(ctx context.Context, key string) (*signer.SignKey, error) { return nil, errors.New(errors.NotFound) }, }, @@ -288,7 +328,7 @@ func TestService_PresentationProof(t *testing.T) { Presentation: []byte(validPresentation), }, signer: &signerfakes.FakeSigner{ - KeyStub: func(key string) (*signer.SignKey, error) { + KeyStub: func(ctx context.Context, key string) (*signer.SignKey, error) { return nil, errors.New(errors.Internal) }, }, @@ -303,7 +343,7 @@ func TestService_PresentationProof(t *testing.T) { Presentation: []byte(validPresentation), }, signer: &signerfakes.FakeSigner{ - KeyStub: func(key string) (*signer.SignKey, error) { + KeyStub: func(ctx context.Context, key string) (*signer.SignKey, error) { return nil, errors.New(errors.Internal) }, }, @@ -318,7 +358,7 @@ func TestService_PresentationProof(t *testing.T) { Presentation: []byte(validPresentation), }, signer: &signerfakes.FakeSigner{ - KeyStub: func(key string) (*signer.SignKey, error) { + KeyStub: func(ctx context.Context, key string) (*signer.SignKey, error) { return &signer.SignKey{ Name: "key23", Type: "rsa4096", @@ -336,7 +376,7 @@ func TestService_PresentationProof(t *testing.T) { Presentation: []byte(validPresentation), }, signer: &signerfakes.FakeSigner{ - KeyStub: func(key string) (*signer.SignKey, error) { + KeyStub: func(ctx context.Context, key string) (*signer.SignKey, error) { return &signer.SignKey{ Name: "key123", Type: "ed25519", @@ -367,7 +407,7 @@ func TestService_PresentationProof(t *testing.T) { Presentation: []byte(validPresentation), }, signer: &signerfakes.FakeSigner{ - KeyStub: func(key string) (*signer.SignKey, error) { + KeyStub: func(ctx context.Context, key string) (*signer.SignKey, error) { return &signer.SignKey{ Name: "key123", Type: "ecdsa-p256", diff --git a/internal/service/signer/signerfakes/fake_signer.go b/internal/service/signer/signerfakes/fake_signer.go index 5b2b696013317d774fe2aae6793758037054f786..9cd9bfbec59386f657a0a197d127c26462dca3b4 100644 --- a/internal/service/signer/signerfakes/fake_signer.go +++ b/internal/service/signer/signerfakes/fake_signer.go @@ -2,16 +2,18 @@ package signerfakes import ( + "context" "sync" "code.vereign.com/gaiax/tsa/signer/internal/service/signer" ) type FakeSigner struct { - KeyStub func(string) (*signer.SignKey, error) + KeyStub func(context.Context, string) (*signer.SignKey, error) keyMutex sync.RWMutex keyArgsForCall []struct { - arg1 string + arg1 context.Context + arg2 string } keyReturns struct { result1 *signer.SignKey @@ -49,18 +51,19 @@ type FakeSigner struct { invocationsMutex sync.RWMutex } -func (fake *FakeSigner) Key(arg1 string) (*signer.SignKey, error) { +func (fake *FakeSigner) Key(arg1 context.Context, arg2 string) (*signer.SignKey, error) { fake.keyMutex.Lock() ret, specificReturn := fake.keyReturnsOnCall[len(fake.keyArgsForCall)] fake.keyArgsForCall = append(fake.keyArgsForCall, struct { - arg1 string - }{arg1}) + arg1 context.Context + arg2 string + }{arg1, arg2}) stub := fake.KeyStub fakeReturns := fake.keyReturns - fake.recordInvocation("Key", []interface{}{arg1}) + fake.recordInvocation("Key", []interface{}{arg1, arg2}) fake.keyMutex.Unlock() if stub != nil { - return stub(arg1) + return stub(arg1, arg2) } if specificReturn { return ret.result1, ret.result2 @@ -74,17 +77,17 @@ func (fake *FakeSigner) KeyCallCount() int { return len(fake.keyArgsForCall) } -func (fake *FakeSigner) KeyCalls(stub func(string) (*signer.SignKey, error)) { +func (fake *FakeSigner) KeyCalls(stub func(context.Context, string) (*signer.SignKey, error)) { fake.keyMutex.Lock() defer fake.keyMutex.Unlock() fake.KeyStub = stub } -func (fake *FakeSigner) KeyArgsForCall(i int) string { +func (fake *FakeSigner) KeyArgsForCall(i int) (context.Context, string) { fake.keyMutex.RLock() defer fake.keyMutex.RUnlock() argsForCall := fake.keyArgsForCall[i] - return argsForCall.arg1 + return argsForCall.arg1, argsForCall.arg2 } func (fake *FakeSigner) KeyReturns(result1 *signer.SignKey, result2 error) {