OLD | NEW |
(Empty) | |
| 1 # Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 # Use of this source code is governed under the Apache License, Version 2.0 |
| 3 # that can be found in the LICENSE file. |
| 4 |
| 5 import collections |
| 6 |
| 7 |
| 8 # Parsed value of JSON at path specified by 'SWARMING_AUTH_PARAMS' env var. |
| 9 AuthParams = collections.namedtuple('AuthParams', [ |
| 10 # Dict with HTTP headers to use when calling Swarming backend (specifically). |
| 11 # They identify the bot to the Swarming backend. Ultimately generated by |
| 12 # 'get_authentication_headers' in bot_config.py. |
| 13 'swarming_http_headers', |
| 14 ]) |
| 15 |
| 16 |
| 17 def prepare_auth_params_json(bot): |
| 18 """Returns a dict to put into JSON file at SWARMING_AUTH_PARAMS. |
| 19 |
| 20 This JSON file contains various tokens and configuration parameters that allow |
| 21 Swarming tasks to make authenticated calls to backends using security context |
| 22 of whoever posted the task. |
| 23 |
| 24 The file is managed by bot_main.py (main Swarming bot process) and consumed by |
| 25 task_running.py and its subprocesses that are aware of Swarming bot |
| 26 authentication. |
| 27 |
| 28 It lives it the task work directory. |
| 29 |
| 30 Args: |
| 31 bot: instance of bot.Bot. |
| 32 """ |
| 33 return { |
| 34 'swarming_http_headers': bot.remote.get_authentication_headers(), |
| 35 } |
| 36 |
| 37 |
| 38 def process_auth_params_json(val): |
| 39 """Takes a dict loaded from SWARMING_AUTH_PARAMS and validates it. |
| 40 |
| 41 Args: |
| 42 val: decoded JSON value read from SWARMING_AUTH_PARAMS file. |
| 43 |
| 44 Returns: |
| 45 AuthParams tuple. |
| 46 |
| 47 Raises: |
| 48 ValueError if val has invalid format. |
| 49 """ |
| 50 if not isinstance(val, dict): |
| 51 raise ValueError('Expecting dict, got %r' % (val,)) |
| 52 |
| 53 headers = val.get('swarming_http_headers') or {} |
| 54 if not isinstance(headers, dict): |
| 55 raise ValueError( |
| 56 'Expecting "swarming_http_headers" to be dict, got %r' % (headers,)) |
| 57 |
| 58 # The headers must be ASCII for sure, so don't bother with picking the |
| 59 # correct unicode encoding, default would work. If not, it'll raise |
| 60 # UnicodeEncodeError, which is subclass of ValueError. |
| 61 headers = {str(k): str(v) for k, v in headers.iteritems()} |
| 62 |
| 63 return AuthParams(headers) |
OLD | NEW |