Skip to content

Commit

Permalink
enable to specify agent connection to insert cert to
Browse files Browse the repository at this point in the history
  • Loading branch information
ph4r05 committed May 25, 2024
1 parent f35df8b commit 7a76147
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions lib/client/sshagent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@ func deleteDuplicateEntries(comment string, agentClient agent.ExtendedAgent, log
return deletedCount, nil
}

func upsertCertIntoAgent(
func upsertCertIntoAgentConnection(
certText []byte,
privateKey interface{},
comment string,
lifeTimeSecs uint32,
confirmBeforeUse bool,
conn net.Conn,
logger log.DebugLogger) error {
pubKey, _, _, _, err := ssh.ParseAuthorizedKey(certText)
if err != nil {
Expand All @@ -72,19 +73,32 @@ func upsertCertIntoAgent(
Comment: comment,
ConfirmBeforeUse: confirmBeforeUse,
}
return withAddedKeyUpsertCertIntoAgent(keyToAdd, logger)
return withAddedKeyUpsertCertIntoAgentConnection(keyToAdd, conn, logger)

Check warning on line 76 in lib/client/sshagent/agent.go

View check run for this annotation

Codecov / codecov/patch

lib/client/sshagent/agent.go#L76

Added line #L76 was not covered by tests
}

func withAddedKeyUpsertCertIntoAgent(certToAdd agent.AddedKey, logger log.DebugLogger) error {
func upsertCertIntoAgent(
certText []byte,
privateKey interface{},
comment string,
lifeTimeSecs uint32,
confirmBeforeUse bool,
logger log.DebugLogger) error {
return upsertCertIntoAgentConnection(certText, privateKey, comment, lifeTimeSecs, confirmBeforeUse, nil, logger)

Check warning on line 86 in lib/client/sshagent/agent.go

View check run for this annotation

Codecov / codecov/patch

lib/client/sshagent/agent.go#L85-L86

Added lines #L85 - L86 were not covered by tests
}

func withAddedKeyUpsertCertIntoAgentConnection(certToAdd agent.AddedKey, conn net.Conn, logger log.DebugLogger) error {

Check warning on line 89 in lib/client/sshagent/agent.go

View check run for this annotation

Codecov / codecov/patch

lib/client/sshagent/agent.go#L89

Added line #L89 was not covered by tests
if certToAdd.Certificate == nil {
return fmt.Errorf("Needs a certificate to be added")
}

conn, err := connectToDefaultSSHAgentLocation()
if err != nil {
return err
var err error = nil
if conn == nil {
conn, err = connectToDefaultSSHAgentLocation()
if err != nil {
return err

Check warning on line 98 in lib/client/sshagent/agent.go

View check run for this annotation

Codecov / codecov/patch

lib/client/sshagent/agent.go#L94-L98

Added lines #L94 - L98 were not covered by tests
}
defer conn.Close()

Check warning on line 100 in lib/client/sshagent/agent.go

View check run for this annotation

Codecov / codecov/patch

lib/client/sshagent/agent.go#L100

Added line #L100 was not covered by tests
}
defer conn.Close()
agentClient := agent.NewClient(conn)

//delete certs in agent with the same comment
Expand All @@ -102,3 +116,7 @@ func withAddedKeyUpsertCertIntoAgent(certToAdd agent.AddedKey, logger log.DebugL

return agentClient.Add(certToAdd)
}

func withAddedKeyUpsertCertIntoAgent(certToAdd agent.AddedKey, logger log.DebugLogger) error {
return withAddedKeyUpsertCertIntoAgentConnection(certToAdd, nil, logger)

Check warning on line 121 in lib/client/sshagent/agent.go

View check run for this annotation

Codecov / codecov/patch

lib/client/sshagent/agent.go#L120-L121

Added lines #L120 - L121 were not covered by tests
}

0 comments on commit 7a76147

Please sign in to comment.