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

parse resource name before removing deleted secret #17004

Conversation

juanvallejo
Copy link
Contributor

@juanvallejo juanvallejo commented Oct 23, 2017

Fixes https://bugzilla.redhat.com/show_bug.cgi?id=1457602

Although unlinking deleted secrets from a serviceaccount is currently
supported, oc secret unlink fails to unlink a deleted secret if its
name is specified as secrets/deleted-secret-name.

This patch parses each secret's name, removing the secrets/ segment
before appending it to a string set of removed secret names.

cc @openshift/cli-review

@openshift-ci-robot openshift-ci-robot added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Oct 23, 2017
}
return names
}

// parseResourceName receives either a resource name as either
// <resource type> / <name> or <name> and returns only the resource <name>.
func parseResourceName(name string) string {
Copy link
Contributor

Choose a reason for hiding this comment

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

This isn't meant to be generic, right? Maybe we could strip the secret(s)/ prefix explicitly in that case. That would also be more clear in my opinion even if we are certain at this point that name has secret/ as the prefix if it contains a slash. Otherwise, what happens if i oc secret unlink whatever/steve?

os::cmd::expect_success 'oc create secret generic deleted-secret'
os::cmd::expect_success 'oc secrets link deployer deleted-secret'
# confirm our soon-to-be-deleted secret has been linked
os::cmd::expect_success 'oc get serviceaccounts/deployer -o yaml |grep -q deleted-secret'
Copy link
Contributor

Choose a reason for hiding this comment

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

Use jsonpath and validate that no secret is linked.

oc get serviceaccount deployer -o jsonpath='{.secrets[?(@.name=="deleted-secret")]}'

@juanvallejo juanvallejo force-pushed the jvallejo/parse-rsrs-arg-when-removing-deleted-secret branch from cd1ce5b to e04dc8d Compare October 23, 2017 18:41
@openshift-ci-robot openshift-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Oct 23, 2017
@juanvallejo
Copy link
Contributor Author

@stevekuznetsov thanks for the feedback, updated tests to use jsonpath; added check to ensure resource type is secret/secrets before "parsing" a user-specified secret name

@juanvallejo juanvallejo force-pushed the jvallejo/parse-rsrs-arg-when-removing-deleted-secret branch from e04dc8d to c0e6b17 Compare October 23, 2017 18:43
return name
}

if segs[0] == "secret" || segs[0] == "secrets" {
Copy link
Contributor

Choose a reason for hiding this comment

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

If you have a slash and this is not true, there is an error, right?

Copy link
Contributor Author

@juanvallejo juanvallejo Oct 23, 2017

Choose a reason for hiding this comment

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

Right, the name will be returned as-is by this function (e.g. pods/my-secret), and the user will end up seeing

secret "pod/my-secret" not found
error: No valid secrets found or secrets not linked to service account

Copy link
Contributor

Choose a reason for hiding this comment

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

OK. I meant more forcefully -- the error really is that a secret name cannot start with pod/ not that pod/my-secret was not found

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good, added a check for this in the Validate method

os::cmd::expect_success 'oc secrets link deployer deleted-secret'
# confirm our soon-to-be-deleted secret has been linked
os::cmd::expect_success_and_text "oc get serviceaccount deployer -o jsonpath='{.secrets[?(@.name==\"deleted-secret\")]}'" 'deleted\-secret'
os::cmd::expect_success 'oc get serviceaccounts/deployer -o yaml |grep -q deleted-secret'
Copy link
Contributor

Choose a reason for hiding this comment

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

So when you unlink you should see no text here

Copy link
Contributor

Choose a reason for hiding this comment

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

In the JSONpath that is -- there should be no yaml grep in the final tests

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, this is testing that the secret has linked. A few lines below from this, I test unlinking by calling os::cmd::expect_success_and_not_text

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also, forgot to remove the old grep check. Will remove that

Although unlinking deleted secrets from a serviceaccount is currently
supported, `oc secret unlink` failed to unlink a deleted secret if its
name was specified as secrets/deleted-secret-name.

This patch parses each secret's name, removing the <secrets/> segment
before appending it to a string set of removed secret names.
@juanvallejo juanvallejo force-pushed the jvallejo/parse-rsrs-arg-when-removing-deleted-secret branch from c0e6b17 to a17af0e Compare October 23, 2017 18:59
Copy link
Contributor

@stevekuznetsov stevekuznetsov left a comment

Choose a reason for hiding this comment

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

/lgtm

@openshift-ci-robot openshift-ci-robot added the lgtm Indicates that a PR is ready to be merged. label Oct 24, 2017
@stevekuznetsov
Copy link
Contributor

/approve
/kind bug

@openshift-merge-robot openshift-merge-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Oct 24, 2017
@openshift-ci-robot openshift-ci-robot added the kind/bug Categorizes issue or PR as related to a bug. label Oct 24, 2017
@openshift-merge-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: juanvallejo, stevekuznetsov

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

Needs approval from an approver in each of these OWNERS Files:

You can indicate your approval by writing /approve in a comment
You can cancel your approval by writing /approve cancel in a comment

@juanvallejo
Copy link
Contributor Author

/test extended_conformance_install_update

@openshift-bot
Copy link
Contributor

/retest

Please review the full test history for this PR and help us cut down flakes.

@openshift-merge-robot
Copy link
Contributor

Automatic merge from submit-queue.

@openshift-merge-robot openshift-merge-robot merged commit faffc2f into openshift:master Oct 25, 2017
@juanvallejo juanvallejo deleted the jvallejo/parse-rsrs-arg-when-removing-deleted-secret branch October 25, 2017 13:55
@@ -68,6 +69,16 @@ func (o SecretOptions) Validate() error {
return errors.New("KubeCoreClient must be present")
}

// if any secret names are of the form <resource>/<name>,
// ensure <resource> is a secret.
for _, secretName := range o.SecretNames {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why isn't this using standard resource builder?

Copy link
Contributor Author

@juanvallejo juanvallejo Oct 30, 2017

Choose a reason for hiding this comment

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

Why isn't this using standard resource builder?

The resource builder is used in the GetSecrets method. The code block here validates that a resource kind is secret if it is specified as <kind>/<name>, before getting to the builder, which ignores NotFound errors in order to allow deleted secrets to still be unlinked. Without this check, if a user provides a command such as: oc secret unlink sa_name pods/not_a_secret, this line would create a secret with the name pods/not_a_secret.

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. kind/bug Categorizes issue or PR as related to a bug. lgtm Indicates that a PR is ready to be merged. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants