@@ -2,6 +2,7 @@ package janitor
22
33import (
44 "context"
5+ "strings"
56 "time"
67
78 armauthorization "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/authorization/armauthorization/v2"
@@ -39,18 +40,18 @@ func (j *Janitor) runRoleAssignments(ctx context.Context, logger *zap.SugaredLog
3940 azureResource , _ := armclient .ParseResourceId (* roleAssignment .Properties .Scope )
4041
4142 roleAssignmentLogger := contextLogger .With (
42- zap .String ("roleAssignmentId" , stringPtrToStringLower (roleAssignment .ID )),
43- zap .String ("scope" , stringPtrToStringLower (roleAssignment .Properties .Scope )),
44- zap .String ("principalId" , stringPtrToStringLower (roleAssignment .Properties .PrincipalID )),
45- zap .String ("principalType" , stringToStringLower (string (* roleAssignment .Properties .PrincipalType ))),
46- zap .String ("roleDefinitionId" , stringPtrToStringLower (roleAssignment .Properties .RoleDefinitionID )),
47- zap .String ("subscriptionID" , stringPtrToStringLower (subscription .SubscriptionID )),
43+ zap .String ("roleAssignmentId" , to . StringLower (roleAssignment .ID )),
44+ zap .String ("scope" , to . StringLower (roleAssignment .Properties .Scope )),
45+ zap .String ("principalId" , to . StringLower (roleAssignment .Properties .PrincipalID )),
46+ zap .String ("principalType" , strings . ToLower (string (* roleAssignment .Properties .PrincipalType ))),
47+ zap .String ("roleDefinitionId" , to . StringLower (roleAssignment .Properties .RoleDefinitionID )),
48+ zap .String ("subscriptionID" , to . StringLower (subscription .SubscriptionID )),
4849 zap .String ("resourceGroup" , azureResource .ResourceGroup ),
4950 )
5051
51- // check if RoleDefinitionID is set
52+ // check if roleAssignment is allowed for cleanup
5253 // do not want to touch other RoleAssignments
53- if stringInSlice ( * roleAssignment . Properties . RoleDefinitionID , j . Conf . Janitor . RoleAssignments . RoleDefintionIds ) {
54+ if j . isRoleAssignmentCleanupAllowed ( roleAssignment ) {
5455 var roleAssignmentTtl * time.Duration
5556 roleAssignmentLogger .Debug ("checking ttl" )
5657
@@ -77,12 +78,12 @@ func (j *Janitor) runRoleAssignments(ctx context.Context, logger *zap.SugaredLog
7778 roleAssignmentLogger .Debugf ("detected ttl %v" , roleAssignmentTtl .String ())
7879
7980 resourceTtl .AddTime (prometheus.Labels {
80- "roleAssignmentId" : stringPtrToStringLower (roleAssignment .ID ),
81- "scope" : stringPtrToStringLower (roleAssignment .Properties .Scope ),
82- "principalId" : stringPtrToStringLower (roleAssignment .Properties .PrincipalID ),
83- "principalType" : stringPtrToStringLower (roleAssignment .Type ),
84- "roleDefinitionId" : stringPtrToStringLower (roleAssignment .Properties .RoleDefinitionID ),
85- "subscriptionID" : stringPtrToStringLower (subscription .SubscriptionID ),
81+ "roleAssignmentId" : to . StringLower (roleAssignment .ID ),
82+ "scope" : to . StringLower (roleAssignment .Properties .Scope ),
83+ "principalId" : to . StringLower (roleAssignment .Properties .PrincipalID ),
84+ "principalType" : to . StringLower (roleAssignment .Type ),
85+ "roleDefinitionId" : to . StringLower (roleAssignment .Properties .RoleDefinitionID ),
86+ "subscriptionID" : to . StringLower (subscription .SubscriptionID ),
8687 "resourceGroup" : azureResource .ResourceGroup ,
8788 }, roleAssignmentExpiry )
8889
@@ -94,16 +95,16 @@ func (j *Janitor) runRoleAssignments(ctx context.Context, logger *zap.SugaredLog
9495 roleAssignmentLogger .Infof ("successfully deleted" )
9596
9697 j .Prometheus .MetricDeletedResource .With (prometheus.Labels {
97- "subscriptionID" : stringPtrToStringLower (subscription .SubscriptionID ),
98- "resourceType" : stringToStringLower (resourceType ),
98+ "subscriptionID" : to . StringLower (subscription .SubscriptionID ),
99+ "resourceType" : strings . ToLower (resourceType ),
99100 }).Inc ()
100101 } else {
101102 // failed delete
102103 roleAssignmentLogger .Error (err .Error ())
103104
104105 j .Prometheus .MetricErrors .With (prometheus.Labels {
105- "subscriptionID" : stringPtrToStringLower (subscription .SubscriptionID ),
106- "resourceType" : stringToStringLower (resourceType ),
106+ "subscriptionID" : to . StringLower (subscription .SubscriptionID ),
107+ "resourceType" : strings . ToLower (resourceType ),
107108 }).Inc ()
108109 }
109110 } else {
@@ -120,3 +121,19 @@ func (j *Janitor) runRoleAssignments(ctx context.Context, logger *zap.SugaredLog
120121 resourceTtl .GaugeSet (j .Prometheus .MetricTtlRoleAssignments )
121122 }
122123}
124+
125+ func (j * Janitor ) isRoleAssignmentCleanupAllowed (roleAssignment * armauthorization.RoleAssignment ) bool {
126+ roleDefinitionID := to .StringLower (roleAssignment .Properties .RoleDefinitionID )
127+ for _ , check := range j .Conf .Janitor .RoleAssignments .RoleDefintionIds {
128+ // sanity check, do not allow empty IDs
129+ if len (check ) == 0 {
130+ continue
131+ }
132+ check = strings .ToLower (check )
133+ if strings .EqualFold (roleDefinitionID , check ) || strings .HasSuffix (roleDefinitionID , check ) {
134+ return true
135+ }
136+ }
137+
138+ return false
139+ }
0 commit comments