From c198f01b2db53a6058315501ec7deaeb01e25964 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Du=C5=A1an=20Klinec?= Date: Sat, 25 May 2024 18:24:37 +0200 Subject: [PATCH 1/3] enable to specify agent connection to insert cert to --- lib/client/sshagent/agent.go | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/lib/client/sshagent/agent.go b/lib/client/sshagent/agent.go index 1bbc8da..b5bbd2a 100644 --- a/lib/client/sshagent/agent.go +++ b/lib/client/sshagent/agent.go @@ -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 { @@ -72,23 +73,32 @@ func upsertCertIntoAgent( Comment: comment, ConfirmBeforeUse: confirmBeforeUse, } - return withAddedKeyUpsertCertIntoAgent(keyToAdd, logger) + return withAddedKeyUpsertCertIntoAgentConnection(keyToAdd, conn, logger) } -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 { conn, err := connectToDefaultSSHAgentLocation() if err != nil { return err } defer conn.Close() + return upsertCertIntoAgentConnection(certText, privateKey, comment, lifeTimeSecs, confirmBeforeUse, conn, logger) +} + +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") + } agentClient := agent.NewClient(conn) //delete certs in agent with the same comment - _, err = deleteDuplicateEntries(certToAdd.Comment, agentClient, logger) + _, err := deleteDuplicateEntries(certToAdd.Comment, agentClient, logger) if err != nil { logger.Printf("failed during deletion err=%s", err) return err @@ -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 + } + defer conn.Close() + return withAddedKeyUpsertCertIntoAgentConnection(certToAdd, conn, logger) +} From 568401b4b57a593ec31e4e25a0109a0c28203339 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Du=C5=A1an=20Klinec?= Date: Mon, 3 Jun 2024 11:13:04 +0200 Subject: [PATCH 2/3] add api --- lib/client/sshagent/api.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/lib/client/sshagent/api.go b/lib/client/sshagent/api.go index 9ee5de3..bf45eff 100644 --- a/lib/client/sshagent/api.go +++ b/lib/client/sshagent/api.go @@ -2,6 +2,7 @@ package sshagent import ( "golang.org/x/crypto/ssh/agent" + "net" "github.com/Cloud-Foundations/golib/pkg/log" ) @@ -15,6 +16,21 @@ func UpsertCertIntoAgent( return upsertCertIntoAgent(certText, privateKey, comment, lifeTimeSecs, false, logger) } +func UpsertCertIntoAgentConnection( + certText []byte, + privateKey interface{}, + comment string, + lifeTimeSecs uint32, + confirmBeforeUse bool, + conn net.Conn, + logger log.DebugLogger) error { + return upsertCertIntoAgentConnection(certText, privateKey, comment, lifeTimeSecs, confirmBeforeUse, conn, logger) +} + func WithAddedKeyUpsertCertIntoAgent(certToAdd agent.AddedKey, logger log.DebugLogger) error { return withAddedKeyUpsertCertIntoAgent(certToAdd, logger) } + +func WithAddedKeyUpsertCertIntoAgentConnection(certToAdd agent.AddedKey, conn net.Conn, logger log.DebugLogger) error { + return withAddedKeyUpsertCertIntoAgentConnection(certToAdd, conn, logger) +} From c9d94921ddca810f103c831f164ff9407371131b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Du=C5=A1an=20Klinec?= Date: Mon, 3 Jun 2024 16:19:05 +0200 Subject: [PATCH 3/3] bump version --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 6c212a6..956b5fc 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ endif BINARY=keymaster # These are the values we want to pass for Version and BuildTime -VERSION?=1.15.3 +VERSION?=1.15.4 DEFAULT_HOST?= VERSION_FLAVOUR?= EXTRA_LDFLAGS?=