Skip to content

Commit

Permalink
fix/add/improve unit tests/pylint
Browse files Browse the repository at this point in the history
  • Loading branch information
meeuw committed Aug 11, 2022
1 parent 22184f8 commit 68f8784
Show file tree
Hide file tree
Showing 4 changed files with 291 additions and 858 deletions.
42 changes: 31 additions & 11 deletions aws_credential_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import pynentry
import toml

__version__ = "0.16.0"
__version__ = "0.18.0"

# Restore logger, set by ykman.cli.__main__ import
logging.disable(logging.NOTSET)
Expand All @@ -51,10 +51,11 @@ def __init__(self, awscred, session_token, expiration, label):
self.label = label

def serialize_credentials(self, fmt="json"):
"""
Serialize credentials to fmt (json or shell)
"""
if fmt == "json":
"""
JSON output for aws credential process
"""
# JSON output for aws credential process
return json.dumps(
{
"Version": 1,
Expand All @@ -64,17 +65,16 @@ def serialize_credentials(self, fmt="json"):
"Expiration": self.expiration.isoformat(),
}
)
elif fmt == "shell":
"""
Shell output with AWS_ environment variables
"""
if fmt == "shell":
# Shell output with AWS_ environment variables
return textwrap.dedent(
f"""\
export AWS_ACCESS_KEY_ID={self.awscred.access_key_id}
export AWS_SECRET_ACCESS_KEY={self.awscred.secret_access_key}
export AWS_SESSION_TOKEN={self.session_token}
"""
)
assert False, f"unknown format {fmt}"

@classmethod
def get_cached_session(cls, label):
Expand Down Expand Up @@ -199,6 +199,7 @@ def get_mfa_session_cached(
return mfa_session


# # pylint: disable=too-many-arguments
def get_assume_session(
access_key,
session,
Expand Down Expand Up @@ -254,14 +255,22 @@ def get_assume_session(

assume_session = AWSCredSession.load_from_credentials(
response["Credentials"],
"{}-assume-session-{}".format(access_key.access_key_id, role_arn),
f"{access_key.access_key_id}-assume-session-{role_arn}",
)

return assume_session


def get_assume_session_cached(
access_key, session, role_arn, policy_arns, policy, source_identity, duration_seconds, serial_number=None, token_code=None
access_key,
session,
role_arn,
policy_arns,
policy,
source_identity,
duration_seconds,
serial_number=None,
token_code=None,
):
"""
Get session for assumed role with caching
Expand All @@ -272,7 +281,15 @@ def get_assume_session_cached(

if assume_session is None:
assume_session = get_assume_session(
access_key, session, role_arn, policy_arns, policy, source_identity, duration_seconds, serial_number, token_code
access_key,
session,
role_arn,
policy_arns,
policy,
source_identity,
duration_seconds,
serial_number,
token_code,
)
return assume_session

Expand Down Expand Up @@ -304,6 +321,9 @@ def traverse_config(config, accumulated, flattened):


def parse_config(config):
"""
flatten/fold configurations
"""
flattened = {}
traverse_config(config, {}, flattened)
for pk, pv in flattened.items():
Expand Down
Loading

0 comments on commit 68f8784

Please sign in to comment.