Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 0 additions & 36 deletions .basedpyright/baseline.json
Original file line number Diff line number Diff line change
Expand Up @@ -1032,16 +1032,6 @@
}
}
],
"./monitoring/mock_uss/__init__.py": [
{
"code": "reportOptionalMemberAccess",
"range": {
"startColumn": 53,
"endColumn": 58,
"lineCount": 1
}
}
],
"./monitoring/mock_uss/auth.py": [
{
"code": "reportArgumentType",
Expand Down Expand Up @@ -1490,24 +1480,6 @@
}
}
],
"./monitoring/mock_uss/msgsigning/__init__.py": [
{
"code": "reportAttributeAccessIssue",
"range": {
"startColumn": 14,
"endColumn": 20,
"lineCount": 1
}
},
{
"code": "reportAttributeAccessIssue",
"range": {
"startColumn": 39,
"endColumn": 61,
"lineCount": 1
}
}
],
"./monitoring/mock_uss/msgsigning/routes_msgsigning.py": [
{
"code": "reportCallIssue",
Expand All @@ -1524,14 +1496,6 @@
"endColumn": 52,
"lineCount": 1
}
},
{
"code": "reportAttributeAccessIssue",
"range": {
"startColumn": 33,
"endColumn": 51,
"lineCount": 1
}
}
],
"./monitoring/mock_uss/riddp/clustering.py": [
Expand Down
142 changes: 0 additions & 142 deletions monitoring/mock_uss/__init__.py
Original file line number Diff line number Diff line change
@@ -1,142 +0,0 @@
import inspect
import os
from collections.abc import Callable
from typing import Any

# Because mock_uss uses gevent, we need to monkey-patch before anything else is loaded.
# https://www.gevent.org/intro.html#monkey-patching
from gevent import monkey

monkey.patch_all()

from loguru import logger # noqa E402
from werkzeug.middleware.proxy_fix import ProxyFix # noqa E402

from monitoring.mock_uss.server import MockUSS # noqa E402

SERVICE_GEOAWARENESS = "geoawareness"
SERVICE_RIDSP = "ridsp"
SERVICE_RIDDP = "riddp"
SERVICE_SCDSC = "scdsc"
SERVICE_MESSAGESIGNING = "msgsigning"
SERVICE_TRACER = "tracer"
SERVICE_INTERACTION_LOGGING = "interaction_logging"
SERVICE_VERSIONING = "versioning"
SERVICE_FLIGHT_PLANNING = "flight_planning"

webapp = MockUSS(__name__)
webapp.secret_key = os.environ.get("SECRET_KEY") or os.urandom(24)
if os.environ.get("MOCK_USS_PROXY_VALUES"):
values = os.environ.get("MOCK_USS_PROXY_VALUES").split(",")
kwargs = {v.split("=")[0].strip(): int(v.split("=")[1]) for v in values}
webapp.wsgi_app = ProxyFix(webapp.wsgi_app, **kwargs)
enabled_services = set()


def import_environment_variable(
var_name: str,
required: bool = True,
default: str | None = None,
mutator: Callable[[str], Any] | None = None,
) -> None:
"""Import a value from a named environment variable into the webapp configuration.

Args:
var_name: Environment variable name (key). Also used as the webapp configuration key for that variable.
required: Whether the variable must be specified by the user. If True, a ValueError will be raised if the
variable is not specified by the user. If False, the webapp configuration will not be populated if no
default is provided. If default is specified, the default value is treated as specification by the user.
default: If the variable is not required, then use this value when it is not specified by the user. The default
value should be the string from the environment variable rather than the output of the mutator, if present.
mutator: If specified, apply this function to the string value of the environment variable to obtain the
variable to actually store in the configuration.
"""
if var_name in os.environ:
str_value = os.environ[var_name]
elif default is not None:
str_value = default
elif required:
stack = inspect.stack()
raise ValueError(
f"System cannot proceed because required environment variable '{var_name}' was not found. Required from {stack[1].filename}:{stack[1].lineno}"
)
else:
str_value = None

if str_value is not None:
webapp.config[var_name] = str_value if mutator is None else mutator(str_value)


def require_config_value(config_key: str) -> None:
if config_key not in webapp.config:
stack = inspect.stack()
raise ValueError(
f"System cannot proceed because required configuration key '{config_key}' was not found. Required from {stack[1].filename}:{stack[1].lineno}"
)


from monitoring.mock_uss import config # noqa E402
from monitoring.mock_uss import logging as logging # noqa E402
from monitoring.mock_uss import routes as basic_routes # noqa F401,F402

if SERVICE_GEOAWARENESS in webapp.config[config.KEY_SERVICES]:
enabled_services.add(SERVICE_GEOAWARENESS)
from monitoring.mock_uss import geoawareness as geoawareness
from monitoring.mock_uss.geoawareness import (
routes as geoawareness_routes, # noqa F401
)

if SERVICE_RIDSP in webapp.config[config.KEY_SERVICES]:
enabled_services.add(SERVICE_RIDSP)
from monitoring.mock_uss import ridsp as ridsp
from monitoring.mock_uss.ridsp import routes as ridsp_routes # noqa F401

if SERVICE_RIDDP in webapp.config[config.KEY_SERVICES]:
enabled_services.add(SERVICE_RIDDP)
from monitoring.mock_uss import riddp as riddp
from monitoring.mock_uss.riddp import routes as riddp_routes # noqa F401

if SERVICE_SCDSC in webapp.config[config.KEY_SERVICES]:
enabled_services.add(SERVICE_SCDSC)
from monitoring.mock_uss.f3548v21 import routes_scd as routes_scd
from monitoring.mock_uss.scd_injection import (
routes as scd_injection_routes, # noqa F401
)

if SERVICE_MESSAGESIGNING in webapp.config[config.KEY_SERVICES]:
enabled_services.add(SERVICE_MESSAGESIGNING)
from monitoring.mock_uss import msgsigning as msgsigning # noqa F401
from monitoring.mock_uss.msgsigning import routes as msgsigning_routes # noqa F401

if SERVICE_INTERACTION_LOGGING in webapp.config[config.KEY_SERVICES]:
enabled_services.add(SERVICE_INTERACTION_LOGGING)
from monitoring.mock_uss.interaction_logging import logger as interactions_logger # noqa F401
from monitoring.mock_uss.interaction_logging import (
routes_interactions_log as routes_interactions_log,
)

logger.info("Interaction logging enabled")
else:
logger.info("Interaction logging disabled")

if SERVICE_TRACER in webapp.config[config.KEY_SERVICES]:
enabled_services.add(SERVICE_TRACER)
from monitoring.mock_uss import tracer as tracer
from monitoring.mock_uss.tracer import routes as tracer_routes # noqa F401

if SERVICE_VERSIONING in webapp.config[config.KEY_SERVICES]:
enabled_services.add(SERVICE_VERSIONING)
from monitoring.mock_uss.versioning import routes as versioning_routes # noqa F401

if SERVICE_FLIGHT_PLANNING in webapp.config[config.KEY_SERVICES]:
enabled_services.add(SERVICE_FLIGHT_PLANNING)
from monitoring.mock_uss.flight_planning import routes as flight_planning_routes # noqa F401

msg = (
"################################################################################\n"
+ "################################ Configuration ################################\n"
+ "\n".join(f"## {key}: {webapp.config[key]}" for key in webapp.config)
+ "\n"
+ "################################################################################"
)
logger.info("Configuration:\n" + msg)
142 changes: 142 additions & 0 deletions monitoring/mock_uss/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import inspect
import os
from collections.abc import Callable
from typing import Any

# Because mock_uss uses gevent, we need to monkey-patch before anything else is loaded.
# https://www.gevent.org/intro.html#monkey-patching
from gevent import monkey

monkey.patch_all()

from loguru import logger # noqa E402
from werkzeug.middleware.proxy_fix import ProxyFix # noqa E402

from monitoring.mock_uss.server import MockUSS # noqa E402

SERVICE_GEOAWARENESS = "geoawareness"
SERVICE_RIDSP = "ridsp"
SERVICE_RIDDP = "riddp"
SERVICE_SCDSC = "scdsc"
SERVICE_MESSAGESIGNING = "msgsigning"
SERVICE_TRACER = "tracer"
SERVICE_INTERACTION_LOGGING = "interaction_logging"
SERVICE_VERSIONING = "versioning"
SERVICE_FLIGHT_PLANNING = "flight_planning"

webapp = MockUSS(__name__)
webapp.secret_key = os.environ.get("SECRET_KEY") or os.urandom(24)
if os.environ.get("MOCK_USS_PROXY_VALUES"):
values = os.environ.get("MOCK_USS_PROXY_VALUES", "").split(",")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FTR: checked this change

kwargs = {v.split("=")[0].strip(): int(v.split("=")[1]) for v in values}
webapp.wsgi_app = ProxyFix(webapp.wsgi_app, **kwargs)
enabled_services = set()


def import_environment_variable(
var_name: str,
required: bool = True,
default: str | None = None,
mutator: Callable[[str], Any] | None = None,
) -> None:
"""Import a value from a named environment variable into the webapp configuration.

Args:
var_name: Environment variable name (key). Also used as the webapp configuration key for that variable.
required: Whether the variable must be specified by the user. If True, a ValueError will be raised if the
variable is not specified by the user. If False, the webapp configuration will not be populated if no
default is provided. If default is specified, the default value is treated as specification by the user.
default: If the variable is not required, then use this value when it is not specified by the user. The default
value should be the string from the environment variable rather than the output of the mutator, if present.
mutator: If specified, apply this function to the string value of the environment variable to obtain the
variable to actually store in the configuration.
"""
if var_name in os.environ:
str_value = os.environ[var_name]
elif default is not None:
str_value = default
elif required:
stack = inspect.stack()
raise ValueError(
f"System cannot proceed because required environment variable '{var_name}' was not found. Required from {stack[1].filename}:{stack[1].lineno}"
)
else:
str_value = None

if str_value is not None:
webapp.config[var_name] = str_value if mutator is None else mutator(str_value)


def require_config_value(config_key: str) -> None:
if config_key not in webapp.config:
stack = inspect.stack()
raise ValueError(
f"System cannot proceed because required configuration key '{config_key}' was not found. Required from {stack[1].filename}:{stack[1].lineno}"
)


from monitoring.mock_uss import config # noqa E402
from monitoring.mock_uss import logging as logging # noqa E402
from monitoring.mock_uss import routes as basic_routes # noqa F401,F402

if SERVICE_GEOAWARENESS in webapp.config[config.KEY_SERVICES]:
enabled_services.add(SERVICE_GEOAWARENESS)
from monitoring.mock_uss import geoawareness as geoawareness
from monitoring.mock_uss.geoawareness import (
routes as geoawareness_routes, # noqa F401
)

if SERVICE_RIDSP in webapp.config[config.KEY_SERVICES]:
enabled_services.add(SERVICE_RIDSP)
from monitoring.mock_uss import ridsp as ridsp
from monitoring.mock_uss.ridsp import routes as ridsp_routes # noqa F401

if SERVICE_RIDDP in webapp.config[config.KEY_SERVICES]:
enabled_services.add(SERVICE_RIDDP)
from monitoring.mock_uss import riddp as riddp
from monitoring.mock_uss.riddp import routes as riddp_routes # noqa F401

if SERVICE_SCDSC in webapp.config[config.KEY_SERVICES]:
enabled_services.add(SERVICE_SCDSC)
from monitoring.mock_uss.f3548v21 import routes_scd as routes_scd
from monitoring.mock_uss.scd_injection import (
routes as scd_injection_routes, # noqa F401
)

if SERVICE_MESSAGESIGNING in webapp.config[config.KEY_SERVICES]:
enabled_services.add(SERVICE_MESSAGESIGNING)
from monitoring.mock_uss import msgsigning as msgsigning # noqa F401
from monitoring.mock_uss.msgsigning import routes as msgsigning_routes # noqa F401

if SERVICE_INTERACTION_LOGGING in webapp.config[config.KEY_SERVICES]:
enabled_services.add(SERVICE_INTERACTION_LOGGING)
from monitoring.mock_uss.interaction_logging import logger as interactions_logger # noqa F401
from monitoring.mock_uss.interaction_logging import (
routes_interactions_log as routes_interactions_log,
)

logger.info("Interaction logging enabled")
else:
logger.info("Interaction logging disabled")

if SERVICE_TRACER in webapp.config[config.KEY_SERVICES]:
enabled_services.add(SERVICE_TRACER)
from monitoring.mock_uss import tracer as tracer
from monitoring.mock_uss.tracer import routes as tracer_routes # noqa F401

if SERVICE_VERSIONING in webapp.config[config.KEY_SERVICES]:
enabled_services.add(SERVICE_VERSIONING)
from monitoring.mock_uss.versioning import routes as versioning_routes # noqa F401

if SERVICE_FLIGHT_PLANNING in webapp.config[config.KEY_SERVICES]:
enabled_services.add(SERVICE_FLIGHT_PLANNING)
from monitoring.mock_uss.flight_planning import routes as flight_planning_routes # noqa F401

msg = (
"################################################################################\n"
+ "################################ Configuration ################################\n"
+ "\n".join(f"## {key}: {webapp.config[key]}" for key in webapp.config)
+ "\n"
+ "################################################################################"
)
logger.info("Configuration:\n" + msg)
2 changes: 1 addition & 1 deletion monitoring/mock_uss/auth.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from monitoring.mock_uss import webapp
from monitoring.mock_uss.app import webapp
from monitoring.monitorlib import auth_validation

from . import config
Expand Down
2 changes: 1 addition & 1 deletion monitoring/mock_uss/config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from monitoring.mock_uss import import_environment_variable
from monitoring.mock_uss.app import import_environment_variable
from monitoring.monitorlib import auth_validation

KEY_TOKEN_PUBLIC_KEY = "MOCK_USS_PUBLIC_KEY"
Expand Down
2 changes: 1 addition & 1 deletion monitoring/mock_uss/dynamic_configuration/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from implicitdict import ImplicitDict

from monitoring.mock_uss import require_config_value, webapp
from monitoring.mock_uss.app import require_config_value, webapp
from monitoring.mock_uss.config import KEY_BEHAVIOR_LOCALITY
from monitoring.monitorlib.locality import Locality, LocalityCode
from monitoring.monitorlib.multiprocessing import SynchronizedValue
Expand Down
2 changes: 1 addition & 1 deletion monitoring/mock_uss/dynamic_configuration/routes.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import flask
from implicitdict import ImplicitDict

from monitoring.mock_uss import webapp
from monitoring.mock_uss.app import webapp
from monitoring.mock_uss.auth import MOCK_USS_CONFIG_SCOPE, requires_scope
from monitoring.mock_uss.dynamic_configuration.configuration import db, get_locality
from monitoring.monitorlib.clients.mock_uss.locality import (
Expand Down
2 changes: 1 addition & 1 deletion monitoring/mock_uss/f3548v21/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from monitoring.mock_uss import require_config_value, webapp
from monitoring.mock_uss.app import require_config_value, webapp
from monitoring.mock_uss.config import KEY_AUTH_SPEC, KEY_DSS_URL
from monitoring.monitorlib import auth
from monitoring.monitorlib.infrastructure import UTMClientSession
Expand Down
2 changes: 1 addition & 1 deletion monitoring/mock_uss/f3548v21/flight_planning.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from uas_standards.astm.f3548.v21.constants import OiMaxPlanHorizonDays, OiMaxVertices
from uas_standards.interuss.automated_testing.scd.v1 import api as scd_api

from monitoring.mock_uss import webapp
from monitoring.mock_uss.app import webapp
from monitoring.mock_uss.config import KEY_BASE_URL
from monitoring.mock_uss.f3548v21 import utm_client
from monitoring.mock_uss.flights.database import FlightRecord, db
Expand Down
2 changes: 1 addition & 1 deletion monitoring/mock_uss/f3548v21/routes_scd.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
PutOperationalIntentDetailsParameters,
)

from monitoring.mock_uss import webapp
from monitoring.mock_uss.app import webapp
from monitoring.mock_uss.auth import requires_scope
from monitoring.mock_uss.f3548v21.flight_planning import (
conflicts_with_flightrecords,
Expand Down
Loading
Loading