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

return error when long-form sa name is used #17061

Conversation

juanvallejo
Copy link
Contributor

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

Returns an error when the long-form name of a ServiceAccount is used
with the --serviceaccount (-z) flag in `oc policy ...' commands.

/assign enj

cc @openshift/cli-review

@openshift-ci-robot openshift-ci-robot added the size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. label Oct 26, 2017
@juanvallejo
Copy link
Contributor Author

/test extended_networking_minimal

@juanvallejo
Copy link
Contributor Author

/retest

@@ -320,6 +321,13 @@ func (o *RoleModificationOptions) CompleteUserWithSA(f *clientcmd.Factory, cmd *
return errors.New("you must specify at least one user or service account")
}

// return an error if a fully-qualified service-account name is used
Copy link
Contributor

Choose a reason for hiding this comment

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

You should instead check that all names are valid SA names.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, kept this check and added an additional check for the overall validity of each given SA name

@juanvallejo juanvallejo force-pushed the jvallejo/prevent-use-of-sa-long-form-names-with-z branch from f94a3a5 to 764476d Compare October 27, 2017 19:12
@openshift-ci-robot openshift-ci-robot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Oct 27, 2017
@juanvallejo juanvallejo force-pushed the jvallejo/prevent-use-of-sa-long-form-names-with-z branch from 764476d to 34f7b71 Compare October 27, 2017 19:12
// return an error if a fully-qualified service-account name is used
for _, sa := range saNames {
if strings.HasPrefix(sa, "system:serviceaccount") {
return errors.New("--serviceaccount (-z) should only be used with short-form serviceaccount names (e.g. \"default\")")
Copy link
Contributor

Choose a reason for hiding this comment

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

Could use back ticks to avoid having to escape.

return errors.New("--serviceaccount (-z) should only be used with short-form serviceaccount names (e.g. \"default\")")
}

if len(validation.ValidateServiceAccountName(sa, false)) > 0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why suppress the actual errors?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Saw this done in at least one other place, so copied it; will store and append the errors to the final message

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated message to show indented causes in new lines:

$ oc policy add-role-to-user admin -z :default
error: ":default" is not a valid serviceaccount name:
  a DNS-1123 subdomain must consist of lower case alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character (e.g. 'example.com', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*')
See 'oc policy add-role-to-user -h' for help and examples.

@juanvallejo juanvallejo force-pushed the jvallejo/prevent-use-of-sa-long-form-names-with-z branch from 34f7b71 to 24088e7 Compare October 27, 2017 22:59
@juanvallejo
Copy link
Contributor Author

@enj thanks for the feedback, review comments addressed

Copy link
Contributor

@enj enj left a comment

Choose a reason for hiding this comment

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

Minor comment. BTW if you put:

Bug 1425398

in the commit message, the bot will link the PR with the BZ.

if errCauses := validation.ValidateServiceAccountName(sa, false); len(errCauses) > 0 {
message := fmt.Sprintf("%q is not a valid serviceaccount name:\n", sa)
for _, cause := range errCauses {
message += " " + cause
Copy link
Contributor

Choose a reason for hiding this comment

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

You could use strings.Join

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I could, although I've seen it done both ways throughout the code - would one format over the other make much difference here?

bug: 1425398
Returns an error when the long-form name of a ServiceAccount is used
with the --serviceaccount (-z) flag in `oc policy ...' commands, or
if the name given is invalid.
@juanvallejo juanvallejo force-pushed the jvallejo/prevent-use-of-sa-long-form-names-with-z branch from 24088e7 to d70ebd6 Compare October 30, 2017 14:15
@juanvallejo
Copy link
Contributor Author

/test extended_conformance_install_update

@enj
Copy link
Contributor

enj commented Oct 30, 2017

/lgtm

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

/approve
/kind bug

@openshift-ci-robot openshift-ci-robot added the kind/bug Categorizes issue or PR as related to a bug. label Oct 30, 2017
@openshift-merge-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: enj, fabianofranz, juanvallejo

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

@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 30, 2017
@openshift-merge-robot
Copy link
Contributor

Automatic merge from submit-queue.

@openshift-merge-robot openshift-merge-robot merged commit a9d975c into openshift:master Oct 30, 2017
@juanvallejo juanvallejo deleted the jvallejo/prevent-use-of-sa-long-form-names-with-z branch October 30, 2017 19:33
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/S Denotes a PR that changes 10-29 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants