#---------------------------------------------------------------
#                         CONFIGURATION
#---------------------------------------------------------------
kind: ConfigMap
apiVersion: v1
metadata:
  namespace: monitor
  name: sentry
data:
  SENTRY_SECRET_KEY: "GENERATE"
  SENTRY_EVENT_RETENTION_DAYS: "30"

  SENTRY_POSTGRES_HOST: postgresql.sentry.svc.cluster.local
  SENTRY_POSTGRES_PORT: "5432"
  SENTRY_DB_USER: postgresql
  SENTRY_DB_PASSWORD: admin123
  SENTRY_DB_NAME: sentry

  SENTRY_REDIS_HOST: redis.sentry.svc.cluster.local
  SENTRY_REDIS_PORT: "6379"

  DEFAULT_BROKERS: "cp-kafka.sentry.svc.cluster.local:9092"

  SNUBA: 'http://sentry-snuba-api:1218'

  SENTRY_EMAIL_HOST: "host"
  SENTRY_EMAIL_PORT: "587"
  SENTRY_EMAIL_USER: "usernmae"
  SENTRY_EMAIL_PASSWORD: "pass"
  SENTRY_EMAIL_USE_TLS: "true"
  SENTRY_SERVER_EMAIL: no-reply@email.com

  SENTRY_ADMIN_USER_EMAIL: admin@email.com
  SENTRY_ADMIN_USER_PASSWORD: admin

  config.yml: |-
    # While a lot of configuration in Sentry can be changed via the UI, for all
    # new-style config (as of 8.0) you can also declare values here in this file
    # to enforce defaults or to ensure they cannot be changed via the UI. For more
    # information see the Sentry documentation.

    ################
    # File storage #
    ################
    # Uploaded media uses these `filestore` settings. The available
    # backends are either `filesystem` or `s3`.

    filestore.backend: 'filesystem'
    filestore.options:
      location: '/data/files'
    dsym.cache-path: '/data/dsym-cache'
    releasefile.cache-path: '/data/releasefile-cache'

    #-----------------------------------
    system.internal-url-prefix: 'http://sentry-web:9000'
    symbolicator.enabled: false
    symbolicator.options:
      url: "http://symbolicator:3021"

    transaction-events.force-disable-internal-project: true

    ######################
    # GitHub Integration #
    ######################
    github-app.id: 
    github-app.name: ''
    github-app.webhook-secret: '' # Use only if configured in GitHub
    github-app.client-id: ''
    github-app.client-secret: ''
    github-app.private-key: |
        -----BEGIN RSA PRIVATE KEY-----
        
        -----END RSA PRIVATE KEY-----

    #####################
    # Slack Integration #
    #####################
    # Refer to https://develop.sentry.dev/integrations/slack/ for setup instructions.

    slack.client-id: ''
    slack.client-secret: ''
    #slack.signing-secret: <signing secret>
    ## If you made your slack bot before july 2020 set legacy-app to True
    slack.legacy-app: True
    ## If legacy-app is True use verfication-token instead of signing-secret
    slack.verification-token: ''

  sentry.conf.py: |-
    # flake8: noqa
    from __future__ import absolute_import
    from sentry.conf.server import *
    from sentry.utils.types import Bool

    import os
    import os.path

    CONF_ROOT = os.path.dirname(__file__)
    env = os.environ.get

    postgres = env("SENTRY_POSTGRES_HOST") or (env("POSTGRES_PORT_5432_TCP_ADDR") and "postgres")
    if postgres:
        DATABASES = {
            "default": {
                "ENGINE": "sentry.db.postgres",
                "NAME": (env("SENTRY_DB_NAME") or env("POSTGRES_ENV_POSTGRES_USER") or "postgres"),
                "USER": (env("SENTRY_DB_USER") or env("POSTGRES_ENV_POSTGRES_USER") or "postgres"),
                "PASSWORD": (env("SENTRY_DB_PASSWORD") or env("POSTGRES_ENV_POSTGRES_PASSWORD") or ""),
                "HOST": postgres,
                "PORT": (env("SENTRY_POSTGRES_PORT") or ""),
            }
        }

    # You should not change this setting after your database has been created
    # unless you have altered all schemas first
    SENTRY_USE_BIG_INTS = True

    # If you're expecting any kind of real traffic on Sentry, we highly recommend
    # configuring the CACHES and Redis settings

    ###########
    # General #
    ###########

    # Instruct Sentry that this install intends to be run by a single organization
    # and thus various UI optimizations should be enabled.
    SENTRY_SINGLE_ORGANIZATION = True

    SENTRY_OPTIONS["system.event-retention-days"] = int(
        env("SENTRY_EVENT_RETENTION_DAYS", "30")
    )

    #########
    # Redis #
    #########

    # Generic Redis configuration used as defaults for various things including:
    # Buffers, Quotas, TSDB

    redis = env("SENTRY_REDIS_HOST") or (env("REDIS_PORT_6379_TCP_ADDR") and "redis")
    if not redis:
        raise Exception(
            "Error: REDIS_PORT_6379_TCP_ADDR (or SENTRY_REDIS_HOST) is undefined, did you forget to `--link` a redis container?"
        )

    redis_password = env("SENTRY_REDIS_PASSWORD") or ""
    redis_port = env("SENTRY_REDIS_PORT") or "6379"
    redis_db = env("SENTRY_REDIS_DB") or "0"

    SENTRY_OPTIONS.update(
        {
            "redis.clusters": {
                "default": {
                    "hosts": {
                        0: {
                            "host": redis,
                            "password": redis_password,
                            "port": redis_port,
                            "db": redis_db,
                        }
                    }
                }
            }
        }
    )

    #########
    # Queue #
    #########

    # See https://docs.getsentry.com/on-premise/server/queue/ for more
    # information on configuring your queue broker and workers. Sentry relies
    # on a Python framework called Celery to manage queues.

    rabbitmq = env("SENTRY_RABBITMQ_HOST") or (env("RABBITMQ_PORT_5672_TCP_ADDR") and "rabbitmq")

    if rabbitmq:
        BROKER_URL = (
            "amqp://"
            + (env("SENTRY_RABBITMQ_USERNAME") or env("RABBITMQ_ENV_RABBITMQ_DEFAULT_USER") or "guest")
            + ":"
            + (env("SENTRY_RABBITMQ_PASSWORD") or env("RABBITMQ_ENV_RABBITMQ_DEFAULT_PASS") or "guest")
            + "@"
            + rabbitmq
            + "/"
            + (env("SENTRY_RABBITMQ_VHOST") or env("RABBITMQ_ENV_RABBITMQ_DEFAULT_VHOST") or "/")
        )
    else:
        BROKER_URL = "redis://:" + redis_password + "@" + redis + ":" + redis_port + "/" + redis_db

    #########
    # Cache #
    #########

    # Sentry currently utilizes two separate mechanisms. While CACHES is not a
    # requirement, it will optimize several high throughput patterns.

    memcached = env("SENTRY_MEMCACHED_HOST") or (env("MEMCACHED_PORT_11211_TCP_ADDR") and "memcached")
    if memcached:
        memcached_port = env("SENTRY_MEMCACHED_PORT") or "11211"
        CACHES = {
            "default": {
                "BACKEND": "django.core.cache.backends.memcached.MemcachedCache",
                "LOCATION": [memcached + ":" + memcached_port],
                "TIMEOUT": 3600,
            }
        }

    # A primary cache is required for things such as processing events
    SENTRY_CACHE = "sentry.cache.redis.RedisCache"

    #########
    # BROKERS #
    #########
    DEFAULT_KAFKA_OPTIONS = {
        "bootstrap.servers": env("DEFAULT_BROKERS"),
        "message.max.bytes": 50000000,
        "socket.timeout.ms": 1000,
    }

    SENTRY_EVENTSTREAM = "sentry.eventstream.kafka.KafkaEventStream"
    SENTRY_EVENTSTREAM_OPTIONS = {"producer_configuration": DEFAULT_KAFKA_OPTIONS}

    KAFKA_CLUSTERS["default"] = DEFAULT_KAFKA_OPTIONS

    ###############
    # Rate Limits #
    ###############

    # Rate limits apply to notification handlers and are enforced per-project
    # automatically.

    SENTRY_RATELIMITER = "sentry.ratelimits.redis.RedisRateLimiter"

    ##################
    # Update Buffers #
    ##################

    # Buffers (combined with queueing) act as an intermediate layer between the
    # database and the storage API. They will greatly improve efficiency on large
    # numbers of the same events being sent to the API in a short amount of time.
    # (read: if you send any kind of real data to Sentry, you should enable buffers)

    SENTRY_BUFFER = "sentry.buffer.redis.RedisBuffer"

    ##########
    # Quotas #
    ##########

    # Quotas allow you to rate limit individual projects or the Sentry install as
    # a whole.

    SENTRY_QUOTAS = "sentry.quotas.redis.RedisQuota"

    ########
    # TSDB #
    ########

    # The TSDB is used for building charts as well as making things like per-rate
    # alerts possible.

    SENTRY_TSDB = "sentry.tsdb.redissnuba.RedisSnubaTSDB"

    #########
    # SNUBA #
    #########

    SENTRY_SEARCH = "sentry.search.snuba.EventsDatasetSnubaSearchBackend"
    SENTRY_SEARCH_OPTIONS = {}
    SENTRY_TAGSTORE_OPTIONS = {}

    ###########
    # Digests #
    ###########

    # The digest backend powers notification summaries.

    SENTRY_DIGESTS = "sentry.digests.backends.redis.RedisBackend"

    ##############
    # Web Server #
    ##############

    SENTRY_WEB_HOST = "0.0.0.0"
    SENTRY_WEB_PORT = 9000
    SENTRY_WEB_OPTIONS = {
        "http": "%s:%s" % (SENTRY_WEB_HOST, SENTRY_WEB_PORT),
        "protocol": "uwsgi",
        # This is needed in order to prevent https://git.io/fj7Lw
        "uwsgi-socket": None,
        "so-keepalive": True,
        # Keep this between 15s-75s as that's what Relay supports
        "http-keepalive": 15,
        "http-chunked-input": True,
        # the number of web workers
        "workers": 3,
        "threads": 4,
        "memory-report": False,
        # Some stuff so uwsgi will cycle workers sensibly
        "max-requests": 100000,
        "max-requests-delta": 500,
        "max-worker-lifetime": 86400,
        # Duplicate options from sentry default just so we don't get
        # bit by sentry changing a default value that we depend on.
        "thunder-lock": True,
        "log-x-forwarded-for": False,
        "buffer-size": 32768,
        "limit-post": 209715200,
        "disable-logging": True,
        "reload-on-rss": 600,
        "ignore-sigpipe": True,
        "ignore-write-errors": True,
        "disable-write-exception": True,
    }

    ###############
    # Mail Server #
    ###############


    email = env("SENTRY_EMAIL_HOST") or (env("SMTP_PORT_25_TCP_ADDR") and "smtp")
    if email:
        SENTRY_OPTIONS["mail.backend"] = "smtp"
        SENTRY_OPTIONS["mail.host"] = email
        SENTRY_OPTIONS["mail.password"] = env("SENTRY_EMAIL_PASSWORD") or ""
        SENTRY_OPTIONS["mail.username"] = env("SENTRY_EMAIL_USER") or ""
        SENTRY_OPTIONS["mail.port"] = int(env("SENTRY_EMAIL_PORT") or 25)
        SENTRY_OPTIONS["mail.use-tls"] = Bool(env("SENTRY_EMAIL_USE_TLS", False))
    else:
        SENTRY_OPTIONS["mail.backend"] = "dummy"

    # The email address to send on behalf of
    SENTRY_OPTIONS["mail.from"] = env("SENTRY_SERVER_EMAIL") or "root@localhost"

    # If you're using mailgun for inbound mail, set your API key and configure a
    # route to forward to /api/hooks/mailgun/inbound/
    SENTRY_OPTIONS["mail.mailgun-api-key"] = env("SENTRY_MAILGUN_API_KEY") or ""

    # If you specify a MAILGUN_API_KEY, you definitely want EMAIL_REPLIES
    if SENTRY_OPTIONS["mail.mailgun-api-key"]:
        SENTRY_OPTIONS["mail.enable-replies"] = True
    else:
        SENTRY_OPTIONS["mail.enable-replies"] = Bool(env("SENTRY_ENABLE_EMAIL_REPLIES", False))

    if SENTRY_OPTIONS["mail.enable-replies"]:
        SENTRY_OPTIONS["mail.reply-hostname"] = env("SENTRY_SMTP_HOSTNAME") or ""

    # If this value ever becomes compromised, it's important to regenerate your
    # SENTRY_SECRET_KEY. Changing this value will result in all current sessions
    # being invalidated.
    secret_key = env("SENTRY_SECRET_KEY")
    if not secret_key:
        raise Exception(
            "Error: SENTRY_SECRET_KEY is undefined, run `generate-secret-key` and set to -e SENTRY_SECRET_KEY"
        )

    if "SENTRY_RUNNING_UWSGI" not in os.environ and len(secret_key) < 32:
        print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
        print("!!                    CAUTION                       !!")
        print("!! Your SENTRY_SECRET_KEY is potentially insecure.  !!")
        print("!!    We recommend at least 32 characters long.     !!")
        print("!!     Regenerate with `generate-secret-key`.       !!")
        print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")

    SENTRY_OPTIONS["system.secret-key"] = secret_key

    ############
    # GEOIP    #
    ############

    GEOIP_PATH_MMDB = '/data/GeoLite2-City.mmdb'

    ############
    # Features #
    ############

    SENTRY_FEATURES["projects:sample-events"] = False
    SENTRY_FEATURES.update(
        {
            feature: True
            for feature in (
                "organizations:discover",
                "organizations:events",
                "organizations:global-views",
                "organizations:incidents",
                "organizations:integrations-issue-basic",
                "organizations:integrations-issue-sync",
                "organizations:invite-members",
                "organizations:metric-alert-builder-aggregate",
                "organizations:sso-basic",
                "organizations:sso-rippling",
                "organizations:sso-saml2",
                "organizations:performance-view",
                "organizations:advanced-search",
                "projects:custom-inbound-filters",
                "projects:data-forwarding",
                "projects:discard-groups",
                "projects:plugins",
                "projects:rate-limits",
                "projects:servicehooks",
            )
        }
    )
