-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add support for Process Credentials Provider #194
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -572,6 +572,68 @@ async def fetch_metadata(self, http: HttpImplementation) -> Metadata: | |||||
) | ||||||
|
||||||
|
||||||
@dataclass | ||||||
class ProcessCredentialsError(Exception): | ||||||
reason: str | ||||||
return_code: int | None | ||||||
stdout: bytes | ||||||
stderr: bytes | ||||||
|
||||||
|
||||||
@dataclass | ||||||
class ProcessCredentials(MetadataCredentials): | ||||||
command: list[str] | ||||||
|
||||||
async def fetch_metadata(self, http: HttpImplementation) -> Metadata: | ||||||
process = await asyncio.create_subprocess_exec( | ||||||
*self.command, | ||||||
stdout=asyncio.subprocess.PIPE, | ||||||
stderr=asyncio.subprocess.PIPE, | ||||||
stdin=asyncio.subprocess.PIPE, | ||||||
) | ||||||
stdout, stderr = await process.communicate(b"") | ||||||
if process.returncode != 0: | ||||||
raise ProcessCredentialsError( | ||||||
f"Process terminated with non-zero return code {process.returncode}: {try_decode(stderr)}", | ||||||
process.returncode, | ||||||
stdout, | ||||||
stderr, | ||||||
) | ||||||
try: | ||||||
data = json.loads(stdout) | ||||||
except json.JSONDecodeError: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmmm I can't recall if they fixed this or not. Back in the day, |
||||||
raise ProcessCredentialsError( | ||||||
f"Process returned non-JSON string: {try_decode(stdout)}", | ||||||
process.returncode, | ||||||
stdout, | ||||||
stderr, | ||||||
) | ||||||
if data["version"] != 1: | ||||||
raise ProcessCredentialsError( | ||||||
f"Process returned unsupported version: {data['version']}", | ||||||
process.returncode, | ||||||
stdout, | ||||||
stderr, | ||||||
) | ||||||
|
||||||
key = Key( | ||||||
data["access_key_id"], | ||||||
data["secret_access_key"], | ||||||
data.get("session_token", None), | ||||||
) | ||||||
return Metadata(key, parse_amazon_timestamp(data["expiration"])) | ||||||
|
||||||
def is_disabled(self) -> bool: | ||||||
return False | ||||||
|
||||||
|
||||||
def try_decode(data: bytes) -> str: | ||||||
try: | ||||||
return data.decode() | ||||||
except: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
unless the intention is to swallow e.g. SystemExit if it happens at just the right time. |
||||||
return repr(data) | ||||||
|
||||||
|
||||||
class TooManyRetries(Exception): | ||||||
pass | ||||||
|
||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import sys | ||
|
||
|
||
def main() -> None: | ||
if sys.argv[1] == "-x": | ||
sys.exit(-1) | ||
else: | ||
sys.stdout.write(sys.argv[1]) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would block if the subprocess produces too much data.
Arguably there should be a default timeout too, although, perhaps in this particular library, the caller has a timeout and will cancel
fetch_metadata
.A simple fix could look smth like:
I dunno if it's worth it in practice.