Skip to content

Commit

Permalink
[uss_qualifier] Inspect all collected queries for the usage of https …
Browse files Browse the repository at this point in the history
…(NET0220) (#188)

* NET0220

* PR feedback
  • Loading branch information
Shastick authored Sep 14, 2023
1 parent 28a776a commit 2a87098
Show file tree
Hide file tree
Showing 26 changed files with 424 additions and 44 deletions.
17 changes: 13 additions & 4 deletions monitoring/monitorlib/fetch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,21 +267,25 @@ def describe_query(
resp: requests.Response,
initiated_at: datetime.datetime,
query_type: Optional[QueryType] = None,
server_id: Optional[str] = None,
) -> Query:
result = Query(
query = Query(
request=describe_request(resp.request, initiated_at),
response=describe_response(resp),
)
if query_type is not None:
result.query_type = query_type
return result
query.query_type = query_type
if server_id is not None:
query.server_id = server_id
return query


def query_and_describe(
client: Optional[infrastructure.UTMClientSession],
verb: str,
url: str,
query_type: Optional[QueryType] = None,
server_id: Optional[str] = None,
**kwargs,
) -> Query:
"""Attempt to perform a query, and then describe the results of that attempt.
Expand All @@ -294,6 +298,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.
**kwargs: Any keyword arguments that should be applied to the <session>.request method when invoking it.
Returns:
Expand All @@ -316,7 +321,10 @@ def query_and_describe(
t0 = datetime.datetime.utcnow()
try:
return describe_query(
client.request(verb, url, **req_kwargs), t0, query_type
client.request(verb, url, **req_kwargs),
t0,
query_type=query_type,
server_id=server_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 @@ -346,6 +354,7 @@ def query_and_describe(
elapsed_s=(t1 - t0).total_seconds(),
reported=StringBasedDateTime(t1),
),
server_id=server_id,
)
if query_type is not None:
result.query_type = query_type
Expand Down
89 changes: 75 additions & 14 deletions monitoring/monitorlib/fetch/rid.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,15 @@ def query_flights(
session: UTMClientSession,
area: s2sphere.LatLngRect,
include_recent_positions: bool = True,
server_id: Optional[str] = None,
) -> FetchedUSSFlights:
return uss_flights(
self.flights_url, area, include_recent_positions, self.rid_version, session
self.flights_url,
area,
include_recent_positions,
self.rid_version,
session,
server_id=server_id,
)


Expand Down Expand Up @@ -534,21 +540,30 @@ def isa(
rid_version: RIDVersion,
session: UTMClientSession,
dss_base_url: str = "",
server_id: Optional[str] = None,
) -> FetchedISA:
if rid_version == RIDVersion.f3411_19:
op = v19.api.OPERATIONS[v19.api.OperationID.GetIdentificationServiceArea]
url = f"{dss_base_url}{op.path}".replace("{id}", isa_id)
return FetchedISA(
v19_query=fetch.query_and_describe(
session, op.verb, url, scope=v19.constants.Scope.Read
session,
op.verb,
url,
scope=v19.constants.Scope.Read,
server_id=server_id,
)
)
elif rid_version == RIDVersion.f3411_22a:
op = v22a.api.OPERATIONS[v22a.api.OperationID.GetIdentificationServiceArea]
url = f"{dss_base_url}{op.path}".replace("{id}", isa_id)
return FetchedISA(
v22a_query=fetch.query_and_describe(
session, op.verb, url, scope=v22a.constants.Scope.DisplayProvider
session,
op.verb,
url,
scope=v22a.constants.Scope.DisplayProvider,
server_id=server_id,
)
)
else:
Expand Down Expand Up @@ -660,6 +675,7 @@ def isas(
rid_version: RIDVersion,
session: UTMClientSession,
dss_base_url: str = "",
server_id: Optional[str] = None,
) -> FetchedISAs:
t0 = rid_version.format_time(start_time)
t1 = rid_version.format_time(end_time)
Expand All @@ -669,7 +685,11 @@ def isas(
url = f"{dss_base_url}{op.path}?area={area}&earliest_time={t0}&latest_time={t1}"
return FetchedISAs(
v19_query=fetch.query_and_describe(
session, op.verb, url, scope=v19.constants.Scope.Read
session,
op.verb,
url,
scope=v19.constants.Scope.Read,
server_id=server_id,
)
)
elif rid_version == RIDVersion.f3411_22a:
Expand All @@ -678,7 +698,11 @@ def isas(
url = f"{dss_base_url}{op.path}?area={area}&earliest_time={t0}&latest_time={t1}"
return FetchedISAs(
v22a_query=fetch.query_and_describe(
session, op.verb, url, scope=v22a.constants.Scope.DisplayProvider
session,
op.verb,
url,
scope=v22a.constants.Scope.DisplayProvider,
server_id=server_id,
)
)
else:
Expand Down Expand Up @@ -755,6 +779,7 @@ def uss_flights(
include_recent_positions: bool,
rid_version: RIDVersion,
session: UTMClientSession,
server_id: Optional[str] = None,
) -> FetchedUSSFlights:
if rid_version == RIDVersion.f3411_19:
query = fetch.query_and_describe(
Expand All @@ -773,6 +798,7 @@ def uss_flights(
else "false",
},
scope=v19.constants.Scope.Read,
server_id=server_id,
)
query.query_type = QueryType.F3411v19Flights
return FetchedUSSFlights(v19_query=query)
Expand All @@ -793,6 +819,7 @@ def uss_flights(
flights_url,
params=params,
scope=v22a.constants.Scope.DisplayProvider,
server_id=server_id,
)
query.query_type = QueryType.F3411v22aFlights
return FetchedUSSFlights(v22a_query=query)
Expand Down Expand Up @@ -879,10 +906,11 @@ def flight_details(
enhanced_details: bool,
rid_version: RIDVersion,
session: UTMClientSession,
server_id: Optional[str] = None,
) -> FetchedUSSFlightDetails:
url = f"{flights_url}/{flight_id}/details"
if rid_version == RIDVersion.f3411_19:
kwargs = {}
kwargs = {"server_id": server_id}
if enhanced_details:
kwargs["params"] = {"enhanced": "true"}
kwargs["scope"] = (
Expand All @@ -894,7 +922,11 @@ def flight_details(
return FetchedUSSFlightDetails(v19_query=query)
elif rid_version == RIDVersion.f3411_22a:
query = fetch.query_and_describe(
session, "GET", url, scope=v22a.constants.Scope.DisplayProvider
session,
"GET",
url,
scope=v22a.constants.Scope.DisplayProvider,
server_id=server_id,
)
return FetchedUSSFlightDetails(v22a_query=query)
else:
Expand Down Expand Up @@ -946,22 +978,33 @@ def all_flights(
session: UTMClientSession,
dss_base_url: str = "",
enhanced_details: bool = False,
server_id: Optional[str] = None,
) -> FetchedFlights:
t = datetime.datetime.utcnow()
isa_list = isas(area, t, t, rid_version, session, dss_base_url)
isa_list = isas(area, t, t, rid_version, session, dss_base_url, server_id=server_id)

uss_flight_queries: Dict[str, FetchedUSSFlights] = {}
uss_flight_details_queries: Dict[str, FetchedUSSFlightDetails] = {}
for flights_url in isa_list.flights_urls:
flights_for_url = uss_flights(
flights_url, area, include_recent_positions, rid_version, session
flights_url,
area,
include_recent_positions,
rid_version,
session,
server_id=server_id,
)
uss_flight_queries[flights_url] = flights_for_url

if get_details and flights_for_url.success:
for flight in flights_for_url.flights:
details = flight_details(
flights_url, flight.id, enhanced_details, rid_version, session
flights_url,
flight.id,
enhanced_details,
rid_version,
session,
server_id=server_id,
)
uss_flight_details_queries[flight.id] = details

Expand Down Expand Up @@ -1039,21 +1082,30 @@ def subscription(
rid_version: RIDVersion,
session: UTMClientSession,
dss_base_url: str = "",
server_id: Optional[str] = None,
) -> FetchedSubscription:
if rid_version == RIDVersion.f3411_19:
op = v19.api.OPERATIONS[v19.api.OperationID.GetSubscription]
url = f"{dss_base_url}{op.path}".replace("{id}", subscription_id)
return FetchedSubscription(
v19_query=fetch.query_and_describe(
session, op.verb, url, scope=v19.constants.Scope.Read
session,
op.verb,
url,
scope=v19.constants.Scope.Read,
server_id=server_id,
)
)
elif rid_version == RIDVersion.f3411_22a:
op = v22a.api.OPERATIONS[v22a.api.OperationID.GetSubscription]
url = f"{dss_base_url}{op.path}".replace("{id}", subscription_id)
return FetchedSubscription(
v22a_query=fetch.query_and_describe(
session, op.verb, url, scope=v22a.constants.Scope.DisplayProvider
session,
op.verb,
url,
scope=v22a.constants.Scope.DisplayProvider,
server_id=server_id,
)
)
else:
Expand Down Expand Up @@ -1137,21 +1189,30 @@ def subscriptions(
rid_version: RIDVersion,
session: UTMClientSession,
dss_base_url: str = "",
server_id: Optional[str] = None,
) -> FetchedSubscriptions:
if rid_version == RIDVersion.f3411_19:
op = v19.api.OPERATIONS[v19.api.OperationID.SearchSubscriptions]
url = f"{dss_base_url}{op.path}?area={rid_v1.geo_polygon_string_from_s2(area)}"
return FetchedSubscriptions(
v19_query=fetch.query_and_describe(
session, op.verb, url, scope=v19.constants.Scope.Read
session,
op.verb,
url,
scope=v19.constants.Scope.Read,
server_id=server_id,
)
)
elif rid_version == RIDVersion.f3411_22a:
op = v22a.api.OPERATIONS[v22a.api.OperationID.SearchSubscriptions]
url = f"{dss_base_url}{op.path}?area={rid_v2.geo_polygon_string_from_s2(area)}"
return FetchedSubscriptions(
v22a_query=fetch.query_and_describe(
session, op.verb, url, scope=v22a.constants.Scope.DisplayProvider
session,
op.verb,
url,
scope=v22a.constants.Scope.DisplayProvider,
server_id=server_id,
)
)
else:
Expand Down
Loading

0 comments on commit 2a87098

Please sign in to comment.