-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
38 lines (33 loc) · 1.12 KB
/
server.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
from flask import Flask
from flask_httpauth import HTTPTokenAuth
import secretscache as sc
# Initialize some globals
api_token_key = 'RotationExample'
aws_region = 'us-east-1'
# Initialize a Flask app
app = Flask(__name__)
auth = HTTPTokenAuth(scheme='Bearer')
# Load up our secrets on boot
s = sc.SecretsCache([api_token_key], aws_region)
@auth.verify_token
def verify_token(token):
print(f'Using token: {token} for demonstration purposes only. Don\'t do this in production!')
# Try the most current secret we have in memory
if token == s.secret(api_token_key):
print(f'Current token match!')
return True
elif token == s.secret(api_token_key, 'Previous'):
print(f'Previous token match!')
return True
else:
# If current and previous fail, go refresh secrets from Parameter Store
# Retry current which may now be new if it was updated
print(f'Token mis-match. Refreshing token')
s.refresh()
if token == s.secret(api_token_key):
return True
return False
@app.route('/')
@auth.login_required
def main():
return 'Open Sesame!'