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

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
15 changes: 15 additions & 0 deletions pkg/oc/admin/policy/modify_roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"errors"
"fmt"
"io"
"strings"

"github.com/spf13/cobra"

kapierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/validation"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
kapi "k8s.io/kubernetes/pkg/api"
Expand Down Expand Up @@ -320,6 +322,19 @@ 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

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`)")
}

if errCauses := validation.ValidateServiceAccountName(sa, false); len(errCauses) > 0 {
message := fmt.Sprintf("%q is not a valid serviceaccount name:\n ", sa)
message += strings.Join(errCauses, "\n ")
return errors.New(message)
}
}

authorizationClient, err := f.OpenshiftInternalAuthorizationClient()
if err != nil {
return err
Expand Down
5 changes: 5 additions & 0 deletions test/cmd/policy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ os::cmd::expect_success "oc login -u system:admin -n '${project}'"
os::cmd::expect_success 'oc delete project policy-login'
os::cmd::expect_failure_and_text 'oc create policybinding default -n myproject' 'error: the server does not support legacy policy resources'

# validate --serviceaccount values
os::cmd::expect_success_and_text 'oc policy add-role-to-user admin -z default' 'role "admin" added\: "default"'
os::cmd::expect_failure_and_text 'oc policy add-role-to-user admin -z system:serviceaccount:test:default' 'should only be used with short\-form serviceaccount names'
os::cmd::expect_failure_and_text 'oc policy add-role-to-user admin -z :invalid' '"\:invalid" is not a valid serviceaccount name'

# This test validates user level policy
os::cmd::expect_failure_and_text 'oc policy add-role-to-user' 'you must specify a role'
os::cmd::expect_failure_and_text 'oc policy add-role-to-user -z NamespaceWithoutRole' 'you must specify a role'
Expand Down