From b9d7ded29b64c9d3b66ae97e966e019fe0537b62 Mon Sep 17 00:00:00 2001 From: jasquat Date: Thu, 27 Oct 2022 10:50:50 -0400 Subject: [PATCH] Squashed 'spiffworkflow-backend/' changes from 7c01d37f5..e7af92d97 e7af92d97 pyl w/ burnettk a57134009 updated development perm yml to work with mvp process model w/ burnettk 496c13120 Merge pull request #163 from sartography/feature/fewer-sentry-traces 1f5e1638c Merge pull request #162 from sartography/feature/support-wildcard-envs c9c7f9d6f fewer sentry traces 3b3315857 lint c8d2b64ce wildcard environment support git-subtree-dir: spiffworkflow-backend git-subtree-split: e7af92d979fbb7f90ba662f012baa17422146cd1 --- src/spiffworkflow_backend/__init__.py | 20 +++++++---- src/spiffworkflow_backend/config/__init__.py | 15 +++++--- src/spiffworkflow_backend/config/default.py | 7 +++- src/spiffworkflow_backend/config/demo.py | 5 +-- .../config/permissions/development.yml | 17 +++++++++- ...yml => terraform_deployed_environment.yml} | 0 .../config/terraform_deployed_environment.rb | 16 +++++++++ .../scripts/get_localtime.py | 34 +++++++++++-------- tests/data/get_localtime/get_localtime.json | 13 ++----- .../scripts/test_get_localtime.py | 30 ++++++++++------ 10 files changed, 108 insertions(+), 49 deletions(-) rename src/spiffworkflow_backend/config/permissions/{demo.yml => terraform_deployed_environment.yml} (100%) create mode 100644 src/spiffworkflow_backend/config/terraform_deployed_environment.rb diff --git a/src/spiffworkflow_backend/__init__.py b/src/spiffworkflow_backend/__init__.py index 6a876723..548cdb34 100644 --- a/src/spiffworkflow_backend/__init__.py +++ b/src/spiffworkflow_backend/__init__.py @@ -157,18 +157,26 @@ def configure_sentry(app: flask.app.Flask) -> None: return None return event - sentry_sample_rate = app.config.get("SENTRY_SAMPLE_RATE") - if sentry_sample_rate is None: - return + sentry_errors_sample_rate = app.config.get("SENTRY_ERRORS_SAMPLE_RATE") + if sentry_errors_sample_rate is None: + raise Exception("SENTRY_ERRORS_SAMPLE_RATE is not set somehow") + + sentry_traces_sample_rate = app.config.get("SENTRY_TRACES_SAMPLE_RATE") + if sentry_traces_sample_rate is None: + raise Exception("SENTRY_TRACES_SAMPLE_RATE is not set somehow") + sentry_sdk.init( dsn=app.config.get("SENTRY_DSN"), integrations=[ FlaskIntegration(), ], environment=app.config["ENV_IDENTIFIER"], - # Set traces_sample_rate to 1.0 to capture 100% + # sample_rate is the errors sample rate. we usually set it to 1 (100%) + # so we get all errors in sentry. + sample_rate=float(sentry_errors_sample_rate), + # Set traces_sample_rate to capture a certain percentage # of transactions for performance monitoring. - # We recommend adjusting this value in production. - traces_sample_rate=float(sentry_sample_rate), + # We recommend adjusting this value to less than 1(00%) in production. + traces_sample_rate=float(sentry_traces_sample_rate), before_send=before_send, ) diff --git a/src/spiffworkflow_backend/config/__init__.py b/src/spiffworkflow_backend/config/__init__.py index 0383f995..c74ea406 100644 --- a/src/spiffworkflow_backend/config/__init__.py +++ b/src/spiffworkflow_backend/config/__init__.py @@ -54,13 +54,20 @@ def setup_config(app: Flask) -> None: else: app.config.from_pyfile(f"{app.instance_path}/config.py", silent=True) - env_config_module = "spiffworkflow_backend.config." + app.config["ENV_IDENTIFIER"] + env_config_prefix = "spiffworkflow_backend.config." + env_config_module = env_config_prefix + app.config["ENV_IDENTIFIER"] try: app.config.from_object(env_config_module) except ImportStringError as exception: - raise ModuleNotFoundError( - f"Cannot find config module: {env_config_module}" - ) from exception + if ( + os.environ.get("TERRAFORM_DEPLOYED_ENVIRONMENT") == "true" + and os.environ.get("SPIFFWORKFLOW_BACKEND_ENV") is not None + ): + app.config.from_object("{env_config_prefix}terraform_deployed_environment") + else: + raise ModuleNotFoundError( + f"Cannot find config module: {env_config_module}" + ) from exception setup_database_uri(app) setup_logger(app) diff --git a/src/spiffworkflow_backend/config/default.py b/src/spiffworkflow_backend/config/default.py index b108eccb..956ed39f 100644 --- a/src/spiffworkflow_backend/config/default.py +++ b/src/spiffworkflow_backend/config/default.py @@ -47,7 +47,12 @@ SPIFFWORKFLOW_BACKEND_PERMISSIONS_FILE_NAME = environ.get( # Sentry Configuration SENTRY_DSN = environ.get("SENTRY_DSN", default="") -SENTRY_SAMPLE_RATE = environ.get("SENTRY_SAMPLE_RATE", default="1.0") +SENTRY_ERRORS_SAMPLE_RATE = environ.get( + "SENTRY_ERRORS_SAMPLE_RATE", default="1" +) # send all errors +SENTRY_TRACES_SAMPLE_RATE = environ.get( + "SENTRY_TRACES_SAMPLE_RATE", default="0.01" +) # send 1% of traces SPIFFWORKFLOW_BACKEND_LOG_LEVEL = environ.get( "SPIFFWORKFLOW_BACKEND_LOG_LEVEL", default="info" diff --git a/src/spiffworkflow_backend/config/demo.py b/src/spiffworkflow_backend/config/demo.py index a09262a6..db5abf0e 100644 --- a/src/spiffworkflow_backend/config/demo.py +++ b/src/spiffworkflow_backend/config/demo.py @@ -1,11 +1,12 @@ -"""Staging.""" +"""Demo environment.""" from os import environ GIT_COMMIT_ON_SAVE = True GIT_COMMIT_USERNAME = "demo" GIT_COMMIT_EMAIL = "demo@example.com" SPIFFWORKFLOW_BACKEND_PERMISSIONS_FILE_NAME = environ.get( - "SPIFFWORKFLOW_BACKEND_PERMISSIONS_FILE_NAME", default="demo.yml" + "SPIFFWORKFLOW_BACKEND_PERMISSIONS_FILE_NAME", + default="terraform_deployed_environment.yml", ) RUN_BACKGROUND_SCHEDULER = ( diff --git a/src/spiffworkflow_backend/config/permissions/development.yml b/src/spiffworkflow_backend/config/permissions/development.yml index 2031189a..6869667b 100644 --- a/src/spiffworkflow_backend/config/permissions/development.yml +++ b/src/spiffworkflow_backend/config/permissions/development.yml @@ -23,6 +23,21 @@ groups: Finance Team: users: [finance_user1] + Project Lead: + users: + [ + jakub, + alex, + dan, + mike, + jason, + jarrad, + elizabeth, + jon, + natalia, + manuchehr, + ] + permissions: admin: groups: [admin] @@ -50,7 +65,7 @@ permissions: uri: /v1.0/process-models/finance/* read-all: - groups: [finance, admin] + groups: [finance, admin, "Project Lead"] users: [] allowed_permissions: [read] uri: /* diff --git a/src/spiffworkflow_backend/config/permissions/demo.yml b/src/spiffworkflow_backend/config/permissions/terraform_deployed_environment.yml similarity index 100% rename from src/spiffworkflow_backend/config/permissions/demo.yml rename to src/spiffworkflow_backend/config/permissions/terraform_deployed_environment.yml diff --git a/src/spiffworkflow_backend/config/terraform_deployed_environment.rb b/src/spiffworkflow_backend/config/terraform_deployed_environment.rb new file mode 100644 index 00000000..f1be3410 --- /dev/null +++ b/src/spiffworkflow_backend/config/terraform_deployed_environment.rb @@ -0,0 +1,16 @@ +"""Terraform-deployed environment.""" +from os import environ + +# default.py already ensured that this key existed as was not None +environment_identifier_for_this_config_file_only = environ["SPIFFWORKFLOW_BACKEND_ENV"] + +GIT_COMMIT_ON_SAVE = True +GIT_COMMIT_USERNAME = environment_identifier_for_this_config_file_only +GIT_COMMIT_EMAIL = f"{environment_identifier_for_this_config_file_only}@example.com" +SPIFFWORKFLOW_BACKEND_PERMISSIONS_FILE_NAME = environ.get( + "SPIFFWORKFLOW_BACKEND_PERMISSIONS_FILE_NAME", default="terraform_deployed_environment.yml" +) + +RUN_BACKGROUND_SCHEDULER = ( + environ.get("RUN_BACKGROUND_SCHEDULER", default="false") == "true" +) diff --git a/src/spiffworkflow_backend/scripts/get_localtime.py b/src/spiffworkflow_backend/scripts/get_localtime.py index 0c7a3064..7dada59c 100644 --- a/src/spiffworkflow_backend/scripts/get_localtime.py +++ b/src/spiffworkflow_backend/scripts/get_localtime.py @@ -1,21 +1,22 @@ -from flask_bpmn.api.api_error import ApiError - -from spiffworkflow_backend.scripts.script import Script - +"""Get_localtime.""" from datetime import datetime -import pytz - from typing import Any from typing import Optional +import pytz +from flask_bpmn.api.api_error import ApiError from SpiffWorkflow.task import Task as SpiffTask # type: ignore +from spiffworkflow_backend.scripts.script import Script + class GetLocaltime(Script): + """GetLocaltime.""" def get_description(self) -> str: + """Get_description.""" return """Converts a Datetime object into a Datetime object for a specific timezone. - Defaults to US/Eastern""" + Defaults to US/Eastern.""" def run( self, @@ -24,20 +25,23 @@ class GetLocaltime(Script): *args: Any, **kwargs: Any ) -> datetime: - if len(args) > 0 or 'datetime' in kwargs: - if 'datetime' in kwargs: - date_time = kwargs['datetime'] + """Run.""" + if len(args) > 0 or "datetime" in kwargs: + if "datetime" in kwargs: + date_time = kwargs["datetime"] else: date_time = args[0] - if 'timezone' in kwargs: - timezone = kwargs['timezone'] + if "timezone" in kwargs: + timezone = kwargs["timezone"] elif len(args) > 1: timezone = args[1] else: - timezone = 'US/Eastern' + timezone = "US/Eastern" localtime: datetime = date_time.astimezone(pytz.timezone(timezone)) return localtime else: - raise ApiError(error_code='missing_datetime', - message='You must include a datetime to convert.') + raise ApiError( + error_code="missing_datetime", + message="You must include a datetime to convert.", + ) diff --git a/tests/data/get_localtime/get_localtime.json b/tests/data/get_localtime/get_localtime.json index e451f3ce..8e5297db 100644 --- a/tests/data/get_localtime/get_localtime.json +++ b/tests/data/get_localtime/get_localtime.json @@ -2,19 +2,12 @@ "title": "Get Timezone", "description": "Form to select a timezone.", "type": "object", - "required": [ - "timezone" - ], + "required": ["timezone"], "properties": { "timezone": { "type": "string", "title": "Timezone", - "enum": [ - "US/Eastern", - "US/Pacific", - "Europe/Berlin", - "Australia/ACT" - ] + "enum": ["US/Eastern", "US/Pacific", "Europe/Berlin", "Australia/ACT"] } } -} \ No newline at end of file +} diff --git a/tests/spiffworkflow_backend/scripts/test_get_localtime.py b/tests/spiffworkflow_backend/scripts/test_get_localtime.py index 9110f736..03ef995e 100644 --- a/tests/spiffworkflow_backend/scripts/test_get_localtime.py +++ b/tests/spiffworkflow_backend/scripts/test_get_localtime.py @@ -1,24 +1,34 @@ +"""Test_get_localtime.""" import datetime -import pytz +import pytz from flask.app import Flask from flask.testing import FlaskClient - -from spiffworkflow_backend.scripts.get_localtime import GetLocaltime -from spiffworkflow_backend.services.process_instance_processor import ProcessInstanceProcessor -from spiffworkflow_backend.services.process_instance_service import ProcessInstanceService - from tests.spiffworkflow_backend.helpers.base_test import BaseTest from tests.spiffworkflow_backend.helpers.test_data import load_test_spec +from spiffworkflow_backend.scripts.get_localtime import GetLocaltime +from spiffworkflow_backend.services.process_instance_processor import ( + ProcessInstanceProcessor, +) +from spiffworkflow_backend.services.process_instance_service import ( + ProcessInstanceService, +) + class TestGetLocaltime(BaseTest): """TestProcessAPi.""" def test_get_localtime_script_directly(self) -> None: + """Test_get_localtime_script_directly.""" current_time = datetime.datetime.now() timezone = "US/Pacific" - result = GetLocaltime().run(task=None, environment_identifier='testing', datetime=current_time, timezone=timezone) + result = GetLocaltime().run( + task=None, + environment_identifier="testing", + datetime=current_time, + timezone=timezone, + ) assert result == current_time.astimezone(pytz.timezone(timezone)) def test_get_localtime_script_through_bpmn( @@ -54,8 +64,8 @@ class TestGetLocaltime(BaseTest): assert spiff_task data = spiff_task.data - some_time = data['some_time'] - localtime = data['localtime'] - timezone = data['timezone'] + some_time = data["some_time"] + localtime = data["localtime"] + timezone = data["timezone"] assert localtime == some_time.astimezone(pytz.timezone(timezone))