forked from Azure/ARO-RP
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
debb5ae
commit 95d10ce
Showing
5 changed files
with
296 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package cluster | ||
|
||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the Apache License 2.0. | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/Azure/ARO-RP/pkg/validate/dynamic" | ||
) | ||
|
||
/*************************************************************** | ||
Monitor the Cluster Service Prinicpal required permissions: | ||
- Network Contributor role on vnet | ||
****************************************************************/ | ||
|
||
func (mon *Monitor) emitValidatePermissions(ctx context.Context) error { | ||
subnets := []dynamic.Subnet{{ | ||
ID: mon.oc.Properties.MasterProfile.SubnetID, | ||
Path: "properties.masterProfile.subnetId", | ||
}} | ||
|
||
err := mon.validator.ValidateVnet(ctx, mon.oc.Location, subnets, mon.oc.Properties.NetworkProfile.PodCIDR, | ||
mon.oc.Properties.NetworkProfile.ServiceCIDR) | ||
|
||
if err != nil { | ||
mon.emitGauge("cluster.validate.permissions", 1, map[string]string{ | ||
"ValidateVnetPermissions": err.Error(), | ||
}) | ||
} | ||
|
||
err = mon.validator.ValidateSubnets(ctx, mon.oc, subnets) | ||
if err != nil { | ||
mon.emitGauge("cluster.validate.permissions", 1, map[string]string{ | ||
"ValidateSubnet": err.Error(), | ||
}) | ||
} | ||
|
||
err = mon.validator.ValidateDiskEncryptionSets(ctx, mon.oc) | ||
if err != nil { | ||
mon.emitGauge("cluster.validate.permissions", 1, map[string]string{ | ||
"ValidateDiskEncryptionSet": err.Error(), | ||
}) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
package cluster | ||
|
||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the Apache License 2.0. | ||
|
||
import ( | ||
"context" | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/golang/mock/gomock" | ||
appsv1 "k8s.io/api/apps/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes/fake" | ||
|
||
mock_metrics "github.com/Azure/ARO-RP/pkg/util/mocks/metrics" | ||
) | ||
|
||
func TestEmitValidatePermissions(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
cli := fake.NewSimpleClientset( | ||
&appsv1.StatefulSet{ // metrics expected | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "name1", | ||
Namespace: "openshift", | ||
}, | ||
Status: appsv1.StatefulSetStatus{ | ||
Replicas: 2, | ||
ReadyReplicas: 1, | ||
}, | ||
}, &appsv1.StatefulSet{ // no metric expected | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "name2", | ||
Namespace: "openshift", | ||
}, | ||
Status: appsv1.StatefulSetStatus{ | ||
Replicas: 2, | ||
ReadyReplicas: 2, | ||
}, | ||
}, &appsv1.StatefulSet{ | ||
ObjectMeta: metav1.ObjectMeta{ // no metric expected -customer | ||
Name: "name2", | ||
Namespace: "customer", | ||
}, | ||
Status: appsv1.StatefulSetStatus{ | ||
Replicas: 2, | ||
ReadyReplicas: 1, | ||
}, | ||
}, | ||
) | ||
|
||
controller := gomock.NewController(t) | ||
defer controller.Finish() | ||
|
||
m := mock_metrics.NewMockEmitter(controller) | ||
|
||
mon := &Monitor{ | ||
cli: cli, | ||
m: m, | ||
} | ||
|
||
m.EXPECT().EmitGauge("statefulset.count", int64(3), map[string]string{}) | ||
|
||
m.EXPECT().EmitGauge("statefulset.statuses", int64(1), map[string]string{ | ||
"name": "name1", | ||
"namespace": "openshift", | ||
"replicas": strconv.Itoa(2), | ||
"readyReplicas": strconv.Itoa(1), | ||
}) | ||
|
||
err := mon.emitValidatePermissions(ctx) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
/* | ||
func TestValidateVnetPermissions(t *testing.T) { | ||
ctx := context.Background() | ||
resourceType := "virtualNetworks" | ||
resourceProvider := "Microsoft.Network" | ||
for _, tt := range []struct { | ||
name string | ||
mocks func(*mock_authorization.MockPermissionsClient, context.CancelFunc) | ||
wantErr string | ||
}{ | ||
{ | ||
name: "pass", | ||
mocks: func(permissionsClient *mock_authorization.MockPermissionsClient, cancel context.CancelFunc) { | ||
permissionsClient.EXPECT(). | ||
ListForResource(gomock.Any(), resourceGroupName, resourceProvider, "", resourceType, vnetName). | ||
Return([]mgmtauthorization.Permission{ | ||
{ | ||
Actions: &[]string{ | ||
"Microsoft.Network/virtualNetworks/join/action", | ||
"Microsoft.Network/virtualNetworks/read", | ||
"Microsoft.Network/virtualNetworks/write", | ||
"Microsoft.Network/virtualNetworks/subnets/join/action", | ||
"Microsoft.Network/virtualNetworks/subnets/read", | ||
"Microsoft.Network/virtualNetworks/subnets/write", | ||
}, | ||
NotActions: &[]string{}, | ||
}, | ||
}, nil) | ||
}, | ||
}, | ||
{ | ||
name: "fail: missing permissions", | ||
mocks: func(permissionsClient *mock_authorization.MockPermissionsClient, cancel context.CancelFunc) { | ||
permissionsClient.EXPECT(). | ||
ListForResource(gomock.Any(), resourceGroupName, resourceProvider, "", resourceType, vnetName). | ||
Do(func(arg0, arg1, arg2, arg3, arg4, arg5 interface{}) { | ||
cancel() | ||
}). | ||
Return( | ||
[]mgmtauthorization.Permission{ | ||
{ | ||
Actions: &[]string{}, | ||
NotActions: &[]string{}, | ||
}, | ||
}, | ||
nil, | ||
) | ||
}, | ||
wantErr: "400: InvalidServicePrincipalPermissions: : The cluster service principal (Application ID: fff51942-b1f9-4119-9453-aaa922259eb7) does not have Network Contributor role on vnet '" + vnetID + "'.", | ||
}, | ||
{ | ||
name: "fail: not found", | ||
mocks: func(permissionsClient *mock_authorization.MockPermissionsClient, cancel context.CancelFunc) { | ||
permissionsClient.EXPECT(). | ||
ListForResource(gomock.Any(), resourceGroupName, resourceProvider, "", resourceType, vnetName). | ||
Do(func(arg0, arg1, arg2, arg3, arg4, arg5 interface{}) { | ||
cancel() | ||
}). | ||
Return( | ||
nil, | ||
autorest.DetailedError{ | ||
StatusCode: http.StatusNotFound, | ||
}, | ||
) | ||
}, | ||
wantErr: "400: InvalidLinkedVNet: : The vnet '" + vnetID + "' could not be found.", | ||
}, | ||
{ | ||
name: "fail: fp/sp has no permission on the target vnet (forbidden error)", | ||
mocks: func(permissionsClient *mock_authorization.MockPermissionsClient, cancel context.CancelFunc) { | ||
permissionsClient.EXPECT(). | ||
ListForResource(gomock.Any(), resourceGroupName, resourceProvider, "", resourceType, vnetName). | ||
Do(func(arg0, arg1, arg2, arg3, arg4, arg5 interface{}) { | ||
cancel() | ||
}). | ||
Return( | ||
nil, | ||
autorest.DetailedError{ | ||
StatusCode: http.StatusForbidden, | ||
Message: "some forbidden error on the resource.", | ||
}, | ||
) | ||
}, | ||
wantErr: "400: InvalidServicePrincipalPermissions: : The cluster service principal (Application ID: fff51942-b1f9-4119-9453-aaa922259eb7) does not have Network Contributor role on vnet '" + vnetID + "'.\nOriginal error message: some forbidden error on the resource.", | ||
}, | ||
} { | ||
t.Run(tt.name, func(t *testing.T) { | ||
controller := gomock.NewController(t) | ||
defer controller.Finish() | ||
ctx, cancel := context.WithCancel(ctx) | ||
defer cancel() | ||
permissionsClient := mock_authorization.NewMockPermissionsClient(controller) | ||
tt.mocks(permissionsClient, cancel) | ||
dv := &dynamic{ | ||
appID: "fff51942-b1f9-4119-9453-aaa922259eb7", | ||
authorizerType: AuthorizerClusterServicePrincipal, | ||
log: logrus.NewEntry(logrus.StandardLogger()), | ||
permissions: permissionsClient, | ||
} | ||
vnetr, err := azure.ParseResourceID(vnetID) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
err = dv.validateVnetPermissions(ctx, vnetr) | ||
utilerror.AssertErrorMessage(t, err, tt.wantErr) | ||
}) | ||
} | ||
} | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters