-
Notifications
You must be signed in to change notification settings - Fork 3
/
ise-delete.py
executable file
Β·436 lines (388 loc) Β· 25.2 KB
/
ise-delete.py
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#!/usr/bin/env python3
"""
Delete ISE resources via REST APIs.
Examples:
ise-delete.py endpoint
ise-delete.py endpoint -tvi
ise-delete.py -tv endpoint
Requires setting the these environment variables using the `export` command:
export ISE_PPAN='1.2.3.4' # hostname or IP address of ISE Primary PAN
export ISE_REST_USERNAME='admin' # ISE ERS admin or operator username
export ISE_REST_PASSWORD='C1sco12345' # ISE ERS admin or operator password
export ISE_CERT_VERIFY=false # validate the ISE certificate
You may add these export lines to a text file and load with `source`:
source ise.sh
"""
__author__ = "Thomas Howard"
__email__ = "[email protected]"
__license__ = "MIT - https://mit-license.org/"
import aiohttp
import argparse
import asyncio
import csv
import io
import json
import math
import os
import random
import signal
import ssl
import sys
import time
import traceback
ICONS = {
# name : icon
'CACHE' : 'β―',
'DOWN' : 'β½',
'ERROR' : 'β',
'NONE' : 'β
',
'FAIL' : 'β',
'INFO' : 'βΉ',
'ID' : 'βΏ',
'LIST' : 'β',
'PASS' : 'β',
'PLAY' : 'β·',
'TIMEOUT' : 'β',
'TIP' : 'π‘',
'UNLOCK' : 'π',
'WARN' : 'β ',
'WATCH' : 'β±',
}
# Limit TCP connection pool size to prevent connection refusals by ISE
# See https://cs.co/ise-scale for concurrent REST connection limits.
# Testing with ISE 3.x shows no performance gain for > ~5 connections.
TCP_LIMIT_DEFAULT=100 # aiohttp.TCPConnector.limit
TCP_LIMIT=10 # πΊISE ERS APIs for GuestType and InternalUser can have problems with 10+ concurrent connections!
REST_PAGE_SIZE = 100
# Dictionary of ISE REST Endpoints mapping to a tuple of the object name and base URL
# 'Resource': ('ERS_Name', 'REST API Base URL')
ISE_REST_ENDPOINTS = {
# Deployment @ https://cs.co/ise-api#!deployment-openapi
'deployment-node': ('-', '/api/v1/deployment/node'),
'node-group': ('-', '/api/v1/deployment/node-group'),
'pan-ha': ('-', '/api/v1/deployment/pan-ha'),
# 'node-interface': ('-', '/api/v1/node/{hostname}/interface'), # π‘ requires ISE node hostname
# 'sxp-interface': ('-', '/api/v1/node/{hostname}/sxp-interface'), # π‘ requires ISE node hostname
# 'profile': ('-', '/api/v1/profile/{hostname}'), # π‘ requires ISE node hostname
'repository': ('-', '/api/v1/repository'), # Repository @ https://cs.co/ise-api#!repository-openapi
# 'repository-name': ('-', '/api/v1/repository/{name}'), # π‘ requires repository {name}
# 'repository-name-files': ('-', '/api/v1/repository/{name}/files'),# π‘ requires repository {name}
# Patching @ https://cs.co/ise-api#!patch-and-hot-patch-openapi
'hotpatch': ('-', '/api/v1/hotpatch'),
'patch': ('-', '/api/v1/patch'),
# DeploymentInfo @ https://cs.co/ise-api#!deploymentinfo
# 'info': ('ERSDeploymentInfo', '/deploymentinfo/getAllInfo'), # 'deploymentinfo'=> deploymentinfo/getAllInfo
'node': ('Node', '/ers/config/node'), # Node @ https://cs.co/ise-api#!node
# 'service': ('Service', '/ers/config/service'), # π empty resource; link:service/null gives 404
'sessionservicenode': ('SessionServiceNode', '/ers/config/sessionservicenode'),
# Certificates @ https://cs.co/ise-api#!certificate-openapi
'trusted-certificate': ('-', '/api/v1/certs/trusted-certificate'), # Get list of all trusted certificates
# 'trusted-certificate-by-id': ('-', '/api/v1/certs/trusted-certificate/{id}'), # Get Trust Certificate By ID
# 'system-certificate': ('-', '/api/v1/certs/system-certificate/{hostName}'), # π‘ requires ISE node hostname
'certificate-signing-request': ('-', '/api/v1/certs/certificate-signing-request'),
# 'system-certificate-by-name': ('-', '/api/v1/certs/system-certificate/{hostName}'), # π‘ requires ISE node hostname. Get all system certificates of a particular node
# Backup Restore
'last-backup-status': ('-', '/api/v1/backup-restore/config/last-backup-status'),
# Upgrade
'upgrade-prepare-status': ('-', '/api/v1/upgrade/prepare/get-status'),
'upgrade-proceed-status': ('-', '/api/v1/upgrade/proceed/get-status'),
'upgrade-stage-status': ('-', '/api/v1/upgrade/stage/get-status'),
'upgrade-summary-status': ('-', '/api/v1/upgrade/summary/get-status'),
# System Settings
'lsd': ('-', '/api/v1/lsd/updateLsdSettings'), # LSD
'settings-proxy': ('-', '/api/v1/system-settings/proxy'),
'settings-ttg': ('-', '/api/v1/system-settings/telemetry/transport-gateway'),
# Data Connect @ https://cs.co/ise-dataconnect
'data-connect-details': ('-', '/api/v1/mnt/data-connect/details'),
'data-connect-settings': ('-', '/api/v1/mnt/data-connect/settings'),
# Identity Stores
'activedirectory': ('ERSActiveDirectory', '/ers/config/activedirectory'),
'restidstore': ('ERSRestIDStore', '/ers/config/restidstore'), # RESTIDStore must be enabled / 404 if none configured
# LDAP @ https://cs.co/ise-api#!ldap
# 'ldap': ('???', '/ers/config/ldap'), # 404 if unconfigured?
# 'ldap-rootcacertificates': ('???', '/ers/config/ldap/rootcacertificates'),
# 'ldap-hosts': ('???', '/ers/config/ldap/hosts'),
# 'ldap-by-name': ('???', '/ers/config/ldap/name/{name}'),
# 'ldap-by-id': ('???', '/ers/config/ldap/{id}'),
# Duo MFA
'duo-sync-ads': ('-', '/api/v1/duo-identitysync/activedirectories'), # Get the list of all configured Active Directories
# 'duo-sync-groups': ('-', '/api/v1/duo-identitysync/adgroups/{activeDirectory}'), # π‘ requires id for a list of all groups in the specified AD
'duo-identitysync': ('-', '/api/v1/duo-identitysync/identitysync'), # Get the list of all Identitysync configurations
'duo-mfa': ('-', '/api/v1/duo-mfa/mfa'), # Get the list of all Duo-MFA configurations
# 'duo-mfa-enable': ('-', '/api/v1/duo-mfa/enable'), # PUT Enable MFA feature
# 'duo-mfa-by-name': ('-', '/api/v1/duo-mfa/mfa/{connectionName}'), # Get the specified Duo-MFA configuration
'duo-mfa-status': ('-', '/api/v1/duo-mfa/status'), # MFA feature enabled status
# pxGrid Direct @ https://cs.co/ise-api#!pxgrid-direct-open-api
'pxgd-config': ('-', '/api/v1/pxgrid-direct/connector-config'),
# 'pxgd-config-by-name': ('-', '/api/v1/pxgrid-direct/connector-config/{name}'),
'pxgd-references': ('-', '/api/v1/pxgrid-direct/dictionary-references'),
'externalradiusserver': ('ExternalRadiusServer', '/ers/config/externalradiusserver'),
'radiusserversequence': ('RadiusServerSequence', '/ers/config/radiusserversequence'),
'idstoresequence': ('IdStoreSequence', '/ers/config/idstoresequence'),
# Network Devices
'networkdevicegroup': ('NetworkDeviceGroup', '/ers/config/networkdevicegroup'),
'networkdevice': ('NetworkDevice', '/ers/config/networkdevice'), #
# TrustSec
'sgt': ('Sgt', '/ers/config/sgt'),
'sgacl': ('Sgacl', '/ers/config/sgacl'),
'sgmapping': ('SGMapping', '/ers/config/sgmapping'),
'sgmappinggroup': ('SGMappingGroup', '/ers/config/sgmappinggroup'),
'sgtvnvlan': ('SgtVNVlanContainer', '/ers/config/sgtvnvlan'),
'egressmatrixcell': ('EgressMatrixCell', '/ers/config/egressmatrixcell'),
'sxpconnections': ('ERSSxpConnection', '/ers/config/sxpconnections'),
'sxplocalbindings': ('ERSSxpLocalBindings', '/ers/config/sxplocalbindings'),
'sxpvpns': ('ERSSxpVpn', '/ers/config/sxpvpns'),
'sgt-reservation': ('-', '/api/v1/sgt/reservation'), # SgtRangeReservation @ https://cs.co/ise-api#!sgt-reservation-openapi
# 'sgt-reservation-by-id': ('-', '/api/v1/sgt/reservation/{clientID}'), # π‘ Requires {id}. Get the reserved range for the specific Client.
# SDA/TrustSec @ https://cs.co/ise-api#!trustsec-openapi
'trustsec-sgacl-nbarapp': ('-', '/api/v1/trustsec/sgacl/nbarapp'),
'trustsec-sgvnmapping': ('-', '/api/v1/trustsec/sgvnmapping'),
'trustsec-virtualnetwork': ('-', '/api/v1/trustsec/virtualnetwork'),
'trustsec-vnvlanmapping': ('-', '/api/v1/trustsec/vnvlanmapping'),
'profilerprofile': ('ProfilerProfile', '/ers/config/profilerprofile'), # Endpoint Profiles @ https://cs.co/ise-api#!profilerprofile
# Endpoints
'endpointgroup': ('EndPointGroup', '/ers/config/endpointgroup'), # EndpointGroups @ https://cs.co/ise-api#!endpointgroup
'endpoint-custom-attribute': ('-', '/api/v1/endpoint-custom-attribute'),
# 'endpoint-custom-attribute-by-name': ('-', '/api/v1/endpoint-custom-attribute/{name}'), # Get custom attribute by name
# 'endpoint-stop-replication': ('-', '/api/v1/stop-replication'), # Endpoint Stop Replication Service
'endpoint': ('ERSEndPoint', '/ers/config/endpoint'), # Endpoint @ https://cs.co/ise-api#!endpoint
# 'endpointcert': ('ERSEndPointCert', '/ers/config/endpointcert'), # π No GET; POST only
'endpoints': ('-', '/api/v1/endpoint'), # π‘ Requires ISE 3.2. Endpoints @ https://cs.co/ise-api#!get-all-endpoints
# 'endpoint-value': ('-', '/api/v1/endpoint/{value}'), # π‘ Requires {value}
# 'endpoint-summary': ('-', '/api/v1/endpoint/deviceType/summary'), # π 404?
# TACACS+
'tacacscommandsets': ('TacacsCommandSets', '/ers/config/tacacscommandsets'), # TACACS @ https://cs.co/ise-api#!tacacscommandsets
'tacacsexternalservers': ('TacacsExternalServer', '/ers/config/tacacsexternalservers'), # π‘ 404 if none configured. TACACS @ https://cs.co/ise-api#!tacacsexternalservers
'tacacsprofile': ('TacacsProfile', '/ers/config/tacacsprofile'), # TACACS @ https://cs.co/ise-api#!tacacsprofile
'tacacsserversequence': ('TacacsServerSequence', '/ers/config/tacacsserversequence'), # π‘ 404 if none configured. TACACS @ https://cs.co/ise-api#!tacacsserversequence
# Policy Sets - RADIUS Network Access
# ERS policy elements
'allowedprotocols': ('AllowedProtocols', '/ers/config/allowedprotocols'),
'authorizationprofile': ('AuthorizationProfile', '/ers/config/authorizationprofile'),
'downloadableacl': ('DownloadableAcl', '/ers/config/downloadableacl'),
'filterpolicy': ('ERSFilterPolicy', '/ers/config/filterpolicy'), # 404 if none configured
# Network Access Policy @ https://cs.co/ise-api#!policy-openapi
# β Network Access policy is the assumed default; prefix "na-" not required
'na-authorization-profiles': ('-', '/api/v1/policy/network-access/authorization-profiles'),
# 'condition-id': ('-', '/api/v1/policy/network-access/condition/{conditionId}'),
'na-condition-policyset': ('-', '/api/v1/policy/network-access/condition/policyset'),
'na-condition-authn': ('-', '/api/v1/policy/network-access/condition-authentication'),
'na-condition-authz': ('-', '/api/v1/policy/network-access/condition-authorization'),
'na-dicts': ('-', '/api/v1/policy/network-access/dictionaries'),
# 'dict-name': ('-', '/api/v1/policy/network-access/dictionaries/{name}'), # π‘ Requires {name}
'na-dict-authn': ('-', '/api/v1/policy/network-access/dictionaries/authentication'),
'na-dict-authz': ('-', '/api/v1/policy/network-access/dictionaries/authorization'),
'na-dict-policyset': ('-', '/api/v1/policy/network-access/dictionaries/policyset'),
'na-identity-stores': ('-', '/api/v1/policy/network-access/identity-stores'),
'na-network-condition': ('-', '/api/v1/policy/network-access/network-condition'),
'na-policy-set': ('-', '/api/v1/policy/network-access/policy-set'),
# 'policy-set-id': ('-', '/api/v1/policy/network-access/policy-set/{id}'),
# 'policy-set-id-authn': ('-', '/api/v1/policy/network-access/policy-set/{policyId}/authentication'),
# 'policy-set-id-authz': ('-', '/api/v1/policy/network-access/policy-set/{policyId}/authorization'),
# 'policy-set-id-exception': ('-', '/api/v1/policy/network-access/policy-set/{policyId}/exception'),
'na-global-exception': ('-', '/api/v1/policy/network-access/policy-set/global-exception'),
'na-security-groups': ('-', '/api/v1/policy/network-access/security-groups'),
'na-service-names': ('-', '/api/v1/policy/network-access/service-names'),
'na-time-condition': ('-', '/api/v1/policy/network-access/time-condition'),
# Policy Sets - TACACS+ Device Admin @ https://cs.co/ise-api#!policy-openapi
# β All Device Admin policy objects have the prefix "da-"
'da-command-sets': ('-', '/api/v1/policy/device-admin/command-sets'),
'da-condition': ('-', '/api/v1/policy/device-admin/condition'),
# 'da-condition-id': ('-', '/api/v1/policy/device-admin/condition/{conditionId}'),
'da-condition-policyset': ('-', '/api/v1/policy/device-admin/condition/policyset'),
'da-condition-authn': ('-', '/api/v1/policy/device-admin/condition-authentication'),
'da-condition-authz': ('-', '/api/v1/policy/device-admin/condition-authorization'),
'da-dict-authn': ('-', '/api/v1/policy/device-admin/dictionaries/authentication'),
'da-dict-authz': ('-', '/api/v1/policy/device-admin/dictionaries/authorization'),
'da-dict-policyset': ('-', '/api/v1/policy/device-admin/dictionaries/policyset'),
'da-identity-stores': ('-', '/api/v1/policy/device-admin/identity-stores'),
'da-policy-set': ('-', '/api/v1/policy/device-admin/policy-set'),
# 'da-policy-set-id': ('-', '/api/v1/policy/device-admin/policy-set/{id}'), # π‘ requires {id}
'da-global-exception': ('-', '/api/v1/policy/device-admin/policy-set/global-exception'),
'da-service-names': ('-', '/api/v1/policy/device-admin/service-names'),
'da-shell-profiles': ('-', '/api/v1/policy/device-admin/shell-profiles'),
'da-time-condition': ('-', '/api/v1/policy/device-admin/time-condition'),
# Users
'adminuser': ('AdminUser', '/ers/config/adminuser'),
'identitygroup': ('IdentityGroup', '/ers/config/identitygroup'),
'internaluser': ('InternalUser', '/ers/config/internaluser'),
# Guest Portals
'portal': ('ERSPortal', '/ers/config/portal'),
'portalglobalsetting': ('PortalCustomizationGlobalSetting', '/ers/config/portalglobalsetting'),
'portaltheme': ('PortalTheme', '/ers/config/portaltheme'),
'hotspotportal': ('HotspotPortal', '/ers/config/hotspotportal'),
'selfregportal': ('SelfRegPortal', '/ers/config/selfregportal'),
'sponsorportal': ('SponsorPortal', '/ers/config/sponsorportal'),
'sponsoredguestportal': ('SponsoredGuestPortal', '/ers/config/sponsoredguestportal'),
# 'guestlocation': ('LocationIdentification', '/ers/config/guestlocation'),
'guestsmtpnotificationsettings': ('ERSGuestSmtpNotificationSettings', '/ers/config/guestsmtpnotificationsettings'),
'guestssid': ('GuestSSID', '/ers/config/guestssid'),
'guesttype': ('GuestType', '/ers/config/guesttype'), # π 500 internal errors?
# 'guestuser': ('GuestUser', '/ers/config/___GuestUser__'), # π requires sponsor account!!!
# 'smsprovider': ('SmsProviderIdentification', '/ers/config/smsprovider'),
'sponsorgroup': ('SponsorGroup', '/ers/config/sponsorgroup'),
'sponsorgroupmember': ('SponsorGroupMember', '/ers/config/sponsorgroupmember'),
# BYOD
'certificateprofile': ('CertificateProfile', '/ers/config/certificateprofile'),
'certificatetemplate': ('ERSCertificateTemplate', '/ers/config/certificatetemplate'),
'byodportal': ('BYODPortal', '/ers/config/byodportal'),
'mydeviceportal': ('MyDevicePortal', '/ers/config/mydeviceportal'),
'nspprofile': ('ERSNSPProfile', '/ers/config/nspprofile'),
'ipsec': ('ipsec', '/api/v1/ipsec'), # IPsec @ https://cs.co/ise-api#!ipsec-openapi
# 'ipsec': ('ipsec', '/api/v1/ipsec/{hostName}/{nadIp}'),
'ipsec-certificates': ('ipsec-certificates','/api/v1/ipsec/certificates'),
# Support / Operations
# 'supportbundle': ('_____', '/ers/config/supportbundle'),
# 'supportbundledownload': ('_____', '/ers/config/_____'),
# 'supportbundlestatus': ('_____', '/ers/config/_____'),
# Task
'task': ('-', '/api/v1/task'),
# 'task-id': ('-', '/api/v1/task/{id}'),
# pxGrid / ANC / RTC / TC-NAC @ https://github.com/cisco-pxgrid/pxgrid-rest-ws/wiki
# 'pxgridsettings': ('PxgridSettings', '/ers/config/pxgridsettings/autoapprove'), # π PUT only; GET not supported!
# 'pxgridnode': ('pxGridNode', '/ers/config/pxgridnode'), # π π 404 always whether pxGrid is enabled or not
'ancendpoint': ('ErsAncEndpoint', '/ers/config/ancendpoint'), # ANCEndpoint @ https://cs.co/ise-api#!ancendpoint
'ancpolicy': ('ErsAncPolicy', '/ers/config/ancpolicy'), # ANCPolicy @ https://cs.co/ise-api#!ancpolicy
# 'ancpolicy-version': ('VersionInfo', '/ers/config/ancpolicy/versioninfo'), # π‘ No `SearchResult`, only `VersionInfo` object. Get ANC policy version information
# 'clearThreatsAndVulneribilities': ('ERSIrfThreatContext', '/ers/config/threat/clearThreatsAndVulneribilities'), # π PUT only; GET not supported! @ https://cs.co/ise-api#!clearThreatsAndVulneribilities
'telemetryinfo': ('TelemetryInfo', '/ers/config/telemetryinfo'), # Telemetry @ https://cs.co/ise-api#!telemetryinfo
# 'acibindings': ('ACIBindings', '/ers/config/acibindings/getall'), # ACI @ https://cs.co/ise-api#!acibindings
# 'acisettings': ('AciSettings', '/ers/config/acisettings'), # ACI @ https://cs.co/ise-api#!acisettings
# Operations
# 'op': ('_____', '/ers/config/_____'),
# 'op/systemconfig': ('_____', '/ers/config/_____'),
# 'op/systemconfig/iseversion': ('_____', '/ers/config/_____'),
# License
'license-system-smart-state': ('-', '/api/v1/license/system/smart-state'),
'license-system-register': ('-', '/api/v1/license/system/register'),
'license-system-tier-state': ('-', '/api/v1/license/system/tier-state'),
'license-system-eval-license': ('-', '/api/v1/license/system/eval-license'),
'license-system-connection-type': ('-', '/api/v1/license/system/connection-type'),
'license-system-feature-to-tier-mapping': ('-', '/api/v1/license/system/feature-to-tier-mapping'),
# FiveG
# '': ('-', ''),
}
async def get_url_response(session, url_q, response_q):
"""
Take a URL from the queue, use the session to GET a response and put it in the response_q.
:param session (aiohttp.ClientSession): the aiohttp session to reuse
:param url_q (asyncio.Queue) : the queue of URLs to GET using the session
:param response_q (asyncio.Queue) : the queue for all responses
"""
while True:
url = await url_q.get() # Wait for item in the queue
response = await session.get(url)
data = await response.json()
if args.verbosity == 1: print(ICONS['DOWN'], end='', flush=True, file=sys.stderr)
await response_q.put(data)
url_q.task_done() # Notify queue item is done
async def extract_ise_resources(response_q:asyncio.Queue=None, resource_q:asyncio.Queue=None):
"""
Extract the JSON resource(s) from the ISE REST API response and add them to the resource queue for further processing.
:param response_q (asyncio.Queue) : the queue for all responses
:param resource_q (asyncio.Queue) : the queue for extracted resources
"""
while True:
data = await response_q.get() # Wait for data in the queue
if args.verbosity == 1: print(ICONS['LIST'], end='', flush=True, file=sys.stderr)
if isinstance(data, dict): # ISE ERS API is a dict: {'SearchResult': {'total': 7, 'resources': [{'id': ... }]}}
resources = data['SearchResult']['resources']
elif isinstance(data, list): # OpenAPI is a list: []
resources = data
else:
print(f"extract_ise_resources(): Unsupported data type: {data}")
for resource in resources:
if args.verbosity >= 3: print(f"{ICONS['PLAY']} {resource['id']} | {resource['name']}")
await resource_q.put(resource)
response_q.task_done() # Notify queue item is done
async def delete_ise_resource_by_id(session:aiohttp.ClientSession=None, path:str=None, resource_q:asyncio.Queue=None):
"""
Delete the specified resource using it's id.
:param session (aiohttp.ClientSession): the aiohttp session to reuse
:param path (str) : the REST API endpoint path
:param resource_q (asyncio.Queue) : the asyncio Queue for ERS resource pages to fetch
"""
while True:
resource = await resource_q.get() # Wait for item in the queue
try:
response = await session.delete(f"{path}/{resource['id']}")
if response.ok:
if args.verbosity == 1: print(f"{ICONS['PASS']}", end='', flush=True, file=sys.stderr)
if args.verbosity >= 2: print(f"{ICONS['PASS']} {response.status} | {resource['id']} | {resource['name']}", file=sys.stdout)
else:
if args.verbosity == 1: print(f"{ICONS['FAIL']}", end='', flush=True, file=sys.stderr)
if args.verbosity >= 2: print(f"{ICONS['FAIL']} {response.status} | {resource['id']} | {resource['name']} : {(await response.json()).popitem()[1]['messages'][0]['title']}", file=sys.stdout)
except Exception as e: # catch *all* exceptions
tb_text = '\n'.join(traceback.format_exc().splitlines()[1:]) # remove 'Traceback (most recent call last):'
print(f"{ICONS['ERROR']} {e.__class__} {tb_text}", file=sys.stderr)
finally:
resource_q.task_done() # Notify queue item is done
async def ise_delete(resource_name:str=None):
"""
Entrypoint for packaged script.
"""
if args.verbosity: print(f"{ICONS['INFO']} Deleting all '{resource_name}'", file=sys.stderr)
name, path = ISE_REST_ENDPOINTS[resource_name.strip(', ')]
env = {k:v for (k, v) in os.environ.items() if k.startswith('ISE_')} # Load environment variables
verify_ssl = False if args.insecure or env['ISE_CERT_VERIFY'][0:1].lower() in ['f','n'] else True
async with aiohttp.ClientSession(
f"https://{env['ISE_PPAN']}",
auth=aiohttp.BasicAuth(login=env['ISE_REST_USERNAME'], password=env['ISE_REST_PASSWORD']),
connector=aiohttp.TCPConnector(limit=TCP_LIMIT, ssl=verify_ssl),
headers={'Accept':'application/json', 'Content-Type':'application/json'}
) as session:
# Create queues for the processing pipeline
url_q = asyncio.Queue() # load up the URL pages
response_q = asyncio.Queue(maxsize=2) # many resources per page size
resource_q = asyncio.Queue(maxsize=TCP_LIMIT+1) # don't bother making more than the connector will use!
response = await session.get(f"{path}?size={REST_PAGE_SIZE}&page=1")
data = await response.json()
await response_q.put(data)
if isinstance(data, dict): # ISE ERS API is a dict: {'SearchResult': {'total': 7, 'resources': [{'id': ... }]}}
total = data['SearchResult']['total']
if args.verbosity: print(f"{ICONS['INFO']} Get {total} x '{resource_name}' ERS resource(s)", file=sys.stderr)
resources = data['SearchResult']['resources']
if total > REST_PAGE_SIZE:
# Enqueue all page URLs beyond the initial REST page size
urls = [f"{path}?size={REST_PAGE_SIZE}&page={page}" for page in range(1, 1+int(total/REST_PAGE_SIZE)+(1 if total%REST_PAGE_SIZE else 0))]
[await url_q.put(url) for url in urls] # enqueue URLs
elif isinstance(data, list): # OpenAPI is a list: []
print(f"{ICONS['WARN']} Only the first page of OpenAPI resources is supported", file=sys.stderr)
else:
print(f"Unsupported resource type: {resource_name} at {path}", file=sys.stderr)
try:
# Schedule all fetch tasks
url_tasks = [asyncio.create_task(get_url_response(session, url_q, response_q))] # only 1 needed
extract_tasks = [asyncio.create_task(extract_ise_resources(response_q, resource_q))]
delete_tasks = [asyncio.create_task(delete_ise_resource_by_id(session, path, resource_q)) for idx in range(TCP_LIMIT*2)]
await url_q.join() # Block until all items in queue are processed
await response_q.join()
await resource_q.join()
except Exception as e:
tb_text = '\n'.join(traceback.format_exc().splitlines()[1:]) # remove 'Traceback (most recent call last):'
print(f"{ICONS['ERROR']} {e.__class__} {tb_text}", file=sys.stderr)
finally:
if args.verbosity == 1: print(flush=True, file=sys.stderr) # newline for verbose updates
[task.cancel() for task in (extract_tasks + url_tasks + delete_tasks)] # Cancel all tasks
if __name__ == '__main__':
"""
Entrypoint for local script.
"""
global args
argp = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawTextHelpFormatter)
argp.add_argument('resource', type=str, help='resource name')
argp.add_argument('-i','--insecure', action='store_true', default=False, help='do not verify certificates for TLS (allow self-signed certs)')
argp.add_argument('-t','--timer', action='store_true', default=False, help='time', required=False)
argp.add_argument('-v','--verbosity', action='count', default=0, help='verbosity')
args = argp.parse_args()
if args.timer: start_time = time.time()
loop = asyncio.get_event_loop()
main_task = asyncio.ensure_future(ise_delete(args.resource))
# Handle CTRL+C interrupts gracefully
for signal in [signal.SIGINT, signal.SIGTERM]:
loop.add_signal_handler(signal, main_task.cancel)
try:
loop.run_until_complete(main_task)
finally:
loop.close()
if args.timer: print(f"β² {'{0:.3f}'.format(time.time() - start_time)} seconds", file=sys.stderr)