diff --git a/common/configuration/1password_provider.go b/common/configuration/1password_provider.go index 58809756cc..2321648f7a 100644 --- a/common/configuration/1password_provider.go +++ b/common/configuration/1password_provider.go @@ -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) } @@ -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) diff --git a/common/configuration/1password_provider_test.go b/common/configuration/1password_provider_test.go index a8fdd86122..c267572bf7 100644 --- a/common/configuration/1password_provider_test.go +++ b/common/configuration/1password_provider_test.go @@ -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) }