Skip to content

Commit

Permalink
WIP:tests
Browse files Browse the repository at this point in the history
Signed-off-by: Simo Sorce <simo@redhat.com>
  • Loading branch information
simo5 committed Jul 24, 2018
1 parent 1d1efa7 commit 727863b
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 47 deletions.
10 changes: 5 additions & 5 deletions test/cmd/authentication.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,21 @@ os::cmd::expect_success "oc policy can-i --list"
whoamitoken="$(oc process -f "${OS_ROOT}/test/testdata/authentication/scoped-token-template.yaml" TOKEN_PREFIX=whoami SCOPE=user:info USER_NAME="${username}" USER_UID="${useruid}" | oc create -f - -o name | awk -F/ '{print $2}')"
os::cmd::expect_success_and_text "oc get user/~ --token='${whoamitoken}'" "${username}"
os::cmd::expect_success_and_text "oc whoami --token='${whoamitoken}'" "${username}"
os::cmd::expect_failure_and_text "oc get pods --token='${whoamitoken}' -n '${project}'" "prevent this action; User \"scoped-user\" cannot list pods in project \"${project}\""
os::cmd::expect_failure_and_text "oc get pods --token='${whoamitoken}' -n '${project}'" "pods is forbidden: User \"scoped-user\" cannot list pods in the namespace \"${project}\""

listprojecttoken="$(oc process -f "${OS_ROOT}/test/testdata/authentication/scoped-token-template.yaml" TOKEN_PREFIX=listproject SCOPE=user:list-scoped-projects USER_NAME="${username}" USER_UID="${useruid}" | oc create -f - -o name | awk -F/ '{print $2}')"
# this token doesn't have rights to see any projects even though it can hit the list endpoint, so an empty list is correct
# we'll add another scope that allows listing all known projects even if this token has no other powers in them.
os::cmd::expect_success_and_not_text "oc get projects --token='${listprojecttoken}'" "${project}"
os::cmd::expect_failure_and_text "oc get user/~ --token='${listprojecttoken}'" 'prevent this action; User "scoped-user" cannot get users.user.openshift.io at the cluster scope'
os::cmd::expect_failure_and_text "oc get pods --token='${listprojecttoken}' -n '${project}'" "prevent this action; User \"scoped-user\" cannot list pods in project \"${project}\""
os::cmd::expect_failure_and_text "oc get user/~ --token='${listprojecttoken}'" 'User "scoped-user" cannot get users.user.openshift.io at the cluster scope'
os::cmd::expect_failure_and_text "oc get pods --token='${listprojecttoken}' -n '${project}'" "User \"scoped-user\" cannot list pods in the namespace \"${project}\""

listprojecttoken="$(oc process -f "${OS_ROOT}/test/testdata/authentication/scoped-token-template.yaml" TOKEN_PREFIX=listallprojects SCOPE=user:list-projects USER_NAME="${username}" USER_UID="${useruid}" | oc create -f - -o name | awk -F/ '{print $2}')"
os::cmd::expect_success_and_text "oc get projects --token='${listprojecttoken}'" "${project}"

adminnonescalatingpowerstoken="$(oc process -f "${OS_ROOT}/test/testdata/authentication/scoped-token-template.yaml" TOKEN_PREFIX=admin SCOPE=role:admin:* USER_NAME="${username}" USER_UID="${useruid}" | oc create -f - -o name | awk -F/ '{print $2}')"
os::cmd::expect_failure_and_text "oc get user/~ --token='${adminnonescalatingpowerstoken}'" 'prevent this action; User "scoped-user" cannot get users.user.openshift.io at the cluster scope'
os::cmd::expect_failure_and_text "oc get secrets --token='${adminnonescalatingpowerstoken}' -n '${project}'" "prevent this action; User \"scoped-user\" cannot list secrets in project \"${project}\""
os::cmd::expect_failure_and_text "oc get user/~ --token='${adminnonescalatingpowerstoken}'" 'User "scoped-user" cannot get users.user.openshift.io at the cluster scope'
os::cmd::expect_failure_and_text "oc get secrets --token='${adminnonescalatingpowerstoken}' -n '${project}'" "User \"scoped-user\" cannot list secrets in the namespace \"${project}\""
os::cmd::expect_success_and_text "oc get 'projects/${project}' --token='${adminnonescalatingpowerstoken}' -n '${project}'" "${project}"

allescalatingpowerstoken="$(oc process -f "${OS_ROOT}/test/testdata/authentication/scoped-token-template.yaml" TOKEN_PREFIX=clusteradmin SCOPE='role:cluster-admin:*:!' USER_NAME="${username}" USER_UID="${useruid}" | oc create -f - -o name | awk -F/ '{print $2}')"
Expand Down
2 changes: 1 addition & 1 deletion test/cmd/basicresources.sh
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ project=$(oc project -q)
os::cmd::expect_success 'oc policy add-role-to-user view view-user'
os::cmd::expect_success 'oc login -u view-user -p anything'
os::cmd::try_until_success 'oc project ${project}'
os::cmd::expect_failure_and_text "oc set env dc/test-deployment-config --list --resolve" "cannot get secrets in project"
os::cmd::expect_failure_and_text "oc set env dc/test-deployment-config --list --resolve" "cannot get secrets in the namespace"
oc login -u system:admin
# clean up
os::cmd::expect_success "oc delete dc/test-deployment-config"
Expand Down
2 changes: 1 addition & 1 deletion test/cmd/policy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ os::cmd::expect_success_and_text 'oc policy scc-review -z default -f ${OS_ROOT}
os::cmd::expect_success_and_text 'oc policy scc-review -z system:serviceaccount:policy-second:default -f ${OS_ROOT}/test/testdata/job.yaml --no-headers=true' 'Job/hello default lax'
os::cmd::expect_success_and_text 'oc policy scc-review -f ${OS_ROOT}/test/extended/testdata/deployments/deployment-simple.yaml --no-headers=true' 'DeploymentConfig/deployment-simple default lax'
os::cmd::expect_success_and_text 'oc policy scc-review -f ${OS_ROOT}/test/testdata/nginx_pod.yaml --no-headers=true' ''
os::cmd::expect_failure_and_text 'oc policy scc-review -z default -f ${OS_ROOT}/test/testdata/job.yaml --namespace=no-exist' 'error: unable to compute Pod Security Policy Review for "hello": podsecuritypolicyreviews.security.openshift.io is forbidden: User "bob" cannot create podsecuritypolicyreviews.security.openshift.io in the namespace "no-exist": User "bob" cannot create podsecuritypolicyreviews.security.openshift.io in project "no-exist"'
os::cmd::expect_failure_and_text 'oc policy scc-review -z default -f ${OS_ROOT}/test/testdata/job.yaml --namespace=no-exist' 'error: unable to compute Pod Security Policy Review for "hello": podsecuritypolicyreviews.security.openshift.io is forbidden: User "bob" cannot create podsecuritypolicyreviews.security.openshift.io in the namespace "no-exist"'
os::cmd::expect_failure_and_text 'oc policy scc-review -z default -f ${OS_ROOT}/test/testdata/pspreview_unsupported_statefulset.yaml' 'error: StatefulSet "rd" with spec.volumeClaimTemplates currently not supported.'
os::cmd::expect_failure_and_text 'oc policy scc-review -z no-exist -f ${OS_ROOT}/test/testdata/job.yaml' 'error: unable to compute Pod Security Policy Review for "hello": unable to retrieve ServiceAccount no-exist: serviceaccount "no-exist" not found'
os::cmd::expect_success "oc login -u system:admin -n '${project}'"
Expand Down
58 changes: 21 additions & 37 deletions test/integration/authorization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,7 @@ func TestAuthorizationSubjectAccessReviewAPIGroup(t *testing.T) {
kubeAuthInterface: clusterAdminSARGetter,
response: authorizationapi.SubjectAccessReviewResponse{
Allowed: true,
Reason: "allowed by openshift authorizer",
Reason: `RBAC: allowed by RoleBinding "admin/hammer-project" of ClusterRole "admin" to User "harold"`,
Namespace: "hammer-project",
},
}.run(t)
Expand All @@ -889,7 +889,7 @@ func TestAuthorizationSubjectAccessReviewAPIGroup(t *testing.T) {
kubeAuthInterface: clusterAdminSARGetter,
response: authorizationapi.SubjectAccessReviewResponse{
Allowed: false,
Reason: `User "harold" cannot get horizontalpodautoscalers in project "hammer-project"`,
Reason: ``,
Namespace: "hammer-project",
},
}.run(t)
Expand All @@ -903,7 +903,7 @@ func TestAuthorizationSubjectAccessReviewAPIGroup(t *testing.T) {
kubeAuthInterface: clusterAdminKubeClient.Authorization(),
response: authorizationapi.SubjectAccessReviewResponse{
Allowed: false,
Reason: `User "harold" cannot get horizontalpodautoscalers.foo in project "hammer-project"`,
Reason: ``,
Namespace: "hammer-project",
},
}.run(t)
Expand All @@ -917,7 +917,7 @@ func TestAuthorizationSubjectAccessReviewAPIGroup(t *testing.T) {
kubeAuthInterface: clusterAdminSARGetter,
response: authorizationapi.SubjectAccessReviewResponse{
Allowed: false,
Reason: `User "harold" cannot get horizontalpodautoscalers.* in project "hammer-project"`,
Reason: ``,
Namespace: "hammer-project",
},
}.run(t)
Expand Down Expand Up @@ -1068,7 +1068,7 @@ func TestAuthorizationSubjectAccessReview(t *testing.T) {
kubeAuthInterface: clusterAdminLocalSARGetter,
response: authorizationapi.SubjectAccessReviewResponse{
Allowed: true,
Reason: "allowed by openshift authorizer",
Reason: `RBAC: allowed by RoleBinding "view/default" of ClusterRole "view" to User "danny"`,
Namespace: "default",
},
}.run(t)
Expand All @@ -1079,7 +1079,7 @@ func TestAuthorizationSubjectAccessReview(t *testing.T) {
kubeAuthInterface: clusterAdminLocalSARGetter,
response: authorizationapi.SubjectAccessReviewResponse{
Allowed: false,
Reason: `User "danny" cannot get projects at the cluster scope`,
Reason: ``,
Namespace: "",
},
}.run(t)
Expand Down Expand Up @@ -1133,7 +1133,7 @@ func TestAuthorizationSubjectAccessReview(t *testing.T) {
kubeAuthInterface: haroldSARGetter,
response: authorizationapi.SubjectAccessReviewResponse{
Allowed: true,
Reason: "allowed by openshift authorizer",
Reason: `RBAC: allowed by RoleBinding "view/hammer-project" of ClusterRole "view" to User "valerie"`,
Namespace: "hammer-project",
},
}.run(t)
Expand All @@ -1144,7 +1144,7 @@ func TestAuthorizationSubjectAccessReview(t *testing.T) {
kubeAuthInterface: markSARGetter,
response: authorizationapi.SubjectAccessReviewResponse{
Allowed: false,
Reason: `User "valerie" cannot get project "mallet-project"`,
Reason: ``,
Namespace: "mallet-project",
},
}.run(t)
Expand All @@ -1160,7 +1160,7 @@ func TestAuthorizationSubjectAccessReview(t *testing.T) {
kubeAuthInterface: markSARGetter,
response: authorizationapi.SubjectAccessReviewResponse{
Allowed: true,
Reason: "allowed by openshift authorizer",
Reason: `RBAC: allowed by RoleBinding "edit/mallet-project" of ClusterRole "edit" to User "edgar"`,
Namespace: "mallet-project",
},
}.run(t)
Expand Down Expand Up @@ -1214,7 +1214,7 @@ func TestAuthorizationSubjectAccessReview(t *testing.T) {
kubeAuthInterface: haroldSARGetter,
response: authorizationapi.SubjectAccessReviewResponse{
Allowed: true,
Reason: "allowed by openshift authorizer",
Reason: `RBAC: allowed by RoleBinding "admin/hammer-project" of ClusterRole "admin" to User "harold"`,
Namespace: "hammer-project",
},
}.run(t)
Expand All @@ -1230,7 +1230,7 @@ func TestAuthorizationSubjectAccessReview(t *testing.T) {
kubeAuthInterface: clusterAdminLocalSARGetter,
response: authorizationapi.SubjectAccessReviewResponse{
Allowed: true,
Reason: "allowed by cluster rule",
Reason: `RBAC: allowed by ClusterRoleBinding "cluster-admins" of ClusterRole "cluster-admin" to Group "system:cluster-admins"`,
Namespace: "",
},
}.run(t)
Expand All @@ -1253,7 +1253,7 @@ func TestAuthorizationSubjectAccessReview(t *testing.T) {
kubeAuthInterface: haroldSARGetter,
response: authorizationapi.SubjectAccessReviewResponse{
Allowed: true,
Reason: "allowed by openshift authorizer",
Reason: `RBAC: allowed by RoleBinding "admin/hammer-project" of ClusterRole "admin" to User "harold"`,
Namespace: "hammer-project",
},
}.run(t)
Expand All @@ -1264,7 +1264,7 @@ func TestAuthorizationSubjectAccessReview(t *testing.T) {
kubeAuthInterface: anonymousSARGetter,
response: authorizationapi.SubjectAccessReviewResponse{
Allowed: true,
Reason: "allowed by openshift authorizer",
Reason: `RBAC: allowed by RoleBinding "edit/hammer-project" of ClusterRole "edit" to User "system:anonymous"`,
Namespace: "hammer-project",
},
}.run(t)
Expand All @@ -1277,7 +1277,7 @@ func TestAuthorizationSubjectAccessReview(t *testing.T) {
kubeAuthInterface: haroldSARGetter,
response: authorizationapi.SubjectAccessReviewResponse{
Allowed: false,
Reason: `User "harold" cannot create pods in project "mallet-project"`,
Reason: ``,
Namespace: "mallet-project",
},
}.run(t)
Expand All @@ -1288,7 +1288,7 @@ func TestAuthorizationSubjectAccessReview(t *testing.T) {
kubeAuthInterface: anonymousSARGetter,
response: authorizationapi.SubjectAccessReviewResponse{
Allowed: false,
Reason: `User "system:anonymous" cannot create pods in project "mallet-project"`,
Reason: ``,
Namespace: "mallet-project",
},
}.run(t)
Expand All @@ -1302,7 +1302,7 @@ func TestAuthorizationSubjectAccessReview(t *testing.T) {
kubeAuthInterface: haroldSARGetter,
response: authorizationapi.SubjectAccessReviewResponse{
Allowed: false,
Reason: `User "harold" cannot create pods in project "nonexistent-project"`,
Reason: ``,
Namespace: "nonexistent-project",
},
}.run(t)
Expand All @@ -1313,7 +1313,7 @@ func TestAuthorizationSubjectAccessReview(t *testing.T) {
kubeAuthInterface: anonymousSARGetter,
response: authorizationapi.SubjectAccessReviewResponse{
Allowed: false,
Reason: `User "system:anonymous" cannot create pods in project "nonexistent-project"`,
Reason: ``,
Namespace: "nonexistent-project",
},
}.run(t)
Expand All @@ -1328,7 +1328,7 @@ func TestAuthorizationSubjectAccessReview(t *testing.T) {
localReview: askCanICreatePolicyBindings,
response: authorizationapi.SubjectAccessReviewResponse{
Allowed: false,
Reason: `User "harold" cannot create policybindings in project "hammer-project"`,
Reason: ``,
Namespace: "hammer-project",
},
}.run(t)
Expand Down Expand Up @@ -1364,14 +1364,6 @@ func TestBrowserSafeAuthorizer(t *testing.T) {
proxyVerb := []string{"api", "v1", "proxy", "namespaces", "ns", "pods", "podX1:8080"}
proxySubresource := []string{"api", "v1", "namespaces", "ns", "pods", "podX1:8080", "proxy", "appEndPoint"}

isUnsafeErr := func(errProxy error) (matches bool) {
if errProxy == nil {
return false
}
return strings.Contains(errProxy.Error(), `cannot "unsafeproxy" "pods" with name "podX1:8080" in project "ns"`) ||
strings.Contains(errProxy.Error(), `cannot get pods/unsafeproxy in project "ns"`)
}

for _, tc := range []struct {
name string
client rest.Interface
Expand All @@ -1383,35 +1375,27 @@ func TestBrowserSafeAuthorizer(t *testing.T) {
name: "safe to proxy verb",
client: userClient.Core().RESTClient(),
path: proxyVerb,

expectUnsafe: false,
},
{
name: "safe to proxy subresource",
client: userClient.Core().RESTClient(),
path: proxySubresource,

expectUnsafe: false,
},
{
name: "unsafe to proxy verb",
client: anonymousClient,
path: proxyVerb,

expectUnsafe: true,
},
{
name: "unsafe to proxy subresource",
client: anonymousClient,
path: proxySubresource,

expectUnsafe: true,
},
} {
errProxy := tc.client.Get().AbsPath(tc.path...).Do().Error()
if errProxy == nil || !kapierror.IsForbidden(errProxy) || tc.expectUnsafe != isUnsafeErr(errProxy) {
t.Errorf("%s: expected forbidden error on GET %s, got %#v (isForbidden=%v, expectUnsafe=%v, actualUnsafe=%v)",
tc.name, tc.path, errProxy, kapierror.IsForbidden(errProxy), tc.expectUnsafe, isUnsafeErr(errProxy))
if errProxy == nil || !kapierror.IsForbidden(errProxy) {
t.Errorf("%s: expected forbidden error on GET %s, got %#v (isForbidden=%v)",
tc.name, tc.path, errProxy, kapierror.IsForbidden(errProxy))
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/integration/bootstrap_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func TestBootstrapPolicySelfSubjectAccessReviews(t *testing.T) {
kubeAuthInterface: valerieKubeClient.Authorization(),
response: authorizationapi.SubjectAccessReviewResponse{
Allowed: false,
Reason: `User "valerie" cannot create policybindings in project "openshift"`,
Reason: ``,
Namespace: "openshift",
},
}.run(t)
Expand Down Expand Up @@ -148,7 +148,7 @@ func TestSelfSubjectAccessReviewsNonExistingNamespace(t *testing.T) {
kubeAuthInterface: valerieKubeClient.Authorization(),
response: authorizationapi.SubjectAccessReviewResponse{
Allowed: false,
Reason: `User "valerie" cannot create pods in project "foo"`,
Reason: ``,
Namespace: "foo",
},
}.run(t)
Expand Down
2 changes: 1 addition & 1 deletion test/integration/oauth_cert_fallback_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestOAuthCertFallback(t *testing.T) {
certUser = "system:admin"

unauthorizedError = "Unauthorized"
anonymousError = `users.user.openshift.io "~" is forbidden: User "system:anonymous" cannot get users.user.openshift.io at the cluster scope: User "system:anonymous" cannot get users.user.openshift.io at the cluster scope`
anonymousError = `users.user.openshift.io "~" is forbidden: User "system:anonymous" cannot get users.user.openshift.io at the cluster scope`
)

// Build master config
Expand Down

0 comments on commit 727863b

Please sign in to comment.