Skip to content

Commit 951a854

Browse files
d-linkoDavid Linko
andauthored
239 invalid use of ari text prefix for binary form of values (#269)
* fixed hadnling reports with null nonces * working on reports with null nonces and header and row len mismatch * fixing adding bad parameter info * updating tbr working branch * fixing up report display * removed ARI infront of hex * upgrading to use grafana infinty and better plot rptsets * working updated grafana and added example infitiy source * fixed Received Reports added ploting edd example * removed logs and added trys back * fixed status for new grafana * fixed flatting * fixing agent alerts in UI * switching to JSON file for alert storage * removed ARI from cborhex * file clean up * fix for handling string based nonce_cbor * Revert "working updated grafana and added example infitiy source" This reverts commit a082db1. * reverting grafana upgrade --------- Co-authored-by: David Linko <david.linko@jhuapl.edu>
1 parent c59a45b commit 951a854

File tree

15 files changed

+265
-210
lines changed

15 files changed

+265
-210
lines changed

anms-core/anms/routes/ARIs/alerts.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
@router.get("/incoming", status_code=status.HTTP_200_OK)
3434
def alerts_get():
3535
MANAGER_CECKER.check_list()
36-
alerts = MANAGER_CECKER.alerts
36+
alerts = MANAGER_CECKER.get_alerts()
3737
return list(alerts.values())
3838

3939

anms-core/anms/routes/ARIs/reports.py

Lines changed: 96 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ async def report_def_by_id(agent_id: int):
7474
# select all reports belonging to the agent
7575
final_res = []
7676
agent_id_str = ""
77+
dec = ace.ari_cbor.Decoder()
78+
enc = ace.ari_text.Encoder()
79+
adms = ace.AdmSet()
80+
adms.load_default_dirs()
81+
nn_func = ace.nickname.Converter(ace.nickname.Mode.FROM_NN , adms.db_session(), False)
7782
stmt = select(Report).where(Report.agent_id == agent_id)
7883
agent_id_stmt = select(RegisteredAgent).where(RegisteredAgent.registered_agents_id == agent_id)
7984
async with get_async_session() as session:
@@ -86,104 +91,83 @@ async def report_def_by_id(agent_id: int):
8691
# select from exec_set
8792
try:
8893
nonce_cbor = res.nonce_cbor
89-
stmt = select(ExecutionSet).where(and_(ExecutionSet.agent_id == agent_id_str, ExecutionSet.nonce_cbor == nonce_cbor) )
90-
result: Result = await session.scalars(stmt)
91-
exc_set = result.all()
92-
for res in exc_set:
93-
ari_val = ""
94-
if(res):
95-
hex_str = res.entries.hex()
96-
hex_str = "0x"+hex_str.upper()
97-
ari_val = await transcoder.transcoder_put_cbor_await(hex_str)
98-
ari_val = ari_val['data']
99-
logger.info(str(nonce_cbor))
100-
addition = {'exec_set': ari_val,'nonce_cbor':str(nonce_cbor)}
101-
if addition not in final_res:
102-
final_res.append(addition)
94+
if(nonce_cbor != b'\xf6'): # not a null nonce
95+
stmt = select(ExecutionSet).where(and_(ExecutionSet.agent_id == agent_id_str, ExecutionSet.nonce_cbor == nonce_cbor) )
96+
result: Result = await session.scalars(stmt)
97+
exc_set = result.all()
98+
for res_exec in exc_set:
99+
ari_val = ""
100+
if(res_exec):
101+
hex_str = res_exec.entries.hex()
102+
hex_str = "0x"+hex_str.upper()
103+
ari_val = await transcoder.transcoder_put_cbor_await(hex_str)
104+
ari_val = ari_val['data']
105+
addition = {'exec_set': ari_val,'nonce_cbor':str(nonce_cbor)}
106+
if addition not in final_res:
107+
final_res.append(addition)
108+
else: #null nonce use report source
109+
rpt_set = res.report_list_cbor.hex()
110+
# Using Ace to translate CBOR into ARI object to process individual parts
111+
in_text = '0x'+rpt_set
112+
ari_rpt = None
113+
try:
114+
in_bytes = ace.cborutil.from_hexstr(in_text)
115+
ari_rpt = dec.decode(io.BytesIO(in_bytes))
116+
except Exception as err:
117+
logger.error(err)
118+
119+
# running through and translating all parts of rptset
120+
for rpt in ari_rpt.value.reports:
121+
try:
122+
enc = ace.ari_text.Encoder()
123+
buf = io.StringIO()
124+
enc.encode(rpt.source, buf)
125+
out_text = buf.getvalue()
126+
ari_val = out_text
127+
# TODO look at better way to handle storing nonce with null
128+
addition = {'exec_set': ari_val,'nonce_cbor':str(nonce_cbor)}
129+
if addition not in final_res:
130+
final_res.append(addition)
131+
except Exception as err:
132+
logger.error(err)
133+
103134
except Exception as e:
104135
logger.error(f"Error {e}, while processing nonce:{nonce_cbor} for agent: {agent_id_str}")
105136

106137
return final_res
107138

108-
109139
# entries tabulated returns header and values in correct order
110-
@router.get("/entries/table/{agent_id}/{nonce_cbor}", status_code=status.HTTP_200_OK,
111-
response_model=list)
112-
async def report_ac(agent_id: int, nonce_cbor: str):
113-
140+
# handling if nonce_cbor is null
141+
@router.get("/entries/table/{agent_id}/{nonce_cbor}", status_code=status.HTTP_200_OK)
142+
async def report_ac(agent_id: int, nonce_cbor: str) -> dict:
114143
ari = None
115144
dec = ace.ari_cbor.Decoder()
116145
enc = ace.ari_text.Encoder()
146+
exec_set_dir = {}
147+
logger.info(nonce_cbor)
148+
logger.info(type(nonce_cbor))
117149
try:
150+
store_nonce = nonce_cbor
118151
nonce_cbor = ast.literal_eval(nonce_cbor)
119152
except Exception as e:
120-
logger.error(f"{e} while processing nonce")
121-
return []
122-
123-
agent_id_str =""
124-
agent_id_stmt = select(RegisteredAgent).where(RegisteredAgent.registered_agents_id == agent_id)
125-
async with get_async_session() as session:
126-
result_agent: Result = await session.scalars(agent_id_stmt)
127-
agent_id_str = result_agent.one_or_none()
128-
agent_id_str = agent_id_str.agent_endpoint_uri
129-
130-
# Load in adms
131-
# get command that made the report as first entry
132-
stmt = select(ExecutionSet).where(and_(ExecutionSet.agent_id == agent_id_str, ExecutionSet.nonce_cbor == nonce_cbor) )
133-
async with get_async_session() as session:
134-
result: Result = await session.scalars(stmt)
153+
try:
154+
nonce_cbor = ast.literal_eval(str(bytes.fromhex(nonce_cbor)))
155+
except Exception as e:
156+
logger.error(f"{e} while processing nonce")
157+
return []
135158

136-
# there should only be one execution per agent per nonce_cbor
137-
# in the event that two occur pull the latest one
138-
result = result.all()
139-
exec_set_dir = {}
140-
141-
if result:
142-
result = result[-1]
143-
exec_set = result.entries.hex()
144-
# use ACE to handle report set decoding
145-
in_text = '0x'+exec_set
146-
try:
147-
in_bytes = ace.cborutil.from_hexstr(in_text)
148-
ari = dec.decode(io.BytesIO(in_bytes))
149-
150-
except Exception as err:
151-
logger.error(err)
152-
153-
# current ARI should be an exection set
154-
if ari:
155-
if type(ari.value) == ace.ari.ExecutionSet:
156-
try:
157-
158-
# run through targets and their parameters to get all things parts translated
159-
for targ in ari.value.targets:
160-
buf = io.StringIO()
161-
exec_set_entry=["time"]
162-
enc.encode(targ, buf)
163-
out_text_targ = buf.getvalue()
164-
if targ is ace.LiteralARI and targ.type_id is ace.StructType.AC:
165-
for part in targ.value:
166-
buf = io.StringIO()
167-
enc.encode(part, buf)
168-
out_text = buf.getvalue()
169-
exec_set_entry.append(out_text)
170-
else:
171-
exec_set_entry.append(out_text_targ)
172-
173-
exec_set_dir[out_text_targ] = [exec_set_entry]
174-
175-
except Exception as err:
176-
logger.error(err)
177-
178-
179-
# final_res.append(exec_set_entry)
159+
160+
# process each report in the rpt set and place inside appropiate nonce case or if null use source as key
161+
# TODO use td off set in report set to update actual time
162+
#
180163
ari = None
181164
stmt = select(Report).where(and_(Report.agent_id == agent_id, Report.nonce_cbor == nonce_cbor) )
182165
async with get_async_session() as session:
183166
result: Result = await session.scalars(stmt)
184167
for res in result.all():
185168
# used to hold final report set
186-
addition = [res.reference_time]
169+
curr_time = res.reference_time
170+
# addition = {time:}
187171
rpt_set = res.report_list_cbor.hex()
188172
# Using Ace to translate CBOR into ARI object to process individual parts
189173
in_text = '0x'+rpt_set
@@ -197,22 +181,45 @@ async def report_ac(agent_id: int, nonce_cbor: str):
197181
# current ARI should be an report set
198182
if ari:
199183
if type(ari.value) == ace.ari.ReportSet:
184+
# for each report in a rptset
185+
# add to the top level nonce dict or to source dict if nonce is null null
200186
for rpt in ari.value.reports:
201187
try:
188+
# structure for the reports
189+
# time: source_name:{[values of reprots ]}
190+
buf = io.StringIO()
191+
enc.encode(rpt.source, buf)
192+
rpt_src = buf.getvalue()
193+
addition = {"time":curr_time, rpt_src:[]}
194+
rpt_entries = []
202195
enc = ace.ari_text.Encoder()
203196
# running through and translating all parts of rptset
204197
for item in rpt.items:
205-
buf = io.StringIO()
206-
enc.encode(item, buf)
207-
out_text = buf.getvalue()
208-
addition.append(out_text)
209-
buf = io.StringIO()
210-
enc.encode(rpt.source, buf)
211-
out_text = buf.getvalue()
198+
# using ace to decode the components
199+
# item = dec.decode(item)
200+
if type(item.value) == ace.ari.Table:
201+
table_vals = []
202+
for tab_val in item.value:
203+
table_vals.append([t.value for t in tab_val])
204+
rpt_entries.append(table_vals)
205+
else:#handle values as normal
206+
buf = io.StringIO()
207+
enc.encode(item, buf)
208+
out_text = buf.getvalue()
209+
rpt_entries.append(out_text)
210+
211+
# placing all the values in the sources section
212+
addition[rpt_src] = rpt_entries
212213

213-
exec_set_dir[out_text].append(addition)
214+
if(nonce_cbor == b'\xf6' ):
215+
curr_dic = exec_set_dir.get(rpt_src,[])
216+
curr_dic.append(addition)
217+
exec_set_dir[rpt_src] = curr_dic
218+
else:
219+
curr_dic = exec_set_dir.get(store_nonce,[])
220+
curr_dic.append(addition)
221+
exec_set_dir[store_nonce] = curr_dic
214222
except Exception as err:
215223
logger.error(err)
216-
217-
return list(exec_set_dir.values())
224+
return exec_set_dir
218225

anms-core/anms/routes/system_status.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
{"name": "anms-ui", "url": "http://anms-ui:9030"},
5050
{"name": "aricodec"},
5151
{"name": "authnz", "url": "http://authnz/authn/login.html"},
52-
{"name": "grafana", "url": "http://grafana:3000"},
52+
{"name": "grafana", "tcp_port": 3000},
5353
{"name": "grafana-image-renderer", "url": "http://grafana-image-renderer:8081"},
5454
{"name": "amp-manager", "url": "http://amp-manager:8089/nm/api/version"},
5555
{"name": "mqtt-broker"},
@@ -78,7 +78,7 @@ def get_containers_status():
7878
if "url" in container:
7979
url = container['url']
8080
try:
81-
response = requests.get(url, timeout=timeout)
81+
response = requests.get(url, timeout=timeout, allow_redirects=False)
8282
if response.status_code == 200:
8383
statuses[name] = "healthy"
8484
else:

0 commit comments

Comments
 (0)