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 28, 2024
1 parent 413cb76 commit c198f01
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 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,23 +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 {
if certToAdd.Certificate == nil {
return fmt.Errorf("Needs a certificate to be added")
}

func upsertCertIntoAgent(
certText []byte,
privateKey interface{},
comment string,
lifeTimeSecs uint32,
confirmBeforeUse bool,
logger log.DebugLogger) error {

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

View check run for this annotation

Codecov / codecov/patch

lib/client/sshagent/agent.go#L85

Added line #L85 was not covered by tests
conn, err := connectToDefaultSSHAgentLocation()
if err != nil {
return err
}
defer conn.Close()
return upsertCertIntoAgentConnection(certText, privateKey, comment, lifeTimeSecs, confirmBeforeUse, conn, logger)

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

View check run for this annotation

Codecov / codecov/patch

lib/client/sshagent/agent.go#L91

Added line #L91 was not covered by tests
}

func withAddedKeyUpsertCertIntoAgentConnection(certToAdd agent.AddedKey, conn net.Conn, logger log.DebugLogger) error {
if certToAdd.Certificate == nil {
return fmt.Errorf("Needs a certificate to be added")

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

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L94 - L96 were not covered by tests
}
agentClient := agent.NewClient(conn)

//delete certs in agent with the same comment
_, err = deleteDuplicateEntries(certToAdd.Comment, agentClient, logger)
_, err := deleteDuplicateEntries(certToAdd.Comment, agentClient, logger)

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

View check run for this annotation

Codecov / codecov/patch

lib/client/sshagent/agent.go#L101

Added line #L101 was not covered by tests
if err != nil {
logger.Printf("failed during deletion err=%s", err)
return err
Expand All @@ -102,3 +112,12 @@ func withAddedKeyUpsertCertIntoAgent(certToAdd agent.AddedKey, logger log.DebugL

return agentClient.Add(certToAdd)
}

func withAddedKeyUpsertCertIntoAgent(certToAdd agent.AddedKey, logger log.DebugLogger) error {
conn, err := connectToDefaultSSHAgentLocation()
if err != nil {
return err

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

View check run for this annotation

Codecov / codecov/patch

lib/client/sshagent/agent.go#L116-L119

Added lines #L116 - L119 were not covered by tests
}
defer conn.Close()
return withAddedKeyUpsertCertIntoAgentConnection(certToAdd, conn, logger)

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

View check run for this annotation

Codecov / codecov/patch

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

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

0 comments on commit c198f01

Please sign in to comment.