Skip to content

Commit

Permalink
fix: encode op "URI" as base64 (#1037)
Browse files Browse the repository at this point in the history
It turns out it's not actually a URI at all, so FTL's parser borks on
it.
  • Loading branch information
alecthomas authored Mar 7, 2024
1 parent db9acb4 commit 847d1c8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Justfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
set positional-arguments

# Start a hot-reloading dev cluster
dev: install-jars
goreman -logtime=false start
Expand Down
18 changes: 13 additions & 5 deletions common/configuration/1password.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package configuration
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"net/url"
"strings"

"github.com/TBD54566975/ftl/internal/exec"
"github.com/TBD54566975/ftl/internal/log"
Expand All @@ -27,7 +29,13 @@ func (o OnePasswordProvider) Load(ctx context.Context, ref Ref, key *url.URL) ([
if err != nil {
return nil, fmt.Errorf("1Password CLI tool \"op\" not found: %w", err)
}
output, err := exec.Capture(ctx, ".", "op", "read", "-n", key.String())

decoded, err := base64.RawStdEncoding.DecodeString(key.Host)
if err != nil {
return nil, fmt.Errorf("1Password secret reference must be a base64 encoded string: %w", err)
}

output, err := exec.Capture(ctx, ".", "op", "read", "-n", string(decoded))
if err != nil {
lines := bytes.Split(output, []byte("\n"))
logger := log.FromContext(ctx)
Expand All @@ -44,11 +52,11 @@ func (o OnePasswordProvider) Store(ctx context.Context, ref Ref, value []byte) (
if err := json.Unmarshal(value, &opref); err != nil {
return nil, fmt.Errorf("1Password value must be a JSON string containing a 1Password secret refererence: %w", err)
}
u, err := url.Parse(opref)
if err != nil {
return nil, fmt.Errorf("invalid 1Password item ID: %w", err)
if !strings.HasPrefix(opref, "op://") {
return nil, fmt.Errorf("1Password secret reference must start with \"op://\"")
}
return u, nil
encoded := base64.RawStdEncoding.EncodeToString([]byte(opref))
return &url.URL{Scheme: "op", Host: encoded}, nil
}

func (o OnePasswordProvider) Writer() bool { return o.OnePassword }

0 comments on commit 847d1c8

Please sign in to comment.