Skip to content
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

try to create AP every time, catch if already exists #1609

Merged
merged 10 commits into from
Oct 8, 2024

Conversation

SofiaSazonova
Copy link
Contributor

Feature or Bugfix

  • Bugfix

Detail

  • Try to create AP every time we process the share
  • Catch error if it already exists
  • Retry on put_access_point_policy

Relates

Security

Please answer the questions below briefly where applicable, or write N/A. Based on
OWASP 10.

  • Does this PR introduce or modify any input fields or queries - this includes
    fetching data from storage outside the application (e.g. a database, an S3 bucket)?
    • Is the input sanitized?
    • What precautions are you taking before deserializing the data you consume?
    • Is injection prevented by parametrizing queries?
    • Have you ensured no eval or similar functions are used?
  • Does this PR introduce any functionality or component that requires authorization?
    • How have you ensured it respects the existing AuthN/AuthZ mechanisms?
    • Are you logging failed auth attempts?
  • Are you using or adding any cryptographic features?
    • Do you use a standard proven implementations?
    • Are the used keys controlled by the customer? Where are they stored?
  • Are you introducing any new policies/roles/users?
    • Have you used the least-privilege principle? How?

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@SofiaSazonova SofiaSazonova linked an issue Oct 3, 2024 that may be closed by this pull request
@SofiaSazonova SofiaSazonova marked this pull request as ready for review October 3, 2024 15:12
Copy link
Contributor

@petrkalos petrkalos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some nits and a question

s3_client.attach_access_point_policy(
access_point_name=self.access_point_name, policy=json.dumps(access_point_policy)
)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need this now and didn't need it previously?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is in case AP is not yet created. Before the retry part was in creation of of AP

Copy link
Contributor

@petrkalos petrkalos Oct 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would explicitly encapsulate the Retry mechanism in the create_bucket_access_point to ensure that all calls to it return when the bucket is created.

For example something like the below....

    def create_bucket_access_point(self, bucket_name: str, access_point_name: str):
        try:
            self._client.create_access_point(AccountId=self._account_id, Name=access_point_name, Bucket=bucket_name)
        except self._client.exceptions.AccessPointAlreadyOwnedByYou:
            ...
        except Exception as e:
            log.exception(f'S3 bucket access point creation failed for location {bucket_name=}')
            raise e
        return Retrying(
            retry_on_exception=(self._client.exceptions.NotFoundException,),
            stop_max_attempt_number=5,
            wait_random_min=1000,
            wait_random_max=3000,
        ).call(self.get_bucket_access_point_arn, access_point_name)['AccessPointArn']

Also you must make sure that you wait enough time. Previous code was waiting for 300secs++, yours wait from 5 to 15 seconds.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't it lead us to the same error? If get_access_point_arn will fail, we again receive the problem of Negative Cash?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, there is no self._client.exceptions.AccessPointAlreadyOwnedByYou: so, I have to check the string

from itertools import count

from retrying import Retrying
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused

return self.try_get_bucket_access_point_arn(bucket_name, access_point_name)

def try_get_bucket_access_point_arn(self, bucket_name: str, access_point_name: str):
for attempt in range(5):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could use @retry(retry_on_result=lambda arn: arn is None, stop_max_attempt_number=10, wait_fixed=30000)

@SofiaSazonova SofiaSazonova merged commit 25c4202 into data-dot-all:main Oct 8, 2024
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

NoSuchAccessPoint exception during share processing
2 participants