This repository has been archived by the owner on Jul 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
nuke-svcacc-acls-apikeys.ps1
156 lines (121 loc) · 5.05 KB
/
nuke-svcacc-acls-apikeys.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
[CmdletBinding()]
param(
[Parameter(Mandatory = $False, Position = 0, ValueFromPipeline = $false)]
[string]
$Prefix = "",
[Parameter(Mandatory = $False, Position = 1, ValueFromPipeline = $false)]
[string]
$Cluster = "",
[Parameter(Mandatory = $False, Position = 2, ValueFromPipeline = $false)]
[switch]
$DisableDryRun = $false
)
$responseServiceAccounts = (ccloud service-account list -o json) | Out-String | ConvertFrom-Json
$serviceAccounts = $responseServiceAccounts | Where-Object { $_.name -like $Prefix}
$responseApiKeys = (ccloud api-key list -o json) | Out-String | ConvertFrom-Json
$apiKeys = @()
$hashTableOfServiceAccountIds = @{}
foreach ($serviceAccount in $serviceAccounts) {
$hashTableOfServiceAccountIds.Add($serviceAccount.id, $serviceAccount.name)
}
foreach ($apiKey in $responseApiKeys) {
If ( $hashTableOfServiceAccountIds.ContainsKey($apiKey.owner) -eq $true ) {
$apiKeys += $apiKey
}
}
# Clear-Variable -name hashTableOfServiceAccountIds
$acls = @()
$responseAcls = (ccloud kafka acl list --cluster $($Cluster) -o json) | Out-String | ConvertFrom-Json
foreach ($acl in $responseAcls) {
if ( $hashTableOfServiceAccountIds.ContainsKey($acl.service_account_id.substring(5)) -eq $true ) {
$acls += $acl
}
}
if ($DisableDryRun) {
Write-Host "`n"
Write-Host "Acls:`n"
foreach($Acl in $acls) {
$serviceAccountId = $Acl.service_account_id.substring(5)
Write-Host "$($serviceAccountId) $($Acl.operation) $($Acl.permission) $($Acl.resource) $($Acl.type) $($Acl.name)"
$additionalArgs = ""
if ( $Acl.type -eq 'PREFIXED' )
{
$additionalArgs += "--prefix"
}
Switch ($Acl.resource)
{
{$_ -match 'TOPIC'} {
$topicName = $Acl.name
if ( $topicName -eq "'*'" )
{
$topicName = '''""'''
}
Write-Host "ccloud kafka acl delete --cluster $($Cluster) --$($Acl.permission.ToLower()) --service-account $($serviceAccountId) --operation $($Acl.operation.ToLower()) --topic $($topicName) $($additionalArgs)"
Invoke-Expression "ccloud kafka acl delete --cluster $($Cluster) --$($Acl.permission.ToLower()) --service-account $($serviceAccountId) --operation $($Acl.operation.ToLower()) --topic $($topicName) $($additionalArgs)"
}
{$_ -match 'GROUP'} {
Invoke-Expression "ccloud kafka acl delete --cluster $($Cluster) --$($Acl.permission.ToLower()) --service-account $($serviceAccountId) --operation $($Acl.operation) --consumer-group $($Acl.name) $($additionalArgs)"
}
{$_ -match 'CLUSTER'} {
Invoke-Expression "ccloud kafka acl delete --cluster $($Cluster) --$($Acl.permission.ToLower()) --service-account $($serviceAccountId) --operation $($Acl.operation) --cluster-scope"
}
}
}
Write-Host "Api keys:`n"
foreach ($ApiKey in $apiKeys) {
Invoke-Expression "ccloud api-key delete $($ApiKey.key)"
}
Write-Host "Service accounts:`n"
foreach ($ServiceAccount in $serviceAccounts) {
Invoke-Expression "ccloud service-account delete $($ServiceAccount.id)"
}
} else {
Write-Host "::DryRun::`n`n"
Write-Host "ServiceAccounts to be deleted:`n"
foreach ($ServiceAccount in $serviceAccounts) {
Write-Host $ServiceAccount.name
Write-Host "`n"
}
Write-Host "ApiKeys to be deleted:`n"
foreach ($ApiKey in $apiKeys) {
Write-Host "$($ApiKey.key) owned by $($hashTableOfServiceAccountIds[$ApiKey.owner])"
Write-Host "`n"
}
# Commands that will be run if this is the case
Write-Host "`nCommands that would've been run if this wasn't a dry-run:`n"
Write-Host "Service accounts:`n"
foreach ($ServiceAccount in $serviceAccounts) {
Write-Host "ccloud service-account delete $($ServiceAccount.id) --cluster $($Cluster)"
}
Write-Host "Api keys:`n"
foreach ($ApiKey in $apiKeys) {
Write-Host "ccloud api-key delete $($ApiKey.key) --cluster $($Cluster)"
}
Write-Host "Acls:`n"
foreach($Acl in $acls) {
$serviceAccountId = $Acl.service_account_id.substring(5)
Write-Host "$($serviceAccountId) $($Acl.operation) $($Acl.permission) $($Acl.resource) $($Acl.type) $($Acl.name)"
$additionalArgs = ""
if ( $Acl.type -eq 'PREFIXED' )
{
$additionalArgs += "--prefix"
}
Switch ($Acl.resource)
{
{$_ -match 'TOPIC'} {
$topicName = $Acl.name
if ( $topicName -eq "'*'" )
{
$topicName = '''""'''
}
Write-Host "ccloud kafka acl delete --cluster $($Cluster) --$($Acl.permission.ToLower()) --service-account $($serviceAccountId) --operation $($Acl.operation.ToLower()) --topic $($topicName) $($additionalArgs)"
}
{$_ -match 'GROUP'} {
Write-Host "ccloud kafka acl delete --cluster $($Cluster) --$($Acl.permission.ToLower()) --service-account $($serviceAccountId) --operation $($Acl.operation) --consumer-group $($Acl.name) $($additionalArgs)"
}
{$_ -match 'CLUSTER'} {
Write-Host "ccloud kafka acl delete --cluster $($Cluster) --$($Acl.permission.ToLower()) --service-account $($serviceAccountId) --operation $($Acl.operation) --cluster-scope"
}
}
}
}