Details
| ID | AWS - S3 |
|---|---|
| Provider | Amazon |
| Category | IT Services |
| From Version | 5.0.0 |
| Docker Image | demisto/boto3py3:1.0.0.10221838 |
| Supported Modules | Agentix XSIAM |
README
Amazon Web Services Simple Storage Service (S3).
This integration was integrated and tested with API Version 2012-11-05.
For detailed instructions about setting up authentication, see: AWS Integrations - Authentication.
Configure AWS - S3 in Cortex
| Parameter | Description | Required |
|---|---|---|
| roleArn | Role Arn | False |
| roleSessionName | Role Session Name | False |
| defaultRegion | AWS Default Region | False |
| sessionDuration | Role Session Duration | False |
| access_key | Access Key | False |
| secret_key | Secret Key | False |
| timeout | The time in seconds till a timeout exception is reached. You can specify just the read timeout (for example 60) or also the connect timeout followed after a comma (for example 60,10). If a connect timeout is not specified, a default of 10 second will be used. | False |
| retries | The maximum number of retry attempts when connection or throttling errors are encountered. Set to 0 to disable retries. The default value is 5 and the limit is 10. Note: Increasing the number of retries will increase the execution time. | False |
| insecure | Trust any certificate (not secure) | False |
| proxy | Use system proxy settings | False |
| Bucket | Bucket used for network connectivity testing. | False |
Commands
You can execute these commands from the CLI, as part of an automation, or in a playbook.
After you successfully execute a command, a DBot message appears in the War Room with the command details.
aws-s3-create-bucket
Create AWS S3 bucket.
Base Command
aws-s3-create-bucket
Input
| Argument Name | Description | Required |
|---|---|---|
| bucket | The name of S3 bucket to create (in lowercase). | Required |
| acl | ACL for S3 bucket. Possible values are: private, public-read, public-read-write, authenticated-read. | Optional |
| locationConstraint | Specifies the region where the bucket will be created. If you don’t specify a region, the bucket will be created in US Standard. | Optional |
| grantFullControl | Allows grantee the read, write, read ACP, and write ACP permissions on the bucket. | Optional |
| grantRead | Allows grantee to list the objects in the bucket. | Optional |
| grantReadACP | Allows grantee to read the bucket ACL. | Optional |
| grantWrite | Allows grantee to create, overwrite, and delete any object in the bucket. | Optional |
| grantWriteACP | Allows grantee to write the ACL for the applicable bucket. | Optional |
| region | The AWS Region, if not specified the default region will be used. | Optional |
| roleArn | The Amazon Resource Name (ARN) of the role to assume. | Optional |
| roleSessionName | An identifier for the assumed role session. | Optional |
| roleSessionDuration | The duration, in seconds, of the role session. The value can range from 900 seconds (15 minutes) up to the maximum session duration setting for the role. | Optional |
Context Output
| Path | Type | Description |
|---|---|---|
| AWS.S3.Buckets.BucketName | string | The name of the bucket that was created. |
| AWS.S3.Buckets.Location | string | The AWS Region the bucket was created. |
Command Example
!aws-s3-create-bucket bucket=test acl=private
Human Readable Output
AWS S3 Buckets
| BucketName | Location |
|---|---|
| test | test |
aws-s3-delete-bucket
Delete AWS S3 bucket.
Base Command
aws-s3-delete-bucket
Input
| Argument Name | Description | Required |
|---|---|---|
| bucket | Name of S3 bucket to delete. | Required |
| region | The AWS Region, if not specified the default region will be used. | Optional |
| roleArn | The Amazon Resource Name (ARN) of the role to assume. | Optional |
| roleSessionName | An identifier for the assumed role session. | Optional |
| roleSessionDuration | The duration, in seconds, of the role session. The value can range from 900 seconds (15 minutes) up to the maximum session duration setting for the role. | Optional |
Context Output
There is no context output for this command.
Command Example
!aws-s3-delete-bucket bucket=test
Human Readable Output
The bucket was deleted.
aws-s3-list-buckets
List all S3 buckets in AWS account
Base Command
aws-s3-list-buckets
Input
| Argument Name | Description | Required |
|---|---|---|
| region | The AWS Region, if not specified the default region will be used. | Optional |
| roleArn | The Amazon Resource Name (ARN) of the role to assume. | Optional |
| roleSessionName | An identifier for the assumed role session. | Optional |
| roleSessionDuration | The duration, in seconds, of the role session. The value can range from 900 seconds (15 minutes) up to the maximum session duration setting for the role. | Optional |
Context Output
| Path | Type | Description |
|---|---|---|
| AWS.S3.Buckets.BucketName | string | The name of the bucket. |
| AWS.S3.Buckets.CreationDate | date | Date the bucket was created. |
Command Example
#### Human Readable Output
AWS S3 Buckets
| BucketName | CreationDate |
| --- | --- |
| backup-lab | 2018-04-29T13:31:57 |
| test | 2018-05-06T06:34:30 |
### aws-s3-get-bucket-policy
***
Get AWS S3 Bucket Policy
#### Base Command
`aws-s3-get-bucket-policy`
#### Input
| **Argument Name** | **Description** | **Required** |
| --- | --- | --- |
| bucket | Name of bucket. | Required |
| region | The AWS Region, if not specified the default region will be used. | Optional |
| roleArn | The Amazon Resource Name (ARN) of the role to assume. | Optional |
| roleSessionName | An identifier for the assumed role session. | Optional |
| roleSessionDuration | The duration, in seconds, of the role session. The value can range from 900 seconds (15 minutes) up to the maximum session duration setting for the role. | Optional |
#### Context Output
| **Path** | **Type** | **Description** |
| --- | --- | --- |
| AWS.S3.Buckets.Policy.Version | string | S3 Bucket Policy Version. |
| AWS.S3.Buckets.Policy.PolicyId | string | S3 Bucket Policy ID. |
| AWS.S3.Buckets.Policy.Sid | string | S3 Bucket Policy Statment ID. |
| AWS.S3.Buckets.Policy.Action | string | S3 Bucket Policy Statment Action. |
| AWS.S3.Buckets.Policy.Principal | string | S3 Bucket Policy Statment Principal. |
| AWS.S3.Buckets.Policy.Resource | string | S3 Bucket Policy Statment Resource. |
| AWS.S3.Buckets.Policy.Effect | string | S3 Bucket Policy Statment Effect. |
| AWS.S3.Buckets.Policy.Json | string | AWS S3 Policy Json output. |
| AWS.S3.Buckets.Policy.BucketName | string | S3 Bucket Name. |
#### Command Example
```!aws-s3-get-bucket-policy bucket=test```
### aws-s3-delete-bucket-policy
***
Deletes the policy from the bucket.
#### Base Command
`aws-s3-delete-bucket-policy`
#### Input
| **Argument Name** | **Description** | **Required** |
| --- | --- | --- |
| bucket | Name of S3 bucket. | Required |
| region | The AWS Region, if not specified the default region will be used. | Optional |
| roleArn | The Amazon Resource Name (ARN) of the role to assume. | Optional |
| roleSessionName | An identifier for the assumed role session. | Optional |
| roleSessionDuration | The duration, in seconds, of the role session. The value can range from 900 seconds (15 minutes) up to the maximum session duration setting for the role. | Optional |
#### Context Output
There is no context output for this command.
#### Command Example
```!aws-s3-delete-bucket-policy bucket=test```
#### Human Readable Output
Policy deleted from test.
### aws-s3-download-file
***
Download a file from S3 bucket to war room.
#### Base Command
`aws-s3-download-file`
#### Input
| **Argument Name** | **Description** | **Required** |
| --- | --- | --- |
| bucket | The name of S3 bucket. | Optional |
| key | The S3 object key to download. | Optional |
| region | The AWS Region, if not specified the default region will be used. | Optional |
| roleArn | The Amazon Resource Name (ARN) of the role to assume. | Optional |
| roleSessionName | An identifier for the assumed role session. | Optional |
| roleSessionDuration | The duration, in seconds, of the role session. The value can range from 900 seconds (15 minutes) up to the maximum session duration setting for the role. | Optional |
#### Context Output
There is no context output for this command.
#### Command Example
```!aws-s3-download-file bucket=test key=test.txt```
### aws-s3-list-bucket-objects
***
List object in S3 bucket.
#### Base Command
`aws-s3-list-bucket-objects`
#### Input
| **Argument Name** | **Description** | **Required** |
| --- | --- | --- |
| bucket | The name of S3 bucket. | Required |
| prefix | Limits the response to keys that begin with the specified prefix. | Optional |
| delimiter | A delimiter is a character you use to group keys. | Optional |
| region | The AWS Region, if not specified the default region will be used. | Optional |
| roleArn | The Amazon Resource Name (ARN) of the role to assume. | Optional |
| roleSessionName | An identifier for the assumed role session. | Optional |
| roleSessionDuration | The duration, in seconds, of the role session. The value can range from 900 seconds (15 minutes) up to the maximum session duration setting for the role. | Optional |
#### Context Output
| **Path** | **Type** | **Description** |
| --- | --- | --- |
| AWS.S3.Buckets.Objects.Key | Unknown | The name of S3 object. |
| AWS.S3.Buckets.Objects.Size | Unknown | Object size. |
| AWS.S3.Buckets.Objects.LastModified | Unknown | Last date object was modified. |
#### Command Example
```!aws-s3-list-bucket-objects bucket=test prefix=testing delimiter='/'```
#### Human Readable Output
AWS S3 Bucket Objects
| Key | Size | LastModified |
| --- | --- | --- |
| demi2018-04-05-14-29-49-76DA472F25CB951F | 323.0 B | 2018-04-05T14:29:51 |
### aws-s3-put-bucket-policy
***
Replaces a policy on a bucket. If the bucket already has a policy, the one in this request completely replaces it.
#### Base Command
`aws-s3-put-bucket-policy`
#### Input
| **Argument Name** | **Description** | **Required** |
| --- | --- | --- |
| bucket | Name of S3 bucket. | Required |
| policy | The bucket policy to apply in json format. | Required |
| confirmRemoveSelfBucketAccess | Set this parameter to true to confirm that you want to remove your permissions to change this bucket policy in the future. Possible values are: True, False. | Optional |
| region | The AWS Region, if not specified the default region will be used. | Optional |
| roleArn | The Amazon Resource Name (ARN) of the role to assume. | Optional |
| roleSessionName | An identifier for the assumed role session. | Optional |
| roleSessionDuration | The duration, in seconds, of the role session. The value can range from 900 seconds (15 minutes) up to the maximum session duration setting for the role. | Optional |
#### Context Output
There is no context output for this command.
#### Command Example
```!aws-s3-put-bucket-policy bucket=test policy={"Version":"2012-10-17","Id":"Policy1519481415511","Statement":[{"Sid":"Stmt1519ds34548138sf5929","Effect":"Allow","Principal":{"AWS":"arn:aws:iam::123456789:user/itai"},"Action":"s3:","Resource":"arn:aws:s3:::test"},{"Sid":"Stmt1345519481414395","Effect":"Allow","Principal":{"AWS":"arn:aws:iam::123456789:user/bob"},"Action":"s3:","Resource":"arn:aws:s3:::test"}]}```
#### Human Readable Output
Successfully applied bucket policy to test bucket.
### aws-s3-upload-file
***
Upload file to S3 bucket
#### Base Command
`aws-s3-upload-file`
#### Input
| **Argument Name** | **Description** | **Required** |
| --- | --- | --- |
| entryID | Entry ID of the file to upload. | Required |
| bucket | The name of the bucket to upload to. | Required |
| key | The name of the key to upload to. | Required |
| region | The AWS Region, if not specified the default region will be used. | Optional |
| roleArn | The Amazon Resource Name (ARN) of the role to assume. | Optional |
| roleSessionName | An identifier for the assumed role session. | Optional |
| roleSessionDuration | The duration, in seconds, of the role session. The value can range from 900 seconds (15 minutes) up to the maximum session duration setting for the role. | Optional |
#### Context Output
There is no context output for this command.
#### Command Example
```!aws-s3-upload-file bucket="bucket name" key="file name to be displayed" entryID=##@##```
#### Human Readable Output
File {file name to be displayed} was uploaded successfully to {bucket name}'
### aws-s3-get-public-access-block
***
Retrieves the PublicAccessBlock configuration for an Amazon S3 bucket.
#### Base Command
`aws-s3-get-public-access-block`
#### Input
| **Argument Name** | **Description** | **Required** |
| --- | --- | --- |
| bucket | The name of the Amazon S3 bucket whose PublicAccessBlock configuration you want to retrieve. | Required |
| region | The AWS Region, if not specified the default region will be used. | Optional |
| roleArn | The Amazon Resource Name (ARN) of the role to assume. | Optional |
| roleSessionName | An identifier for the assumed role session. | Optional |
| roleSessionDuration | The duration, in seconds, of the role session. The value can range from 900 seconds (15 minutes) up to the maximum session duration setting for the role. | Optional |
#### Context Output
| **Path** | **Type** | **Description** |
| --- | --- | --- |
| AWS.S3.Buckets.BucketName.PublicAccessBlockConfiguration.BlockPublicAcls | Boolean | Specifies whether Amazon S3 should block public access control lists (ACLs) for this bucket and objects in this bucket. |
| AWS.S3.Buckets.BucketName.PublicAccessBlockConfiguration.IgnorePublicAcls | Boolean | Specifies whether Amazon S3 should ignore public ACLs for this bucket and objects in this bucket. |
| AWS.S3.Buckets.BucketName.PublicAccessBlockConfiguration.BlockPublicPolicy | Boolean | Specifies whether Amazon S3 should block public bucket policies for this bucket. |
| AWS.S3.Buckets.BucketName.PublicAccessBlockConfiguration.RestrictPublicBuckets | Boolean | Specifies whether Amazon S3 should restrict public bucket policies for this bucket. |
#### Command Example
```!aws-s3-get-public-access-block bucket="bucket name"```
#### Human Readable Output
AWS S3 Bucket Public Access Block
| BlockPublicAcls | IgnorePublicAcls | BlockPublicPolicy | RestrictPublicBuckets |
| --- | --- | --- | --- |
| True | False | True | False |
### aws-s3-put-public-access-block
***
Creates or modifies the PublicAccessBlock configuration for an Amazon S3 bucket.
#### Base Command
`aws-s3-put-public-access-block`
#### Input
| **Argument Name** | **Description** | **Required** |
| --- | --- | --- |
| bucket | The name of the bucket to upload to. | Required |
| BlockPublicAcls | Specifies whether Amazon S3 should block public access control lists (ACLs) for this bucket and objects in this bucket. | Required |
| IgnorePublicAcls | Specifies whether Amazon S3 should ignore public ACLs for this bucket and objects in this bucket. | Required |
| BlockPublicPolicy | Specifies whether Amazon S3 should block public bucket policies for this bucket. | Required |
| RestrictPublicBuckets | Specifies whether Amazon S3 should restrict public bucket policies for this bucket. | Required |
| region | The AWS Region, if not specified the default region will be used. | Optional |
| roleArn | The Amazon Resource Name (ARN) of the role to assume. | Optional |
| roleSessionName | An identifier for the assumed role session. | Optional |
| roleSessionDuration | The duration, in seconds, of the role session. The value can range from 900 seconds (15 minutes) up to the maximum session duration setting for the role. | Optional |
#### Context Output
There is no context output for this command.
#### Command Example
```!aws-s3-put-public-access-block bucket="bucket name" BlockPublicAcls=True IgnorePublicAcls=False BlockPublicPolicy=True RestrictPublicBuckets=True```
#### Human Readable Output
Successfully applied public access block to the {bucket} bucket.
### aws-s3-get-bucket-encryption
***
Get AWS S3 Bucket Encryption
#### Base Command
`aws-s3-get-bucket-encryption`
#### Input
| **Argument Name** | **Description** | **Required** |
| --- | --- | --- |
| bucket | The name of the bucket from which the server-side encryption configuration is retrieved. | Required |
| expectedBucketOwner | The account ID of the exepcted bucket owner. | Optional |
| region | The AWS Region, if not specified the default region will be used. | Optional |
| roleArn | The Amazon Resource Name (ARN) of the role to assume. | Optional |
| roleSessionName | An identifier for the assumed role session. | Optional |
| roleSessionDuration | The duration, in seconds, of the role session. The value can range from 900 seconds (15 minutes) up to the maximum session duration setting for the role. | Optional |
#### Context Output
| **Path** | **Type** | **Description** |
| --- | --- | --- |
| AWS.S3.Buckets.BucketName.ServerSideEncryptionConfiguration.Rules.ApplyServerSideEncryptionByDefault.SSEAlgorithm | String | S3 Bucket Encryption SSE Algorithm. |
| AWS.S3.Buckets.BucketName.ServerSideEncryptionConfiguration.Rules.ApplyServerSideEncryptionByDefault.KMSMasterKeyID | String | S3 Bucket Encryption KMS Master Key ID. |
| AWS.S3.Buckets.BucketName.ServerSideEncryptionConfiguration.Rules.BucketKeyEnabled | Boolean | S3 Bucket Encryption Key Enabled. |
#### Command Example
```!aws-s3-put-public-access-block bucket="bucket name" BlockPublicAcls=True IgnorePublicAcls=False BlockPublicPolicy=True RestrictPublicBuckets=True```
#### Context Example
{
“AWS”: {
“S3”: {
“Buckets”: [
{
“BucketName”: “bucket-a”,
“ServerSideEncryptionConfiguration”: {
“Rules”: [
{
“ApplyServerSideEncryptionByDefault”: {
“SSEAlgorithm”: “AES256”
}
}
]
}
}
]
}
}
}
```
Configuration parameters
roleArn— Role ArnroleSessionName— Role Session NamedefaultRegion— AWS Default Region (required)sessionDuration— Role Session Durationcredentials— Access Keyaccess_key— Access Keysecret_key— Secret Keytimeout— Timeoutretries— Retriesendpoint_url— PrivateLink service URL.sts_endpoint_url— STS PrivateLink URL.sts_regional_endpoint— AWS STS Regional Endpointsbucket— Bucketinsecure— Trust any certificate (not secure)proxy— Use system proxy settings
Commands (12)
-
aws-s3-create-bucketCreate AWS S3 bucket.
-
aws-s3-delete-bucketDelete AWS S3 bucket.
-
aws-s3-delete-bucket-policyDeletes the policy from the bucket.
-
aws-s3-download-fileDownload a file from S3 bucket to war room.
-
aws-s3-get-bucket-encryptionGet AWS S3 Bucket Encryption.
-
aws-s3-get-bucket-policyGet AWS S3 Bucket Policy.
-
aws-s3-get-public-access-blockRetrieves the PublicAccessBlock configuration for an Amazon S3 bucket.
-
aws-s3-list-bucket-objectsList object in S3 bucket.
-
aws-s3-list-bucketsList all S3 buckets in AWS account.
-
aws-s3-put-bucket-policyReplaces a policy on a bucket. If the bucket already has a policy, the one in this request completely replaces it.
-
aws-s3-put-public-access-blockCreates or modifies the PublicAccessBlock configuration for an Amazon S3 bucket.
-
aws-s3-upload-fileUpload file to S3 bucket.
import io import json import math from datetime import date, datetime from http import HTTPStatus import demistomock as demisto import urllib3.util from AWSApiModule import * # noqa: E402 from CommonServerPython import * # Disable insecure warnings urllib3.disable_warnings() SERVICE = "s3" """HELPER FUNCTIONS""" def convert_size(size_bytes): if size_bytes == 0: return "0B" size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB") i = int(math.floor(math.log(size_bytes, 1024))) p = math.pow(1024, i) s = round(size_bytes / p, 2) return f"{s} {size_name[i]}" class DatetimeEncoder(json.JSONEncoder): # pylint: disable=method-hidden def default(self, obj): if isinstance(obj, datetime): return obj.strftime("%Y-%m-%dT%H:%M:%S") elif isinstance(obj, date): return obj.strftime("%Y-%m-%d") # Let the base class default method raise the TypeError return json.JSONEncoder.default(self, obj) def create_bucket_command(args: Dict[str, Any], aws_client: AWSClient) -> CommandResults: client = aws_client.aws_session( service=SERVICE, region=args.get("region"), role_arn=args.get("roleArn"), role_session_name=args.get("roleSessionName"), role_session_duration=args.get("roleSessionDuration"), ) data = [] kwargs = {"Bucket": args.get("bucket", "").lower()} if args.get("acl") is not None: kwargs.update({"ACL": args.get("acl")}) if args.get("locationConstraint") is not None: kwargs.update({"CreateBucketConfiguration": {"LocationConstraint": args.get("locationConstraint")}}) if args.get("grantFullControl") is not None: kwargs.update({"GrantFullControl": args.get("grantFullControl")}) if args.get("grantRead") is not None: kwargs.update({"GrantRead": args.get("grantRead")}) if args.get("grantReadACP") is not None: kwargs.update({"GrantReadACP": args.get("grantReadACP")}) if args.get("grantWrite") is not None: kwargs.update({"GrantWrite": args.get("grantWrite")}) if args.get("grantWriteACP") is not None: kwargs.update({"GrantWriteACP": args.get("grantWriteACP")}) response = client.create_bucket(**kwargs) data.append({"BucketName": args.get("bucket"), "Location": response["Location"]}) human_readable = tableToMarkdown("AWS S3 Buckets", data) return CommandResults(readable_output=human_readable, outputs=data, outputs_prefix="AWS.S3.Buckets") def delete_bucket_command(args: Dict[str, Any], aws_client: AWSClient) -> CommandResults: client = aws_client.aws_session( service=SERVICE, region=args.get("region"), role_arn=args.get("roleArn"), role_session_name=args.get("roleSessionName"), role_session_duration=args.get("roleSessionDuration"), ) response = client.delete_bucket(Bucket=args.get("bucket", "").lower()) if response["ResponseMetadata"]["HTTPStatusCode"] == HTTPStatus.NO_CONTENT: return CommandResults(readable_output=f"The requested bucket '{args.get('bucket')}' was deleted") return CommandResults(readable_output=f"The requested bucket '{args.get('bucket')}' was not found") def list_buckets_command(args: Dict[str, Any], aws_client: AWSClient) -> CommandResults: client = aws_client.aws_session( service=SERVICE, region=args.get("region"), role_arn=args.get("roleArn"), role_session_name=args.get("roleSessionName"), role_session_duration=args.get("roleSessionDuration"), ) data = [] response = client.list_buckets() for bucket in response["Buckets"]: data.append( {"BucketName": bucket["Name"], "CreationDate": datetime.strftime(bucket["CreationDate"], "%Y-%m-%dT%H:%M:%S")} ) human_readable = tableToMarkdown("AWS S3 Buckets", data) return CommandResults( readable_output=human_readable, outputs_prefix="AWS.S3.Buckets", outputs_key_field="BucketName", outputs=data ) def get_bucket_policy_command(args: Dict[str, Any], aws_client: AWSClient) -> CommandResults: client = aws_client.aws_session( service=SERVICE, region=args.get("region"), role_arn=args.get("roleArn"), role_session_name=args.get("roleSessionName"), role_session_duration=args.get("roleSessionDuration"), ) data = [] response = client.get_bucket_policy(Bucket=args.get("bucket", "").lower()) policy = json.loads(response["Policy"]) statements = policy["Statement"] for statement in statements: data.append( { "BucketName": args.get("bucket"), "PolicyId": policy.get("Id"), "PolicyVersion": policy.get("Version"), "Sid": statement.get("Sid"), "Action": statement.get("Action"), "Principal": statement.get("Principal"), "Resource": statement.get("Resource"), "Effect": statement.get("Effect"), "Json": response.get("Policy"), } ) human_readable = tableToMarkdown("AWS S3 Bucket Policy", data) return CommandResults( readable_output=human_readable, outputs_prefix="AWS.S3.Buckets", outputs_key_field="BucketName", outputs=data ) def put_bucket_policy_command(args: Dict[str, Any], aws_client: AWSClient) -> CommandResults: client = aws_client.aws_session( service=SERVICE, region=args.get("region"), role_arn=args.get("roleArn"), role_session_name=args.get("roleSessionName"), role_session_duration=args.get("roleSessionDuration"), ) kwargs = {"Bucket": args.get("bucket", "").lower(), "Policy": args.get("policy")} if args.get("confirmRemoveSelfBucketAccess") is not None: kwargs.update({"ConfirmRemoveSelfBucketAccess": args.get("confirmRemoveSelfBucketAccess") == "True"}) response = client.put_bucket_policy(**kwargs) if response["ResponseMetadata"]["HTTPStatusCode"] == HTTPStatus.OK: return CommandResults(readable_output=f"Successfully applied bucket policy to {args.get('bucket')} bucket") return CommandResults(readable_output=f"Couldn't apply bucket policy to {args.get('bucket')} bucket") def delete_bucket_policy_command(args: Dict[str, Any], aws_client: AWSClient) -> CommandResults: client = aws_client.aws_session( service=SERVICE, region=args.get("region"), role_arn=args.get("roleArn"), role_session_name=args.get("roleSessionName"), role_session_duration=args.get("roleSessionDuration"), ) client.delete_bucket_policy(Bucket=args.get("bucket", "").lower()) return CommandResults(readable_output=f"Policy deleted from {args.get('bucket')}") def download_file_command(args: Dict[str, Any], aws_client: AWSClient): client = aws_client.aws_session( service=SERVICE, region=args.get("region"), role_arn=args.get("roleArn"), role_session_name=args.get("roleSessionName"), role_session_duration=args.get("roleSessionDuration"), ) data = io.BytesIO() client.download_fileobj(args.get("bucket", "").lower(), args.get("key"), data) demisto.results(fileResult(args.get("key"), data.getvalue())) def list_objects_command(args: Dict[str, Any], aws_client: AWSClient) -> CommandResults: client = aws_client.aws_session( service=SERVICE, region=args.get("region"), role_arn=args.get("roleArn"), role_session_name=args.get("roleSessionName"), role_session_duration=args.get("roleSessionDuration"), ) data = [] kwargs = {"Bucket": args.get("bucket")} if args.get("delimiter") is not None: kwargs.update({"Delimiter": args.get("delimiter")}) if args.get("prefix") is not None: kwargs.update({"Prefix": args.get("prefix")}) client.list_objects(**kwargs) paginator = client.get_paginator("list_objects") for response in paginator.paginate(**kwargs): if response.get("Contents", None): for key in response["Contents"]: data.append( { "Key": key["Key"], "Size": convert_size(key["Size"]), "LastModified": datetime.strftime(key["LastModified"], "%Y-%m-%dT%H:%M:%S"), } ) if len(data) > 0: human_readable = tableToMarkdown("AWS S3 Bucket Objects", data) return CommandResults( readable_output=human_readable, outputs_prefix="AWS.S3.Buckets", outputs_key_field="BucketName", outputs=data ) return CommandResults(readable_output=f"The {args.get('bucket')} bucket contains no objects.") def get_file_path(file_id): filepath_result = demisto.getFilePath(file_id) return filepath_result def upload_file_command(args: Dict[str, Any], aws_client: AWSClient) -> CommandResults: client = aws_client.aws_session( service=SERVICE, region=args.get("region"), role_arn=args.get("roleArn"), role_session_name=args.get("roleSessionName"), role_session_duration=args.get("roleSessionDuration"), ) path = get_file_path(args.get("entryID")) with open(path["path"], "rb") as data: client.upload_fileobj(data, args.get("bucket"), args.get("key")) return CommandResults(readable_output=f"File {args.get('key')} was uploaded successfully to {args.get('bucket')}") def get_public_access_block(args: Dict[str, Any], aws_client: AWSClient) -> CommandResults: client = aws_client.aws_session( service=SERVICE, region=args.get("region"), role_arn=args.get("roleArn"), role_session_name=args.get("roleSessionName"), role_session_duration=args.get("roleSessionDuration"), ) response = client.get_public_access_block(Bucket=args.get("bucket")) public_access_block_configuration = response.get("PublicAccessBlockConfiguration") data = { "BucketName": args.get("bucket"), "PublicAccessBlockConfiguration": { "BlockPublicAcls": public_access_block_configuration.get("BlockPublicAcls"), "IgnorePublicAcls": public_access_block_configuration.get("IgnorePublicAcls"), "BlockPublicPolicy": public_access_block_configuration.get("BlockPublicPolicy"), "RestrictPublicBuckets": public_access_block_configuration.get("RestrictPublicBuckets"), }, } human_readable = tableToMarkdown("AWS S3 Bucket Public Access Block", data) return CommandResults( outputs=data, readable_output=human_readable, outputs_prefix="AWS.S3.Buckets", outputs_key_field="BucketName" ) def put_public_access_block(args: Dict[str, Any], aws_client: AWSClient) -> CommandResults: client = aws_client.aws_session( service=SERVICE, region=args.get("region"), role_arn=args.get("roleArn"), role_session_name=args.get("roleSessionName"), role_session_duration=args.get("roleSessionDuration"), ) kwargs = { "Bucket": args.get("bucket"), "PublicAccessBlockConfiguration": { "BlockPublicAcls": argToBoolean(args.get("BlockPublicAcls")), "IgnorePublicAcls": argToBoolean(args.get("IgnorePublicAcls")), "BlockPublicPolicy": argToBoolean(args.get("BlockPublicPolicy")), "RestrictPublicBuckets": argToBoolean(args.get("RestrictPublicBuckets")), }, } response = client.put_public_access_block(**kwargs) if response["ResponseMetadata"]["HTTPStatusCode"] == HTTPStatus.OK: return CommandResults(readable_output=f"Successfully applied public access block to the {args.get('bucket')} bucket") return CommandResults(readable_output=f"Couldn't apply public access block to the {args.get('bucket')} bucket") def get_bucket_encryption(args: Dict[str, Any], aws_client: AWSClient) -> CommandResults: client = aws_client.aws_session( service=SERVICE, region=args.get("region"), role_arn=args.get("roleArn"), role_session_name=args.get("roleSessionName"), role_session_duration=args.get("roleSessionDuration"), ) kwargs = {"Bucket": args.get("bucket")} if args.get("expectedBucketOwner") is not None: kwargs.update({"ExpectedBucketOwner": args.get("expectedBucketOwner")}) try: response = client.get_bucket_encryption(**kwargs) except client.exceptions.ClientError as ex: if ex.response.get("Error", {}).get("Code", "") != "ServerSideEncryptionConfigurationNotFoundError": raise ex response = {} data = { "BucketName": args.get("bucket"), "ServerSideEncryptionConfiguration": response.get("ServerSideEncryptionConfiguration"), } human_readable = tableToMarkdown("AWS S3 Bucket Encryption", data) return CommandResults( outputs=data, readable_output=human_readable, outputs_prefix="AWS.S3.Buckets", outputs_key_field="BucketName" ) def test_module(aws_client: AWSClient, bucket: str) -> str | CommandResults: """ test the integration connectivity. Args: aws_client: A Boto3 AWS S3 client. bucket(str): The bucket name. Returns: A string whether the connectivity test was successful. """ client = aws_client.aws_session(service=SERVICE) try: demisto.debug("executing list_buckets") response = client.list_buckets() if response["ResponseMetadata"]["HTTPStatusCode"] == HTTPStatus.OK: return "ok" else: demisto.debug(f"list_buckets failed with {response=}") raise DemistoException(f"received status code {response['ResponseMetadata']['HTTPStatusCode']}") except Exception: if bucket: demisto.debug(f"executing list_objects_v2 with {bucket=}") response_objects = client.list_objects_v2(Bucket=bucket) if response_objects["ResponseMetadata"]["HTTPStatusCode"] == HTTPStatus.OK: return "ok" else: return CommandResults( content_format=EntryFormat.TEXT, entry_type=EntryType.ERROR, readable_output=f"received status code {response_objects['ResponseMetadata']['HTTPStatusCode']}", ) else: demisto.debug("No bucket provided, raising an error.") raise def main(): # pragma: no cover params = demisto.params() aws_default_region = params.get("defaultRegion") aws_role_arn = params.get("roleArn") aws_role_session_name = params.get("roleSessionName") aws_role_session_duration = params.get("sessionDuration") aws_role_policy = None aws_access_key_id = params.get("credentials", {}).get("identifier") or params.get("access_key") aws_secret_access_key = params.get("credentials", {}).get("password") or params.get("secret_key") verify_certificate = not params.get("insecure", True) timeout = params.get("timeout") retries = params.get("retries") or 5 sts_endpoint_url = params.get("sts_endpoint_url") or None endpoint_url = params.get("endpoint_url") or None bucket = params.get("bucket", "") try: command = demisto.command() validate_params(aws_default_region, aws_role_arn, aws_role_session_name, aws_access_key_id, aws_secret_access_key) aws_client = AWSClient( aws_default_region, aws_role_arn, aws_role_session_name, aws_role_session_duration, aws_role_policy, aws_access_key_id, aws_secret_access_key, verify_certificate, timeout, retries, sts_endpoint_url=sts_endpoint_url, endpoint_url=endpoint_url, ) args = demisto.args() demisto.info(f"Command being called is {demisto.command()}") if command == "test-module": return_results(test_module(aws_client, bucket)) elif command == "aws-s3-create-bucket": return_results(create_bucket_command(args, aws_client)) elif command == "aws-s3-delete-bucket": return_results(delete_bucket_command(args, aws_client)) elif command == "aws-s3-list-buckets": return_results(list_buckets_command(args, aws_client)) elif command == "aws-s3-get-bucket-policy": return_results(get_bucket_policy_command(args, aws_client)) elif command == "aws-s3-put-bucket-policy": return_results(put_bucket_policy_command(args, aws_client)) elif command == "aws-s3-delete-bucket-policy": return_results(delete_bucket_policy_command(args, aws_client)) elif command == "aws-s3-download-file": download_file_command(args, aws_client) elif command == "aws-s3-list-bucket-objects": return_results(list_objects_command(args, aws_client)) elif command == "aws-s3-upload-file": return_results(upload_file_command(args, aws_client)) elif command == "aws-s3-get-public-access-block": return_results(get_public_access_block(args, aws_client)) elif command == "aws-s3-put-public-access-block": return_results(put_public_access_block(args, aws_client)) elif command == "aws-s3-get-bucket-encryption": return_results(get_bucket_encryption(args, aws_client)) else: raise NotImplementedError(f"{command} command is not implemented.") except Exception as e: return_error(f"Failed to execute {command} command.\nError:\n{e!s}") if __name__ in ("__builtin__", "builtins", "__main__"): main()