-
Notifications
You must be signed in to change notification settings - Fork 2
/
make-ise-endpoint.py
executable file
Β·318 lines (279 loc) Β· 11.9 KB
/
make-ise-endpoint.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
#!/usr/bin/env python3
"""
Generate the specified number of random ISE endpoint resources using REST APIs.
Examples:
make-endpoint.py -h
make-endpoint.py -n 10
make-endpoint.py -f yaml -g random -n 6
make-endpoint.py -vtn 1000 --group IOT
make-endpoint.py -tvf csv -g random -n 1000000 > endpoints_1M.csv # β± 10.287 seconds
You may add these export lines to a text file and load with `source`:
source ise-env.sh
"""
__author__ = "Thomas Howard"
__email__ = "[email protected]"
__license__ = "MIT - https://mit-license.org/"
import aiohttp
import asyncio
import argparse
import csv
import io
import json
import os
import random
import requests
import sys
import time
import yaml
FORMATS = ['csv', 'json', 'pretty', 'line', 'yaml']
FORMAT_DEFAULT = 'json'
# ISE 3.4 Context Visibility > Export, β "Importable only"
# β Note that ISE does not include custom endpoint attributes for import
ISE_CV_DEFAULT_ENDPOINT_EXPORT_COLUMNS = [
'MACAddress',
'EndPointPolicy',
'IdentityGroup',
'Description',
# 'DeviceRegistrationStatus',
# 'BYODRegistration',
# 'Device Type',
# 'EmailAddress',
# 'ip',
# 'FirstName',
# 'host-name',
# 'LastName',
# 'MDMServerID',
# 'MDMServerName',
# 'MDMEnrolled',
# 'Location',
# 'PortalUser',
'User-Name',
'StaticAssignment',
'StaticGroupAssignment',
# 'MDMOSVersion',
# 'PortalUser.FirstName',
# 'PortalUser.LastName',
# 'PortalUser.EmailAddress',
# 'PortalUser.PhoneNumber',
# 'PortalUser.GuestType',
# 'PortalUser.GuestStatus',
# 'PortalUser.Location',
# 'PortalUser.GuestSponsor',
# 'PortalUser.CreationType',
# 'AUPAccepted',
]
ICONS = {
# name : icon
'ERROR' : 'β',
'INFO' : 'βΉ',
'PLAY' : 'β·',
'TIMER' : 'β±',
}
endpoint_groups_registry = {} # id : name
mac_registry = {}
def endpoint_to_csv(endpoint:dict=None) -> dict:
"""
Returns a dictionary mapping endpoint attributes to an importable endpoint CSV format.
:param endpoint (dict) : a dictionary representing an endpoint
"""
return {
'MACAddress' : endpoint.get('mac', ''),
'EndPointPolicy' : endpoint.get('endpointPolicy', ''), # endpoint profile id?
'IdentityGroup' : endpoint.get('groupId', ''),
'Description' : endpoint.get('description', ''),
# 'DeviceRegistrationStatus' : 'NotRegistered',
# 'BYODRegistration' : 'Unknown',
# 'Device Type' : 'Device Type#All Device Types#T-800',
# 'EmailAddress' : None,
# 'ip' : None,
# 'FirstName' : None,
# 'host-name' : None,
# 'LastName' : None,
# 'MDMServerID' : None,
# 'MDMServerName' : None,
# 'MDMEnrolled' : None,
# 'Location' : 'Location#All Locations#AMER#US#XYZ',
# 'PortalUser' : None,
'User-Name' : endpoint.get('name', ''),
'StaticAssignment' : endpoint.get('staticProfileAssignment', 'false'),
'StaticGroupAssignment' : endpoint.get('staticGroupAssignment', 'false'),
# 'MDMOSVersion' : None,
# 'PortalUser.FirstName' : None,
# 'PortalUser.LastName' : None,
# 'PortalUser.EmailAddress' : None,
# 'PortalUser.PhoneNumber' : None,
# 'PortalUser.GuestType' : None,
# 'PortalUser.GuestStatus' : None,
# 'PortalUser.Location' : None,
# 'PortalUser.GuestSponsor' : None,
# 'PortalUser.CreationType' : None,
# 'AUPAccepted' : None,
}
def get_random_mac (oui:str=None):
"""
Returns a unique MAC address with the format `XX:XX:XX:XX:XX:XX`.
:param oui (str) (optional) an organizationally unique identifier of the MAC address
"""
SEP = ':' # byte separator
oui = '{:06X}'.format(random.randint(1, 16777216)) if oui is None else oui # 16777216 == 2^24
nic = random.randint(1, 16777216) # starting number for MAC's NIC address
mac = SEP.join([(oui '{:06X}'.format(nic))[idx:idx 2] for idx in range(0, 12, 2)]) # Format MAC XX:XX:XX:XX:XX:XX
while (mac in mac_registry):
mac = SEP.join([(oui '{:06X}'.format(nic 1))[idx:idx 2] for idx in range(0, 12, 2)])
mac_registry[mac] = True
return mac
def get_endpointgroup_id(name:str='Unknown'):
"""
Returns the id of the endpoint group with the specified name, otherwise `None`.
:param name (str) : the name of the endpoint group
"""
if args.verbosity >= 2: print(f"{ICONS['PLAY']} get_endpointgroup_id(name={name})", file=sys.stderr)
names = [k for k,v in endpoint_groups_registry.items() if v == name]
if len(names) == 0:
return None
return names[0]
def show(resources:list=None, format:str=FORMAT_DEFAULT, filepath:str='-', headers:list=None, name:str='objects'):
"""
Show/print/dump the resources in the specified format to the file. `sys.stdout` ('-') by default.
- resources (list) : a list of list items to show
- headers (list) : the name of the resource. Example: endpoint, sgt, etc.
- format (str): one the following formats:
- `csv` : Show the items in a Comma-Separated Value (CSV) format
- `json` : Show the items as a single JSON string
- `yaml` : Show the items as YAML with 2-space indents
- filepath (str) : Default: `sys.stdout`
"""
if resources == None: return
if not format in FORMATS: raise ValueError(f"Unsupported format: {format}")
if format == 'csv' and headers is None: raise ValueError(f"CSV requires headers")
if format != 'csv' and name is None: raise ValueError(f"JSON and YAML require an object name")
if args.verbosity >= 2: print(f"{ICONS['PLAY']} show({len(resources)} as {format} to {filepath})", file=sys.stderr)
# π‘ Do not close sys.stdout or it may not be re-opened with multiple show() calls
fh = sys.stdout if filepath == '-' else open(filepath, 'w') # write to sys.stdout/terminal by default
if format == 'csv': # CSV
writer = csv.DictWriter(fh, headers) # , encoding='utf-8')
writer.writeheader()
writer.writerows(resources)
elif format == 'json': # JSON, one long string
print(json.dumps({ name: resources }), file=fh)
elif format == 'line': # 1 JSON line per object
print('{', file=fh)
print(f'"{name}" : [', file=fh)
print(",\n".join([json.dumps(r) for r in resources]), file=fh)
print(']\n}', file=fh)
elif format == 'pretty': # pretty-print
print(json.dumps({ name: resources }, indent=2), file=fh)
elif format == 'yaml': # YAML
print(yaml.dump({ name: resources }, indent=2, default_flow_style=False), file=fh)
else: # just in case something gets through the CLI parser
print(f' π Unknown format: {format}', file=sys.stderr)
def make_endpoint(
name:str=None,
mac:str=None,
description:str=None,
group:str=None,
group_type:str='id', # ['id','name']
# profile:str=None, # π§ ToDo
# identityStore:str=None, # π§ ToDo
# customAttributes:dict=None, # π§ ToDo
# mdmAttributes:dict=None, # π§ ToDo
):
"""
Make an ISE endpoint object.
:param name (str) : None,
:param mac (str) : None,
:param description (str) : None,
:param group (str) : None,
"""
if args.verbosity >= 2: print(f"{ICONS['PLAY']} make_endpoint(name={name}, mac={mac}, description={description}, group={group})", file=sys.stderr)
mac = get_random_mac()
group = 'Unknown' if group is None else group
group = random.choice(list(endpoint_groups_registry.values())) if group == 'random' else group
group = get_endpointgroup_id(group) if group_type == 'id' else group # CSV uses name, JSON & YAML use
# ISE OpenAPI endpoint (/ers/config/endpoint)
endpoint = {
'name': mac,
'description': '', # faker.sentence(nb_words=8), # optional
'mac': mac,
'profileId': None, # optional
'staticProfileAssignment' : False,
'staticProfileAssignmentDefined': False, # optional
'groupId': group,
'staticGroupAssignment' : False,
'staticGroupAssignmentDefined' : False, # optional
# 'portalUser': '', # optional
# 'identityStore': 'InternalUsers', # optional
# 'identityStoreId': '', # optional
# 'customAttributes': { # optional
# 'customAttributes': { }
# },
# 'mdmAttributes': { }, # optional
}
if args.verbosity >= 2: print(f"{ICONS['INFO']} make_endpoint(): {endpoint}", file=sys.stderr)
return endpoint
# ISE OpenAPI endpoint (/api/v1/endpoint)
# {
# "id": "028436a0-ff80-11ee-996b-bacbf94e72cd",
# "name": "A7:F2:61:F1:D6:C6",
# "description": "",
# "customAttributes": null,
# "mdmAttributes": null,
# "groupId": "aa0e8b20-8bff-11e6-996c-525400b48521",
# "identityStore": "",
# "identityStoreId": "",
# "mac": "A7:F2:61:F1:D6:C6",
# "portalUser": "",
# "profileId": "",
# "ipAddress": null,
# "vendor": null,
# "productId": null,
# "serialNumber": null,
# "deviceType": null,
# "softwareRevision": null,
# "hardwareRevision": null,
# "protocol": null,
# "staticGroupAssignment": false,
# "staticProfileAssignment": false,
# "assetId": null,
# "assetName": null,
# "assetIpAddress": null,
# "assetVendor": null,
# "assetProductId": null,
# "assetSerialNumber": null,
# "assetDeviceType": null,
# "assetSwRevision": null,
# "assetHwRevision": null,
# "assetProtocol": null,
# "assetConnectedLinks": null
# }
if __name__ == '__main__':
"""
Entrypoint for local script.
"""
argp = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawTextHelpFormatter)
argp.add_argument('-n', '--number', action='store', type=int, default=1, help='number of endpoints to create', required=False)
argp.add_argument('-f', '--format', choices=FORMATS, default='yaml', help='Output format or styling')
argp.add_argument('-g', '--group', action='store', type=str, default='Unknown', help='endpoint group name; `random` chooses randomly', required=False)
argp.add_argument('-t', '--timer', action='store_true', default=False, help='show total runtime, in seconds')
argp.add_argument('-v', '--verbosity', action='count', default=0, help='verbosity count',)
global args # promote to global scope for use in other functions
args = argp.parse_args()
if args.timer: start_time = time.time()
env = {k:v for (k, v) in os.environ.items() if k.startswith('ISE_')} # Load environment variables
# π‘ Cache endpoint group ids & names - there is no guarantee of consistent ids across ISE deployments even for default groups
# π§ ToDo: Implement paging for >100 endpoint groups
response = requests.get(
f"https://{env['ISE_PPAN']}/ers/config/endpointgroup?&size=100",
auth=(env['ISE_REST_USERNAME'], env['ISE_REST_PASSWORD']),
headers={ 'Accept': 'application/json' },
verify=(False if env['ISE_CERT_VERIFY'][0].lower() in ['f','n'] else True)
)
for item in (response.json())['SearchResult']['resources']:
endpoint_groups_registry[item['id']] = item['name'] # cache id to name
endpoints = []
for idx in range(1, args.number 1):
endpoints.append(make_endpoint(group=args.group, group_type=('id' if args.format == 'json' else 'name')))
if args.format == 'csv':
endpoints = [endpoint_to_csv(endpoint) for endpoint in endpoints] # return [dict] for CSV
show(endpoints, format=args.format, filepath='-', name='endpoint', headers=ISE_CV_DEFAULT_ENDPOINT_EXPORT_COLUMNS)
if args.timer: print(f"{ICONS['TIMER']} {'{0:.3f}'.format(time.time() - start_time)} seconds", file=sys.stderr)