Skip to content

Commit

Permalink
Revert "refactor: make asset endpoint profile opcua additional config…
Browse files Browse the repository at this point in the history
…uration …"

This reverts commit a6eb075.
  • Loading branch information
vilit1 authored Jan 22, 2025
1 parent 152657f commit ec6f9d6
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 177 deletions.
1 change: 1 addition & 0 deletions azext_edge/edge/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ class SecurityModes(Enum):
class SecurityPolicies(Enum):
"""Security policies for the OPCUA connector."""

# TODO: add in user friendly input mapping
none = "none"
basic128 = "Basic128Rsa15"
basic256 = "Basic256"
Expand Down

This file was deleted.

47 changes: 28 additions & 19 deletions azext_edge/edge/providers/rpsaas/adr/asset_endpoint_profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,20 @@ def update(


# Helpers
def _assert_above_min(param: str, value: int, minimum: int = 0) -> str:
if value < minimum:
return f"The parameter {param} needs to be at least {minimum}.\n"
return ""


def _raise_if_connector_error(connector_type: str, error_msg: str):
if error_msg:
raise InvalidArgumentValueError(
f"The following {connector_type} connector arguments are invalid:\n {error_msg}"
)


# TODO: use jsonschema lib
def _build_opcua_config(
original_config: Optional[str] = None,
application_name: Optional[str] = None,
Expand All @@ -237,12 +251,13 @@ def _build_opcua_config(
sub_life_time: Optional[int] = None,
**_
) -> str:
from .additional_configuration_schemas import OPCUA_SCHEMA
config = json.loads(original_config) if original_config else {}

error_msg = ""
if application_name:
config["applicationName"] = application_name
if keep_alive:
error_msg += _assert_above_min("--keep-alive", keep_alive)
config["keepAliveMilliseconds"] = keep_alive
if run_asset_discovery is not None:
config["runAssetDiscovery"] = run_asset_discovery
Expand All @@ -253,10 +268,13 @@ def _build_opcua_config(
]) and not config.get("defaults"):
config["defaults"] = {}
if default_publishing_interval:
error_msg += _assert_above_min("--default-publishing-int", default_publishing_interval, -1)
config["defaults"]["publishingIntervalMilliseconds"] = default_publishing_interval
if default_sampling_interval:
error_msg += _assert_above_min("--default-sampling-int", default_sampling_interval, -1)
config["defaults"]["samplingIntervalMilliseconds"] = default_sampling_interval
if default_queue_size:
error_msg += _assert_above_min("--default-queue-size", default_queue_size, 0)
config["defaults"]["queueSize"] = default_queue_size

# session
Expand All @@ -265,21 +283,27 @@ def _build_opcua_config(
]) and not config.get("session"):
config["session"] = {}
if session_timeout:
error_msg += _assert_above_min("--session-timeout", session_timeout)
config["session"]["timeoutMilliseconds"] = session_timeout
if session_keep_alive:
error_msg += _assert_above_min("--session-keep-alive", session_keep_alive)
config["session"]["keepAliveIntervalMilliseconds"] = session_keep_alive
if session_reconnect_period:
error_msg += _assert_above_min("--session-reconnect-period", session_reconnect_period)
config["session"]["reconnectPeriodMilliseconds"] = session_reconnect_period
if session_reconnect_exponential_back_off:
error_msg += _assert_above_min("--session-reconnect-backoff", session_reconnect_exponential_back_off, -1)
config["session"]["reconnectExponentialBackOffMilliseconds"] = session_reconnect_exponential_back_off

# subscription
if any([sub_life_time, sub_max_items]) and not config.get("subscription"):
config["subscription"] = {}
if sub_life_time:
config["subscription"]["lifeTimeMilliseconds"] = sub_life_time
error_msg += _assert_above_min("--subscription-life-time", sub_life_time)
config["subscription"]["maxItems"] = sub_life_time
if sub_max_items:
config["subscription"]["maxItems"] = sub_max_items
error_msg += _assert_above_min("--subscription-max-items", sub_max_items, 1)
config["subscription"]["lifeTimeMilliseconds"] = sub_max_items

# security
if any([
Expand All @@ -293,7 +317,7 @@ def _build_opcua_config(
if security_policy:
config["security"]["securityPolicy"] = "http://opcfoundation.org/UA/SecurityPolicy#" + security_policy

_validate_additional_configuration(schema=OPCUA_SCHEMA, config=config)
_raise_if_connector_error(connector_type="OPCUA", error_msg=error_msg)
return json.dumps(config)


Expand Down Expand Up @@ -418,18 +442,3 @@ def _update_properties(
username_reference=username_reference,
password_reference=password_reference
)


def _validate_additional_configuration(schema: dict, config: dict):
from jsonschema import Draft7Validator, ValidationError
validator = Draft7Validator(schema=schema)

errors = []
error: ValidationError
for error in validator.iter_errors(config):
errors.append(f"- argument for {error.schema['description']}: {error.message}")

if errors:
raise InvalidArgumentValueError(
"Invalid Additional Configuration arguments:\n" + "\n".join(errors)
)
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,28 @@
)

from azext_edge.edge.providers.rpsaas.adr.asset_endpoint_profiles import (
_assert_above_min,
_build_opcua_config,
_build_query_body,
_process_additional_configuration,
_process_authentication,
_update_properties,
AEPAuthModes
)
from azext_edge.edge.common import AEPAuthModes, SecurityPolicies, SecurityModes
from ....generators import generate_random_string


@pytest.mark.parametrize("value", [-1, 100])
@pytest.mark.parametrize("minimum", [-1, 0])
def test_assert_above_min(value, minimum):
param = generate_random_string()
result = _assert_above_min(param=param, value=value, minimum=minimum)
if value < minimum:
assert param in result
else:
assert result == ""


@pytest.mark.parametrize("original_config", [
None,
{
Expand All @@ -50,8 +62,8 @@
},
"security": {
"autoAcceptUntrustedServerCertificates": False,
"securityMode": SecurityModes.none.value,
"securityPolicy": "http://opcfoundation.org/UA/SecurityPolicy#" + SecurityPolicies.basic256sha256.value
"securityMode": generate_random_string(),
"securityPolicy": generate_random_string()
}
}
])
Expand All @@ -69,8 +81,8 @@
"session_keep_alive": 666,
"session_reconnect_period": 777,
"session_reconnect_exponential_back_off": 888,
"security_policy": SecurityPolicies.aes128.value,
"security_mode": SecurityModes.sign_and_encrypt.value,
"security_policy": generate_random_string(),
"security_mode": generate_random_string(),
"sub_max_items": 999,
"sub_life_time": 1000,
},
Expand Down Expand Up @@ -117,8 +129,8 @@ def test_build_opcua_config(original_config, req):

og_sub = original_config.get("subscription", {})
res_sub = result.get("subscription", {})
assert res_sub.get("maxItems") == req.get("sub_max_items", og_sub.get("maxItems"))
assert res_sub.get("lifeTimeMilliseconds") == req.get("sub_life_time", og_sub.get("lifeTimeMilliseconds"))
assert res_sub.get("maxItems") == req.get("sub_life_time", og_sub.get("maxItems"))
assert res_sub.get("lifeTimeMilliseconds") == req.get("sub_max_items", og_sub.get("lifeTimeMilliseconds"))

og_security = original_config.get("security", {})
res_security = result.get("security", {})
Expand Down Expand Up @@ -169,23 +181,22 @@ def test_build_opcua_config_error(req):
)
assert e.value.error_msg
min_dict = {
"default_publishing_interval": (-1, "--default-publishing-int/--dpi"),
"default_sampling_interval": (-1, "--default-sampling-int/--dsi"),
"default_queue_size": (0, "--default-queue-size/--dqs"),
"keep_alive": (0, "--keep-alive/--ka"),
"session_timeout": (0, "--session-timeout/--st"),
"session_keep_alive": (0, "--session-keep-alive/--ska"),
"session_reconnect_period": (0, "--session-reconnect-period/--srp"),
"session_reconnect_exponential_back_off": (-1, "--session-reconnect-backoff/--srb"),
"sub_max_items": (1, "--subscription-max-items/--smi"),
"sub_life_time": (0, "--subscription-life-time/--slt"),
"default_publishing_interval": (-1, "--default-publishing-int"),
"default_sampling_interval": (-1, "--default-sampling-int"),
"default_queue_size": (0, "--default-queue-size"),
"keep_alive": (0, "--keep-alive"),
"session_timeout": (0, "--session-timeout"),
"session_keep_alive": (0, "--session-keep-alive"),
"session_reconnect_period": (0, "--session-reconnect-period"),
"session_reconnect_exponential_back_off": (-1, "--session-reconnect-backoff"),
"sub_max_items": (1, "--subscription-max-items"),
"sub_life_time": (0, "--subscription-life-time"),
}
expected_error_params = [
param for param in req if (min_dict.get(param) is not None) and (req[param] < min_dict[param][0])
]
for param in expected_error_params:
assert f"- argument for {min_dict[param][1]}: {req[param]}" in e.value.error_msg
assert f"{min_dict[param][0]}" in e.value.error_msg
assert f"{min_dict[param][1]} needs to be at least {min_dict[param][0]}." in e.value.error_msg


@pytest.mark.parametrize("asset_endpoint_profile_name", [None, generate_random_string()])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,9 @@ def assert_opcua_props(result, **expected):

# subscription
if expected.get("sub_life_time"):
result_config["subscription"]["lifeTimeMilliseconds"] = expected["sub_life_time"]
result_config["subscription"]["maxItems"] = expected["sub_life_time"]
if expected.get("sub_max_items"):
result_config["subscription"]["maxItems"] = expected["sub_max_items"]
result_config["subscription"]["lifeTimeMilliseconds"] = expected["sub_max_items"]

# security
if expected.get("accept_untrusted_certs") is not None:
Expand Down
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
"protobuf~=4.25.0",
"opentelemetry-proto~=1.20.0",
"packaging>=23.2",
"jsonschema~=4.23.0"
]

CLASSIFIERS = [
Expand Down

0 comments on commit ec6f9d6

Please sign in to comment.