#!/usr/bin/env python3

import os
import click
from policy_sentry.shared.config import create_policy_sentry_config_directory, create_audit_directory
from pathlib import Path
from policy_sentry.shared.database import connect_db, create_database

# https://gist.github.com/0xdabbad00/489c188a154cb1074f724dec375318b2
ALL_AWS_SERVICES = [
    "a4b",
    "account",
    "acm",
    "acm-pca",
    "amplify",
    "apigateway",
    "application-autoscaling",
    "appstream",
    "appsync",
    "artifact",
    "athena",
    "autoscaling",
    "autoscaling-plans",
    "aws-marketplace",
    "aws-marketplace-management",
    "aws-portal",
    "backup",
    "batch",
    "budgets",
    "ce",
    "chime",
    "cloud9",
    "clouddirectory",
    "cloudformation",
    "cloudfront",
    "cloudhsm",
    "cloudsearch",
    "cloudtrail",
    "cloudwatch",
    "codebuild",
    "codecommit",
    "codedeploy",
    "codepipeline",
    "codestar",
    "cognito-identity",
    "cognito-idp",
    "cognito-sync",
    "comprehend",
    "comprehendmedical",
    "config",
    "connect",
    "cur",
    "datapipeline",
    "datasync",
    "dax",
    "deeplens",
    "devicefarm",
    "directconnect",
    "discovery",
    "dlm",
    "dms",
    "ds",
    "dynamodb",
    "ec2",
    "ec2messages",
    "ecr",
    "ecs",
    "eks",
    "elastic-inference",
    "elasticache",
    "elasticbeanstalk",
    "elasticfilesystem",
    "elasticloadbalancing",
    "elasticmapreduce",
    "elastictranscoder",
    "es",
    "events",
    "execute-api",
    "firehose",
    "fms",
    "freertos",
    "fsx",
    "gamelift",
    "glacier",
    "globalaccelerator",
    "glue",
    "greengrass",
    "groundtruthlabeling",
    "guardduty",
    "health",
    "iam",
    "importexport",
    "inspector",
    "iot",
    "iot1click",
    "iotanalytics",
    "iotevents",
    "iotsitewise",
    "kafka",
    "kinesis",
    "kinesisanalytics",
    "kinesisvideo",
    "kms",
    "lambda",
    "lex",
    "license-manager",
    "lightsail",
    "logs",
    "machinelearning",
    "macie",
    "mechanicalturk",
    "mediaconnect",
    "mediaconvert",
    "medialive",
    "mediapackage",
    "mediastore",
    "mediatailor",
    "mgh",
    "mobileanalytics",
    "mobilehub",
    "mobiletargeting",
    "mq",
    "neptune-db",
    "opsworks",
    "opsworks-cm",
    "organizations",
    "pi",
    "polly",
    "pricing",
    "quicksight",
    "ram",
    "rds",
    "redshift",
    "rekognition",
    "resource-groups",
    "robomaker",
    "route53",
    "route53domains",
    "route53resolver",
    "s3",
    "sagemaker",
    "sdb",
    "secretsmanager",
    "securityhub",
    "serverlessrepo",
    "servicecatalog",
    "servicediscovery",
    "servicequotas",
    "ses",
    "shield",
    "signer",
    "sms",
    "sms-voice",
    "snowball",
    "sns",
    "sqs",
    "ssm",
    "ssmmessages",
    "sso",
    "sso-directory",
    "states",
    "storagegateway",
    "sts",
    "sumerian",
    "support",
    "swf",
    "tag",
    "textract",
    "transcribe",
    "transfer",
    "translate",
    "trustedadvisor",
    "waf",
    "waf-regional",
    "wam",
    "wellarchitected",
    "workdocs",
    "worklink",
    "workmail",
    "workspaces",
    "xray"
]



@click.command()
def initialize():
    """
    Create a local database to store AWS IAM information, which can be used to generate IAM policies and analyze them for least privilege.
    """
    # Create the config directory
    database_path = create_policy_sentry_config_directory()
    # Create audit directory to host list of permissions for analyze_iam_policy
    create_audit_directory()
    # Connect to the database at that path with sqlalchemy
    db_session = connect_db(database_path)
    # Fill in the database with data on the AWS services
    create_database(db_session, ALL_AWS_SERVICES)
    print("Created tables for all services!")
