"""Constants and settings for collaborator report generation.""" import os import boto3 import sentry_sdk from cryptography.hazmat.backends import default_backend from cryptography.hazmat.primitives import serialization from sentry_sdk.integrations.aws_lambda import AwsLambdaIntegration SERVICE_NAME = "lambda-collaborator-report-trigger" # Environment PROD_ENVIRONMENT = "prod" DEV_ENVIRONMENT = "dev" QA_ENVIRONMENT = "qa" TEST_ENVIRONMENT = "test" ENVIRONMENT = os.environ.get("Environment", DEV_ENVIRONMENT) # noqa: SIM112 SENTRY_DSN = os.getenv("SENTRY_DSN") def _get_secret(secrets_manager, secret_name): """Get a secrets from the secrets manager. Args: secrets_manager (SecretsManager.Client): Manager to get secret from secret_name (str): Name of the secret to retrieve Returns: str """ secret_id = f"{ENVIRONMENT}/{SERVICE_NAME}/{secret_name}" secret_response = secrets_manager.get_secret_value(SecretId=secret_id) return secret_response["SecretString"] COLLABORATOR_DB_CONFIG = { "user": os.environ.get("COLLAB_DB_USER"), "host": os.environ.get("COLLAB_DB_HOST"), "database": os.environ.get("COLLAB_DB_DATABASE"), } if ENVIRONMENT in [PROD_ENVIRONMENT, QA_ENVIRONMENT]: session = boto3.Session() secrets_manager_client = session.client("secretsmanager") passphrase = _get_secret(secrets_manager_client, "SNOWFLAKE_KEY_PASSPHRASE") private_key = _get_secret(secrets_manager_client, "SNOWFLAKE_PRIVATE_KEY") private_key_bytes = bytes(private_key, "utf-8") sentry_dsn = _get_secret(secrets_manager_client, "SENTRY_DSN") if sentry_dsn: sentry_sdk.init( dsn=sentry_dsn, environment=ENVIRONMENT, integrations=[AwsLambdaIntegration()], ) COLLABORATOR_DB_CONFIG["password"] = _get_secret(secrets_manager_client, "COLLAB_DB_PASS") else: passphrase = os.environ.get("SNOWFLAKE_KEY_PASSPHRASE", "") private_key_path = os.environ.get("SNOWFLAKE_PRIVATE_KEY_PATH") private_key = os.environ.get("SNOWFLAKE_PRIVATE_KEY") if private_key_path: with open(private_key_path, "rb") as key: private_key_bytes = key.read() elif private_key: private_key_bytes = bytes(private_key, "utf-8") elif ENVIRONMENT != TEST_ENVIRONMENT: raise ValueError("SNOWFLAKE_PRIVATE_KEY_PATH or SNOWFLAKE_PRIVATE_KEY must be set.") COLLABORATOR_DB_CONFIG["password"] = os.environ.get("COLLAB_DB_PASS") if ENVIRONMENT != TEST_ENVIRONMENT: passphrase_bytes = bytes(passphrase, "utf-8") decrypted_key = serialization.load_pem_private_key( private_key_bytes, password=(passphrase_bytes or None), backend=default_backend(), ) decrypted_key_bytes = decrypted_key.private_bytes( encoding=serialization.Encoding.DER, format=serialization.PrivateFormat.PKCS8, encryption_algorithm=serialization.NoEncryption(), ) else: decrypted_key_bytes = bytes("", "utf-8") SNOWFLAKE_DB_CONFIG = { "role": os.environ.get("SNOWFLAKE_ROLE"), "warehouse": os.environ.get("SNOWFLAKE_WAREHOUSE"), "db": os.environ.get("SNOWFLAKE_DATABASE"), "database": os.environ.get("SNOWFLAKE_DATABASE"), "schema": os.environ.get("SNOWFLAKE_SCHEMA"), "user": os.environ.get("SNOWFLAKE_USER"), "account": os.environ.get("SNOWFLAKE_ACCOUNT"), "private_key": decrypted_key_bytes, } SNOWFLAKE_OBJECTS = { "s3_stage": os.environ.get("SNOWFLAKE_S3_STAGE"), "temp_split": "temp_split", "temp_collaborator": "temp_collaborator", "dim_track": os.environ.get("SNOWFLAKE_DIM_TRACK"), "dim_track_artists_aggregated": os.environ.get("SNOWFLAKE_DIM_TRACK_ARTISTS_AGGREGATED"), "dim_transactiontype": os.environ.get("SNOWFLAKE_DIM_TRANSACTIONTYPE"), "dim_country": os.environ.get("SNOWFLAKE_DIM_COUNTRY"), "dim_store": os.environ.get("SNOWFLAKE_DIM_STORE"), "dim_label": os.environ.get("SNOWFLAKE_DIM_LABEL"), "dim_release": os.environ.get("SNOWFLAKE_DIM_RELEASE"), "dim_artist": os.environ.get("SNOWFLAKE_DIM_ARTIST"), "abacus_fact_sales": os.environ.get("SNOWFLAKE_ABACUS_FACT_SALES"), "unified_fact_sales": os.environ.get("SNOWFLAKE_UNIFIED_FACT_SALES"), "abacus_statement_period": os.environ.get("SNOWFLAKE_ABACUS_STATEMENT_PERIOD"), } TRANSACTION_TYPES_PERFORMANCE_RIGHTS = [ "RC", # Performance Royalty Collections "EH", # Performance Royalty Collections - Ephemeral Portion "PX", # Audiovisual "CC", # Cable Radio "CX", # Copyright Term Extension "ER", # Exclusive Rights "RW", # Internet Radio and Webcasting "MA", # Making Available / Streaming "MD", # Music Dubbing "NC", # Non-Controlled Rights Owner Income "PA", # Private Copying - Audio "PV", # Private Copying - Audiovisual "PP", # Public Performance "RR", # Radio Broadcast "RL", # Rental & Lending "SL", # Satellite Radio "TV", # TV Broadcast "UP", # Unidentified Private Copying "UN", # Unidentified Public Performance and Broadcast ]