-
-
Notifications
You must be signed in to change notification settings - Fork 652
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
s3: Load aws env vars from local environment #22008
Changes from 2 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 |
---|---|---|
|
@@ -9,6 +9,8 @@ | |
from urllib.parse import urlsplit | ||
|
||
from pants.engine.download_file import URLDownloadHandler | ||
from pants.engine.env_vars import EnvironmentVars, EnvironmentVarsRequest | ||
from pants.engine.environment import ChosenLocalEnvironmentName, EnvironmentName | ||
from pants.engine.fs import Digest, NativeDownloadFile | ||
from pants.engine.internals.native_engine import FileDigest | ||
from pants.engine.internals.selectors import Get | ||
|
@@ -29,9 +31,12 @@ class AWSCredentials: | |
|
||
|
||
@rule | ||
async def access_aws_credentials() -> AWSCredentials: | ||
async def access_aws_credentials( | ||
local_environment_name: ChosenLocalEnvironmentName, | ||
) -> AWSCredentials: | ||
try: | ||
from botocore import credentials, session | ||
from botocore import credentials | ||
from botocore import session as boto_session | ||
except ImportError: | ||
logger.warning( | ||
softwrap( | ||
|
@@ -48,7 +53,44 @@ async def access_aws_credentials() -> AWSCredentials: | |
) | ||
raise | ||
|
||
session = session.get_session() | ||
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. was previously shadowing the import so I updated the import to an alias to avoid confusion |
||
env_vars = await Get( | ||
EnvironmentVars, | ||
{ | ||
EnvironmentVarsRequest( | ||
[ | ||
"AWS_PROFILE", | ||
"AWS_REGION", | ||
"AWS_ACCESS_KEY_ID", | ||
"AWS_SECRET_ACCESS_KEY", | ||
"AWS_SESSION_TOKEN", | ||
] | ||
): EnvironmentVarsRequest, | ||
local_environment_name.val: EnvironmentName, | ||
}, | ||
) | ||
|
||
session = boto_session.Session() | ||
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. botocore session reference https://github.com/boto/botocore/blob/develop/botocore/session.py |
||
|
||
aws_profile = env_vars.get("AWS_PROFILE") | ||
if aws_profile: | ||
session.set_config_variable("profile", aws_profile) | ||
|
||
aws_region = env_vars.get("AWS_REGION") | ||
if aws_region: | ||
session.set_config_variable("region", aws_region) | ||
|
||
aws_access_key = env_vars.get("AWS_ACCESS_KEY_ID") | ||
aws_secret_key = env_vars.get("AWS_SECRET_ACCESS_KEY") | ||
aws_session_token = env_vars.get("AWS_SESSION_TOKEN") | ||
if aws_access_key and aws_secret_key: | ||
session.set_credentials( | ||
credentials.Credentials( | ||
access_key=aws_access_key, | ||
secret_key=aws_secret_key, | ||
token=aws_session_token, | ||
) | ||
) | ||
|
||
creds = credentials.create_credential_resolver(session).load_credentials() | ||
|
||
return AWSCredentials(creds) | ||
|
@@ -136,7 +178,7 @@ class DownloadS3AuthorityVirtualHostedStyleURL(URLDownloadHandler): | |
|
||
@rule | ||
async def download_file_from_virtual_hosted_s3_authority( | ||
request: DownloadS3AuthorityVirtualHostedStyleURL, aws_credentials: AWSCredentials | ||
request: DownloadS3AuthorityVirtualHostedStyleURL, | ||
) -> Digest: | ||
split = urlsplit(request.url) | ||
bucket, aws_netloc = split.netloc.split(".", 1) | ||
|
@@ -157,9 +199,7 @@ class DownloadS3AuthorityPathStyleURL(URLDownloadHandler): | |
|
||
|
||
@rule | ||
async def download_file_from_path_s3_authority( | ||
request: DownloadS3AuthorityPathStyleURL, aws_credentials: AWSCredentials | ||
) -> Digest: | ||
async def download_file_from_path_s3_authority(request: DownloadS3AuthorityPathStyleURL) -> Digest: | ||
split = urlsplit(request.url) | ||
_, bucket, key = split.path.split("/", 2) | ||
return await Get( | ||
|
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.
I'm not totally happy with these tests as I couldn't get the assertions working that the exact same object is returned, but in local testing with pantsd it appears to work correctly