-
Notifications
You must be signed in to change notification settings - Fork 88
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
Functionality to create storage buckets in GCP and AWS #291
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
1b9bd0f
GCS Create
Fryyyyy 46160a1
Add test for GCS
Fryyyyy 5da2fed
Create S3 Bucket
Fryyyyy 1a2371c
CLI tool for S3
Fryyyyy 2547e64
S3 create bucket test
Fryyyyy 582f78e
Fix comment
Fryyyyy 88fe5be
Small linting thing
Fryyyyy 9d60be5
Merge branch 'master' into create_storage
Fryyyyy 3eb0387
Pylint fixes
Fryyyyy cb067d1
Merge branch 'create_storage' of https://github.com/Fryyyyy/cloud-for…
Fryyyyy 7e20a21
Fixes from review
Fryyyyy 551f8c6
Merge branch 'master' into create_storage
giovannt0 2322c2e
Review fixes
Fryyyyy 6ccbb2e
Merge branch 'master' into create_storage
Fryyyyy 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 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
This file contains 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
This file contains 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,83 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright 2021 Google Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
"""Bucket functionality.""" | ||
|
||
from typing import TYPE_CHECKING, Dict, Optional, Any | ||
|
||
from libcloudforensics import errors | ||
from libcloudforensics.providers.aws.internal import common | ||
|
||
if TYPE_CHECKING: | ||
# TYPE_CHECKING is always False at runtime, therefore it is safe to ignore | ||
# the following cyclic import, as it it only used for type hints | ||
from libcloudforensics.providers.aws.internal import account # pylint: disable=cyclic-import | ||
|
||
|
||
class S3: | ||
"""Class that represents AWS S3 storage services. | ||
|
||
Attributes: | ||
aws_account (AWSAccount): The account for the resource. | ||
name (str): The name of the bucket. | ||
region (str): The region in which the bucket resides. | ||
""" | ||
|
||
def __init__(self, | ||
aws_account: 'account.AWSAccount') -> None: | ||
"""Initialize the AWS S3 resource. | ||
|
||
Args: | ||
aws_account (AWSAccount): The account for the resource. | ||
""" | ||
|
||
self.aws_account = aws_account | ||
|
||
def CreateBucket( | ||
self, | ||
name: str, | ||
region: Optional[str] = None, | ||
acl: str = 'private') -> Dict[str, Any]: | ||
"""Create an S3 storage bucket. | ||
|
||
Args: | ||
name (str): The name of the bucket. | ||
region (str): Optional. The region in which the bucket resides. | ||
acl (str): Optional. The canned ACL with which to create the bucket. | ||
Default is 'private'. | ||
Appropriate values for the Canned ACLs are here: | ||
https://docs.aws.amazon.com/AmazonS3/latest/userguide/acl-overview.html#canned-acl # pylint: disable=line-too-long | ||
|
||
Returns: | ||
Dict: An API operation object for a S3 bucket. | ||
https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html#S3.Bucket.create # pylint: disable=line-too-long | ||
|
||
Raises: | ||
ResourceCreationError: If the bucket couldn't be created. | ||
""" | ||
|
||
client = self.aws_account.ClientApi(common.S3_SERVICE) | ||
try: | ||
bucket = client.create_bucket( | ||
Bucket=name, | ||
ACL=acl, | ||
CreateBucketConfiguration={ | ||
'LocationConstraint': region or self.aws_account.default_region | ||
}) # type: Dict[str, Any] | ||
return bucket | ||
except client.exceptions.ClientError as exception: | ||
raise errors.ResourceCreationError( | ||
'Could not create bucket {0:s}: {1:s}'.format( | ||
name, str(exception)), | ||
__name__) from exception |
This file contains 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
This file contains 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
This file contains 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,43 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright 2021 Google Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
"""Tests for AWS module - s3.py.""" | ||
|
||
import typing | ||
import unittest | ||
import mock | ||
|
||
from tests.providers.aws import aws_mocks | ||
|
||
|
||
class AWSS3Test(unittest.TestCase): | ||
"""Test AWS S3 class.""" | ||
# pylint: disable=line-too-long | ||
|
||
@typing.no_type_check | ||
@mock.patch('libcloudforensics.providers.aws.internal.account.AWSAccount.ClientApi') | ||
def testCreateBucket(self, mock_s3_api): | ||
"""Test that the Bucket is created.""" | ||
storage = mock_s3_api.return_value.create_bucket | ||
storage.return_value = aws_mocks.MOCK_CREATE_BUCKET | ||
create_bucket = aws_mocks.FAKE_STORAGE.CreateBucket('test-bucket') | ||
|
||
storage.assert_called_with( | ||
Bucket='test-bucket', | ||
ACL='private', | ||
CreateBucketConfiguration={ | ||
'LocationConstraint': aws_mocks.FAKE_AWS_ACCOUNT.default_region | ||
}) | ||
self.assertEqual(200, create_bucket['ResponseMetadata']['HTTPStatusCode']) | ||
self.assertEqual('http://test-bucket.s3.amazonaws.com/', create_bucket['Location']) | ||
Fryyyyy marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains 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
This file contains 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
This file contains 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
This file contains 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
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.
Should fix the mypy problem
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.
Ok