Skip to content

Commit

Permalink
Merge branch 'dev' into handle_totp_90sec_web
Browse files Browse the repository at this point in the history
  • Loading branch information
i-oden authored May 13, 2022
2 parents e271f3f + 350791f commit d250321
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,4 @@ Please add a _short_ line describing the PR you make, if the PR implements a spe
- Add ability to switch to using TOTP and back to HOTP for MFA ([#936](https://github.com/scilifelabdatacentre/dds_web/issues/936))
- Patch: Fix the warning in web for too soon TOTP login (within 90 seconds) ([#1173](https://github.com/ScilifelabDataCentre/dds_web/pull/1173))
- Bug: Do not remove the bucket when emptying the project ([#1172](https://github.com/ScilifelabDataCentre/dds_web/pull/1172))
- New `add-missing-buckets` argument option to the `lost-files` flask command ([#1174](https://github.com/ScilifelabDataCentre/dds_web/pull/1174))
12 changes: 11 additions & 1 deletion dds_web/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ def update_uploaded_file_with_log(project, path_to_log_file):


@click.command("lost-files")
@click.argument("action_type", type=click.Choice(["find", "list", "delete"]))
@click.argument("action_type", type=click.Choice(["find", "list", "delete", "add-missing-buckets"]))
@flask.cli.with_appcontext
def lost_files_s3_db(action_type: str):
"""
Expand All @@ -494,6 +494,7 @@ def lost_files_s3_db(action_type: str):
"""
from dds_web.database import models
import boto3
from dds_web.utils import bucket_is_valid

for unit in models.Unit.query:
session = boto3.session.Session()
Expand All @@ -514,6 +515,15 @@ def lost_files_s3_db(action_type: str):
)
except resource.meta.client.exceptions.NoSuchBucket:
flask.current_app.logger.warning("Missing bucket %s", project.bucket)
if action_type == "add-missing-buckets":
valid, message = bucket_is_valid(bucket_name=project.bucket)
if not valid:
flask.current_app.logger.warning(
f"Could not create bucket '{project.bucket}' for project '{project.public_id}': {message}"
)
else:
resource.create_bucket(Bucket=project.bucket)
flask.current_app.logger.info(f"Bucket '{project.bucket}' created.")
continue

try:
Expand Down
21 changes: 21 additions & 0 deletions dds_web/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,3 +389,24 @@ def create_one_time_password_email(user, hotp_value):
)

return msg


def bucket_is_valid(bucket_name):
"""Verify that the bucket name is valid."""
valid = False
message = ""
if not (3 <= len(bucket_name) <= 63):
message = f"The bucket name has the incorrect length {len(bucket_name)}"
elif re.findall(r"[^a-zA-Z0-9.-]", bucket_name):
message = "The bucket name contains invalid characters."
elif bucket_name[0].isalnum():
message = "The bucket name must begin with a letter or number."
elif bucket_name.count(".") > 2:
message = "The bucket name cannot contain more than two dots."
elif bucket_name.startswith("xn--"):
message = "The bucket name cannot begin with the 'xn--' prefix."
elif bucket_name.endswith("-s3alias"):
message = "The bucket name cannot end with the '-s3alias' suffix."
else:
valid = True
return valid, message

0 comments on commit d250321

Please sign in to comment.