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

virtctl: add commands to dynamically set SSH keys and passwords on a VM #9818

Merged
merged 3 commits into from Jun 14, 2023

Conversation

akrejcir
Copy link
Contributor

@akrejcir akrejcir commented May 29, 2023

What this PR does / why we need it:
Added 3 commands to virtctl:

  • virtctl credentials add-ssh-key - Adds an SSH public key to one of the secrets that are specified in AccessCredentials field of a VM or VMI.
  • virtctl credentials remove-ssh-key - Removes an SSH key from one of the secretes.
  • virtctl credentials set-password - Sets a user's password in one of the secretes.

Release note:

Added "virtctl credentials" commands to dynamically change SSH keys in a VM, and to set user's password.

@kubevirt-bot kubevirt-bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. dco-signoff: yes Indicates the PR's author has DCO signed all their commits. size/XXL labels May 29, 2023
@akrejcir
Copy link
Contributor Author

/cc @0xFelix @ksimon1

@akrejcir
Copy link
Contributor Author

/retest

@alicefr
Copy link
Member

alicefr commented May 30, 2023

@akrejcir does the secret need already to exist before adding the ssh key? If so, shouldn't we create the secret together with the first key we want to add?

@akrejcir
Copy link
Contributor Author

Good point. Yes the secret needs to exist. We cannot change running VM, but we can create the secret for a stopped VM.

/hold

@kubevirt-bot kubevirt-bot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label May 30, 2023
pkg/virtctl/credentials/credentials_test.go Outdated Show resolved Hide resolved

func secretContainsKey(secretData map[string][]byte, key string) bool {
for _, data := range secretData {
lines := strings.Split(string(data), "\n")
Copy link
Member

Choose a reason for hiding this comment

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

Is it correct checking the ssh key line by line? I mean we could have new lines or not but the key is the same? Shouldn't we trim the data and eliminate then \n and then compare?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

One data entry in the Secret contains a file, that can contain multiple keys, each on it's own line.
That's why this loop checks each line separately.

pkg/virtctl/credentials/credentials.go Outdated Show resolved Hide resolved
tests/virtctl/credentials.go Outdated Show resolved Hide resolved
@alicefr
Copy link
Member

alicefr commented May 30, 2023

What happens if the propagation method for the ssh key injection is cloud-init? Is this command not supported?

Copy link
Member

@0xFelix 0xFelix left a comment

Choose a reason for hiding this comment

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

This is quite a large PR. Tbh. I'd like to see it split up into smaller an easier to review PRs.

Added some comments.

pkg/virtctl/credentials/credentials.go Outdated Show resolved Hide resolved
pkg/virtctl/credentials/credentials.go Outdated Show resolved Hide resolved
pkg/virtctl/credentials/credentials.go Outdated Show resolved Hide resolved
pkg/virtctl/credentials/credentials.go Outdated Show resolved Hide resolved
pkg/virtctl/credentials/credentials.go Outdated Show resolved Hide resolved
pkg/virtctl/credentials/credentials.go Outdated Show resolved Hide resolved
pkg/virtctl/credentials/credentials.go Outdated Show resolved Hide resolved
pkg/virtctl/credentials/credentials.go Outdated Show resolved Hide resolved
pkg/virtctl/credentials/credentials.go Outdated Show resolved Hide resolved
pkg/virtctl/credentials/credentials_test.go Outdated Show resolved Hide resolved
@alicefr
Copy link
Member

alicefr commented Jun 1, 2023

This is quite a large PR. Tbh. I'd like to see it split up into smaller an easier to review PRs.

Added some comments.

IMO, if we try to reduce the code duplication into functions, the size of the pr should reasonably decrease

@kubevirt-bot kubevirt-bot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jun 1, 2023
@akrejcir akrejcir force-pushed the virtctl-credentials branch 2 times, most recently from 45ea683 to d4b4082 Compare June 2, 2023 15:33
@kubevirt-bot kubevirt-bot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jun 2, 2023
@akrejcir
Copy link
Contributor Author

akrejcir commented Jun 2, 2023

Added new functionality.

Can you take another look?

@akrejcir
Copy link
Contributor Author

akrejcir commented Jun 5, 2023

/hold cancel

@kubevirt-bot kubevirt-bot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Jun 5, 2023
pkg/virtctl/credentials/common/common.go Show resolved Hide resolved
pkg/virtctl/credentials/set-password/set-password.go Outdated Show resolved Hide resolved
pkg/virtctl/credentials/set-password/set-password.go Outdated Show resolved Hide resolved
if err != nil {
// TODO: Check error type and only execute second patch for some errors
// Try adding the array
patchData := patchToAddAccessCredentialArray(accessCredential)
Copy link
Member

Choose a reason for hiding this comment

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

When is this necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If the VM has no credentials, the slice is nil, and patch will fail, because path /spec/template/spec/accessCredentials/- does not exist. So this second patch is used to add the array.

pkg/virtctl/credentials/add-key/add-key_test.go Outdated Show resolved Hide resolved
pkg/virtctl/credentials/remove-key/remove-key_test.go Outdated Show resolved Hide resolved
pkg/virtctl/credentials/remove-key/remove-key_test.go Outdated Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

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

What about clearing the password secret?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is no QEMU GA command to clear a password that has been set for a user. We could clear the secret, but the VM would still have the password.

Copy link
Member

Choose a reason for hiding this comment

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

ACK, might be worth adding a --clear flag in a follow up.

pkg/virtctl/credentials/set-password/set-password_test.go Outdated Show resolved Hide resolved
@akrejcir akrejcir force-pushed the virtctl-credentials branch 3 times, most recently from b640227 to f4bbf9b Compare June 5, 2023 20:07
@akrejcir akrejcir force-pushed the virtctl-credentials branch 2 times, most recently from e1581e8 to f4d4cdf Compare June 6, 2023 14:22
@akrejcir
Copy link
Contributor Author

akrejcir commented Jun 6, 2023

/retest

Copy link
Member

@0xFelix 0xFelix left a comment

Choose a reason for hiding this comment

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

Can you add examples to the cmds and give some context when they are working (using qemu guest propagation) or not working (using cloud init propagation)?

Also, do you think the init/updating/helper code in the test files could be deduplicated?

@akrejcir
Copy link
Contributor Author

I've added usage examples.

@akrejcir
Copy link
Contributor Author

@0xFelix , @alicefr Can you take another look?

The command adds a SSH public key to the secret
specified in the "AccessCredentials" field of a VM or VMI.

Signed-off-by: Andrej Krejcir <akrejcir@redhat.com>
The command removes specified key from all the secrets
that are specified in the "AccessCredentials" list.

Signed-off-by: Andrej Krejcir <akrejcir@redhat.com>
The command modifies a secret to set the user's password.

Signed-off-by: Andrej Krejcir <akrejcir@redhat.com>
Copy link
Member

@0xFelix 0xFelix left a comment

Choose a reason for hiding this comment

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

The descriptions of the commands could still be a little improved and IMO the unit test code could still be deduplicated but let's do it in a follow up. For now this looks good to me.

/lgtm

@kubevirt-bot kubevirt-bot added the lgtm Indicates that a PR is ready to be merged. label Jun 12, 2023
@akrejcir
Copy link
Contributor Author

/retest

1 similar comment
@akrejcir
Copy link
Contributor Author

/retest


keySecret, err = cli.CoreV1().Secrets(util.NamespaceTestDefault).Create(context.Background(), keySecret, metav1.CreateOptions{})
Expect(err).ToNot(HaveOccurred())
DeferCleanup(func() {
Copy link
Member

Choose a reason for hiding this comment

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

Is this necessary? Aren't the secrets deleted by default on the namespace test cleanup?

Expect(secret.Data).To(ContainElement([]byte(testKey2)))
})

It("[test_id:TODO] should add ssh key from a file", func() {
Copy link
Member

Choose a reason for hiding this comment

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

test_id:TODO should be updated


// First, Try to add the new access credential to the existing array.
_, err = cli.VirtualMachine(vm.Namespace).Patch(cmd.Context(), vm.Name, types.JSONPatchType, common.MustMarshalPatch(accessCredentialPatch), &metav1.PatchOptions{})
if err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

nit:any chances to check the actual error? Maybe with Contains?

@alicefr
Copy link
Member

alicefr commented Jun 13, 2023

/approve
Left a couple of questions and nit comments.
I'm also missing the answer to #9818 (comment) . If not addressed, this could be done in a follow PR

@kubevirt-bot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: alicefr

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@kubevirt-bot kubevirt-bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jun 13, 2023
@kubevirt-bot
Copy link
Contributor

kubevirt-bot commented Jun 14, 2023

@akrejcir: The following test failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
pull-kubevirt-e2e-k3d-1.25-sriov d4b4082 link true /test pull-kubevirt-e2e-k3d-1.25-sriov

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here.

@kubevirt-commenter-bot
Copy link

/retest-required
This bot automatically retries required jobs that failed/flaked on approved PRs.
Silence the bot with an /lgtm cancel or /hold comment for consistent failures.

@kubevirt-bot kubevirt-bot merged commit 7a4a4e3 into kubevirt:main Jun 14, 2023
34 checks passed
@akrejcir akrejcir deleted the virtctl-credentials branch June 21, 2023 06:59
@akrejcir
Copy link
Contributor Author

/cherry-pick release-1.0

@kubevirt-bot
Copy link
Contributor

@akrejcir: new pull request created: #9969

In response to this:

/cherry-pick release-1.0

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. dco-signoff: yes Indicates the PR's author has DCO signed all their commits. lgtm Indicates that a PR is ready to be merged. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/XXL
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants