Skip to content
Open
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
6 changes: 6 additions & 0 deletions ssh/agent/keyring.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ var errLocked = errors.New("agent: locked")

// NewKeyring returns an Agent that holds keys in memory. It is safe
// for concurrent use by multiple goroutines.
//
// The returned Agent only supports the "lifetime" constraint.
func NewKeyring() Agent {
return &keyring{}
}
Expand Down Expand Up @@ -146,12 +148,16 @@ func (r *keyring) List() ([]*Key, error) {
// Insert adds a private key to the keyring. If a certificate
// is given, that certificate is added as public key. Note that
// any constraints given are ignored.
// Add returns an error if key contains ConfirmBeforeUse
func (r *keyring) Add(key AddedKey) error {
r.mu.Lock()
defer r.mu.Unlock()
if r.locked {
return errLocked
}
if key.ConfirmBeforeUse {
return errors.New("agent: confirm before use constraint is not supported")
}
signer, err := ssh.NewSignerFromKey(key.PrivateKey)

if err != nil {
Expand Down
13 changes: 13 additions & 0 deletions ssh/agent/keyring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,16 @@ func TestAddDuplicateKey(t *testing.T) {
t.Fatal("key with the updated comment not found")
}
}

func TestAddKeyWithConfirmBeforeUse(t *testing.T) {
agent, cleanup := startKeyringAgent(t)
defer cleanup()
key := testPrivateKeys["rsa"]
err := agent.Add(AddedKey{
PrivateKey: key,
ConfirmBeforeUse: true,
})
if err == nil {
t.Fatal("adding a key with confirm before use constraint succeeded")
}
}
5 changes: 2 additions & 3 deletions ssh/test/agent_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ func TestAgentForward(t *testing.T) {
t.Fatalf("Error adding key: %s", err)
}
if err := keyring.Add(agent.AddedKey{
PrivateKey: testPrivateKeys["ecdsa"],
ConfirmBeforeUse: true,
LifetimeSecs: 3600,
PrivateKey: testPrivateKeys["ecdsa"],
LifetimeSecs: 3600,
}); err != nil {
t.Fatalf("Error adding key with constraints: %s", err)
}
Expand Down