-
Notifications
You must be signed in to change notification settings - Fork 0
Add S3 access validation script #1
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
Open
chengjianshia
wants to merge
1
commit into
main
Choose a base branch
from
codex/create-script-for-amazon-s3-login
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| #!/usr/bin/env python3 | ||
| """Simple S3 login validation tool. | ||
|
|
||
| This script verifies access to an S3 bucket using AWS credentials provided | ||
| via command-line flags, environment variables, or an AWS profile. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import argparse | ||
| import os | ||
| import sys | ||
| from typing import Optional | ||
|
|
||
|
|
||
| def build_parser() -> argparse.ArgumentParser: | ||
| parser = argparse.ArgumentParser( | ||
| description="Validate S3 bucket access with provided AWS credentials.", | ||
| ) | ||
| parser.add_argument("--bucket", required=True, help="Target S3 bucket name") | ||
| parser.add_argument("--region", default=os.environ.get("AWS_REGION"), help="AWS region") | ||
| parser.add_argument("--profile", help="AWS profile name (uses shared config/credentials)") | ||
| parser.add_argument("--access-key", help="AWS access key ID") | ||
| parser.add_argument("--secret-key", help="AWS secret access key") | ||
| parser.add_argument("--session-token", help="AWS session token") | ||
| parser.add_argument( | ||
| "--prefix", | ||
| default="", | ||
| help="Optional prefix to test list access (default: root)", | ||
| ) | ||
| return parser | ||
|
|
||
|
|
||
| def resolve_credentials(args: argparse.Namespace) -> dict[str, Optional[str]]: | ||
| return { | ||
| "aws_access_key_id": args.access_key or os.environ.get("AWS_ACCESS_KEY_ID"), | ||
| "aws_secret_access_key": args.secret_key or os.environ.get("AWS_SECRET_ACCESS_KEY"), | ||
| "aws_session_token": args.session_token or os.environ.get("AWS_SESSION_TOKEN"), | ||
| "region_name": args.region, | ||
| } | ||
|
|
||
|
|
||
| def main() -> int: | ||
| parser = build_parser() | ||
| args = parser.parse_args() | ||
|
|
||
| try: | ||
| import boto3 | ||
| from botocore.exceptions import BotoCoreError, ClientError | ||
| except ImportError: | ||
| print("boto3 is required. Install with: pip install boto3", file=sys.stderr) | ||
| return 1 | ||
|
|
||
| session_kwargs = {} | ||
| if args.profile: | ||
| session_kwargs["profile_name"] = args.profile | ||
|
|
||
| credentials = resolve_credentials(args) | ||
| session = boto3.Session(**{k: v for k, v in credentials.items() if v}) | ||
| if session_kwargs: | ||
| session = boto3.Session(**session_kwargs, **{k: v for k, v in credentials.items() if v}) | ||
|
|
||
| s3 = session.client("s3", region_name=credentials.get("region_name")) | ||
|
|
||
| try: | ||
| s3.head_bucket(Bucket=args.bucket) | ||
| if args.prefix is not None: | ||
| s3.list_objects_v2(Bucket=args.bucket, Prefix=args.prefix, MaxKeys=1) | ||
| except (ClientError, BotoCoreError) as exc: | ||
| print(f"Access check failed: {exc}", file=sys.stderr) | ||
| return 1 | ||
|
|
||
| print(f"Access verified for bucket '{args.bucket}'.") | ||
| return 0 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| raise SystemExit(main()) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
When
--profileis supplied, the code still merges in credentials from flags and environment (resolve_credentials), and then passes them alongsideprofile_nametoboto3.Session. In boto3, explicit credential arguments take precedence, so if a user hasAWS_ACCESS_KEY_ID/SECRETexported (a common case in CI or local shells), the profile is silently ignored and the access check is performed against the wrong account. This defeats the purpose of--profileand can lead to false positives/negatives when validating bucket access for another profile. Consider omitting env/flag credentials when--profileis set (or explicitly erroring on conflicts).Useful? React with 👍 / 👎.