This repository has been archived by the owner on Sep 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from ahayden/alb
Local reverse proxy to internal service
- Loading branch information
Showing
4 changed files
with
128 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#!/usr/bin/env python | ||
|
||
import jwt | ||
import requests | ||
import base64 | ||
import json | ||
import boto3 | ||
|
||
from mod_python import apache | ||
|
||
region = 'us-east-1' | ||
|
||
def headerparserhandler(req): | ||
|
||
jwt = req.headers_in['x-amzn-oidc-data'] #proxy.conf ensures this header exists | ||
|
||
if session_user(jwt) == approved_user(): | ||
return apache.OK | ||
else: | ||
return apache.HTTP_UNAUTHORIZED #the userid claim does not match the userid tag | ||
|
||
def approved_user(): | ||
meta = requests.get('http://169.254.169.254/latest/meta-data/instance-id') | ||
instance_id = meta.text | ||
|
||
ec2 = boto3.resource('ec2',region) | ||
vm = ec2.Instance(instance_id) | ||
|
||
#TODO handle exception on multiple tags in this list | ||
for tags in vm.tags: | ||
if tags["Key"] == 'Protected/AccessApprovedCaller': | ||
approved_caller = tags["Value"] | ||
|
||
return approved_caller.split(':')[1] #return userid portion of tag | ||
|
||
def session_user(encoded_jwt): | ||
|
||
# The x-amzn-oid-data header is a base64-encoded JWT signed by the ALB | ||
# validating the signature of the JWT means the payload is authentic | ||
# per http://docs.aws.amazon.com/elasticloadbalancing/latest/application/listener-authenticate-users.html | ||
# Step 1: Get the key id from JWT headers (the kid field) | ||
#encoded_jwt = headers.dict['x-amzn-oidc-data'] | ||
jwt_headers = encoded_jwt.split('.')[0] | ||
|
||
decoded_jwt_headers = base64.b64decode(jwt_headers) | ||
decoded_jwt_headers = decoded_jwt_headers.decode("utf-8") | ||
decoded_json = json.loads(decoded_jwt_headers) | ||
kid = decoded_json['kid'] | ||
|
||
# Step 2: Get the public key from regional endpoint | ||
url = 'https://public-keys.auth.elb.' + region + '.amazonaws.com/' + kid | ||
req = requests.get(url) | ||
pub_key = req.text | ||
|
||
# Step 3: Get the payload | ||
payload = jwt.decode(encoded_jwt, pub_key, algorithms=['ES256']) | ||
#TODO handle validation errors, this call validates the signature | ||
|
||
return payload['userid'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<VirtualHost _default_:443> | ||
|
||
SSLEngine On | ||
SSLCertificateFile /etc/ssl/certs/ssl-cert-snakeoil.pem | ||
SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key | ||
|
||
ProxyRequests Off | ||
ProxyPreserveHost On | ||
|
||
<LocationMatch /EC2_INSTANCE_ID/> | ||
AddHandler mod_python .py | ||
PythonPath "sys.path+['/usr/lib/cgi-bin', 'usr/lib/python36.zip', '/usr/lib/python3.6', '/usr/lib/python3.6/lib-dynload', '/usr/local/lib/python3.6/dist-packages', '/usr/lib/python3/dist-packages']" | ||
PythonHandler access | ||
PythonHeaderParserHandler access | ||
ProxyPass http://localhost:8787/ | ||
ProxyPassReverse http://localhost:8787/$0 | ||
</LocationMatch> | ||
</VirtualHost> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[Unit] | ||
Description=RStudio Server | ||
After=network-online.target | ||
Wants=network-online.target | ||
|
||
[Service] | ||
Type=forking | ||
PIDFile=/var/run/rstudio-server.pid | ||
User=ubuntu | ||
ExecStart=/usr/lib/rstudio-server/bin/rserver --auth-none 1 | ||
ExecStop=/usr/bin/killall -TERM rserver | ||
KillMode=none | ||
Restart=on-failure | ||
|
||
[Install] | ||
WantedBy=multi-user.target |