Skip to content

RBAC Migration Followup #4: Use upstream subject locator directly #16034

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

Closed
wants to merge 1 commit into from
Closed

RBAC Migration Followup #4: Use upstream subject locator directly #16034

wants to merge 1 commit into from

Conversation

abstractj
Copy link

@abstractj abstractj commented Aug 29, 2017

This PR attempts to fix the follow up #4 from RBAC migration.

Remaining task:

  • Merge my changes with @tiran's code to convert []rbac.Subject -> users, groups
    []string

@abstractj abstractj requested a review from enj August 29, 2017 15:07
@openshift-merge-robot openshift-merge-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Aug 29, 2017
@openshift-merge-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: abstractj
We suggest the following additional approver: liggitt

Assign the PR to them by writing /assign @liggitt in a comment when ready.

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

@abstractj
Copy link
Author

/unassign @liggitt

/assign @enj

@abstractj
Copy link
Author

/retest

groups := sets.String{}
for _, subject := range subjects {
switch subject.Kind {
case rbac.UserKind:
Copy link
Contributor

Choose a reason for hiding this comment

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

need to handle ServiceAccount and map to a username

Copy link
Author

Choose a reason for hiding this comment

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

@liggitt I was wondering if we should take the code from here https://github.com/openshift/origin/pull/16034/files#diff-5654e020d459e05c8fcca57d5675ece4R59 and move to an utility function. In this way would be possible to reuse here and also at reviewer.go
Does it make sense? If yes, which place would be the best for it?

Copy link
Contributor

Choose a reason for hiding this comment

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

sure, a utility function to go from subjects to users/groups is fine

users := sets.String{}
groups := sets.String{}
for _, subject := range subjects {
switch subject.Kind {
Copy link
Contributor

Choose a reason for hiding this comment

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

same here

@@ -62,6 +61,7 @@ import (
securityapiv1 "github.com/openshift/origin/pkg/security/apis/security/v1"
templateapiv1 "github.com/openshift/origin/pkg/template/apis/template/v1"
userapiv1 "github.com/openshift/origin/pkg/user/apis/user/v1"
"k8s.io/kubernetes/plugin/pkg/auth/authorizer/rbac"
Copy link
Contributor

Choose a reason for hiding this comment

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

group with kube imports

enj
enj previously requested changes Aug 29, 2017
scopeLimitedAuthorizer := scope.NewAuthorizer(roleBasedAuthorizer, clusterRoleGetter, messageMaker)

authorizer := authorizerunion.New(
authorizerfactory.NewPrivilegedGroups(user.SystemPrivilegedGroup), // authorizes system:masters to do anything, just like upstream
scopeLimitedAuthorizer)

return authorizer, subjectLocator
return authorizer, kubeSubjectLocator
Copy link
Contributor

Choose a reason for hiding this comment

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

This should no longer take or return the kubeSubjectLocator

// users := sets.String{}
// groups := sets.String{}
// namespace := attributes.GetNamespace()
// subjects, err := a.delegate.AllowedSubjects(attributes)
Copy link
Contributor

Choose a reason for hiding this comment

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

@abstractj sync with @tiran to use this as the foundation for two public functions:

  1. []rbac.Subject -> users, groups []string
  2. users, groups []string -> []rbac.Subject

Put those in a new file at pkg/authorization/registry/util/subject.go for now.

Copy link
Contributor

Choose a reason for hiding this comment

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

[]rbac.Subject, defaultServiceAccountNamespace -> users, groups []string

do we need the reverse?

Copy link
Contributor

Choose a reason for hiding this comment

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

@abstractj
Copy link
Author

/retest

3 similar comments
@dobbymoodge
Copy link
Contributor

/retest

@abstractj
Copy link
Author

/retest

@abstractj
Copy link
Author

/retest

@abstractj abstractj requested review from enj and liggitt August 31, 2017 12:14
@enj
Copy link
Contributor

enj commented Aug 31, 2017

@abstractj you broke it 😄

coverage: 61.6% of statements
ok  	github.com/openshift/origin/vendor/k8s.io/kubernetes/vendor/k8s.io/kube-gen/cmd/openapi-gen/generators	1.549s	coverage: 61.6% of statements
[WARNING] `go test` had the following output to stderr:
# github.com/openshift/origin/pkg/authorization/registry/localresourceaccessreview
pkg/authorization/registry/localresourceaccessreview/rest_test.go:75: cannot use authorizer (type *testAuthorizer) as type "github.com/openshift/origin/vendor/k8s.io/kubernetes/plugin/pkg/auth/authorizer/rbac".SubjectLocator in argument to resourceaccessreview.NewREST:
	*testAuthorizer does not implement "github.com/openshift/origin/vendor/k8s.io/kubernetes/plugin/pkg/auth/authorizer/rbac".SubjectLocator (missing AllowedSubjects method)
pkg/authorization/registry/localresourceaccessreview/rest_test.go:123: cannot use r.authorizer (type *testAuthorizer) as type "github.com/openshift/origin/vendor/k8s.io/kubernetes/plugin/pkg/auth/authorizer/rbac".SubjectLocator in argument to resourceaccessreview.NewREST:
	*testAuthorizer does not implement "github.com/openshift/origin/vendor/k8s.io/kubernetes/plugin/pkg/auth/authorizer/rbac".SubjectLocator (missing AllowedSubjects method)
# github.com/openshift/origin/pkg/authorization/registry/resourceaccessreview
pkg/authorization/registry/resourceaccessreview/rest_test.go:108: cannot use r.authorizer (type *testAuthorizer) as type "github.com/openshift/origin/vendor/k8s.io/kubernetes/plugin/pkg/auth/authorizer/rbac".SubjectLocator in field value:
	*testAuthorizer does not implement "github.com/openshift/origin/vendor/k8s.io/kubernetes/plugin/pkg/auth/authorizer/rbac".SubjectLocator (missing AllowedSubjects method)
[INFO] No compiled `junitreport` binary was found. Attempting to build one using:
[INFO]   $ hack/build-go.sh tools/junitreport
++ Building go targets for linux/amd64: tools/junitreport
[INFO] hack/build-go.sh exited with code 0 after 00h 00m 17s
[INFO] jUnit XML report placed at _output/scripts/test-go/artifacts/gotest_report_4HKfT.xml
Of 6084 tests executed in 2616.257s, 6065 succeeded, 0 failed, and 19 were skipped.
In suite "github.com/openshift/origin/vendor/k8s.io/kubernetes/pkg/cloudprovider/providers/cloudstack", test case "TestNewCSCloud" was skipped:
=== RUN   TestNewCSCloud
--- SKIP: TestNewCSCloud (0.00s)
	cloudstack_test.go:90: No config found in environment
In suite "github.com/openshift/origin/vendor/k8s.io/kubernetes/pkg/cloudprovider/providers/cloudstack", test case "TestLoadBalancer" was skipped:
=== RUN   TestLoadBalancer
--- SKIP: TestLoadBalancer (0.00s)

@openshift-merge-robot openshift-merge-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Sep 7, 2017
@openshift-merge-robot openshift-merge-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Oct 2, 2017
@abstractj
Copy link
Author

abstractj commented Oct 2, 2017

@enj I just did a rebase, but I've been banging my head against the wall with rest_test.go. I would appreciate if have some minutes of your life to give me a help.

Now that I changed the test I get the following output running rest_test.go:

	rest_test.go:186: diff (
		
		A: authorizer.AttributesRecord){User:(user.Info)<nil> Verb:(string)get Namespace:(string)unittest APIGroup:(string) APIVersion:(string) Resource:(string)pods Subresource:(string) Name:(string) ResourceRequest:(bool)true Path:(string)}
		
		B: interface {})<nil>
		
	rest_test.go:175: diff (*authorization.ResourceAccessReviewResponse){TypeMeta:(v1.TypeMeta){Kind:(string) APIVersion:(string)} Namespace:(string)unittest Users:(sets.String)map[
		
		A: one:{} two:{}] Groups:(sets.String)map[three:{} four:{}] EvaluationError:(string)}
		
		B: ] Groups:(sets.String)map[] EvaluationError:(string)}
		
	rest_test.go:186: diff (
		
		A: authorizer.AttributesRecord){User:(user.Info)<nil> Verb:(string)delete Namespace:(string)unittest APIGroup:(string) APIVersion:(string) Resource:(string)deploymentConfig Subresource:(string) Name:(string) ResourceRequest:(bool)true Path:(string)}
		
		B: interface {})<nil>
		

Process finished with exit code 1

Which I believe, the cause is exclusively my ignorance about it.

@abstractj
Copy link
Author

/retest

1 similar comment
@abstractj
Copy link
Author

/retest

@abstractj abstractj changed the title [DO NOT MERGE] - RBAC Migration Followup #4: Use upstream subject locator directly RBAC Migration Followup #4: Use upstream subject locator directly Oct 4, 2017
@abstractj abstractj requested a review from simo5 October 4, 2017 02:48
@abstractj abstractj dismissed enj’s stale review October 4, 2017 02:59

All the changes requested were already included, I believe.

@abstractj
Copy link
Author

@simo5 could you please give it a try and let me know what do you think? Now rest_test.go is passing, just not sure what's wrong in ci/openshift-jenkins/unit job

@simo5
Copy link
Contributor

simo5 commented Oct 4, 2017

This is the error:

pkg/authorization/registry/resourceaccessreview/rest_test.go:108: cannot use r.authorizer (type *testAuthorizer) as type "github.com/openshift/origin/vendor/k8s.io/kubernetes/plugin/pkg/auth/authorizer/rbac".SubjectLocator in field value:
	*testAuthorizer does not implement "github.com/openshift/origin/vendor/k8s.io/kubernetes/plugin/pkg/auth/authorizer/rbac".SubjectLocator (missing AllowedSubjects method)```

@openshift-ci-robot openshift-ci-robot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Oct 5, 2017
@abstractj
Copy link
Author

/retest

4 similar comments
@abstractj
Copy link
Author

/retest

@abstractj
Copy link
Author

/retest

@abstractj
Copy link
Author

/retest

@0xmichalis
Copy link
Contributor

/retest

@abstractj
Copy link
Author

/retest

1 similar comment
@abstractj
Copy link
Author

/retest

@enj enj added this to the 3.8.0 milestone Oct 10, 2017
@enj
Copy link
Contributor

enj commented Oct 10, 2017

/hold

This is not a bug, docs or tests so cannot be merged until 3.8.

I still need to review it though.

@openshift-ci-robot openshift-ci-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Oct 10, 2017
return a.users, a.groups, nil
}
return a.users, a.groups, errors.New(a.err)
subjects := authorizationutil.BuildRBACSubjects(a.users.List(), a.groups.List())
Copy link
Contributor

Choose a reason for hiding this comment

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

This is fine if the goal is to keep the diff small. But in reality the test authorizer should just return RBAC subject directly.

Copy link
Author

Choose a reason for hiding this comment

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

@enj I rebased/updated this PR with the changes you request. But this is the only part of your review which I'm not following.

The goal of this change was to keep the diff small as possible, plus I couldn't find any examples of usage to include RBAC subject directly. But I will be more than happy to look at this if you have any pointers of snippets.

@@ -4,29 +4,30 @@ import (
"errors"
"fmt"

authorizationutil "github.com/openshift/origin/pkg/authorization/util"
Copy link
Contributor

Choose a reason for hiding this comment

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

Move import to openshfit block.

return a.users, a.groups, nil
}
return a.users, a.groups, errors.New(a.err)
subjects := authorizationutil.BuildRBACSubjects(a.users.List(), a.groups.List())
Copy link
Contributor

@enj enj Oct 10, 2017

Choose a reason for hiding this comment

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

Same comment as earlier.

This is fine if the goal is to keep the diff small. But in reality the test authorizer should just return RBAC subject directly.

@@ -23,3 +25,35 @@ func BuildRBACSubjects(users, groups []string) []rbac.Subject {

return subjects
}

func ExpandSubjects(namespace string, subjects []rbac.Subject) (sets.String, sets.String, error) {
var err error
Copy link
Contributor

Choose a reason for hiding this comment

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

list of errors

users.Insert(name)
}
default:
err = fmt.Errorf("Unrecognized kinds for subjects: %s. "+
Copy link
Contributor

Choose a reason for hiding this comment

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

append to list of errors

}

}
return users, groups, err
Copy link
Contributor

Choose a reason for hiding this comment

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

NewAggregate on the errors

users, groups, expandSubjectError := authorizationutil.ExpandSubjects(namespaceName, subjects)

if expandSubjectError != nil {
return nil, expandSubjectError
Copy link
Contributor

Choose a reason for hiding this comment

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

This should not be fatal, it should add to review.evaluationError

@@ -73,7 +74,9 @@ func (r *authorizerReviewer) Review(namespaceName string) (Review, error) {
ResourceRequest: true,
}

users, groups, err := r.policyChecker.GetAllowedSubjects(attributes)
subjects, err := r.policyChecker.AllowedSubjects(attributes)
Copy link
Contributor

Choose a reason for hiding this comment

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

Something like this:

func (r *authorizerReviewer) Review(namespaceName string) (Review, error) {
	attributes := kauthorizer.AttributesRecord{
		Verb:            "get",
		Namespace:       namespaceName,
		Resource:        "namespaces",
		Name:            namespaceName,
		ResourceRequest: true,
	}

	// err is non fatal and partial success is possible
	subjects, err := r.policyChecker.AllowedSubjects(attributes)
	// expandSubjectError is non fatal and partial success is possible
	users, groups, expandSubjectError := authorizationutil.ExpandSubjects(namespaceName, subjects)

	review := &defaultReview{
		users:  users.List(),
		groups: groups.List(),
	}

	if err != nil {
		review.evaluationError = err.Error()
	}
	if expandSubjectError != nil {
		review.evaluationError += " " + expandSubjectError.Error()
	}

	return review, nil
}

@@ -63,7 +63,9 @@ func (r *REST) Create(ctx apirequest.Context, obj runtime.Object, _ bool) (runti
}

attributes := util.ToDefaultAuthorizationAttributes(nil, resourceAccessReview.Action.Namespace, resourceAccessReview.Action)
users, groups, err := r.subjectLocator.GetAllowedSubjects(attributes)
subjects, err := r.subjectLocator.AllowedSubjects(attributes)
Copy link
Contributor

Choose a reason for hiding this comment

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

Something like:

func (r *REST) Create(ctx apirequest.Context, obj runtime.Object, _ bool) (runtime.Object, error) {
	resourceAccessReview, ok := obj.(*authorizationapi.ResourceAccessReview)
	if !ok {
		return nil, kapierrors.NewBadRequest(fmt.Sprintf("not a resourceAccessReview: %#v", obj))
	}
	if errs := authorizationvalidation.ValidateResourceAccessReview(resourceAccessReview); len(errs) > 0 {
		return nil, kapierrors.NewInvalid(authorizationapi.Kind(resourceAccessReview.Kind), "", errs)
	}

	user, ok := apirequest.UserFrom(ctx)
	if !ok {
		return nil, kapierrors.NewInternalError(errors.New("missing user on request"))
	}

	// if a namespace is present on the request, then the namespace on the on the RAR is overwritten.
	// This is to support backwards compatibility.  To have gotten here in this state, it means that
	// the authorizer decided that a user could run an RAR against this namespace
	if namespace := apirequest.NamespaceValue(ctx); len(namespace) > 0 {
		resourceAccessReview.Action.Namespace = namespace

	} else if err := r.isAllowed(user, resourceAccessReview); err != nil {
		// this check is mutually exclusive to the condition above.  localSAR and localRAR both clear the namespace before delegating their calls
		// We only need to check if the RAR is allowed **again** if the authorizer didn't already approve the request for a legacy call.
		return nil, err
	}

	attributes := util.ToDefaultAuthorizationAttributes(nil, resourceAccessReview.Action.Namespace, resourceAccessReview.Action)

	// err is non fatal and partial success is possible
	subjects, err := r.subjectLocator.AllowedSubjects(attributes)
	// expandSubjectError is non fatal and partial success is possible
	users, groups, expandSubjectError := authorizationutil.ExpandSubjects(resourceAccessReview.Action.Namespace, subjects)

	response := &authorizationapi.ResourceAccessReviewResponse{
		Namespace: resourceAccessReview.Action.Namespace,
		Users:     users,
		Groups:    groups,
	}

	if err != nil {
		response.EvaluationError = err.Error()
	}
	if err != nil {
		response.EvaluationError += " " + expandSubjectError.Error()
	}

	return response, nil
}

@openshift-merge-robot openshift-merge-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Oct 11, 2017
@openshift-merge-robot openshift-merge-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Nov 8, 2017
@abstractj abstractj requested a review from enj November 9, 2017 01:41
@abstractj
Copy link
Author

Fixing the build....hold on

@abstractj
Copy link
Author

/retest

@abstractj
Copy link
Author

/hold cancel

@openshift-ci-robot openshift-ci-robot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Nov 10, 2017
@enj
Copy link
Contributor

enj commented Nov 10, 2017

/hold

Needs to wait until Monday at the earliest.

@openshift-ci-robot openshift-ci-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Nov 10, 2017
@openshift-ci-robot
Copy link

openshift-ci-robot commented Nov 13, 2017

@abstractj: The following tests failed, say /retest to rerun them all:

Test name Commit Details Rerun command
ci/openshift-jenkins/origin/verify 9fbc0c3 link /test origin-verify
ci/openshift-jenkins/experimental/integration e5955e7 link /test origin-it
ci/openshift-jenkins/extended_conformance_install_update 566de13 link /test extended_conformance_install_update
ci/openshift-jenkins/extended_conformance_gce 566de13 link /test extended_conformance_gce

Full PR test history. Your PR dashboard. Please help us cut down on flakes by linking to an open issue when you hit one in your PR.

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.

@openshift-merge-robot
Copy link
Contributor

@abstractj PR needs rebase

@openshift-merge-robot openshift-merge-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Nov 29, 2017
@enj
Copy link
Contributor

enj commented Jan 18, 2018

Superseded by #18152

@enj enj closed this Jan 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants