Skip to content

Commit efb227d

Browse files
ph4r05Dušan Klinec
andauthored
enable to specify agent connection to insert cert to (#231)
* enable to specify agent connection to insert cert to * add api * bump version --------- Co-authored-by: Dušan Klinec <dklinec@purestorage.com>
1 parent f88e2e0 commit efb227d

3 files changed

Lines changed: 44 additions & 9 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ endif
1010
BINARY=keymaster
1111

1212
# These are the values we want to pass for Version and BuildTime
13-
VERSION?=1.15.3
13+
VERSION?=1.15.4
1414
DEFAULT_HOST?=
1515
VERSION_FLAVOUR?=
1616
EXTRA_LDFLAGS?=

lib/client/sshagent/agent.go

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,13 @@ func deleteDuplicateEntries(comment string, agentClient agent.ExtendedAgent, log
5050
return deletedCount, nil
5151
}
5252

53-
func upsertCertIntoAgent(
53+
func upsertCertIntoAgentConnection(
5454
certText []byte,
5555
privateKey interface{},
5656
comment string,
5757
lifeTimeSecs uint32,
5858
confirmBeforeUse bool,
59+
conn net.Conn,
5960
logger log.DebugLogger) error {
6061
pubKey, _, _, _, err := ssh.ParseAuthorizedKey(certText)
6162
if err != nil {
@@ -72,23 +73,32 @@ func upsertCertIntoAgent(
7273
Comment: comment,
7374
ConfirmBeforeUse: confirmBeforeUse,
7475
}
75-
return withAddedKeyUpsertCertIntoAgent(keyToAdd, logger)
76+
return withAddedKeyUpsertCertIntoAgentConnection(keyToAdd, conn, logger)
7677
}
7778

78-
func withAddedKeyUpsertCertIntoAgent(certToAdd agent.AddedKey, logger log.DebugLogger) error {
79-
if certToAdd.Certificate == nil {
80-
return fmt.Errorf("Needs a certificate to be added")
81-
}
82-
79+
func upsertCertIntoAgent(
80+
certText []byte,
81+
privateKey interface{},
82+
comment string,
83+
lifeTimeSecs uint32,
84+
confirmBeforeUse bool,
85+
logger log.DebugLogger) error {
8386
conn, err := connectToDefaultSSHAgentLocation()
8487
if err != nil {
8588
return err
8689
}
8790
defer conn.Close()
91+
return upsertCertIntoAgentConnection(certText, privateKey, comment, lifeTimeSecs, confirmBeforeUse, conn, logger)
92+
}
93+
94+
func withAddedKeyUpsertCertIntoAgentConnection(certToAdd agent.AddedKey, conn net.Conn, logger log.DebugLogger) error {
95+
if certToAdd.Certificate == nil {
96+
return fmt.Errorf("Needs a certificate to be added")
97+
}
8898
agentClient := agent.NewClient(conn)
8999

90100
//delete certs in agent with the same comment
91-
_, err = deleteDuplicateEntries(certToAdd.Comment, agentClient, logger)
101+
_, err := deleteDuplicateEntries(certToAdd.Comment, agentClient, logger)
92102
if err != nil {
93103
logger.Printf("failed during deletion err=%s", err)
94104
return err
@@ -102,3 +112,12 @@ func withAddedKeyUpsertCertIntoAgent(certToAdd agent.AddedKey, logger log.DebugL
102112

103113
return agentClient.Add(certToAdd)
104114
}
115+
116+
func withAddedKeyUpsertCertIntoAgent(certToAdd agent.AddedKey, logger log.DebugLogger) error {
117+
conn, err := connectToDefaultSSHAgentLocation()
118+
if err != nil {
119+
return err
120+
}
121+
defer conn.Close()
122+
return withAddedKeyUpsertCertIntoAgentConnection(certToAdd, conn, logger)
123+
}

lib/client/sshagent/api.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package sshagent
22

33
import (
44
"golang.org/x/crypto/ssh/agent"
5+
"net"
56

67
"github.com/Cloud-Foundations/golib/pkg/log"
78
)
@@ -15,6 +16,21 @@ func UpsertCertIntoAgent(
1516
return upsertCertIntoAgent(certText, privateKey, comment, lifeTimeSecs, false, logger)
1617
}
1718

19+
func UpsertCertIntoAgentConnection(
20+
certText []byte,
21+
privateKey interface{},
22+
comment string,
23+
lifeTimeSecs uint32,
24+
confirmBeforeUse bool,
25+
conn net.Conn,
26+
logger log.DebugLogger) error {
27+
return upsertCertIntoAgentConnection(certText, privateKey, comment, lifeTimeSecs, confirmBeforeUse, conn, logger)
28+
}
29+
1830
func WithAddedKeyUpsertCertIntoAgent(certToAdd agent.AddedKey, logger log.DebugLogger) error {
1931
return withAddedKeyUpsertCertIntoAgent(certToAdd, logger)
2032
}
33+
34+
func WithAddedKeyUpsertCertIntoAgentConnection(certToAdd agent.AddedKey, conn net.Conn, logger log.DebugLogger) error {
35+
return withAddedKeyUpsertCertIntoAgentConnection(certToAdd, conn, logger)
36+
}

0 commit comments

Comments
 (0)