Skip to content
This repository has been archived by the owner on Mar 16, 2024. It is now read-only.

change: also recheck permissions if generation changed but image didn't #2248

Merged
merged 3 commits into from
Oct 16, 2023
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
8 changes: 8 additions & 0 deletions pkg/controller/appdefinition/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
v1 "github.com/acorn-io/runtime/pkg/apis/internal.acorn.io/v1"
"github.com/acorn-io/runtime/pkg/appdefinition"
"github.com/acorn-io/runtime/pkg/condition"
"github.com/acorn-io/runtime/pkg/controller/permissions"
)

func ParseAppImage(req router.Request, resp router.Response) error {
Expand All @@ -30,6 +31,13 @@ func ParseAppImage(req router.Request, resp router.Response) error {
return nil
}

// Migration for AppScopedPermissions
if len(appInstance.Status.Staged.AppScopedPermissions) == 0 &&
appInstance.Status.Staged.PermissionsObservedGeneration == appInstance.Generation &&
len(appInstance.Status.Staged.ImagePermissionsDenied) == 0 {
appInstance.Status.Staged.AppScopedPermissions = permissions.GetAppScopedPermissions(appInstance, appSpec)
}

appInstance.Status.AppSpec = *appSpec
status.Success()
return nil
Expand Down
36 changes: 20 additions & 16 deletions pkg/controller/permissions/permissions_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ func CheckPermissions(req router.Request, _ router.Response) error {
}

// Early exit
if app.Status.Staged.AppImage.ID == "" ||
app.Status.Staged.AppImage.Digest == app.Status.AppImage.Digest ||
if (app.Status.Staged.AppImage.ID == "" ||
app.Status.Staged.AppImage.Digest == app.Status.AppImage.Digest) &&
app.Status.Staged.PermissionsObservedGeneration == app.Generation {
// IAR disabled? Allow the Image if we're not re-checking permissions
if enabled, err := config.GetFeature(req.Ctx, req.Client, profiles.FeatureImageAllowRules); err != nil {
Expand Down Expand Up @@ -114,20 +114,7 @@ func CheckPermissions(req router.Request, _ router.Response) error {
details.AppImage.Digest, appImage.Digest)
}

// ServiceNames of the current app level (i.e. not nested Acorns/Services)
scvnames := maps.Keys(details.AppSpec.Containers)
scvnames = append(scvnames, maps.Keys(details.AppSpec.Jobs)...)
scvnames = append(scvnames, maps.Keys(details.AppSpec.Services)...)

// Only consider the scope of the current app level (i.e. not nested Acorns/Services)
grantedPerms := app.Spec.GetGrantedPermissions()
scopedGrantedPerms := []v1.Permissions{}
for i, p := range grantedPerms {
if slices.Contains(scvnames, p.ServiceName) {
scopedGrantedPerms = append(scopedGrantedPerms, grantedPerms[i])
}
}

scopedGrantedPerms := GetAppScopedPermissions(app, details.AppSpec)
app.Status.Staged.AppScopedPermissions = scopedGrantedPerms

// If iraEnabled, check if the Acorn images are authorized to request the defined permissions.
Expand Down Expand Up @@ -174,3 +161,20 @@ func CheckPermissions(req router.Request, _ router.Response) error {

return nil
}

func GetAppScopedPermissions(app *v1.AppInstance, appSpec *v1.AppSpec) []v1.Permissions {
// ServiceNames of the current app level (i.e. not nested Acorns/Services)
svcnames := maps.Keys(appSpec.Containers)
svcnames = append(svcnames, maps.Keys(appSpec.Jobs)...)
svcnames = append(svcnames, maps.Keys(appSpec.Services)...)

// Only consider the scope of the current app level (i.e. not nested Acorns/Services)
grantedPerms := app.Spec.GetGrantedPermissions()
scopedGrantedPerms := []v1.Permissions{}
for i, p := range grantedPerms {
if slices.Contains(svcnames, p.ServiceName) {
scopedGrantedPerms = append(scopedGrantedPerms, grantedPerms[i])
}
}
return scopedGrantedPerms
}
Loading