Skip to content

Commit

Permalink
Only accept a string signing key in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chriso committed Jun 7, 2024
1 parent 4f5556d commit 4086d91
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
17 changes: 4 additions & 13 deletions dispatchserver/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package dispatchserver
import (
"context"
"crypto/ed25519"
"encoding/base64"
"fmt"
"net/http"
_ "unsafe"

Expand All @@ -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
}
Expand All @@ -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)
}

Expand All @@ -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 }
}

Expand Down
9 changes: 8 additions & 1 deletion dispatchtest/endpoint.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package dispatchtest

import (
"crypto/ed25519"
"encoding/base64"
"fmt"
"net/http"
"net/http/httptest"

Expand Down Expand Up @@ -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)
}

0 comments on commit 4086d91

Please sign in to comment.