Skip to content

Commit

Permalink
fix: correctly JSON decode DSN (#1721)
Browse files Browse the repository at this point in the history
  • Loading branch information
alecthomas authored Jun 10, 2024
1 parent bf87cac commit a34562b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
8 changes: 6 additions & 2 deletions internal/modulecontext/from_secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"fmt"
"strings"

"github.com/TBD54566975/ftl/go-runtime/encoding"
)

// DatabasesFromSecrets finds DSNs in secrets and creates a map of databases.
Expand All @@ -25,8 +27,10 @@ func DatabasesFromSecrets(ctx context.Context, module string, secrets map[string
if !strings.EqualFold(moduleName, module) {
continue
}
dsnStr := string(maybeDSN)
dsn := dsnStr[1 : len(dsnStr)-1] // chop leading + trailing quotes
var dsn string
if err := encoding.Unmarshal(maybeDSN, &dsn); err != nil {
return nil, fmt.Errorf("could not unmarshal DSN %q: %w", maybeDSN, err)
}
db, err := NewDatabase(DBTypePostgres, dsn)
if err != nil {
return nil, fmt.Errorf("could not create database %q with DSN %q: %w", dbName, maybeDSN, err)
Expand Down
8 changes: 5 additions & 3 deletions internal/modulecontext/from_secrets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ import (
"context" //nolint:depguard
"testing"

"github.com/alecthomas/assert/v2"

ftlv1 "github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1"
"github.com/TBD54566975/ftl/internal/log"
"github.com/alecthomas/assert/v2"
)

func TestFromSecrets(t *testing.T) {
ctx := log.ContextWithNewDefaultLogger(context.Background())

secrets := map[string][]byte{
"FTL_DSN_ECHO_ECHO": []byte("\"postgres://echo:echo@localhost:5432/echo\""),
"FTL_DSN_ECHO_ECHO": []byte(`"postgres://echo:echo@localhost:5432/echo?sslmode=disable\u0026user=echo\u0026password=echo"
`),
}
databases, err := DatabasesFromSecrets(ctx, "echo", secrets)
assert.NoError(t, err)
Expand All @@ -24,7 +26,7 @@ func TestFromSecrets(t *testing.T) {
Configs: map[string][]byte{},
Secrets: map[string][]byte{},
Databases: []*ftlv1.ModuleContextResponse_DSN{
{Name: "echo", Dsn: "postgres://echo:echo@localhost:5432/echo"},
{Name: "echo", Dsn: `postgres://echo:echo@localhost:5432/echo?sslmode=disable&user=echo&password=echo`},
},
}, response)
}

0 comments on commit a34562b

Please sign in to comment.