forked from phess/purl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
purl
42 lines (34 loc) · 1.38 KB
/
purl
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
#!/usr/bin/env python2
## Emulate curl, but in python.
import requests
import os
import sys
CA_CERT='/etc/rhsm/ca/redhat-uep.pem'
ENTITLEMENT_DIR = '/etc/pki/entitlement'
def find_client_certpairs(where = ENTITLEMENT_DIR):
dircontents = os.listdir(where)
pemfiles = [ f.strip('.pem') for f in dircontents if f.endswith('.pem') ]
if len(pemfiles) % 2 != 0:
print('*** Unexpected: odd number of pemfiles in', where)
pemkeys = [ k for k in pemfiles if k.endswith('-key') ]
pemcerts = [ c for c in pemfiles if not c.endswith('-key') ]
pempairs = [ (c,c+'-key') for c in pemcerts if c+'-key' in pemkeys ]
return pempairs
URL = sys.argv[-1]
certpairs = find_client_certpairs()
multi_pairs = len(certpairs) > 1
if multi_pairs:
print('Multiple certificate-key pairs found under %s, will try to access the URL once with each pair.'%ENTITLEMENT_DIR)
for cert,key in certpairs:
cert = ENTITLEMENT_DIR + '/' + cert + '.pem'
key = ENTITLEMENT_DIR + '/' + key + '.pem'
print('Trying to access the URL below with the parameters below:')
print(' URL: %s' % URL)
print(' CA certificate: %s' % CA_CERT)
print(' Client certificate: %s' % cert)
print(' Client key: %s' % key)
print('\n=> Response is...')
resp = requests.get(URL, cert=(cert,key), verify=CA_CERT)
print(resp)
print '\n=> Response contents are...'
print(resp.text)