-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
63 lines (49 loc) · 1.96 KB
/
client.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
import base64
import hashlib
import json
import requests
from ecdsa import NIST256p, SigningKey
from ecdsa.util import randrange_from_seed__trytryagain
###############################################################################
# Generate signing key
#
# Create a signing key from a known seed to make testing easier. The public
# key (aka verification key) is pasted into the envoy.yaml config.
###############################################################################
def make_key(key_seed: bytes):
secexp = randrange_from_seed__trytryagain(key_seed, NIST256p.order)
return SigningKey.from_secret_exponent(secexp, curve=NIST256p)
seed = b'00000000000000000000000000000001'
private_key = make_key(seed)
public_key_pem = private_key.verifying_key.to_pem().decode('utf8')
print(f'public key = \n{public_key_pem}')
###############################################################################
# Create and sign JWT
###############################################################################
header = {
"alg": "ES256",
"typ": "JWT",
"kid": "1234",
"signer": "arn:aws:elasticloadbalancing:us-east-2:1234567890:loadbalancer/app/foobar"
}
claims = {
"sub": "1234567890",
"name": "Slava",
"email": "[email protected]"
}
jwt = '{}.{}'.format(
base64.standard_b64encode(json.dumps(header).encode('utf8')).decode('utf8'),
base64.standard_b64encode(json.dumps(claims).encode('utf8')).decode('utf8')
)
signature = private_key.sign(jwt.encode('utf8'), hashfunc=hashlib.sha256)
signature = base64.urlsafe_b64encode(signature).decode('utf8')
jwt = f'{jwt}.{signature}'
###############################################################################
# Make request to Envoy
###############################################################################
headers = {
'x-amzn-oidc-data': jwt
}
print(f'request headers: {headers}')
resp = requests.get(url='http://localhost:18000', headers=headers)
print(f'response: {resp.status_code} {resp.text}')