2015-10-26 23:06:05 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2016-04-29 15:51:22 +00:00
|
|
|
from datetime import datetime, timedelta
|
2016-04-21 16:40:16 +00:00
|
|
|
from urlparse import urlunparse
|
|
|
|
|
|
|
|
from jinja2 import Template
|
2016-04-29 15:51:22 +00:00
|
|
|
from cachetools import lru_cache
|
2015-10-26 23:06:05 +00:00
|
|
|
|
2016-05-02 19:29:31 +00:00
|
|
|
import release
|
|
|
|
import os.path
|
2016-04-29 15:51:22 +00:00
|
|
|
|
2015-10-26 23:06:05 +00:00
|
|
|
from app import app
|
|
|
|
from data.model.release import set_region_release
|
|
|
|
from util.config.database import sync_database_with_config
|
2016-04-27 19:37:48 +00:00
|
|
|
from util.generatepresharedkey import generate_key
|
2015-10-26 23:06:05 +00:00
|
|
|
|
|
|
|
|
2016-04-29 15:51:22 +00:00
|
|
|
@lru_cache(maxsize=1)
|
2016-04-29 01:10:28 +00:00
|
|
|
def get_audience():
|
2016-04-28 19:29:21 +00:00
|
|
|
audience = app.config.get('JWTPROXY_AUDIENCE')
|
2016-04-29 01:10:28 +00:00
|
|
|
|
|
|
|
if audience:
|
|
|
|
return audience
|
|
|
|
|
|
|
|
scheme = app.config.get('PREFERRED_URL_SCHEME')
|
|
|
|
hostname = app.config.get('SERVER_HOSTNAME')
|
|
|
|
|
|
|
|
# hostname includes port, use that
|
|
|
|
if ':' in hostname:
|
|
|
|
return urlunparse((scheme, hostname, '', '', '', ''))
|
|
|
|
|
|
|
|
# no port, guess based on scheme
|
|
|
|
if scheme == 'https':
|
|
|
|
port = '443'
|
|
|
|
else:
|
|
|
|
port = '80'
|
|
|
|
|
|
|
|
return urlunparse((scheme, hostname + ':' + port, '', '', '', ''))
|
|
|
|
|
|
|
|
|
2016-05-02 19:29:31 +00:00
|
|
|
def setup_jwt_proxy():
|
2016-04-29 15:51:22 +00:00
|
|
|
"""
|
2016-05-02 19:29:31 +00:00
|
|
|
Creates a service key for quay to use in the jwtproxy and generates the JWT proxy configuration.
|
2016-04-29 15:51:22 +00:00
|
|
|
"""
|
2016-05-02 19:29:31 +00:00
|
|
|
if os.path.exists('conf/jwtproxy_conf.yaml'):
|
|
|
|
# Proxy is already setup.
|
|
|
|
return
|
|
|
|
|
|
|
|
# Generate the key for this Quay instance to use.
|
2016-04-29 15:51:22 +00:00
|
|
|
minutes_until_expiration = app.config.get('QUAY_SERVICE_KEY_EXPIRATION', 120)
|
2016-04-29 16:32:47 +00:00
|
|
|
expiration = datetime.now() + timedelta(minutes=minutes_until_expiration)
|
2016-05-02 19:29:31 +00:00
|
|
|
quay_key, quay_key_id = generate_key('quay', get_audience(), expiration_date=expiration)
|
2016-04-29 15:51:22 +00:00
|
|
|
|
2016-05-02 19:29:31 +00:00
|
|
|
with open('conf/quay.kid', mode='w') as f:
|
2016-04-29 16:20:30 +00:00
|
|
|
f.truncate(0)
|
2016-05-02 19:29:31 +00:00
|
|
|
f.write(quay_key_id)
|
2016-04-29 16:20:30 +00:00
|
|
|
|
2016-05-02 19:29:31 +00:00
|
|
|
with open('conf/quay.pem', mode='w') as f:
|
2016-04-29 15:51:22 +00:00
|
|
|
f.truncate(0)
|
|
|
|
f.write(quay_key.exportKey())
|
|
|
|
|
2016-05-02 19:29:31 +00:00
|
|
|
# Generate the JWT proxy configuration.
|
2016-04-29 01:10:28 +00:00
|
|
|
audience = get_audience()
|
2016-04-21 16:40:16 +00:00
|
|
|
registry = audience + '/keys'
|
2016-05-02 19:29:31 +00:00
|
|
|
security_issuer = app.config.get('SECURITY_SCANNER_ISSUER_NAME', 'security_scanner')
|
2016-04-21 16:40:16 +00:00
|
|
|
|
2016-05-02 19:29:31 +00:00
|
|
|
with open("conf/jwtproxy_conf.yaml.jnj") as f:
|
2016-04-21 16:40:16 +00:00
|
|
|
template = Template(f.read())
|
|
|
|
rendered = template.render(
|
|
|
|
audience=audience,
|
2016-04-27 19:37:48 +00:00
|
|
|
registry=registry,
|
2016-05-02 19:29:31 +00:00
|
|
|
key_id=quay_key_id,
|
|
|
|
security_issuer=security_issuer,
|
2016-04-21 16:40:16 +00:00
|
|
|
)
|
|
|
|
|
2016-05-02 19:29:31 +00:00
|
|
|
with open('conf/jwtproxy_conf.yaml', 'w') as f:
|
2016-04-21 16:40:16 +00:00
|
|
|
f.write(rendered)
|
|
|
|
|
|
|
|
|
2015-10-26 23:06:05 +00:00
|
|
|
def main():
|
|
|
|
if app.config.get('SETUP_COMPLETE', False):
|
|
|
|
sync_database_with_config(app.config)
|
2016-05-02 19:29:31 +00:00
|
|
|
setup_jwt_proxy()
|
2015-10-26 23:06:05 +00:00
|
|
|
|
|
|
|
# Record deploy
|
|
|
|
if release.REGION and release.GIT_HEAD:
|
|
|
|
set_region_release(release.SERVICE, release.REGION, release.GIT_HEAD)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|