-
Notifications
You must be signed in to change notification settings - Fork 70
✨ Add preauthorizer checks to Boxcutter applier #2443
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ import ( | |
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "io/fs" | ||
| "maps" | ||
| "slices" | ||
|
|
@@ -16,6 +17,8 @@ import ( | |
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
| "k8s.io/apimachinery/pkg/runtime" | ||
| "k8s.io/apiserver/pkg/authentication/user" | ||
| "k8s.io/apiserver/pkg/authorization/authorizer" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| "sigs.k8s.io/controller-runtime/pkg/client/apiutil" | ||
| "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" | ||
|
|
@@ -25,6 +28,7 @@ import ( | |
| helmclient "github.com/operator-framework/helm-operator-plugins/pkg/client" | ||
|
|
||
| ocv1 "github.com/operator-framework/operator-controller/api/v1" | ||
| "github.com/operator-framework/operator-controller/internal/operator-controller/authorization" | ||
| "github.com/operator-framework/operator-controller/internal/operator-controller/labels" | ||
| "github.com/operator-framework/operator-controller/internal/shared/util/cache" | ||
| ) | ||
|
|
@@ -279,28 +283,27 @@ type Boxcutter struct { | |
| Scheme *runtime.Scheme | ||
| RevisionGenerator ClusterExtensionRevisionGenerator | ||
| Preflights []Preflight | ||
| PreAuthorizer authorization.PreAuthorizer | ||
| FieldOwner string | ||
| } | ||
|
|
||
| func (bc *Boxcutter) getObjects(rev *ocv1.ClusterExtensionRevision) []client.Object { | ||
| var objs []client.Object | ||
| for _, phase := range rev.Spec.Phases { | ||
| for _, phaseObject := range phase.Objects { | ||
| objs = append(objs, &phaseObject.Object) | ||
| } | ||
| } | ||
| return objs | ||
| } | ||
|
|
||
| func (bc *Boxcutter) createOrUpdate(ctx context.Context, obj client.Object) error { | ||
| if obj.GetObjectKind().GroupVersionKind().Empty() { | ||
| gvk, err := apiutil.GVKForObject(obj, bc.Scheme) | ||
| // createOrUpdate creates or updates the revision object. PreAuthorization checks are performed to ensure the | ||
| // manifestManager has sufficient permissions to manage the revision and its resources | ||
| func (bc *Boxcutter) createOrUpdate(ctx context.Context, manifestManager user.Info, rev *ocv1.ClusterExtensionRevision) error { | ||
| if rev.GetObjectKind().GroupVersionKind().Empty() { | ||
| gvk, err := apiutil.GVKForObject(rev, bc.Scheme) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| obj.GetObjectKind().SetGroupVersionKind(gvk) | ||
| rev.GetObjectKind().SetGroupVersionKind(gvk) | ||
| } | ||
| return bc.Client.Patch(ctx, obj, client.Apply, client.FieldOwner(bc.FieldOwner), client.ForceOwnership) | ||
|
|
||
| // Run auth preflight checks | ||
| if err := bc.runPreAuthorizationChecks(ctx, manifestManager, rev); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return bc.Client.Patch(ctx, rev, client.Apply, client.FieldOwner(bc.FieldOwner), client.ForceOwnership) | ||
| } | ||
|
|
||
| func (bc *Boxcutter) Apply(ctx context.Context, contentFS fs.FS, ext *ocv1.ClusterExtension, objectLabels, revisionAnnotations map[string]string) error { | ||
|
|
@@ -329,7 +332,7 @@ func (bc *Boxcutter) Apply(ctx context.Context, contentFS fs.FS, ext *ocv1.Clust | |
| desiredRevision.Spec.Revision = currentRevision.Spec.Revision | ||
| desiredRevision.Name = currentRevision.Name | ||
|
|
||
| err := bc.createOrUpdate(ctx, desiredRevision) | ||
| err := bc.createOrUpdate(ctx, getUserInfo(ext), desiredRevision) | ||
| switch { | ||
| case apierrors.IsInvalid(err): | ||
| // We could not update the current revision due to trying to update an immutable field. | ||
|
|
@@ -344,7 +347,7 @@ func (bc *Boxcutter) Apply(ctx context.Context, contentFS fs.FS, ext *ocv1.Clust | |
| } | ||
|
|
||
| // Preflights | ||
| plainObjs := bc.getObjects(desiredRevision) | ||
| plainObjs := getObjects(desiredRevision) | ||
| for _, preflight := range bc.Preflights { | ||
| if shouldSkipPreflight(ctx, preflight, ext, state) { | ||
| continue | ||
|
|
@@ -379,14 +382,31 @@ func (bc *Boxcutter) Apply(ctx context.Context, contentFS fs.FS, ext *ocv1.Clust | |
| return fmt.Errorf("garbage collecting old revisions: %w", err) | ||
| } | ||
|
|
||
| if err := bc.createOrUpdate(ctx, desiredRevision); err != nil { | ||
| if err := bc.createOrUpdate(ctx, getUserInfo(ext), desiredRevision); err != nil { | ||
| return fmt.Errorf("creating new Revision: %w", err) | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // runPreAuthorizationChecks runs PreAuthorization checks if the PreAuthorizer is set. An error will be returned if | ||
| // the ClusterExtension service account does not have the necessary permissions to manage the revision's resources | ||
| func (bc *Boxcutter) runPreAuthorizationChecks(ctx context.Context, manifestManager user.Info, rev *ocv1.ClusterExtensionRevision) error { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know that you're taking this variable name from the example already set in rbac.go, but to me the term "manifestManager" implies a more complex object than just user.Info, especially given we have stuff like the manager.Manager in our codebase. This is purely a nitpick, but I wonder if a different variable name would help future legibility of the code. Maybe manifestUser? or manifestOwner? The latter might not be entirely accurate though.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I took the previous variable name, I think. Though, I'm happy to updated it to anything. The reason I thought |
||
| if bc.PreAuthorizer == nil { | ||
| return nil | ||
| } | ||
|
|
||
| // collect the revision manifests | ||
| manifestReader, err := revisionManifestReader(rev) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // run preauthorization check | ||
| return formatPreAuthorizerOutput(bc.PreAuthorizer.PreAuthorize(ctx, manifestManager, manifestReader, revisionManagementPerms(rev))) | ||
| } | ||
|
|
||
| // garbageCollectOldRevisions deletes archived revisions beyond ClusterExtensionRevisionRetentionLimit. | ||
| // Active revisions are never deleted. revisionList must be sorted oldest to newest. | ||
| func (bc *Boxcutter) garbageCollectOldRevisions(ctx context.Context, revisionList []ocv1.ClusterExtensionRevision) error { | ||
|
|
@@ -445,3 +465,45 @@ func splitManifestDocuments(file string) []string { | |
| } | ||
| return docs | ||
| } | ||
|
|
||
| // getObjects returns a slice of all objects in the revision | ||
| func getObjects(rev *ocv1.ClusterExtensionRevision) []client.Object { | ||
| var objs []client.Object | ||
| for _, phase := range rev.Spec.Phases { | ||
| for _, phaseObject := range phase.Objects { | ||
| objs = append(objs, &phaseObject.Object) | ||
| } | ||
| } | ||
| return objs | ||
| } | ||
|
|
||
| // revisionManifestReader returns an io.Reader containing all manifests in the revision | ||
| func revisionManifestReader(rev *ocv1.ClusterExtensionRevision) (io.Reader, error) { | ||
| var manifestBuilder strings.Builder | ||
| for _, obj := range getObjects(rev) { | ||
| objBytes, err := yaml.Marshal(obj) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("error generating revision manifest: %w", err) | ||
| } | ||
| manifestBuilder.WriteString("---\n") | ||
| manifestBuilder.WriteString(string(objBytes)) | ||
| manifestBuilder.WriteString("\n") | ||
| } | ||
| return strings.NewReader(manifestBuilder.String()), nil | ||
| } | ||
|
|
||
| func revisionManagementPerms(rev *ocv1.ClusterExtensionRevision) func(user.Info) []authorizer.AttributesRecord { | ||
| return func(user user.Info) []authorizer.AttributesRecord { | ||
| return []authorizer.AttributesRecord{ | ||
| { | ||
| User: user, | ||
| Name: rev.Name, | ||
| APIGroup: ocv1.GroupVersion.Group, | ||
| APIVersion: ocv1.GroupVersion.Version, | ||
| Resource: "clusterextensionrevisions/finalizers", | ||
| ResourceRequest: true, | ||
| Verb: "update", | ||
| }, | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.