Skip to content

Commit 3e1deb5

Browse files
authored
♻️ Exposes to front-end whether payment plugin is enabled (⚠️ ops) (#4794)
1 parent f7aa8a8 commit 3e1deb5

File tree

13 files changed

+65
-60
lines changed

13 files changed

+65
-60
lines changed

.env-devel

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,11 @@ INVITATIONS_USERNAME=admin
6666

6767
LOG_FORMAT_LOCAL_DEV_ENABLED=1
6868

69+
WEBSERVER_PAYMENTS={}
6970
PAYMENTS_ACCESS_TOKEN_EXPIRE_MINUTES=30
7071
PAYMENTS_ACCESS_TOKEN_SECRET_KEY=2c0411810565e063309be1457009fb39ce023946f6a354e6935107b57676
7172
PAYMENTS_FAKE_COMPLETION_DELAY_SEC=10
72-
PAYMENTS_FAKE_COMPLETION=0 # NOTE: this can be 1 ONLY if WEBSERVER_DEV_FEATURES_ENABLED=1
73+
PAYMENTS_FAKE_COMPLETION=0
7374
PAYMENTS_GATEWAY_API_KEY=replace-with-api-key
7475
PAYMENTS_GATEWAY_API_SECRET=replace-with-api-secret
7576
PAYMENTS_GATEWAY_URL=https://fake-payment-gateway.com

packages/models-library/src/models_library/utils/string_substitution.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
import sys
66
from collections import UserDict
77
from string import Template
8-
from typing import Any
8+
from typing import Any, Final
99

10-
OSPARC_IDENTIFIER_PREFIX = "OSPARC_VARIABLE_"
10+
OSPARC_IDENTIFIER_PREFIX: Final[str] = "OSPARC_VARIABLE_"
1111

1212

1313
def upgrade_identifier(identifier: str) -> str:

services/docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ services:
386386
TWILIO_AUTH_TOKEN: ${TWILIO_AUTH_TOKEN}
387387
TWILIO_COUNTRY_CODES_W_ALPHANUMERIC_SID_SUPPORT: ${TWILIO_COUNTRY_CODES_W_ALPHANUMERIC_SID_SUPPORT}
388388

389-
# WEBSERVER_PAYMENTS
389+
WEBSERVER_PAYMENTS: ${WEBSERVER_PAYMENTS}
390390
PAYMENTS_FAKE_COMPLETION_DELAY_SEC: ${PAYMENTS_FAKE_COMPLETION_DELAY_SEC}
391391
PAYMENTS_FAKE_COMPLETION: ${PAYMENTS_FAKE_COMPLETION}
392392
PAYMENTS_FAKE_GATEWAY_URL: ${PAYMENTS_GATEWAY_URL}

services/web/server/src/simcore_service_webserver/application_settings.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,6 @@ def is_enabled(self, field_name: str) -> bool:
296296
return bool(getattr(self, field_name, None))
297297

298298
def _get_disabled_public_plugins(self) -> list[str]:
299-
plugins_disabled = []
300299
# NOTE: this list is limited for security reasons. An unbounded list
301300
# might reveal critical info on the settings of a deploy to the client.
302301
# TODO: more reliable definition of a "plugin" and whether it can be advertised or not
@@ -305,13 +304,15 @@ def _get_disabled_public_plugins(self) -> list[str]:
305304
"WEBSERVER_CLUSTERS",
306305
"WEBSERVER_EXPORTER",
307306
"WEBSERVER_META_MODELING",
307+
"WEBSERVER_PAYMENTS",
308308
"WEBSERVER_SCICRUNCH",
309309
"WEBSERVER_VERSION_CONTROL",
310310
}
311-
for field_name in PUBLIC_PLUGIN_CANDIDATES:
312-
if not self.is_enabled(field_name):
313-
plugins_disabled.append(field_name)
314-
return plugins_disabled
311+
return [
312+
plugin_name
313+
for plugin_name in PUBLIC_PLUGIN_CANDIDATES
314+
if not self.is_enabled(plugin_name)
315+
]
315316

316317
def _export_by_alias(self, **kwargs) -> dict[str, Any]:
317318
# This is a small helper to assist export functions since aliases are no longer used by

services/web/server/src/simcore_service_webserver/application_settings_utils.py

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,24 @@
55
# refactoring would report very little. E.g. many test fixtures were based on given configs
66
#
77

8+
import functools
89
import logging
910
from typing import Any
1011

12+
from aiohttp import web
1113
from pydantic.types import SecretStr
14+
from servicelib.aiohttp.typing_extension import Handler
1215

13-
from .application_settings import ApplicationSettings
16+
from ._constants import MSG_UNDER_DEVELOPMENT
17+
from .application_settings import ApplicationSettings, get_settings
1418

1519
_logger = logging.getLogger(__name__)
1620

1721

1822
def convert_to_app_config(app_settings: ApplicationSettings) -> dict[str, Any]:
1923
"""Maps current ApplicationSettings object into former trafaret-based config"""
2024

21-
cfg = {
25+
return {
2226
"version": "1.0",
2327
"main": {
2428
"host": app_settings.WEBSERVER_SERVER_HOST,
@@ -33,7 +37,6 @@ def convert_to_app_config(app_settings: ApplicationSettings) -> dict[str, Any]:
3337
},
3438
"tracing": {
3539
"enabled": 1 if app_settings.WEBSERVER_TRACING is not None else 0,
36-
# "zipkin_endpoint": f"{getattr(app_settings.WEBSERVER_TRACING, 'TRACING_ZIPKIN_ENDPOINT', None)}",
3740
},
3841
"socketio": {"enabled": app_settings.WEBSERVER_SOCKETIO},
3942
"db": {
@@ -166,14 +169,17 @@ def convert_to_app_config(app_settings: ApplicationSettings) -> dict[str, Any]:
166169
"wallets": {"enabled": app_settings.WEBSERVER_WALLETS},
167170
}
168171

169-
return cfg
170172

173+
def convert_to_environ_vars( # noqa: C901, PLR0915, PLR0912
174+
cfg: dict[str, Any]
175+
) -> dict[str, Any]:
176+
"""Creates envs dict out of config dict
171177
172-
def convert_to_environ_vars(cfg: dict[str, Any]) -> dict[str, Any]:
173-
"""Creates envs dict out of config dict"""
174-
# NOTE: maily used for testing traferet vs settings_library
178+
NOTE: ONLY used to support legacy introduced by traferet vs settings_library.
179+
"""
175180
# pylint:disable=too-many-branches
176181
# pylint:disable=too-many-statements
182+
177183
envs = {}
178184

179185
def _set_if_disabled(field_name, section):
@@ -182,9 +188,8 @@ def _set_if_disabled(field_name, section):
182188
field = ApplicationSettings.__fields__[field_name]
183189
if not enabled:
184190
envs[field_name] = "null" if field.allow_none else "0"
185-
else:
186-
if field.type_ == bool:
187-
envs[field_name] = "1"
191+
elif field.type_ == bool:
192+
envs[field_name] = "1"
188193

189194
if main := cfg.get("main"):
190195
envs["WEBSERVER_PORT"] = main.get("port")
@@ -193,7 +198,6 @@ def _set_if_disabled(field_name, section):
193198

194199
if section := cfg.get("tracing"):
195200
_set_if_disabled("WEBSERVER_TRACING", section)
196-
# envs["TRACING_ZIPKIN_ENDPOINT"] = section.get("zipkin_endpoint")
197201

198202
if db := cfg.get("db"):
199203
if section := db.get("postgres"):
@@ -312,3 +316,19 @@ def _set_if_disabled(field_name, section):
312316

313317
# NOTE: The final envs list prunes all env vars set to None
314318
return {k: v for k, v in envs.items() if v is not None}
319+
320+
321+
#
322+
# decorators
323+
#
324+
325+
326+
def requires_dev_feature_enabled(handler: Handler):
327+
@functools.wraps(handler)
328+
async def _handler_under_dev(request: web.Request):
329+
app_settings = get_settings(request.app)
330+
if not app_settings.WEBSERVER_DEV_FEATURES_ENABLED:
331+
raise NotImplementedError(MSG_UNDER_DEVELOPMENT)
332+
return await handler(request)
333+
334+
return _handler_under_dev

services/web/server/src/simcore_service_webserver/payments/settings.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ class PaymentsSettings(BaseCustomSettings, MixinServiceSettings):
3131
)
3232

3333
# NOTE: PAYMENTS_FAKE_* settings are temporary until some features are moved to the payments service
34-
3534
PAYMENTS_FAKE_COMPLETION: bool = Field(
3635
default=False, description="Enables fake completion. ONLY for testing purposes"
3736
)
@@ -68,9 +67,9 @@ def base_url(self) -> str:
6867

6968
@validator("PAYMENTS_FAKE_COMPLETION")
7069
@classmethod
71-
def check_dev_feature_enabled(cls, v):
72-
if v and not os.environ.get("WEBSERVER_DEV_FEATURES_ENABLED", False):
73-
msg = "PAYMENTS_FAKE_COMPLETION only allowed when WEBSERVER_DEV_FEATURES_ENABLED=1"
70+
def _payments_cannot_be_faken_in_production(cls, v):
71+
if v is True and "production" in os.environ.get("SWARM_STACK_NAME", ""):
72+
msg = "PAYMENTS_FAKE_COMPLETION only allowed FOR TESTING PURPOSES and cannot be released to production"
7473
raise ValueError(msg)
7574
return v
7675

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
from typing import Final
22

33
APP_RABBITMQ_CONSUMERS_KEY: Final[str] = f"{__name__}.rabbit_consumers"
4+
5+
MSG_RESOURCE_USAGE_TRACKER_SERVICE_UNAVAILABLE: Final[
6+
str
7+
] = "Currently resource usage tracker service is unavailable, please try again later"

services/web/server/src/simcore_service_webserver/resource_usage/_observer.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
33
"""
44

5-
# pylint: disable=unused-argument
65

76
import logging
87

@@ -27,6 +26,9 @@ async def _on_user_disconnected(
2726
app: web.Application,
2827
product_name: ProductName,
2928
) -> None:
29+
assert product_name # nosec
30+
assert client_session_id # nosec
31+
3032
# Get all user wallets and unsubscribe
3133
user_wallet = await wallets_api.list_wallets_for_user(
3234
app, user_id=user_id, product_name=product_name

services/web/server/src/simcore_service_webserver/resource_usage/_utils.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
import asyncio
22
import logging
3+
from collections.abc import Iterator
34
from contextlib import contextmanager
4-
from typing import Final, Iterator
55

66
from aiohttp import ClientSession, web
77
from aiohttp.client_exceptions import ClientConnectionError, ClientResponseError
88
from servicelib.aiohttp.client_session import get_client_session
99

10-
_logger = logging.getLogger(__name__)
11-
10+
from ._constants import MSG_RESOURCE_USAGE_TRACKER_SERVICE_UNAVAILABLE
1211

13-
MSG_RESOURCE_USAGE_TRACKER_SERVICE_UNAVAILABLE: Final[
14-
str
15-
] = "Currently resource usage tracker service is unavailable, please try again later"
12+
_logger = logging.getLogger(__name__)
1613

1714

1815
@contextmanager

services/web/server/src/simcore_service_webserver/resource_usage/resource_usage_tracker_client.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ async def list_service_runs_by_user_and_product(
5151

5252
async def list_service_runs_by_user_and_product_and_wallet(
5353
app: web.Application,
54+
*,
5455
user_id: UserID,
5556
product_name: str,
5657
wallet_id: WalletID,

0 commit comments

Comments
 (0)