Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(1password): handle cli integration #62

Merged
merged 2 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion foomo/squadron/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ func (c *Command) execute(ctx context.Context, r *readline.Readline) error {

{ // handle 1password
if c.op != nil {
if ok, _ := c.op.IsAuthenticated(); !ok {
if ok, _ := c.op.IsAuthenticated(ctx); !ok {
c.l.Info("missing 1password session, please login")
if err := c.op.SignIn(ctx); err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion onepassword/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
func AuthChecker(p *OnePassword) check.Checker {
return func(ctx context.Context, l log.Logger) check.Info {
name := "1Password"
if ok, _ := p.IsAuthenticated(); ok {
if ok, _ := p.IsAuthenticated(ctx); ok {
return check.NewSuccessInfo(name, "Authenticated")
} else {
return check.NewFailureInfo(name, "Run `op auth` to sign into 1password")
Expand Down
2 changes: 1 addition & 1 deletion onepassword/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (c *Command) register(ctx context.Context, r *readline.Readline) error {
}

func (c *Command) auth(ctx context.Context, r *readline.Readline) error {
if ok, _ := c.op.IsAuthenticated(); ok {
if ok, _ := c.op.IsAuthenticated(ctx); ok {
c.l.Info("Already signed in")
return nil
} else if err := c.op.SignIn(ctx); err != nil {
Expand Down
26 changes: 16 additions & 10 deletions onepassword/onepassword.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,15 @@ func New(l log.Logger, cache cache.Cache, opts ...Option) (*OnePassword, error)
// ~ Public methods
// ------------------------------------------------------------------------------------------------

func (op *OnePassword) IsAuthenticated() (bool, error) {
func (op *OnePassword) IsAuthenticated(ctx context.Context) (bool, error) {
var sessChanged bool
sess := os.Getenv("OP_SESSION_" + op.cfg.Account)

// check for enabled cli integration
if _, err := exec.CommandContext(ctx, "op", "account", "get", "--account", op.cfg.Account).CombinedOutput(); err == nil {
return true, nil
}

op.isSignedInLock.Lock()
defer op.isSignedInLock.Unlock()

Expand All @@ -104,7 +110,7 @@ func (op *OnePassword) IsAuthenticated() (bool, error) {
}

if sessChanged || op.isSignedInTime.IsZero() || time.Since(op.isSignedInTime) > time.Minute*10 {
out, err := exec.Command("op", "account", "--account", op.cfg.Account, "get", "--format", "json").Output()
out, err := exec.CommandContext(ctx, "op", "account", "--account", op.cfg.Account, "get", "--format", "json").Output()
if err != nil {
return false, fmt.Errorf("%w: %s", err, string(out))
}
Expand All @@ -119,15 +125,15 @@ func (op *OnePassword) IsAuthenticated() (bool, error) {

if data.Name == op.cfg.Account {
op.isSignedInTime = time.Now()
op.watch()
op.watch(context.WithoutCancel(ctx))
return true, nil
}
}
return true, nil
}

func (op *OnePassword) SignIn(ctx context.Context) error {
if ok, _ := op.IsAuthenticated(); ok {
if ok, _ := op.IsAuthenticated(ctx); ok {
return nil
}

Expand Down Expand Up @@ -175,7 +181,7 @@ $ export OP_SESSION_%s=%s
`, op.cfg.TokenFilename)
}
}
op.watch()
op.watch(context.WithoutCancel(ctx))
return nil
}

Expand All @@ -189,7 +195,7 @@ func (op *OnePassword) Get(ctx context.Context, secret Secret) (string, error) {
return strings.ReplaceAll(strings.TrimSpace(value), "\\n", "\n"), nil
}
} else {
if ok, _ := op.IsAuthenticated(); !ok {
if ok, _ := op.IsAuthenticated(ctx); !ok {
return "", ErrNotSignedIn
} else if fields := op.clientGet(ctx, secret.Vault, secret.Item); len(fields) == 0 {
return "", fmt.Errorf("could not find secret '%s' '%s'", secret.Vault, secret.Item)
Expand All @@ -209,7 +215,7 @@ func (op *OnePassword) GetDocument(ctx context.Context, secret Secret) (string,
return value, nil
}
} else {
if ok, _ := op.IsAuthenticated(); !ok {
if ok, _ := op.IsAuthenticated(ctx); !ok {
return "", ErrNotSignedIn
} else if value := op.clientGetDoument(ctx, secret.Vault, secret.Item); len(value) == 0 {
return "", fmt.Errorf("could not find document '%s' '%s'", secret.Vault, secret.Item)
Expand All @@ -220,7 +226,7 @@ func (op *OnePassword) GetDocument(ctx context.Context, secret Secret) (string,
}

func (op *OnePassword) GetOnetimePassword(ctx context.Context, account, uuid string) (string, error) {
if ok, _ := op.IsAuthenticated(); !ok {
if ok, _ := op.IsAuthenticated(ctx); !ok {
return "", ErrNotSignedIn
}

Expand Down Expand Up @@ -417,11 +423,11 @@ func (op *OnePassword) connectGetFileContent(vaultQuery, itemQuery, fileUUID str
}).(string)
}

func (op *OnePassword) watch() {
func (op *OnePassword) watch(ctx context.Context) {
if v, ok := op.watching[op.cfg.Account]; !ok || !v {
go func() {
for {
if ok, err := op.IsAuthenticated(); err != nil {
if ok, err := op.IsAuthenticated(ctx); err != nil {
op.l.Warnf("\n1password session keep alive failed for '%s' (%s)", op.cfg.Account, err.Error())
op.watching[op.cfg.Account] = false
return
Expand Down
Loading