Index: scripts/slave/recipe_modules/service_account/api.py |
diff --git a/scripts/slave/recipe_modules/service_account/api.py b/scripts/slave/recipe_modules/service_account/api.py |
new file mode 100644 |
index 0000000000000000000000000000000000000000..0d018cd061ec53ebbdb60804cd31681b28ca0ab3 |
--- /dev/null |
+++ b/scripts/slave/recipe_modules/service_account/api.py |
@@ -0,0 +1,42 @@ |
+# Copyright 2016 The Chromium Authors. All rights reserved. |
+# Use of this source code is governed by a BSD-style license that can be |
+# found in the LICENSE file. |
+ |
+"""API for generating oauth2 tokens from locally stored secrets. |
+ |
+This is a thin wrapper over the authutil go executable, which itself calls |
+https://github.com/luci/luci-go/blob/master/client/authcli/authcli.go |
+""" |
+ |
+from recipe_engine import recipe_api |
+ |
+ |
+class ServiceAccountApi(recipe_api.RecipeApi): |
+ |
+ def _config_defaults(self): |
+ if self.m.platform.is_win: |
+ self.set_config('service_account_windows') |
+ else: |
+ self.set_config('service_account_default') |
+ |
+ def get_token(self, account): |
+ if self.c is None: |
+ self._config_defaults() |
+ account_file = self.m.path.join(self.c.accounts_path, |
+ 'service-account-%s.json' % account) |
+ try: |
+ # TODO: authutil is to be deployed using cipd. |
+ step_result = self.m.step('get access token', |
+ [self.c.authutil_path, 'token', |
+ '-service-account-json=' + account_file], |
+ stdout=self.m.raw_io.output()) |
+ except self.m.step.StepFailure as ex: |
+ if not self.m.path.exists(self.c.authutil_path): |
+ ex.result.presentation.logs['Authutil not found'] = [ |
+ 'The authutil binary was not found at the default location.', |
+ '', |
+ 'Build the following go module: infra/go/infra/tools/authutil', |
+ 'and deploy it to: ' + self.c.authutil_path ] |
+ raise |
+ |
+ return step_result.stdout.strip() |