From 4086d91c8e9e8386eb2c818627917650033d28bf Mon Sep 17 00:00:00 2001 From: Chris O'Hara Date: Fri, 7 Jun 2024 13:34:58 +1000 Subject: [PATCH] Only accept a string signing key in tests --- dispatchserver/endpoint.go | 17 ++++------------- dispatchtest/endpoint.go | 9 ++++++++- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/dispatchserver/endpoint.go b/dispatchserver/endpoint.go index a32f5cd..817cd4b 100644 --- a/dispatchserver/endpoint.go +++ b/dispatchserver/endpoint.go @@ -3,8 +3,6 @@ package dispatchserver import ( "context" "crypto/ed25519" - "encoding/base64" - "fmt" "net/http" _ "unsafe" @@ -23,7 +21,7 @@ import ( // useful when testing a Dispatch endpoint. type EndpointClient struct { httpClient connect.HTTPClient - signingKey string + signingKey ed25519.PrivateKey client sdkv1connect.FunctionServiceClient } @@ -40,12 +38,8 @@ func NewEndpointClient(endpointUrl string, opts ...EndpointClientOption) (*Endpo } // Setup request signing. - if c.signingKey != "" { - privateKey, err := base64.StdEncoding.DecodeString(c.signingKey) - if err != nil || len(privateKey) != ed25519.PrivateKeySize { - return nil, fmt.Errorf("invalid signing key: %v", c.signingKey) - } - signer := auth.NewSigner(ed25519.PrivateKey(privateKey)) + if c.signingKey != nil { + signer := auth.NewSigner(c.signingKey) c.httpClient = signer.Client(c.httpClient) } @@ -65,11 +59,8 @@ type EndpointClientOption func(*EndpointClient) // SigningKey sets the signing key to use when signing requests bound // for the endpoint. // -// The signing key should be a base64-encoded ed25519.PrivateKey, e.g. -// one provided by the KeyPair helper function. -// // By default the EndpointClient does not sign requests to the endpoint. -func SigningKey(signingKey string) EndpointClientOption { +func SigningKey(signingKey ed25519.PrivateKey) EndpointClientOption { return func(c *EndpointClient) { c.signingKey = signingKey } } diff --git a/dispatchtest/endpoint.go b/dispatchtest/endpoint.go index 8f8bdb0..8c9e420 100644 --- a/dispatchtest/endpoint.go +++ b/dispatchtest/endpoint.go @@ -1,6 +1,9 @@ package dispatchtest import ( + "crypto/ed25519" + "encoding/base64" + "fmt" "net/http" "net/http/httptest" @@ -55,5 +58,9 @@ func (e *EndpointServer) Close() { // The signing key should be a base64-encoded ed25519.PrivateKey, e.g. // one provided by the KeyPair helper function. func SigningKey(signingKey string) dispatchserver.EndpointClientOption { - return dispatchserver.SigningKey(signingKey) + pk, err := base64.StdEncoding.DecodeString(signingKey) + if err != nil || len(pk) != ed25519.PrivateKeySize { + panic(fmt.Errorf("invalid signing key: %v", signingKey)) + } + return dispatchserver.SigningKey(pk) }