Skip to content

Commit

Permalink
recoverysigner: make allowed-source-account optional (#5052)
Browse files Browse the repository at this point in the history
What
This PR makes the --allowed-source-account flag optional.

Why
When ALLOWED_SOURCE_ACCOUNT is not specified or is empty, strings.Split(opts.AllowedSourceAccounts, ",") will return a slice containing an empty string. And we will get an error at keypair.ParseAddress(addressStr).
  • Loading branch information
howardtw authored Sep 14, 2023
1 parent af0378b commit 908871b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
12 changes: 7 additions & 5 deletions exp/services/recoverysigner/internal/serve/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,14 @@ func getHandlerDeps(opts Options) (handlerDeps, error) {
}

allowedSourceAccounts := []*keypair.FromAddress{}
for _, addressStr := range strings.Split(opts.AllowedSourceAccounts, ",") {
accountAddress, err := keypair.ParseAddress(addressStr)
if err != nil {
return handlerDeps{}, errors.Wrap(err, "parsing allowed source accounts")
if opts.AllowedSourceAccounts != "" {
for _, addressStr := range strings.Split(opts.AllowedSourceAccounts, ",") {
accountAddress, err := keypair.ParseAddress(addressStr)
if err != nil {
return handlerDeps{}, errors.Wrap(err, "parsing allowed source accounts")
}
allowedSourceAccounts = append(allowedSourceAccounts, accountAddress)
}
allowedSourceAccounts = append(allowedSourceAccounts, accountAddress)
}

deps := handlerDeps{
Expand Down
35 changes: 35 additions & 0 deletions exp/services/recoverysigner/internal/serve/serve_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package serve

import (
"encoding/json"
"testing"

"github.com/stellar/go/keypair"
supportlog "github.com/stellar/go/support/log"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/square/go-jose.v2"
)

func TestGetHandlerDeps(t *testing.T) {
signingKey := "SBWLXUTJR2CGVPGCZDIGGLQDPX7ZGGBHBFXBJ555MNIQ2PZCCLM643Z3"
signingKeyFull := keypair.MustParseFull(signingKey)

opts := Options{
Logger: supportlog.DefaultLogger,
SigningKeys: signingKey,
SEP10JWKS: `{"keys":[{"kty":"EC","crv":"P-256","alg":"ES256","x":"i8chX_7Slm4VQ_Y6XBWVBnxIO5-XSWH1GJsXWNkal3E","y":"G22r0OgrcQnkfCAqsS6wvtHgR0SbfvXNJy6-jJfvc94"}]}`,
}

sep10JWKS := jose.JSONWebKeySet{}
err := json.Unmarshal([]byte(opts.SEP10JWKS), &sep10JWKS)
require.NoError(t, err)

got, err := getHandlerDeps(opts)
assert.NoError(t, err)

assert.Equal(t, []*keypair.Full{signingKeyFull}, got.SigningKeys)
assert.Equal(t, []*keypair.FromAddress{signingKeyFull.FromAddress()}, got.SigningAddresses)
assert.Equal(t, sep10JWKS, got.SEP10JWKS)
assert.Equal(t, []*keypair.FromAddress{}, got.AllowedSourceAccounts)
}

0 comments on commit 908871b

Please sign in to comment.