Skip to content
Draft
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
17 changes: 16 additions & 1 deletion internal/subroutine/authorization_model_generation.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ func (a *AuthorizationModelGenerationSubroutine) GetName() string {
// Process implements lifecycle.Subroutine.
func (a *AuthorizationModelGenerationSubroutine) Process(ctx context.Context, instance lifecyclecontrollerruntime.RuntimeObject) (ctrl.Result, errors.OperatorError) {
binding := instance.(*kcpapisv1alpha2.APIBinding)
log := logger.LoadLoggerFromContext(ctx)

internalAPIBindings := []string{"core.platform-mesh.io", "system.platform-mesh.io"}

Expand Down Expand Up @@ -247,6 +248,11 @@ func (a *AuthorizationModelGenerationSubroutine) Process(ctx context.Context, in
var apiExport kcpapisv1alpha2.APIExport
err = apiExportCluster.GetClient().Get(ctx, types.NamespacedName{Name: binding.Spec.Reference.Export.Name}, &apiExport)
if err != nil {
return ctrl.Result{},errors.NewOperatorError(err, true, true)
}

allAuthorizationModels := securityv1alpha1.AuthorizationModelList{}
if err := a.allClient.List(ctx, &allAuthorizationModels); err != nil {
return ctrl.Result{}, errors.NewOperatorError(err, true, true)
}

Expand All @@ -257,6 +263,15 @@ func (a *AuthorizationModelGenerationSubroutine) Process(ctx context.Context, in
return ctrl.Result{}, errors.NewOperatorError(err, true, true)
}

modelName := toK8sName(resourceSchema.Spec.Group, resourceSchema.Spec.Names.Plural, accountInfo.Spec.Organization.Name)

if slices.ContainsFunc(allAuthorizationModels.Items, func(m securityv1alpha1.AuthorizationModel) bool {
return m.Name == modelName
}) {
log.Warn().Msg(fmt.Sprintf("authorization model: %s already exist", modelName))
continue
}

longestRelationName := fmt.Sprintf("create_%s_%s", resourceSchema.Spec.Group, resourceSchema.Spec.Names.Plural)

group := resourceSchema.Spec.Group
Expand All @@ -278,7 +293,7 @@ func (a *AuthorizationModelGenerationSubroutine) Process(ctx context.Context, in

model := securityv1alpha1.AuthorizationModel{
ObjectMeta: metav1.ObjectMeta{
Name: toK8sName(resourceSchema.Spec.Group, resourceSchema.Spec.Names.Plural, accountInfo.Spec.Organization.Name),
Name: modelName,
},
}

Expand Down
88 changes: 88 additions & 0 deletions internal/subroutine/authorization_model_generation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

accountv1alpha1 "github.com/platform-mesh/account-operator/api/v1alpha1"
securityv1alpha1 "github.com/platform-mesh/security-operator/api/v1alpha1"
"github.com/platform-mesh/security-operator/internal/subroutine"
"github.com/platform-mesh/security-operator/internal/subroutine/mocks"
"github.com/stretchr/testify/assert"
Expand All @@ -13,6 +14,7 @@ import (

apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"

Expand Down Expand Up @@ -51,6 +53,7 @@ func mockAccountInfo(cl *mocks.MockClient, orgName, originCluster string) {
}).Once()
}


func TestAuthorizationModelGeneration_Process(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -115,6 +118,12 @@ func TestAuthorizationModelGeneration_Process(t *testing.T) {
}
return nil
}).Once()
allClient.EXPECT().List(mock.Anything, mock.Anything).RunAndReturn(func(ctx context.Context, ol client.ObjectList, lo ...client.ListOption) error {
if list, ok := ol.(*securityv1alpha1.AuthorizationModelList); ok {
list.Items = []securityv1alpha1.AuthorizationModel{}
}
return nil
}).Once()
kcpClient.EXPECT().Get(mock.Anything, mock.Anything, mock.Anything).RunAndReturn(func(ctx context.Context, nn types.NamespacedName, o client.Object, opts ...client.GetOption) error {
if rs, ok := o.(*kcpapisv1alpha1.APIResourceSchema); ok {
rs.Spec.Group = "group"
Expand Down Expand Up @@ -154,6 +163,12 @@ func TestAuthorizationModelGeneration_Process(t *testing.T) {
}
return nil
}).Once()
allClient.EXPECT().List(mock.Anything, mock.Anything).RunAndReturn(func(ctx context.Context, ol client.ObjectList, lo ...client.ListOption) error {
if list, ok := ol.(*securityv1alpha1.AuthorizationModelList); ok {
list.Items = []securityv1alpha1.AuthorizationModel{}
}
return nil
}).Once()
kcpClient.EXPECT().Get(mock.Anything, mock.Anything, mock.Anything).RunAndReturn(func(ctx context.Context, nn types.NamespacedName, o client.Object, opts ...client.GetOption) error {
if rs, ok := o.(*kcpapisv1alpha1.APIResourceSchema); ok {
rs.Spec.Group = "group"
Expand Down Expand Up @@ -184,6 +199,12 @@ func TestAuthorizationModelGeneration_Process(t *testing.T) {
}
return nil
}).Once()
allClient.EXPECT().List(mock.Anything, mock.Anything).RunAndReturn(func(ctx context.Context, ol client.ObjectList, lo ...client.ListOption) error {
if list, ok := ol.(*securityv1alpha1.AuthorizationModelList); ok {
list.Items = []securityv1alpha1.AuthorizationModel{}
}
return nil
}).Once()
kcpClient.EXPECT().Get(mock.Anything, mock.Anything, mock.Anything).RunAndReturn(func(ctx context.Context, nn types.NamespacedName, o client.Object, opts ...client.GetOption) error {
if rs, ok := o.(*kcpapisv1alpha1.APIResourceSchema); ok {
rs.Spec.Group = "group"
Expand Down Expand Up @@ -227,6 +248,12 @@ func TestAuthorizationModelGeneration_Process(t *testing.T) {
}
return nil
}).Once()
allClient.EXPECT().List(mock.Anything, mock.Anything).RunAndReturn(func(ctx context.Context, ol client.ObjectList, lo ...client.ListOption) error {
if list, ok := ol.(*securityv1alpha1.AuthorizationModelList); ok {
list.Items = []securityv1alpha1.AuthorizationModel{}
}
return nil
}).Once()
kcpClient.EXPECT().Get(mock.Anything, mock.Anything, mock.Anything).Return(assert.AnError)
},
},
Expand All @@ -245,6 +272,12 @@ func TestAuthorizationModelGeneration_Process(t *testing.T) {
}
return nil
}).Once()
allClient.EXPECT().List(mock.Anything, mock.Anything).RunAndReturn(func(ctx context.Context, ol client.ObjectList, lo ...client.ListOption) error {
if list, ok := ol.(*securityv1alpha1.AuthorizationModelList); ok {
list.Items = []securityv1alpha1.AuthorizationModel{}
}
return nil
}).Once()
kcpClient.EXPECT().Get(mock.Anything, mock.Anything, mock.Anything).RunAndReturn(func(ctx context.Context, nn types.NamespacedName, o client.Object, opts ...client.GetOption) error {
if rs, ok := o.(*kcpapisv1alpha1.APIResourceSchema); ok {
rs.Spec.Group = "veryverylonggroup.platform-mesh.org"
Expand Down Expand Up @@ -281,6 +314,61 @@ func TestAuthorizationModelGeneration_Process(t *testing.T) {
manager.EXPECT().GetCluster(mock.Anything, "export-cluster").Return(nil, assert.AnError)
},
},
{
name: "error on List AuthorizationModels in Process",
binding: newApiBinding("foo", "bar"),
expectError: true,
mockSetup: func(manager *mocks.MockManager, allClient *mocks.MockClient, cluster *mocks.MockCluster, kcpClient *mocks.MockClient) {
manager.EXPECT().ClusterFromContext(mock.Anything).Return(cluster, nil)
cluster.EXPECT().GetClient().Return(kcpClient)
mockAccountInfo(kcpClient, "org", "origin")
manager.EXPECT().GetCluster(mock.Anything, mock.Anything).Return(cluster, nil)
kcpClient.EXPECT().Get(mock.Anything, mock.Anything, mock.Anything).RunAndReturn(func(ctx context.Context, nn types.NamespacedName, o client.Object, opts ...client.GetOption) error {
if ae, ok := o.(*kcpapisv1alpha2.APIExport); ok {
ae.Spec.Resources = []kcpapisv1alpha2.ResourceSchema{{Schema: "schema1"}}
return nil
}
return nil
}).Once()
allClient.EXPECT().List(mock.Anything, mock.Anything).Return(assert.AnError).Once()
},
},
{
name: "skip model creation when model already exists in Process",
binding: newApiBinding("foo", "bar"),
mockSetup: func(manager *mocks.MockManager, allClient *mocks.MockClient, cluster *mocks.MockCluster, kcpClient *mocks.MockClient) {
manager.EXPECT().ClusterFromContext(mock.Anything).Return(cluster, nil)
cluster.EXPECT().GetClient().Return(kcpClient)
mockAccountInfo(kcpClient, "org", "origin")
manager.EXPECT().GetCluster(mock.Anything, mock.Anything).Return(cluster, nil)
kcpClient.EXPECT().Get(mock.Anything, mock.Anything, mock.Anything).RunAndReturn(func(ctx context.Context, nn types.NamespacedName, o client.Object, opts ...client.GetOption) error {
if ae, ok := o.(*kcpapisv1alpha2.APIExport); ok {
ae.Spec.Resources = []kcpapisv1alpha2.ResourceSchema{{Schema: "schema1"}}
return nil
}
return nil
}).Once()
// model "group-foos-org" already exists — creation must be skipped
allClient.EXPECT().List(mock.Anything, mock.Anything).RunAndReturn(func(ctx context.Context, ol client.ObjectList, lo ...client.ListOption) error {
if list, ok := ol.(*securityv1alpha1.AuthorizationModelList); ok {
list.Items = []securityv1alpha1.AuthorizationModel{
{ObjectMeta: metav1.ObjectMeta{Name: "group-foos-org"}},
}
}
return nil
}).Once()
kcpClient.EXPECT().Get(mock.Anything, mock.Anything, mock.Anything).RunAndReturn(func(ctx context.Context, nn types.NamespacedName, o client.Object, opts ...client.GetOption) error {
if rs, ok := o.(*kcpapisv1alpha1.APIResourceSchema); ok {
rs.Spec.Group = "group"
rs.Spec.Names.Plural = "foos"
rs.Spec.Names.Singular = "foo"
rs.Spec.Scope = apiextensionsv1.ClusterScoped
return nil
}
return nil
}).Once()
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
Expand Down
Loading