Skip to content

Commit 1a5109d

Browse files
feat(ec2): Allow using ec2 profile as creds (canonical#412)
1 parent faf2d9c commit 1a5109d

File tree

5 files changed

+30
-17
lines changed

5 files changed

+30
-17
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1!10.2.0
1+
1!10.3.0

docs/clouds/ec2.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ The following page documents the AWS EC2 cloud integration in pycloudlib.
44

55
## Credentials
66

7-
To access EC2 requires users to have an access key id and secret access key. These should be set in pycloudlib.toml.
7+
To access EC2 requires users to have either an access key id and secret access key or a profile using SSO.
8+
These should be set in pycloudlib.toml.
89

910
### AWS Dotfile (Deprecated)
1011

@@ -25,13 +26,14 @@ region = us-west-2
2526

2627
### Passed Directly (Deprecated)
2728

28-
The credential and region information can also be provided directly when initializing the EC2 object:
29+
The credential, region,and profile information can also be provided directly when initializing the EC2 object:
2930

3031
```python
3132
ec2 = pycloudlib.EC2(
3233
access_key_id='KEY_VALUE',
3334
secret_access_key='KEY_VALUE',
34-
region='us-west-2'
35+
region='us-west-2',
36+
profile="work",
3537
)
3638
```
3739

pycloudlib.toml.template

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,15 @@ tenant_id = ""
2424
# key_name = "" # Defaults to your username if not set
2525

2626
[ec2]
27-
# Most values can be found in ~/.aws/credentials or ~/.aws/config
28-
access_key_id = "" # in ~/.aws/credentials
29-
secret_access_key = "" # in ~/.aws/credentials
3027
region = "" # in ~/.aws/config
28+
# If 'aws configure sso' has been run, 'profile' should be the only credentials needed
29+
profile = "" # in ~/.aws/config
30+
31+
# If profile is given, these are not necessary.
32+
# They can be found in ~/.aws/credentials or ~/.aws/config
33+
# access_key_id = "" # in ~/.aws/credentials
34+
# secret_access_key = "" # in ~/.aws/credentials
35+
3136
# public_key_path = "/root/id_rsa.pub"
3237
# private_key_path = "" # Defaults to 'public_key_path' without the '.pub'
3338
# key_name = "" # can be found with `aws ec2 describe-key-pairs`
@@ -72,7 +77,7 @@ config_path = "~/.oci/config"
7277
availability_domain = "" # Likely in ~/.oci/oci_cli_rc
7378
compartment_id = "" # Likely in ~/.oci/oci_cli_rc
7479
# region = "us-phoenix-1" # will use region from oci config file if not specified
75-
# profile = "DEFAULT" # will use default profile from oci config file if not specified
80+
# profile = "DEFAULT" # will use default profile from oci config file if not specified
7681
# public_key_path = "~/.ssh/id_rsa.pub"
7782
# private_key_path = "" # Defaults to 'public_key_path' without the '.pub'
7883
# key_name = "" # Defaults to your username if not set

pycloudlib/ec2/cloud.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,7 @@
1111
from pycloudlib.ec2.instance import EC2Instance
1212
from pycloudlib.ec2.util import _get_session, _tag_resource
1313
from pycloudlib.ec2.vpc import VPC
14-
from pycloudlib.errors import (
15-
CloudSetupError,
16-
ImageNotFoundError,
17-
PycloudlibError,
18-
)
14+
from pycloudlib.errors import CloudSetupError, ImageNotFoundError, PycloudlibError
1915
from pycloudlib.util import LTS_RELEASES, UBUNTU_RELEASE_VERSION_MAP
2016

2117
# Images before mantic don't have gp3 disk type
@@ -36,6 +32,7 @@ def __init__(
3632
access_key_id: Optional[str] = None,
3733
secret_access_key: Optional[str] = None,
3834
region: Optional[str] = None,
35+
profile: Optional[str] = None,
3936
):
4037
"""Initialize the connection to EC2.
4138
@@ -50,6 +47,7 @@ def __init__(
5047
access_key_id: user's access key ID
5148
secret_access_key: user's secret access key
5249
region: region to login to
50+
profile: profile to use from ~/.aws/config
5351
"""
5452
super().__init__(
5553
tag,
@@ -59,11 +57,16 @@ def __init__(
5957
)
6058
self._log.debug("logging into EC2")
6159

60+
access_key_id = access_key_id or self.config.get("access_key_id")
61+
secret_access_key = secret_access_key or self.config.get("secret_access_key")
62+
region = region or self.config.get("region")
63+
profile = profile or self.config.get("profile")
6264
try:
6365
session = _get_session(
64-
access_key_id or self.config.get("access_key_id"),
65-
secret_access_key or self.config.get("secret_access_key"),
66-
region or self.config.get("region"),
66+
access_key_id=access_key_id,
67+
secret_access_key=secret_access_key,
68+
region=region,
69+
profile=profile,
6770
)
6871
self.client = session.client("ec2")
6972
self.resource = session.resource("ec2")

pycloudlib/ec2/util.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ def _decode_console_output_as_bytes(parsed, **kwargs):
4444
parsed["OutputBytes"] = base64.b64decode(orig)
4545

4646

47-
def _get_session(access_key_id, secret_access_key, region):
47+
def _get_session(
48+
access_key_id=None, secret_access_key=None, region=None, profile=None
49+
) -> boto3.Session:
4850
"""Get EC2 session.
4951
5052
Args:
@@ -67,4 +69,5 @@ def _get_session(access_key_id, secret_access_key, region):
6769
aws_access_key_id=access_key_id,
6870
aws_secret_access_key=secret_access_key,
6971
region_name=region,
72+
profile_name=profile,
7073
)

0 commit comments

Comments
 (0)