Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable to specify agent connection to insert cert to #231

Merged
merged 3 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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?=
Expand Down
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 @@
return deletedCount, nil
}

func upsertCertIntoAgent(
func upsertCertIntoAgentConnection(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None of the api (public calls are being modified here). What calls do you actually need? Looking a the keymaster code, it seems like The public side of this call is not needed.

Copy link
Collaborator Author

@ph4r05 ph4r05 May 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Purelogin calls UpsertCertIntoAgent from [email protected]/lib/client/sshagent/api.go , I need to be able to specify connection there

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 @@
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
Copy link
Contributor

@cviecco cviecco May 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

creation of the new agent connection should also go here

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, pls check now

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 @@

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
}
16 changes: 16 additions & 0 deletions lib/client/sshagent/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import (
"golang.org/x/crypto/ssh/agent"
"net"

"github.com/Cloud-Foundations/golib/pkg/log"
)
Expand All @@ -15,6 +16,21 @@
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)

Check warning on line 27 in lib/client/sshagent/api.go

View check run for this annotation

Codecov / codecov/patch

lib/client/sshagent/api.go#L26-L27

Added lines #L26 - L27 were not covered by tests
}

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)

Check warning on line 35 in lib/client/sshagent/api.go

View check run for this annotation

Codecov / codecov/patch

lib/client/sshagent/api.go#L34-L35

Added lines #L34 - L35 were not covered by tests
}
Loading