-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpublic_no_access.py
More file actions
353 lines (299 loc) · 11.5 KB
/
public_no_access.py
File metadata and controls
353 lines (299 loc) · 11.5 KB
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
# -*- coding: utf-8 -*-
""":Mod: public_no_access_json
:Synopsis:
Reports PASTA metadata and data resources that lack public read access,
outputs the report as JSON formatted text.
:Author:
Duane Costa
:Created:
2/16/18
"""
import logging
import os
import sys
from base64 import b64decode, b64encode
from docopt import docopt
import requests
import xml.etree.ElementTree as ET
import json
logging.basicConfig(format='%(asctime)s %(levelname)s (%(name)s): %(message)s',
datefmt='%Y-%m-%d %H:%M:%S%z',
# filename='$NAME' + '.log',
level=logging.INFO)
logger = logging.getLogger('public_no_access_report')
def get_metadata_acl(base_url=None, path=None, cookies=None):
"""
Gets the metadata access control block from PASTA
"""
url = base_url + '/package/metadata/acl/eml/' + path
try:
r = requests.get(url, cookies=cookies)
if r.status_code != requests.codes.ok:
logger.error(
'Bad status code ({code}) for {url}'.format(
code=r.status_code, url=url))
else:
return r.text.strip()
except Exception as e:
logger.error(e)
def get_data_acl(base_url=None, path=None, cookies=None):
"""
Gets the data access control block from PASTA
"""
url = base_url + '/package/data/acl/eml/' + path
try:
r = requests.get(url, cookies=cookies)
if r.status_code != requests.codes.ok:
logger.error(
'Bad status code ({code}) for {url}'.format(
code=r.status_code, url=url))
else:
return r.text.strip()
except Exception as e:
logger.error(e)
def get_resource_map(base_url=None, path=None, cookies=None):
"""
Gets the resource map for a given package ID from PASTA
"""
resource_map = ""
url = base_url + '/package/eml/' + path
try:
r = requests.get(url, cookies=cookies)
if r.status_code != requests.codes.ok:
logger.error(
'Bad status code ({code}) for {url}'.format(
code=r.status_code, url=url))
else:
resource_map = r.text.strip()
except Exception as e:
logger.error(e)
return resource_map
def get_identifiers(base_url=None, scope=None):
"""
Gets the list of identifiers for a given scope from PASTA
"""
try:
url = base_url + '/package/eml/' + scope
r = requests.get(url)
if r.status_code != requests.codes.ok:
logger.error('Bad status code ({code}) for {url}'.format(
code=r.status_code, url=url))
else:
return [_.strip() for _ in (r.text).split('\n')]
except Exception as e:
logger.error(e)
def get_newest_revision(base_url=None, scope=None, identifier=None):
"""
Gets the newest revision of a data package from PASTA
"""
url = base_url + '/package/eml/' + scope + '/' + identifier \
+ '?filter=newest'
try:
r = requests.get(url)
if r.status_code != requests.codes.ok:
logger.error(
'Bad status code ({code}) for {url}'.format(
code=r.status_code, url=url))
else:
return r.text.strip()
except Exception as e:
logger.error(e)
def get_scopes(base_url=None):
"""
Gets the complete list of scopes from PASTA
"""
try:
url = base_url + '/package/eml'
r = requests.get(url)
if r.status_code != requests.codes.ok:
logger.error('Bad status code ({code}) for {url}'.format(
code=r.status_code, url=url))
else:
return [_.strip() for _ in (r.text).split('\n')]
except Exception as e:
logger.error(e)
def has_public_read_access(acl_xml=None):
"""
Boolean to determine whether an access control list XML contains
an entry for public read access
"""
has_public_read = False
try:
root = ET.fromstring(acl_xml)
for child in root:
has_principal_public = False
has_permission_read = False
if child.tag == 'allow':
for grandchild in child:
if grandchild.tag == 'principal' and \
grandchild.text == 'public':
has_principal_public = True
if grandchild.tag == 'permission' and \
grandchild.text == 'read':
has_permission_read = True
if has_principal_public and has_permission_read:
has_public_read = True
except Exception as e:
logger.error(e)
return has_public_read
def parse_metadata_resource(resource_map=None):
"""
Returns the metadata resource found in a PASTA resource map
"""
metadata_resource = ""
if resource_map:
resources = resource_map.split('\n')
for resource in resources:
if '/metadata/' in resource:
metadata_resource = resource
break
return metadata_resource
def parse_data_resources(resource_map=None):
"""
Returns a list of data resources found in a PASTA resource map
"""
data_resources = []
if resource_map:
resources = resource_map.split('\n')
for resource in resources:
if '/data/' in resource:
data_resources.append(resource)
return data_resources
def package_id_to_path(package_id=None):
"""
Derives a slash-separated path from a dot-separated package ID
"""
return package_id.replace('.', '/')
def authenticate(base_url=None, creds=None):
"""
Authenticates with PASTA using the supplied credentials,
returning the cookies containing the authentication token
for subsequent usage
"""
service = '/package/eml'
if (creds):
dn, pw = tuple(creds.split(':'))
url = base_url + service
r = requests.get(url, auth=(dn, pw))
auth_token = r.cookies['auth-token']
cookies = {'auth-token' : auth_token}
return cookies
else:
return None
def get_resource_dict(package_id, resource_id, acl_xml):
"""
Derives a resource_dict dictionary from the supplied package ID,
resource ID, and access control XML values
"""
resource_dict = {"package_id" : package_id,
"resource_id" : resource_id,
"acl_xml" : acl_xml
}
return resource_dict
def main(argv):
"""
Reports on PASTA metadata and data resources that lack public read access.
Usage:
public_no_access_report.py [-u | --url <url>]
[-c | --creds <creds>]
[-s | --scope <scope>]
[-o | --output <output>]
public_no_access_report.py -h | --help
Options:
-u --url Base URL of PASTA services,
e.g. 'https://pasta.lternet.edu'
-c --creds Authentication credentials
-s --scope Restrict to given scope
-o --output Output results to file;
the filename should have a .json extension
-h --help This page
"""
args = docopt(str(main.__doc__))
url = args['<url>']
creds = args['<creds>']
scope = args['<scope>']
ignored_scopes = ('lter-landsat', 'lter-landsat-ledaps', 'ecotrends')
output = args['<output>']
if not url:
BASE_URL = "http://localhost:8888"
else :
BASE_URL = url
if not creds:
logger.error("Specify authentication credentials with '-c <creds>'")
sys.exit()
my_cookies = authenticate(BASE_URL, creds)
if not my_cookies:
logger.error("Failed to authenticate")
sys.exit()
non_public_metadata = []
non_public_data = []
if scope is None:
scopes = get_scopes(base_url=BASE_URL)
else:
scopes = [scope]
if output is None:
fp = sys.stdout
else:
fp = open(output, 'w')
for scope in scopes:
if scope not in ignored_scopes:
identifiers = get_identifiers(base_url=BASE_URL, scope=scope)
for identifier in identifiers:
revision = get_newest_revision(base_url=BASE_URL,
scope=scope,
identifier=identifier)
id = scope + '.' + identifier + '.' + revision
logger.info("Working on package ID: " + id)
path = package_id_to_path(id)
# Get the resource map, if we have read access to it
resource_map = get_resource_map(base_url=BASE_URL,
path=path,
cookies=my_cookies)
metadata_resource = parse_metadata_resource(resource_map)
metadata_acl_xml = ""
try:
metadata_acl_xml = get_metadata_acl(base_url=BASE_URL,
path=path,
cookies=my_cookies)
if has_public_read_access(metadata_acl_xml):
# Get the data resources from the resource map
data_resources = parse_data_resources(resource_map)
for data_resource in data_resources:
entity_id = data_resource.split('/')[-1]
entity_path = path + '/' + entity_id
try:
data_acl_xml = get_data_acl(base_url=BASE_URL,
path=entity_path,
cookies=my_cookies)
if not has_public_read_access(data_acl_xml):
rdict = get_resource_dict(id,
data_resource,
data_acl_xml)
non_public_data.append(rdict)
except Exception as e:
logger.error(e)
rdict = get_resource_dict(id,
data_resource,
data_acl_xml)
non_public_data.append(rdict)
else:
rdict = get_resource_dict(id,
metadata_resource,
metadata_acl_xml)
non_public_metadata.append(rdict)
except Exception as e:
logger.error(e)
rdict = get_resource_dict(id,
metadata_resource,
metadata_acl_xml)
non_public_metadata.append(rdict)
non_public_resources = {}
non_public_resources["metadata"] = non_public_metadata
non_public_resources["data"] = non_public_data
json.dump(non_public_resources, fp, indent=2, separators=(',', ': '))
print('', file=fp)
if (output):
fp.close()
logger.info("Finished program")
if __name__ == "__main__":
main(sys.argv)