#!/usr/bin/env python3 """ Script to upload the self-contained AWS Key Rotation Guide to S3. This makes the guide accessible to all users who receive the email notification. """ # These imports may show as linting errors in local IDE but will work # when boto3 is installed in the environment import boto3 import botocore import mimetypes from pathlib import Path # S3 bucket name where the guide will be hosted S3_BUCKET_NAME = 'orcd-public' #public bucket for hosting publicly accessible files def upload_file_to_s3(file_path, s3_key, content_type=None): """Upload a file to S3 with the specified key and content type.""" s3_client = boto3.client('s3') # Determine content type if not provided if content_type is None: content_type = mimetypes.guess_type(file_path)[0] or 'application/octet-stream' try: # Upload the file (bucket policy should handle public access) s3_client.upload_file( str(file_path), S3_BUCKET_NAME, s3_key, ExtraArgs={ 'ContentType': content_type # No ACL setting - rely on bucket policy for public access } ) print(f"Uploaded {file_path} to s3://{S3_BUCKET_NAME}/{s3_key}") return True except FileNotFoundError: print(f"Error: File not found: {file_path}") return False except botocore.exceptions.ClientError as e: print(f"AWS Error uploading {file_path} to S3: {e}") return False except Exception as e: print(f"Unexpected error uploading {file_path} to S3: {e}") return False def main(): """Main function to upload the self-contained guide to S3.""" # Get the base directory of the project base_dir = Path(__file__).parent.parent # First, create the self-contained guide if it doesn't exist self_contained_guide_path = base_dir / 'guide' / 'AWS_Access_Key_Rotation_Guide_Self_Contained.html' if not self_contained_guide_path.exists(): print("Self-contained guide not found. Creating it now...") # Import the create_self_contained_guide script import sys sys.path.append(str(Path(__file__).parent)) import create_self_contained_guide create_self_contained_guide.main() # Upload the self-contained HTML guide success = upload_file_to_s3(self_contained_guide_path, 'aws-key-rotation-guide/aws_key_rotation_guide.html', 'text/html') if success: # Get the S3 URL using the Route53 domain s3_url = f"https://orcd-public.theorchard.io/aws-key-rotation-guide/aws_key_rotation_guide.html" print("\nGuide uploaded successfully!") print(f"Access it at: {s3_url}") print("\nRemember to update the email templates with this URL.") else: print("\nFailed to upload guide to S3. Please check your AWS credentials and permissions.") print("Make sure the bucket exists and you have write access to it.") if __name__ == "__main__": main()