-
Notifications
You must be signed in to change notification settings - Fork 37
/
asa.html
172 lines (168 loc) · 6.7 KB
/
asa.html
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
<!doctype html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, minimum-scale=0.25">
<title>Export ASA</title>
<style>
body {
font-family: sans-serif;
}
pre {
background-color: #eee;
padding: 8px;
}
@media (prefers-color-scheme: dark) {
*, a {
background-color: #121212;
color: #ddd;
}
pre {
background-color: #333;
color: #ddd;
}
}
</style>
</head>
<body>
<h1>Export ASA</h1>
<p>Export ASA objects (eg, project servers/server-users/groups; audits are a work in progress). See
<a href=https://github.com/gabrielsroka/gabrielsroka.github.io/blob/master/asa.html>source</a>.</p>
<b>Setup</b>
<ol>
<li>Show your bookmarks toolbar. In Chrome, … > Bookmarks > Show Bookmarks Bar. In Firefox, right-click in the title bar and click Bookmarks Toolbar.
<li>Drag/drop this <a id=bm>ASA v1</a> to the bookmarks toolbar.
</ol>
<br>
<b>Usage</b>
<ol>
<li>In your ASA tenant, select a project or click Projects to export all projects.
Or click Audits and find the results in the DevTools console (export to CSV is a work in progress).
<li>Click the "ASA" button from your bookmarks toolbar.
<li>For debugging, it's helpful to open the browser's DevTools and look at the Network tab.
</ol>
<script id=code>
(async function () {
const {team, path, projectName, headers} = init(); /* Call this first! */
const projectNames = [];
if (path == 'project') {
projectNames.push(projectName);
} else if (path == 'projects') {
for await (const project of getObjects('/projects')) {
projectNames.push(project.name);
}
} else if (path == 'events') {
async function* getAudits() {
for await (const page of getPages('/v1/teams/' + team + '/auditsV2')) {
Object.values(page.related_objects).forEach(ro => ros[ro.type] = true);
Object.keys(page.related_objects).forEach(k => lens[k.length] = true);
for (const o of page.list) {
if (o.details.actor) o.__actor = page.related_objects[o.details.actor];
if (o.details.project_id || o.details.project_name) o.__project = page.related_objects[o.details.project_id || o.details.project_name];
if (o.details.server_id) o.__server = page.related_objects[o.details.server_id];
if (o.details.client) o.__client = page.related_objects[o.details.client];
if (o.details.target_user) o.__target_user = page.related_objects[o.details.target_user];
Object.entries(o.details).forEach(([k, v]) => {if (v?.length == 44) ds[k] = true});
yield o;
}
}
}
console.clear();
const ros = {};
const ds = {};
const lens = {};
const audits = [];
for await (const audit of getAudits()) {
audits.push(audit);
}
console.table(audits);
console.table(ros);
console.table(ds);
console.table(lens);
return;
} else {
alert('Select a project or click Projects first, then re-run this.');
return;
}
/* Set these headers to select which attributes are exported to CSV.
see https://developer.okta.com/docs/reference/advanced-server-access-api */
exportProjects('server users', '/server_users',
'project_name,user_name,type,status,admin,windows_server_user_name,server_user_name,unix_uid,unix_gid,id');
exportProjects('servers', '/servers',
'project_name,team_name,id,hostname,alt_names,bastion,canonical_name,access_address,os,os_type,services,registered_at,last_seen,' +
'sftd_version,cloud_provider,ssh_host_keys,broker_host_certs,' +
'instance_details,instance_details.instance_id,instance_details.account_id,instance_details.availability_zone,instance_details.networks,instance_details.public_ip_v4,instance_details.instance_type,' +
'state,managed,deleted_at,ad_guid,ad_dn,instance_id,source');
exportProjects('groups', '/groups',
'project_name,id,group,name,group_id,server_access,server_admin,create_server_group,removed_at,deleted_at,unix_gid,server_group_name,profile_attributes');
async function exportProjects(filename, path, header) {
const objects = [];
for (const projectName of projectNames) {
for await (const object of getObjects('/projects/' + projectName + path)) {
object.project_name = projectName;
objects.push(toCSV(...header.split(',').map(h => dot(object, h))));
}
}
downloadCSV(header, objects, (projectNames.length == 1 ? projectNames[0] + '' : 'all projects') + ' ' + filename);
}
/* helper code */
function init() {
const [team, path, projectName] = location.pathname.split('/').slice(2);
const cookies = Object.fromEntries(document.cookie.split('; ').map(c => c.split('=')).map(([key, val]) => [key, JSON.parse(decodeURIComponent(val))]));
const token = cookies['SFT_logged-in_store_' + team].token;
const headers = {authorization: 'Bearer ' + token};
return {team, path, projectName, headers};
}
async function* getObjects(url) {
for await (const page of getPages('/v1/teams/' + team + url)) {
for (const o of page.list) {
yield o;
}
}
}
async function* getPages(url) {
while (url) {
const r = await get(url);
const page = await r.json();
yield page;
url = r.headers.get('link')?.match('<https://[^/]+(/[^>]+)>; rel="next"')?.[1];
}
}
async function get(url) {
return fetch(url, {method: 'get', headers});
}
function toCSV(...fields) {
return fields.map(field => {
if (field == undefined) {
var s = '';
} else {
s = field.toString();
if (s == '[object Object]') s = JSON.stringify(field);
s = s.replace(/"/g, '""');
}
return `"${s}"`;
}).join(',');
}
function downloadCSV(header, lines, filename) {
const a = document.body.appendChild(document.createElement('a'));
a.href = URL.createObjectURL(new Blob([header + '\n' + lines.join('\n')], {type: 'text/csv'}));
const date = new Date().toISOString().replace(/T/, ' ').replace(/:/g, '-').slice(0, 19);
a.download = `${filename} ${date}.csv`;
a.click();
document.body.removeChild(a);
}
function dot(o, dots) {
var ps = dots.split(".");
for (var p in ps) {
o = o[ps[p]];
if (o == null) break;
}
return o;
}
})
</script>
<script>
bm.href = 'javascript:' + code.innerText + '();';
</script>
</div>
</body>
</html>