github.com/kubewharf/katalyst-core@v0.5.3/pkg/util/credential/authorization/dynamic_test.go (about)

     1  /*
     2  Copyright 2022 The Katalyst Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package authorization
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  
    24  	"github.com/kubewharf/katalyst-api/pkg/apis/config/v1alpha1"
    25  	"github.com/kubewharf/katalyst-core/pkg/config/agent/dynamic"
    26  	"github.com/kubewharf/katalyst-core/pkg/config/generic"
    27  	"github.com/kubewharf/katalyst-core/pkg/util/credential"
    28  )
    29  
    30  func Test_dynamicConfAccessControl_Verify(t *testing.T) {
    31  	t.Parallel()
    32  
    33  	conf := generic.NewAuthConfiguration()
    34  
    35  	configuration := dynamic.NewDynamicAgentConfiguration()
    36  	dynamicConf := dynamic.NewConfiguration()
    37  	dynamicConf.AccessControlPolicies = []v1alpha1.AccessControlPolicy{
    38  		{
    39  			Username: "user-1",
    40  			PolicyRule: v1alpha1.PolicyRule{
    41  				Resources: []string{PermissionTypeAll},
    42  			},
    43  		},
    44  		{
    45  			Username: "user-2",
    46  			PolicyRule: v1alpha1.PolicyRule{
    47  				Resources: []string{PermissionTypeHttpEndpoint},
    48  			},
    49  		},
    50  	}
    51  	configuration.SetDynamicConfiguration(dynamicConf)
    52  
    53  	accessControl, err := NewDynamicConfAccessControl(conf, configuration)
    54  	assert.NoError(t, err)
    55  	assert.NotNil(t, accessControl)
    56  
    57  	secretBackedAC := accessControl.(*dynamicConfAccessControl)
    58  	secretBackedAC.updateAuthInfoFromDynamicConf()
    59  
    60  	type args struct {
    61  		authInfo       credential.AuthInfo
    62  		targetResource PermissionType
    63  	}
    64  	tests := []struct {
    65  		name    string
    66  		args    args
    67  		wantErr bool
    68  	}{
    69  		{
    70  			name: "invalid user",
    71  			args: args{
    72  				authInfo: credential.BasicAuthInfo{
    73  					Username: "user-3",
    74  				},
    75  				targetResource: PermissionTypeHttpEndpoint,
    76  			},
    77  			wantErr: true,
    78  		},
    79  		{
    80  			name: "user has permission",
    81  			args: args{
    82  				authInfo: credential.BasicAuthInfo{
    83  					Username: "user-1",
    84  				},
    85  				targetResource: PermissionTypeHttpEndpoint,
    86  			},
    87  			wantErr: false,
    88  		},
    89  		{
    90  			name: "user has no permission",
    91  			args: args{
    92  				authInfo: credential.BasicAuthInfo{
    93  					Username: "user-2",
    94  				},
    95  				targetResource: PermissionTypeEvictionPlugin,
    96  			},
    97  			wantErr: true,
    98  		},
    99  	}
   100  	for _, tt := range tests {
   101  		tt := tt
   102  		t.Run(tt.name, func(t *testing.T) {
   103  			t.Parallel()
   104  			if err := accessControl.Verify(tt.args.authInfo, tt.args.targetResource); (err != nil) != tt.wantErr {
   105  				t.Errorf("Verify() error = %v, wantErr %v", err, tt.wantErr)
   106  			}
   107  		})
   108  	}
   109  }