Skip to content

Commit

Permalink
[uss_qualifier/netrid] Fix #244; rename server_id to participant_id; …
Browse files Browse the repository at this point in the history
…some refactorings and fixes (#247)

* [uss_qualifier] attach participant id to net0210 check, fix Issue #244

* rename server_id -> participant_id

* factor out polling; refactor some parts of scenarios

* add aud override in NoAuth

* fix participant_id setter

* fix several issues

* fix small issues

* add TODO

---------

Co-authored-by: Mickaël Misbach <[email protected]>
Co-authored-by: Mickaël Misbach <[email protected]>
  • Loading branch information
3 people authored Oct 20, 2023
1 parent 71538a0 commit 9753ad1
Show file tree
Hide file tree
Showing 27 changed files with 586 additions and 508 deletions.
26 changes: 15 additions & 11 deletions monitoring/monitorlib/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,29 @@ class NoAuth(AuthAdapter):

EXPIRATION = 3600 # seconds

def __init__(self, sub: str = "uss_noauth"):
def __init__(self, sub: str = "uss_noauth", aud_override: Optional[str] = None):
super().__init__()
self.sub = sub
self._aud_override = aud_override

# Overrides method in AuthAdapter
def issue_token(self, intended_audience: str, scopes: List[str]) -> str:
timestamp = int((datetime.datetime.utcnow() - _UNIX_EPOCH).total_seconds())
claims = {
"sub": self.sub,
"client_id": self.sub,
"scope": " ".join(scopes),
"aud": intended_audience,
"nbf": timestamp - 1,
"exp": timestamp + NoAuth.EXPIRATION,
"iss": "NoAuth",
"jti": str(uuid.uuid4()),
}
if self._aud_override is not None:
claims["aud"] = self._aud_override
jwt = jwcrypto.jwt.JWT(
header={"typ": "JWT", "alg": "RS256"},
claims={
"sub": self.sub,
"client_id": self.sub,
"scope": " ".join(scopes),
"aud": intended_audience,
"nbf": timestamp - 1,
"exp": timestamp + NoAuth.EXPIRATION,
"iss": "NoAuth",
"jti": str(uuid.uuid4()),
},
claims=claims,
algs=["RS256"],
)
jwt.make_signed_token(NoAuth.dummy_private_key)
Expand Down
6 changes: 3 additions & 3 deletions monitoring/monitorlib/clients/versioning/client_interuss.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class InterUSSVersioningClient(VersioningClient):
def __init__(self, session: UTMClientSession, participant_id: ParticipantID):
super(InterUSSVersioningClient, self).__init__(participant_id)
self._session = session
self._server_id = participant_id
self._participant_id = participant_id

def get_version(self, version_type: Optional[str]) -> GetVersionResponse:
op = api.OPERATIONS[api.OperationID.GetVersion]
Expand All @@ -29,8 +29,8 @@ def get_version(self, version_type: Optional[str]) -> GetVersionResponse:
"query_type": QueryType.InterUSSVersioningGetVersion,
"scope": Scope.ReadSystemVersions,
}
if self._server_id:
kwargs["server_id"] = self._server_id
if self._participant_id:
kwargs["participant_id"] = self._participant_id
query = query_and_describe(**kwargs)
if query.status_code != 200:
raise VersionQueryError(
Expand Down
16 changes: 8 additions & 8 deletions monitoring/monitorlib/fetch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ class Query(ImplicitDict):
request: RequestDescription
response: ResponseDescription

server_id: Optional[str]
participant_id: Optional[str]
"""If specified, identifier of the USS/participant hosting the server involved in this query."""

query_type: Optional[QueryType]
Expand Down Expand Up @@ -283,16 +283,16 @@ def describe_query(
resp: requests.Response,
initiated_at: datetime.datetime,
query_type: Optional[QueryType] = None,
server_id: Optional[str] = None,
participant_id: Optional[str] = None,
) -> Query:
query = Query(
request=describe_request(resp.request, initiated_at),
response=describe_response(resp),
)
if query_type is not None:
query.query_type = query_type
if server_id is not None:
query.server_id = server_id
if participant_id is not None:
query.participant_id = participant_id
return query


Expand All @@ -301,7 +301,7 @@ def query_and_describe(
verb: str,
url: str,
query_type: Optional[QueryType] = None,
server_id: Optional[str] = None,
participant_id: Optional[str] = None,
**kwargs,
) -> Query:
"""Attempt to perform a query, and then describe the results of that attempt.
Expand All @@ -314,7 +314,7 @@ def query_and_describe(
verb: HTTP verb to perform at the specified URL.
url: URL to query.
query_type: If specified, the known type of query that this is.
server_id: If specified, the participant identifier of the server being queried.
participant_id: If specified, the participant identifier of the server being queried.
**kwargs: Any keyword arguments that should be applied to the <session>.request method when invoking it.
Returns:
Expand Down Expand Up @@ -350,7 +350,7 @@ def query_and_describe(
client.request(verb, url, **req_kwargs),
t0,
query_type=query_type,
server_id=server_id,
participant_id=participant_id,
)
except (requests.Timeout, urllib3.exceptions.ReadTimeoutError) as e:
failure_message = f"query_and_describe attempt {attempt + 1} from PID {os.getpid()} to {verb} {url} failed with timeout {type(e).__name__}: {str(e)}"
Expand Down Expand Up @@ -380,7 +380,7 @@ def query_and_describe(
elapsed_s=(t1 - t0).total_seconds(),
reported=StringBasedDateTime(t1),
),
server_id=server_id,
participant_id=participant_id,
)
if query_type is not None:
result.query_type = query_type
Expand Down
84 changes: 52 additions & 32 deletions monitoring/monitorlib/fetch/rid.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
from __future__ import annotations

import datetime
from typing import Dict, List, Optional, Any, Union

from implicitdict import ImplicitDict, StringBasedDateTime
import s2sphere
from uas_standards.astm.f3411 import v19, v22a
import uas_standards.astm.f3411.v19.api
import uas_standards.astm.f3411.v19.constants
import uas_standards.astm.f3411.v22a.api
import uas_standards.astm.f3411.v22a.constants
import yaml
from implicitdict import ImplicitDict, StringBasedDateTime
from uas_standards.astm.f3411 import v19, v22a
from uas_standards.astm.f3411.v22a.api import RIDHeight
from yaml.representer import Representer

Expand Down Expand Up @@ -123,15 +124,15 @@ def query_flights(
session: UTMClientSession,
area: s2sphere.LatLngRect,
include_recent_positions: bool = True,
server_id: Optional[str] = None,
participant_id: Optional[str] = None,
) -> FetchedUSSFlights:
return uss_flights(
self.flights_url,
area,
include_recent_positions,
self.rid_version,
session,
server_id=server_id,
participant_id=participant_id,
)


Expand Down Expand Up @@ -639,13 +640,30 @@ def success(self) -> bool:
def errors(self) -> List[str]:
raise NotImplementedError("RIDQuery.errors must be overriden")

def set_server_id(self, server_id: str):
@property
def participant_id(self) -> Optional[str]:
if self.rid_version == RIDVersion.f3411_19:
if "participant_id" in self.v19_query:
return self.v19_query.participant_id
else:
return None
elif self.rid_version == RIDVersion.f3411_22a:
if "participant_id" in self.v22a_query:
return self.v22a_query.participant_id
else:
return None
else:
raise NotImplementedError(
f"Cannot retrieve participant_id using RID version {self.rid_version}"
)

def set_participant_id(self, participant_id: str) -> None:
if self.v19_query is not None:
self.v19_query.server_id = server_id
self.v19_query.participant_id = participant_id
elif self.v22a_query is not None:
self.v22a_query.server_id = server_id
self.v22a_query.participant_id = participant_id
else:
raise NotImplementedError(f"Cannot set server_id")
raise NotImplementedError(f"Cannot set participant_id")


class FetchedISA(RIDQuery):
Expand Down Expand Up @@ -721,7 +739,7 @@ def isa(
rid_version: RIDVersion,
session: UTMClientSession,
dss_base_url: str = "",
server_id: Optional[str] = None,
participant_id: Optional[str] = None,
) -> FetchedISA:
if rid_version == RIDVersion.f3411_19:
op = v19.api.OPERATIONS[v19.api.OperationID.GetIdentificationServiceArea]
Expand All @@ -732,7 +750,7 @@ def isa(
op.verb,
url,
scope=v19.constants.Scope.Read,
server_id=server_id,
participant_id=participant_id,
)
)
elif rid_version == RIDVersion.f3411_22a:
Expand All @@ -744,7 +762,7 @@ def isa(
op.verb,
url,
scope=v22a.constants.Scope.DisplayProvider,
server_id=server_id,
participant_id=participant_id,
)
)
else:
Expand Down Expand Up @@ -856,7 +874,7 @@ def isas(
rid_version: RIDVersion,
session: UTMClientSession,
dss_base_url: str = "",
server_id: Optional[str] = None,
participant_id: Optional[str] = None,
) -> FetchedISAs:
url_time_params = ""
if start_time is not None:
Expand All @@ -874,7 +892,7 @@ def isas(
op.verb,
url,
scope=v19.constants.Scope.Read,
server_id=server_id,
participant_id=participant_id,
)
)
elif rid_version == RIDVersion.f3411_22a:
Expand All @@ -887,7 +905,7 @@ def isas(
op.verb,
url,
scope=v22a.constants.Scope.DisplayProvider,
server_id=server_id,
participant_id=participant_id,
)
)
else:
Expand Down Expand Up @@ -964,7 +982,7 @@ def uss_flights(
include_recent_positions: bool,
rid_version: RIDVersion,
session: UTMClientSession,
server_id: Optional[str] = None,
participant_id: Optional[str] = None,
) -> FetchedUSSFlights:
if rid_version == RIDVersion.f3411_19:
query = fetch.query_and_describe(
Expand All @@ -983,9 +1001,9 @@ def uss_flights(
else "false",
},
scope=v19.constants.Scope.Read,
server_id=server_id,
query_type=QueryType.F3411v19Flights,
participant_id=participant_id,
)
query.query_type = QueryType.F3411v19Flights
return FetchedUSSFlights(v19_query=query)
elif rid_version == RIDVersion.f3411_22a:
params = {
Expand All @@ -1004,9 +1022,9 @@ def uss_flights(
flights_url,
params=params,
scope=v22a.constants.Scope.DisplayProvider,
server_id=server_id,
query_type=QueryType.F3411v22aFlights,
participant_id=participant_id,
)
query.query_type = QueryType.F3411v22aFlights
return FetchedUSSFlights(v22a_query=query)
else:
raise NotImplementedError(
Expand Down Expand Up @@ -1091,11 +1109,11 @@ def flight_details(
enhanced_details: bool,
rid_version: RIDVersion,
session: UTMClientSession,
server_id: Optional[str] = None,
participant_id: Optional[str] = None,
) -> FetchedUSSFlightDetails:
url = f"{flights_url}/{flight_id}/details"
if rid_version == RIDVersion.f3411_19:
kwargs = {"server_id": server_id}
kwargs = {"participant_id": participant_id}
if enhanced_details:
kwargs["params"] = {"enhanced": "true"}
kwargs["scope"] = (
Expand All @@ -1111,7 +1129,7 @@ def flight_details(
"GET",
url,
scope=v22a.constants.Scope.DisplayProvider,
server_id=server_id,
participant_id=participant_id,
)
return FetchedUSSFlightDetails(v22a_query=query)
else:
Expand Down Expand Up @@ -1163,7 +1181,7 @@ def all_flights(
session: UTMClientSession,
dss_base_url: str = "",
enhanced_details: bool = False,
server_id: Optional[str] = None,
dss_participant_id: Optional[str] = None,
) -> FetchedFlights:
t = datetime.datetime.utcnow()
isa_list = isas(
Expand All @@ -1173,7 +1191,7 @@ def all_flights(
rid_version,
session,
dss_base_url,
server_id=server_id,
participant_id=dss_participant_id,
)

uss_flight_queries: Dict[str, FetchedUSSFlights] = {}
Expand All @@ -1185,7 +1203,9 @@ def all_flights(
include_recent_positions,
rid_version,
session,
server_id=server_id,
# Note that we have no clue at this point which participant the flights_url is for,
# this can only be determined later by comparing injected and observed flights.
participant_id=None,
)
uss_flight_queries[flights_url] = flights_for_url

Expand All @@ -1197,7 +1217,7 @@ def all_flights(
enhanced_details,
rid_version,
session,
server_id=server_id,
participant_id=None,
)
uss_flight_details_queries[flight.id] = details

Expand Down Expand Up @@ -1275,7 +1295,7 @@ def subscription(
rid_version: RIDVersion,
session: UTMClientSession,
dss_base_url: str = "",
server_id: Optional[str] = None,
participant_id: Optional[str] = None,
) -> FetchedSubscription:
if rid_version == RIDVersion.f3411_19:
op = v19.api.OPERATIONS[v19.api.OperationID.GetSubscription]
Expand All @@ -1286,7 +1306,7 @@ def subscription(
op.verb,
url,
scope=v19.constants.Scope.Read,
server_id=server_id,
participant_id=participant_id,
)
)
elif rid_version == RIDVersion.f3411_22a:
Expand All @@ -1298,7 +1318,7 @@ def subscription(
op.verb,
url,
scope=v22a.constants.Scope.DisplayProvider,
server_id=server_id,
participant_id=participant_id,
)
)
else:
Expand Down Expand Up @@ -1382,7 +1402,7 @@ def subscriptions(
rid_version: RIDVersion,
session: UTMClientSession,
dss_base_url: str = "",
server_id: Optional[str] = None,
participant_id: Optional[str] = None,
) -> FetchedSubscriptions:
if rid_version == RIDVersion.f3411_19:
op = v19.api.OPERATIONS[v19.api.OperationID.SearchSubscriptions]
Expand All @@ -1393,7 +1413,7 @@ def subscriptions(
op.verb,
url,
scope=v19.constants.Scope.Read,
server_id=server_id,
participant_id=participant_id,
)
)
elif rid_version == RIDVersion.f3411_22a:
Expand All @@ -1405,7 +1425,7 @@ def subscriptions(
op.verb,
url,
scope=v22a.constants.Scope.DisplayProvider,
server_id=server_id,
participant_id=participant_id,
)
)
else:
Expand Down
Loading

0 comments on commit 9753ad1

Please sign in to comment.