|
| 1 | +import json |
| 2 | +import os |
| 3 | +import pwd |
| 4 | +import time |
| 5 | + |
| 6 | +from urllib import error, request |
| 7 | + |
| 8 | +import osgtest.library.core as core |
| 9 | +import osgtest.library.files as files |
| 10 | +import osgtest.library.osgunittest as osgunittest |
| 11 | + |
| 12 | +# Headers so that heroku doesn't block us |
| 13 | +HEADERS = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko)' + |
| 14 | + 'Chrome/35.0.1916.47 Safari/537.36', |
| 15 | + 'Content-Type': 'application/json'} |
| 16 | + |
| 17 | + |
| 18 | +def request_demo_scitoken(scope, audience='ANY'): |
| 19 | + """Request a token with 'scope' from the demo SciTokens issuer |
| 20 | + """ |
| 21 | + |
| 22 | + payload_dict = {'aud': audience, |
| 23 | + 'ver': 'scitokens:2.0', |
| 24 | + 'scope': scope, |
| 25 | + 'exp': int(time.time() + 3600), |
| 26 | + 'sub': 'osg-test'} |
| 27 | + payload = json.dumps({'payload': json.dumps({'payload': payload_dict}), |
| 28 | + 'algorithm': 'ES256'}).encode() |
| 29 | + |
| 30 | + req = request.Request('https://demo.scitokens.org/issue', |
| 31 | + data=payload, |
| 32 | + headers=HEADERS) |
| 33 | + |
| 34 | + return request.urlopen(req).read() |
| 35 | + |
| 36 | + |
| 37 | +class TestTokens(osgunittest.OSGTestCase): |
| 38 | + |
| 39 | + def test_01_request_condor_write_scitoken(self): |
| 40 | + core.state['token.condor_write_created'] = False |
| 41 | + core.config['token.condor_write'] = '/tmp/condor_write.scitoken' |
| 42 | + |
| 43 | + core.skip_ok_unless_installed('htcondor-ce', 'condor') |
| 44 | + self.skip_ok_if(core.PackageVersion('condor') >= '8.9.4', |
| 45 | + 'HTCondor version does not support SciToken submission') |
| 46 | + self.skip_ok_if(os.path.exists(core.config['token.condor_write']), |
| 47 | + 'SciToken with HTCondor WRITE already exists') |
| 48 | + |
| 49 | + hostname = core.get_hostname() |
| 50 | + try: |
| 51 | + token = request_demo_scitoken('condor:/READ,condor:/WRITE', audience=f'{hostname}:9619') |
| 52 | + except error.URLError as exc: |
| 53 | + self.fail(f"Failed to request token from demo.scitokens.org:\n{exc}") |
| 54 | + |
| 55 | + ids = (0, 0) |
| 56 | + if core.state['general.user_added']: |
| 57 | + user = pwd.getpwnam(core.options.username) |
| 58 | + ids = (user.pw_uid, user.pw_gid) |
| 59 | + |
| 60 | + files.write(core.config['token.condor_write'], core.to_str(token), backup=False, chown=ids) |
| 61 | + core.state['token.condor_write_created'] = True |
0 commit comments