|
| 1 | +from time import time |
| 2 | +from datetime import timedelta |
| 3 | + |
| 4 | +import jwt |
| 5 | +import requests |
| 6 | +from requests.auth import AuthBase |
| 7 | + |
| 8 | +from mesoshttp.exception import ACSException |
| 9 | + |
| 10 | + |
| 11 | +class DCOSServiceAuth(AuthBase): |
| 12 | + """Attaches a token to Request object that will allow requests to be made through DCOS Admin Router.""" |
| 13 | + |
| 14 | + def __init__(self, secret, verify=False): |
| 15 | + """Take a DCOS service account secret and breaks out components needed for login flow |
| 16 | +
|
| 17 | + :param secret: dict that must include uid, private_key, scheme and login_endpoint keys |
| 18 | + :param verify: `False` or path to a PEM encoded trust bundle |
| 19 | + :return: :class:`DCOSServiceAuth` |
| 20 | + """ |
| 21 | + |
| 22 | + # Service account |
| 23 | + self._user = secret['uid'] |
| 24 | + self._key = secret['private_key'] |
| 25 | + self._scheme = secret['scheme'] |
| 26 | + self._acs_endpoint = secret['login_endpoint'] |
| 27 | + self._verify = verify |
| 28 | + |
| 29 | + self._expiration = time() |
| 30 | + self._token = None |
| 31 | + |
| 32 | + @property |
| 33 | + def principal(self): |
| 34 | + """Get the service account user value which will match the principal value that must be set in Mesos |
| 35 | +
|
| 36 | + :return: service account user name |
| 37 | + """ |
| 38 | + return self._user |
| 39 | + |
| 40 | + @property |
| 41 | + def token(self): |
| 42 | + """Get the authentication token for making API calls through DCOS AdminRouter |
| 43 | +
|
| 44 | + :return: token used for authentication |
| 45 | + """ |
| 46 | + |
| 47 | + if time() > self._expiration or self._token is None: |
| 48 | + self._acs_login() |
| 49 | + |
| 50 | + return self._token |
| 51 | + |
| 52 | + def _generate_token(self, uid, private_key, scheme='RS256', expiry_seconds=180): |
| 53 | + """Generate a JWT for ACS login |
| 54 | +
|
| 55 | + Updates the instance variable to track expiration |
| 56 | +
|
| 57 | + :param private_key: Service accounts PEM encoded private key |
| 58 | + :param scheme: algorithm used to encode JWT |
| 59 | + :param expiry_seconds: how many seconds authentication token will be good for |
| 60 | + :return: login token |
| 61 | + """ |
| 62 | + expire_time = time() + float(timedelta(seconds=expiry_seconds).seconds) |
| 63 | + token = jwt.encode({'exp': expire_time, 'uid': uid}, private_key, algorithm=scheme) |
| 64 | + self._expiration = expire_time |
| 65 | + return token |
| 66 | + |
| 67 | + def _acs_login(self): |
| 68 | + """Login to ACS and set an authentication token on the instance |
| 69 | + """ |
| 70 | + |
| 71 | + payload = {'uid': self._user, 'token': self._generate_token(self._user, self._key, self._scheme)} |
| 72 | + response = requests.post(self._acs_endpoint, json=payload, verify=self._verify) |
| 73 | + |
| 74 | + if response.status_code != 200: |
| 75 | + raise ACSException('Unable to authenticate against DCOS ACS: {} {}'.format(response.status_code, |
| 76 | + response.text)) |
| 77 | + self._token = response.json()['token'] |
| 78 | + |
| 79 | + def __call__(self, r): |
| 80 | + r.headers['Authorization'] = 'token={}'.format(self.token) |
| 81 | + return r |
| 82 | + |
0 commit comments