-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from tomplus/feat/async_gcp_token
feat: async refresh token, remove synchronous libs
- Loading branch information
Showing
12 changed files
with
236 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import asyncio.subprocess | ||
import json | ||
import shlex | ||
from types import SimpleNamespace | ||
|
||
|
||
async def google_auth_credentials(provider): | ||
|
||
if 'cmd-path' not in provider or 'cmd-args' not in provider: | ||
raise ValueError('GoogleAuth via gcloud is supported! Values for cmd-path, cmd-args are required.') | ||
|
||
cmd = shlex.split(' '.join((provider['cmd-path'], provider['cmd-args']))) | ||
cmd_exec = asyncio.create_subprocess_exec(*cmd, | ||
stdin=None, | ||
stdout=asyncio.subprocess.PIPE, | ||
stderr=asyncio.subprocess.PIPE) | ||
proc = await cmd_exec | ||
|
||
data = await proc.stdout.read() | ||
data = data.decode('ascii').rstrip() | ||
data = json.loads(data) | ||
|
||
await proc.wait() | ||
return SimpleNamespace( | ||
token=data['credential']['access_token'], | ||
expiry=data['credential']['token_expiry'] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Copyright 2016 The Kubernetes Authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from asynctest import TestCase, main | ||
|
||
from .google_auth import google_auth_credentials | ||
|
||
|
||
class TestGoogleAuth(TestCase): | ||
|
||
async def test_google_auth_credentials(self): | ||
|
||
provider = { | ||
'cmd-path': '/bin/echo', | ||
'cmd-args': '{\\"credential\\": {\\"access_token\\": \\"token\\", ' | ||
'\\"token_expiry\\": \\"2001.01.01T00:00:00Z\\"}}' | ||
} | ||
|
||
ret = await google_auth_credentials(provider) | ||
|
||
self.assertEqual(ret.token, 'token') | ||
self.assertEqual(ret.expiry, '2001.01.01T00:00:00Z') | ||
|
||
async def test_google_auth_credentials_exception(self): | ||
|
||
with self.assertRaisesRegex(ValueError, "cmd-path, cmd-args are required."): | ||
await google_auth_credentials({}) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.