Skip to content

Commit

Permalink
test: new items in 1p have multiple entries so dont assume one
Browse files Browse the repository at this point in the history
  • Loading branch information
gak committed May 21, 2024
1 parent 00cf305 commit a48a08e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
13 changes: 9 additions & 4 deletions common/configuration/1password_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,12 @@ func (o OnePasswordProvider) Load(ctx context.Context, ref Ref, key *url.URL) ([
return nil, fmt.Errorf("get item failed: %w", err)
}

secret, ok := slices.Find(full.Fields, func(item entry) bool {
return item.ID == "password"
})
secret, ok := full.value("password")
if !ok {
return nil, fmt.Errorf("password field not found in item %q", ref)
}

jsonSecret, err := json.Marshal(secret.Value)
jsonSecret, err := json.Marshal(secret)
if err != nil {
return nil, fmt.Errorf("json marshal failed: %w", err)
}
Expand Down Expand Up @@ -119,6 +117,13 @@ type entry struct {
Value string `json:"value"`
}

func (i item) value(field string) (string, bool) {
secret, ok := slices.Find(i.Fields, func(item entry) bool {
return item.ID == field
})
return secret.Value, ok
}

// op --format json item get --vault Personal "With Spaces"
func getItem(ctx context.Context, vault string, ref Ref) (*item, error) {
logger := log.FromContext(ctx)
Expand Down
8 changes: 6 additions & 2 deletions common/configuration/1password_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,16 @@ func Test1PasswordProvider(t *testing.T) {

value, err := getItem(ctx, vault, Ref{Name: module})
assert.NoError(t, err)
assert.Equal(t, "hunter1", value.Fields[0].Value)
secret, ok := value.value("password")
assert.True(t, ok)
assert.Equal(t, "hunter1", secret)

err = editItem(ctx, vault, Ref{Name: module}, "hunter2")
assert.NoError(t, err)

value, err = getItem(ctx, vault, Ref{Name: module})
assert.NoError(t, err)
assert.Equal(t, "hunter2", value.Fields[0].Value)
secret, ok = value.value("password")
assert.True(t, ok)
assert.Equal(t, "hunter2", secret)
}

0 comments on commit a48a08e

Please sign in to comment.