Skip to content

Commit

Permalink
1Password: cache repeated lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
n-g committed Nov 16, 2023
1 parent ab0301f commit f35cbbf
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion internal/providers/onepassword/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,18 @@ type provider struct {
mu sync.Mutex
ensureAccountErr error
once sync.Once
cache map[string][]byte
}

func (p *provider) Read(ctx context.Context, ref string) ([]byte, error) {
// Serialize reads to ensure there is only one approval pop-up.
p.mu.Lock()
defer p.mu.Unlock()

if data, ok := p.cache[ref]; ok {
return data, nil
}

// If no account is configured, `op read` does not fail but waits for user input.
// Hence, we ensure that a user account is indeed configured.
if err := p.ensureAccount(ctx); err != nil {
Expand All @@ -60,7 +65,9 @@ func (p *provider) Read(ctx context.Context, ref string) ([]byte, error) {
return nil, fnerrors.InvocationError("1Password", "failed to invoke %q: %w", c.String(), err)
}

return b.Bytes(), nil
data := b.Bytes()
p.cache[ref] = data
return data, nil
}

func (p *provider) ensureAccount(ctx context.Context) error {
Expand Down

0 comments on commit f35cbbf

Please sign in to comment.