bedrock.config

This module holds the configuration for the application.

bedrock init should have created a file called config.py in app/config/. That has the base that Bedrock expects, but you're free to add more.

The configuration variables that Bedrock expects are:

  • YOURAPP_LOG_LEVEL
  • YOURAPP_ENVIRONMENT
  • YOURAPP_DATABASE_HOST
  • YOURAPP_DATABASE_PORT
  • YOURAPP_DATABASE_NAME
  • YOURAPP_DATABASE_SCHEMA
  • YOURAPP_DATABASE_USERNAME
  • YOURAPP_DATABASE_PASSWORD
  • YOURAPP_DATABASE_TIMEOUT
  • YOURAPP_IDP_JWKS_URI
  • YOURAPP_DATABASE_SECRET_ARN
  1"""
  2This module holds the configuration for the application.
  3
  4`bedrock init` should have created a file called `config.py` in `app/config/`.
  5That has the base that Bedrock expects, but you're free to add more.
  6
  7The configuration variables that Bedrock expects are:
  8* `YOURAPP_LOG_LEVEL`
  9* `YOURAPP_ENVIRONMENT`
 10* `YOURAPP_DATABASE_HOST`
 11* `YOURAPP_DATABASE_PORT`
 12* `YOURAPP_DATABASE_NAME`
 13* `YOURAPP_DATABASE_SCHEMA`
 14* `YOURAPP_DATABASE_USERNAME`
 15* `YOURAPP_DATABASE_PASSWORD`
 16* `YOURAPP_DATABASE_TIMEOUT`
 17* `YOURAPP_IDP_JWKS_URI`
 18* `YOURAPP_DATABASE_SECRET_ARN`
 19"""
 20
 21from bedrock.helpers.environment import env, get_all_by_prefix
 22
 23_PREFIX = "BEDROCK"
 24
 25config = {
 26    "log_level": env(f"{_PREFIX}_LOG_LEVEL", "INFO").upper(),
 27    "environment": env(f"{_PREFIX}_ENVIRONMENT", "production").lower(),
 28    "envelope_by_default": env(f"{_PREFIX}_ENVELOPE_BY_DEFAULT", "true").lower() == "true",
 29    "database": {
 30        "engine": "postgres",
 31        "host": env(f"{_PREFIX}_DATABASE_HOST", "localhost"),
 32        "port": env(f"{_PREFIX}_DATABASE_PORT", "54321"),
 33        "database_name": env(f"{_PREFIX}_DATABASE_NAME", "your_app"),
 34        "schema": env(f"{_PREFIX}_DATABASE_SCHEMA", ""),
 35        "username": env(f"{_PREFIX}_DATABASE_USERNAME", "master"),
 36        "password": env(f"{_PREFIX}_DATABASE_PASSWORD", "your_app"),
 37        "options": env(f"{_PREFIX}_DATABASE_OPTIONS", ""),
 38        "timeout": int(env(f"{_PREFIX}_DATABASE_TIMEOUT", "10"))
 39    },
 40    "auth": {
 41        "idp": {
 42            "jwks_uri": env(f"{_PREFIX}_IDP_JWKS_URI", ""),
 43            "audience": env(f"{_PREFIX}_IDP_AUDIENCE", ""),
 44        },
 45        "auth0": {
 46            "jwks_uri": env(f"{_PREFIX}_AUTH0_JWKS_URI", ""),
 47            "audience": env(f"{_PREFIX}_AUTH0_AUDIENCE", ""),
 48        },
 49        "keycloak": {
 50            "jwks_uri": env(f"{_PREFIX}_KEYCLOAK_JWKS_URI", ""),
 51            "audience": env(f"{_PREFIX}_KEYCLOAK_AUDIENCE", ""),
 52        }
 53    },
 54    "secrets": {
 55        "database": env(f"{_PREFIX}_DATABASE_SECRET_ARN", "")
 56    },
 57    "storage": {
 58        "bucket_name": env(f"{_PREFIX}_STORAGE_BUCKET_NAME", ""),
 59        "region": env(f"{_PREFIX}_STORAGE_REGION", "eu-west-1"),
 60    },
 61    "msk": {
 62        "bootstrap_urls": env(f"{_PREFIX}_MSK_BOOTSTRAP_URLS", "localhost:29092"),
 63        "client_id": env(f"{_PREFIX}_MSK_CLIENT_ID", f"{_PREFIX}-kafka"),
 64        "connection_type": env(f"{_PREFIX}_MSK_CONNECTION_TYPE", "PLAINTEXT"),
 65        "connection_timeout": int(env(f"{_PREFIX}_MSK_TIMEOUT", "2000"))
 66    },
 67    # @formatter:off
 68    "allowed_keys": {
 69        "global": "",
 70        **{k.replace(f"{_PREFIX}_ALLOWED_KEYS_", "").lower().replace("_", "-"): v for k, v in get_all_by_prefix(f"{_PREFIX}_ALLOWED_KEYS_").items()}
 71    },
 72    "workers": {
 73        **{k.replace(f"{_PREFIX}_WORKER_ARN_", "").replace("_ARN", "").lower().replace("_", "-"): v for k, v in get_all_by_prefix(f"{_PREFIX}_WORKER_").items() if k.endswith("_ARN")},
 74    },
 75    "lambdas": {
 76        **{k.replace(f"{_PREFIX}_LAMBDA_ARN_", "").replace("_ARN", "").lower().replace("_", "-"): v for k, v in get_all_by_prefix(f"{_PREFIX}_LAMBDA_").items() if k.endswith("_ARN")}
 77    },
 78    # @formatter:on
 79    "aws": {
 80        "endpoint": env(f"{_PREFIX}_AWS_ENDPOINT", ""),
 81        "region": env(f"{_PREFIX}_AWS_REGION", ""),
 82        "access_key_id": env(f"{_PREFIX}_AWS_ACCESS_KEY_ID", ""),
 83        "secret_access_key": env(f"{_PREFIX}_AWS_SECRET_ACCESS_KEY", ""),
 84        "api_gateway": {
 85            "gateway_id": env(f"{_PREFIX}_AWS_API_GATEWAY_ID", "local"),
 86            "stage_name": env(f"{_PREFIX}_AWS_API_GATEWAY_STAGE_NAME", "")
 87        }
 88    },
 89    "cache": {
 90        "host": env(f"{_PREFIX}_CACHE_HOST", "localhost"),
 91        "port": int(env(f"{_PREFIX}_CACHE_PORT", "6379")),
 92        "topic_prefix": env(f"{_PREFIX}_CACHE_TOPIC_PREFIX", f"{_PREFIX.lower()}"),
 93    },
 94    "websocket": {
 95        "ping_interval": env(f"{_PREFIX}_WEBSOCKET_PING_INTERVAL", "300"),
 96    },
 97}
 98"""
 99Your config should look somewhat like this
100"""
101
102development_config = {
103    "is_local": env(f"{_PREFIX}__DEV__IS_LOCAL", "false").lower() == "true",
104} if config["environment"] in ["development", "testing"] else {}
105
106internals = {
107    "models_location": env(f"{_PREFIX}__INTERNALS__MODELS_LOCATION", "./model"),
108    "models_prefix": env(f"{_PREFIX}__INTERNALS__MODELS_PREFIX", "model"),
109    "websockets_location": env(f"{_PREFIX}__INTERNALS__WEBSOCKETS_LOCATION", "./websocket_endpoints"),
110    "websockets_prefix": env(f"{_PREFIX}__INTERNALS__WEBSOCKETS_PREFIX", "websocket_endpoints")
111}
112
113secrets = {}
114
115__version__ = "0.0.0"
116
117
118def get_prefix():  # pragma: unit
119    """
120    Gets the version of the application.
121    """
122    global _PREFIX
123    return _PREFIX
124
125
126def set_prefix(prefix: str):  # pragma: unit
127    """
128    Sets the prefix for the application. This is used to prefix environment variables.
129    :param prefix: The prefix to use. E.g. `"BEDROCK"` or `"bedrock"` will prefix environment variables with `"BEDROCK_"`
130    """
131    global _PREFIX
132    _PREFIX = prefix.upper().replace("-", "_")
133
134
135def get_version():  # pragma: unit
136    """
137    Gets the version of the application.
138    """
139    return __version__
140
141
142def set_version(version: str):  # pragma: unit
143    """
144    Sets the version for the application.
145    :param version: The version to use. E.g. `"0.0.0"`
146    """
147    global __version__
148    __version__ = version
149
150
151def get_config_params():  # pragma: unit
152    """
153    Gets the version of the application.
154    """
155    global config
156    return config
157
158
159def set_config_params(new_config: dict):  # pragma: unit
160    """
161    Sets the configuration parameters for the application.
162    This dictionary is not merged with the application's configuration, it replaces it - this is for performance reasons
163    but may change in the future.
164
165    At least, bedrock expects something like `config` above.
166
167    :param new_config: The new configuration parameters to use.
168    :return:
169    """
170    global config
171    config = new_config
172
173
174def set_secrets(secret_dict: dict):  # pragma: unit
175    """
176    Sets secrets cache.
177    """
178    global secrets
179    secrets = secret_dict
180
181
182def get_secrets():  # pragma: unit
183    """
184    Gets cached secrets.
185    """
186    global secrets
187    return secrets
188
189
190def set_config(parameters=None, version=None, prefix=None):  # pragma: unit
191    """
192    Single method to set all configuration needed.
193
194    :param parameters: See `set_config_params`
195    :param version: See `set_version`
196    :param prefix: See `set_prefix`
197    """
198    if parameters:
199        set_config_params(parameters)
200    if version:
201        set_version(version)
202    if prefix:
203        set_prefix(prefix)
config = {'log_level': 'INFO', 'environment': 'production', 'envelope_by_default': True, 'database': {'engine': 'postgres', 'host': 'localhost', 'port': '54321', 'database_name': 'your_app', 'schema': '', 'username': 'master', 'password': 'your_app', 'options': '', 'timeout': 10}, 'auth': {'idp': {'jwks_uri': '', 'audience': ''}, 'auth0': {'jwks_uri': '', 'audience': ''}, 'keycloak': {'jwks_uri': '', 'audience': ''}}, 'secrets': {'database': ''}, 'storage': {'bucket_name': '', 'region': 'eu-west-1'}, 'msk': {'bootstrap_urls': 'localhost:29092', 'client_id': 'BEDROCK-kafka', 'connection_type': 'PLAINTEXT', 'connection_timeout': 2000}, 'allowed_keys': {'global': ''}, 'workers': {}, 'lambdas': {}, 'aws': {'endpoint': '', 'region': '', 'access_key_id': '', 'secret_access_key': '', 'api_gateway': {'gateway_id': 'local', 'stage_name': ''}}, 'cache': {'host': 'localhost', 'port': 6379, 'topic_prefix': 'bedrock'}, 'websocket': {'ping_interval': '300'}}

Your config should look somewhat like this

development_config = {}
internals = {'models_location': './tests/_shared_resources/cities', 'models_prefix': 'tests._shared_resources.cities', 'websockets_location': './websocket_endpoints', 'websockets_prefix': 'websocket_endpoints'}
secrets = {}
def get_prefix():
119def get_prefix():  # pragma: unit
120    """
121    Gets the version of the application.
122    """
123    global _PREFIX
124    return _PREFIX

Gets the version of the application.

def set_prefix(prefix: str):
127def set_prefix(prefix: str):  # pragma: unit
128    """
129    Sets the prefix for the application. This is used to prefix environment variables.
130    :param prefix: The prefix to use. E.g. `"BEDROCK"` or `"bedrock"` will prefix environment variables with `"BEDROCK_"`
131    """
132    global _PREFIX
133    _PREFIX = prefix.upper().replace("-", "_")

Sets the prefix for the application. This is used to prefix environment variables.

Parameters
  • prefix: The prefix to use. E.g. "BEDROCK" or "bedrock" will prefix environment variables with "BEDROCK_"
def get_version():
136def get_version():  # pragma: unit
137    """
138    Gets the version of the application.
139    """
140    return __version__

Gets the version of the application.

def set_version(version: str):
143def set_version(version: str):  # pragma: unit
144    """
145    Sets the version for the application.
146    :param version: The version to use. E.g. `"0.0.0"`
147    """
148    global __version__
149    __version__ = version

Sets the version for the application.

Parameters
  • version: The version to use. E.g. "0.0.0"
def get_config_params():
152def get_config_params():  # pragma: unit
153    """
154    Gets the version of the application.
155    """
156    global config
157    return config

Gets the version of the application.

def set_config_params(new_config: dict):
160def set_config_params(new_config: dict):  # pragma: unit
161    """
162    Sets the configuration parameters for the application.
163    This dictionary is not merged with the application's configuration, it replaces it - this is for performance reasons
164    but may change in the future.
165
166    At least, bedrock expects something like `config` above.
167
168    :param new_config: The new configuration parameters to use.
169    :return:
170    """
171    global config
172    config = new_config

Sets the configuration parameters for the application. This dictionary is not merged with the application's configuration, it replaces it - this is for performance reasons but may change in the future.

At least, bedrock expects something like config above.

Parameters
  • new_config: The new configuration parameters to use.
Returns
def set_secrets(secret_dict: dict):
175def set_secrets(secret_dict: dict):  # pragma: unit
176    """
177    Sets secrets cache.
178    """
179    global secrets
180    secrets = secret_dict

Sets secrets cache.

def get_secrets():
183def get_secrets():  # pragma: unit
184    """
185    Gets cached secrets.
186    """
187    global secrets
188    return secrets

Gets cached secrets.

def set_config(parameters=None, version=None, prefix=None):
191def set_config(parameters=None, version=None, prefix=None):  # pragma: unit
192    """
193    Single method to set all configuration needed.
194
195    :param parameters: See `set_config_params`
196    :param version: See `set_version`
197    :param prefix: See `set_prefix`
198    """
199    if parameters:
200        set_config_params(parameters)
201    if version:
202        set_version(version)
203    if prefix:
204        set_prefix(prefix)

Single method to set all configuration needed.

Parameters