Skip to content
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

feat(ec2): Allow using ec2 profile as creds #412

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1!9.0.0
1!9.1.0
13 changes: 9 additions & 4 deletions pycloudlib.toml.template
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,15 @@ tenant_id = ""
# key_name = "" # Defaults to your username if not set

[ec2]
# Most values can be found in ~/.aws/credentials or ~/.aws/config
access_key_id = "" # in ~/.aws/credentials
secret_access_key = "" # in ~/.aws/credentials
region = "" # in ~/.aws/config
# If 'aws configure sso' has been run, 'profile' should be the only credentials needed
profile = "" # in ~/.aws/config

# With modern SSO, these should no longer be necessary.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: With modern SSO sounds a bit pretentious and likely prone to age bad. Something like the following?

Suggested change
# With modern SSO, these should no longer be necessary.
# If profile is given, these are not necessary.

# They can be found in ~/.aws/credentials or ~/.aws/config
# access_key_id = "" # in ~/.aws/credentials
# secret_access_key = "" # in ~/.aws/credentials

# public_key_path = "/root/id_rsa.pub"
# private_key_path = "" # Defaults to 'public_key_path' without the '.pub'
# key_name = "" # can be found with `aws ec2 describe-key-pairs`
Expand Down Expand Up @@ -72,7 +77,7 @@ config_path = "~/.oci/config"
availability_domain = "" # Likely in ~/.oci/oci_cli_rc
compartment_id = "" # Likely in ~/.oci/oci_cli_rc
# region = "us-phoenix-1" # will use region from oci config file if not specified
# profile = "DEFAULT" # will use default profile from oci config file if not specified
# profile = "DEFAULT" # will use default profile from oci config file if not specified
# public_key_path = "~/.ssh/id_rsa.pub"
# private_key_path = "" # Defaults to 'public_key_path' without the '.pub'
# key_name = "" # Defaults to your username if not set
Expand Down
15 changes: 12 additions & 3 deletions pycloudlib/ec2/cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ def __init__(
access_key_id: Optional[str] = None,
secret_access_key: Optional[str] = None,
region: Optional[str] = None,
profile: Optional[str] = None,
):
"""Initialize the connection to EC2.

Expand All @@ -50,6 +51,7 @@ def __init__(
access_key_id: user's access key ID
secret_access_key: user's secret access key
region: region to login to
profile: profile to use from ~/.aws/config
"""
super().__init__(
tag,
Expand All @@ -59,11 +61,18 @@ def __init__(
)
self._log.debug("logging into EC2")

access_key_id = access_key_id or self.config.get("access_key_id")
secret_access_key = secret_access_key or self.config.get(
"secret_access_key"
)
region = region or self.config.get("region")
profile = profile or self.config.get("profile")
try:
session = _get_session(
access_key_id or self.config.get("access_key_id"),
secret_access_key or self.config.get("secret_access_key"),
region or self.config.get("region"),
access_key_id=access_key_id,
secret_access_key=secret_access_key,
region=region,
profile=profile,
)
self.client = session.client("ec2")
self.resource = session.resource("ec2")
Expand Down
5 changes: 4 additions & 1 deletion pycloudlib/ec2/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ def _decode_console_output_as_bytes(parsed, **kwargs):
parsed["OutputBytes"] = base64.b64decode(orig)


def _get_session(access_key_id, secret_access_key, region):
def _get_session(
access_key_id=None, secret_access_key=None, region=None, profile=None
) -> boto3.Session:
"""Get EC2 session.

Args:
Expand All @@ -69,4 +71,5 @@ def _get_session(access_key_id, secret_access_key, region):
aws_access_key_id=access_key_id,
aws_secret_access_key=secret_access_key,
region_name=region,
profile_name=profile,
)
Loading