@@ -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+ }
0 commit comments