Skip to content

Commit

Permalink
Separate searching for ssh-agent identity into a function
Browse files Browse the repository at this point in the history
  • Loading branch information
sio committed Sep 21, 2023
1 parent 6d8eab8 commit 1476314
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 26 deletions.
34 changes: 34 additions & 0 deletions secrets/access/acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
_ "github.com/mattn/go-sqlite3"
"golang.org/x/crypto/ssh"

"github.com/sio/pond/secrets/agent"
"github.com/sio/pond/secrets/master"
"github.com/sio/pond/secrets/util"
)
Expand Down Expand Up @@ -154,3 +155,36 @@ func (acl *ACL) Check(key ssh.PublicKey, c Capability, dir string) error {
}

var ErrPermissionDenied = errors.New("permission denied")

// Connect to ssh-agent and find an identity that has sufficient permissions
func (acl *ACL) FindAgent(caps []Capability, paths []string) (*agent.Conn, error) {
signer, err := agent.New(nil)
if err != nil {
return nil, err
}
fail := func(err error) (*agent.Conn, error) {
_ = signer.Close()
return nil, err
}
identities := signer.ListKeys()
if len(identities) == 0 {
return fail(fmt.Errorf("no identities available in ssh-agent"))
}
loop_id:
for _, id := range identities {
for _, capability := range caps {
for _, path := range paths {
err = acl.Check(id, Required[capability], path)
if err != nil {
continue loop_id
}
}
}
err = signer.SetIdentity(id)
if err != nil {
return fail(err)
}
return signer, nil
}
return fail(fmt.Errorf("ssh-agent: no matching identity out of %d tried", len(identities)))
}
31 changes: 5 additions & 26 deletions secrets/cmd/secretctl/cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"golang.org/x/crypto/ssh"

"github.com/sio/pond/secrets/access"
"github.com/sio/pond/secrets/agent"
"github.com/sio/pond/secrets/master"
"github.com/sio/pond/secrets/repo"
"github.com/sio/pond/secrets/util"
Expand Down Expand Up @@ -82,36 +81,16 @@ func (c *CertCmd) delegateUser(r *repo.Repository, to ssh.PublicKey, lifetime ti
if err != nil {
return "", err
}
signer, err := agent.New(nil)
signer, err := acl.FindAgent(caps, c.Path)
if err != nil {
return "", err
}
defer func() { _ = signer.Close() }()
identities := signer.ListKeys()
if len(identities) == 0 {
return "", fmt.Errorf("no identities available in ssh-agent")
}
loop_id:
for _, id := range identities {
for _, capability := range caps {
for _, p := range c.Path {
err = acl.Check(id, access.Required[capability], p)
if err != nil {
continue loop_id
}
}
}
err = signer.SetIdentity(id)
if err != nil {
return "", err
}
cert, err := access.DelegateUser(signer, to, caps, c.Path, c.User, lifetime)
if err != nil {
return "", err
}
return r.Save(cert)
cert, err := access.DelegateUser(signer, to, caps, c.Path, c.User, lifetime)
if err != nil {
return "", err
}
return "", fmt.Errorf("ssh-agent: not enough permissions to issue this certificate (tried %d identities)", len(identities))
return r.Save(cert)
}

func (c *CertCmd) delegateAdmin(r *repo.Repository, to ssh.PublicKey, lifetime time.Duration) (path string, err error) {
Expand Down

0 comments on commit 1476314

Please sign in to comment.