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

feat: intercept ssh keyManager facade in model proxy #1523

Open
wants to merge 3 commits into
base: v3
Choose a base branch
from

Conversation

kian99
Copy link
Contributor

@kian99 kian99 commented Jan 14, 2025

Description

Currently this PR builds on #1521.

This PR allows JIMM to intercept model calls for the SSH KeyManager facade and direct the calls to JIMM's business logic rather than forwarding the calls to the backing Juju controllers.

This is being done in order to support the SSH proxy efforts.
Ideally, in Juju 4 these methods would be done on the controller api and all this logic would move into the jujuapi package. I've left comments to that effect. If before that point we want to refactor the model proxy then the logic has been cleanly split so that the code you see inside internal/rpcproxy/sshkeys.go is all the logic/code that would go inside of jujuapi.

I've manually tested the changes by spinning up JIMM and adding a Juju 3.5 controller and tried the following commands (not in this specific order):

juju list-ssh-keys
juju list-ssh-keys --full
juju add-ssh-key "$(cat somekey.pub)"
juju remove-ssh-key <comment>
juju remove-ssh-key <fingerprint>

Fixes JUJU-7346

Engineering checklist

  • Documentation updated
  • Covered by unit tests
  • Covered by integration tests

@kian99 kian99 requested a review from a team as a code owner January 14, 2025 15:13
@kian99 kian99 force-pushed the intercept-ssh-command-followup-2 branch 3 times, most recently from c9abbad to bceb99e Compare January 15, 2025 08:40
The model proxy will intercept calls to the keyManager facade and persist user keys in JIMM rather than passing these calls along to the Juju controller. This is being done in order to support the SSH proxy efforts.
Ideally, in Juju 4 these methods would be done on the controller api and all this logic would move into the jujuapi package.
@kian99 kian99 force-pushed the intercept-ssh-command-followup-2 branch from bceb99e to 58d722c Compare January 15, 2025 10:45
internal/rpcproxy/rpcproxy.go Show resolved Hide resolved
internal/rpcproxy/sshkeys.go Outdated Show resolved Hide resolved
return jujuparams.StringsResults{}, err
}

var formatter func(key sshkeys.PublicKey) string
Copy link
Collaborator

Choose a reason for hiding this comment

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

is this what juju does? it seems super suspicious.. i would expect formatting to happen in the cmd package

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes it's all done server side - https://github.com/juju/juju/blob/3.6/apiserver/facades/client/keymanager/keymanager.go#L111
It seems to be like this because of legacy reasons, i.e. the return type is a list of strings rather than some well defined type that the client can then format.

internal/rpcproxy/sshkeys.go Outdated Show resolved Hide resolved
@@ -0,0 +1,123 @@
// Copyright 2025 Canonical.
Copy link
Collaborator

Choose a reason for hiding this comment

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

could we rename this file to keymanagerfacade.go?

Copy link
Contributor Author

@kian99 kian99 Jan 15, 2025

Choose a reason for hiding this comment

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

What about just keymanager.go? Changing it to that for now.

internal/rpcproxy/sshkeys.go Outdated Show resolved Hide resolved
@kian99 kian99 requested a review from alesstimec January 15, 2025 12:34
// with the authenticated user.
func (s *keyManagerFacade) AddKeys(ctx context.Context, args jujuparams.ModifyUserSSHKeys) (jujuparams.ErrorResults, error) {
var res []jujuparams.ErrorResult
errF := func(err error, msg string) jujuparams.ErrorResult {
Copy link
Collaborator

Choose a reason for hiding this comment

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

suggestion: just have func(msg string) jujuparams.ErrorResult.. you are already creating a message string every time you call errF, so appending the err.Error() each time, would be a minor change.

Copy link
Contributor Author

@kian99 kian99 Jan 15, 2025

Choose a reason for hiding this comment

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

Good point, as I was changing it I noticed jujuparams.Error also accepts an error code. If I want to fill that in I need to keep the error in the signature. I'm now populating the error code, so I've mostly left things the same, let me know what you think.

Copy link
Collaborator

Choose a reason for hiding this comment

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

then perhaps

func(msg, errorCode string) jujuparams.ErrorResult {
  		return jujuparams.ErrorResult{Error: &jujuparams.Error{
			Code:    string(errors.ErrorCode(err)),
			Message: msg,
		}}
}

but just a suggestion, i won't insist on it..

internal/rpcproxy/keymanager.go Show resolved Hide resolved
// with the authenticated user.
func (s *keyManagerFacade) AddKeys(ctx context.Context, args jujuparams.ModifyUserSSHKeys) (jujuparams.ErrorResults, error) {
var res []jujuparams.ErrorResult
errF := func(err error, msg string) jujuparams.ErrorResult {
Copy link
Collaborator

Choose a reason for hiding this comment

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

then perhaps

func(msg, errorCode string) jujuparams.ErrorResult {
  		return jujuparams.ErrorResult{Error: &jujuparams.Error{
			Code:    string(errors.ErrorCode(err)),
			Message: msg,
		}}
}

but just a suggestion, i won't insist on it..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants