Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(198)

Side by Side Diff: appengine/swarming/swarming_bot/bot_code/remote_client.py

Issue 2024313003: Send authorization headers when calling Swarming backend. (Closed) Base URL: https://chromium.googlesource.com/external/github.com/luci/luci-py@master
Patch Set: Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 threading
6
7 from utils import net
8
9
10 # RemoteClient will attempt to refresh the authentication headers once they are
11 # this close to the expiration.
12 AUTH_HEADERS_EXPIRATION_SEC = 6*60
13
14
15 class InitializationError(Exception):
16 """Raised by RemoteClient.initialize on fatal errors."""
17 def __init__(self, last_error):
18 super(InitializationError, self).__init__('Failed to grab auth headers')
19 self.last_error = last_error
20
21
22 class RemoteClient(object):
23 """RemoteClient knows how to make authenticated calls to the backend.
24
25 It also holds in-memory cache of authentication headers and periodically
26 refreshes them (by calling supplied callback, that usually is implemented in
27 terms of bot_config.get_authentication_headers() function).
28
29 If the callback is None, skips authentication (this is used during initial
30 stages of the bot bootstrap).
31 """
32
33 def __init__(self, server, auth_headers_callback):
34 self._server = server
35 self._auth_headers_callback = auth_headers_callback
36 self._uses_auth = False
37 self._lock = threading.Lock()
38
39 def initialize(self, quit_bit):
40 """Grabs initial auth headers, retrying on errors a bunch of times.
41
42 Raises InitializationError if all attempts fail. Aborts attempts and returns
43 if quit_bit is set to signaled state.
44 """
45 # TODO(vadimsh): Implement.
46
47 @property
48 def uses_auth(self):
49 """Returns True if get_authentication_headers() returns some headers.
50
51 If bot_config.get_authentication_headers() is not implement it will return
52 False.
53 """
54 return self._uses_auth
55
56 def get_authentication_headers(self):
57 """Returns a dict with the headers, refreshing them if necessary.
58
59 Will always return a dict (perhaps empty if no auth headers are provided by
60 the callback).
61 """
62 with self._lock:
63 # TODO(vadimsh): Implement.
64 return {}
65
66 def url_read_json(self, url_path, data=None):
67 """Does POST (if data is not None) or GET request to a JSON endpoint."""
68 return net.url_read_json(
69 self._server + url_path,
70 data=data,
71 headers=self.get_authentication_headers(),
72 timeout=AUTH_HEADERS_EXPIRATION_SEC,
73 follow_redirects=False)
74
75 def url_retrieve(self, filepath, url_path):
76 """Fetches the file from the given URL path on the server."""
77 return net.url_retrieve(
78 filepath,
79 self._server + url_path,
80 headers=self.get_authentication_headers(),
81 timeout=AUTH_HEADERS_EXPIRATION_SEC)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698