praetorian-cli 2.2.4__py3-none-any.whl → 2.2.6__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- praetorian_cli/handlers/add.py +20 -27
- praetorian_cli/handlers/agent.py +29 -0
- praetorian_cli/handlers/get.py +4 -2
- praetorian_cli/handlers/utils.py +59 -0
- praetorian_cli/sdk/entities/configurations.py +1 -1
- praetorian_cli/sdk/entities/definitions.py +11 -2
- praetorian_cli/sdk/entities/files.py +10 -5
- praetorian_cli/sdk/entities/risks.py +10 -5
- praetorian_cli/sdk/entities/search.py +6 -4
- praetorian_cli/sdk/model/globals.py +1 -0
- praetorian_cli/sdk/test/test_conversation.py +195 -0
- praetorian_cli/sdk/test/test_z_cli.py +24 -0
- praetorian_cli/ui/conversation/__init__.py +3 -0
- praetorian_cli/ui/conversation/textual_chat.py +622 -0
- {praetorian_cli-2.2.4.dist-info → praetorian_cli-2.2.6.dist-info}/METADATA +1 -1
- {praetorian_cli-2.2.4.dist-info → praetorian_cli-2.2.6.dist-info}/RECORD +20 -17
- {praetorian_cli-2.2.4.dist-info → praetorian_cli-2.2.6.dist-info}/WHEEL +0 -0
- {praetorian_cli-2.2.4.dist-info → praetorian_cli-2.2.6.dist-info}/entry_points.txt +0 -0
- {praetorian_cli-2.2.4.dist-info → praetorian_cli-2.2.6.dist-info}/licenses/LICENSE +0 -0
- {praetorian_cli-2.2.4.dist-info → praetorian_cli-2.2.6.dist-info}/top_level.txt +0 -0
praetorian_cli/handlers/add.py
CHANGED
|
@@ -5,7 +5,7 @@ import click
|
|
|
5
5
|
|
|
6
6
|
from praetorian_cli.handlers.chariot import chariot
|
|
7
7
|
from praetorian_cli.handlers.cli_decorators import cli_handler, praetorian_only
|
|
8
|
-
from praetorian_cli.handlers.utils import error
|
|
8
|
+
from praetorian_cli.handlers.utils import error, parse_configuration_value
|
|
9
9
|
from praetorian_cli.sdk.model.globals import AddRisk, Asset, Seed, Kind
|
|
10
10
|
|
|
11
11
|
|
|
@@ -274,40 +274,33 @@ def setting(sdk, name, value):
|
|
|
274
274
|
@add.command()
|
|
275
275
|
@cli_handler
|
|
276
276
|
@click.option('-n', '--name', required=True, help='Name of the configuration')
|
|
277
|
-
@click.option('-e', '--entry', required=
|
|
277
|
+
@click.option('-e', '--entry', required=False, multiple=True,
|
|
278
|
+
help='Key-value pair in format key=value. Can be specified multiple times to set multiple values.')
|
|
279
|
+
@click.option('--string', 'string_value', required=False,
|
|
280
|
+
help='Set the configuration value to a string')
|
|
281
|
+
@click.option('--integer', 'integer_value', required=False,
|
|
282
|
+
help='Set the configuration value to an integer')
|
|
283
|
+
@click.option('--float', 'float_value', required=False,
|
|
284
|
+
help='Set the configuration value to a floating point number')
|
|
278
285
|
@praetorian_only
|
|
279
|
-
def configuration(sdk, name, entry):
|
|
286
|
+
def configuration(sdk, name, entry, string_value, integer_value, float_value):
|
|
280
287
|
""" Add a configuration
|
|
281
288
|
|
|
282
|
-
This command adds, or overwrites if exists, a
|
|
289
|
+
This command adds, or overwrites if exists, a configuration value.
|
|
290
|
+
|
|
291
|
+
Configuration values can be provided as a mapping of key-value pairs using
|
|
292
|
+
``--entry`` (the previous behavior), or as primitive values using
|
|
293
|
+
``--string``, ``--integer``, or ``--float``.
|
|
283
294
|
|
|
284
295
|
\b
|
|
285
296
|
Example usages:
|
|
286
297
|
- praetorian chariot add configuration --name "nuclei" --entry extra-tags=http,sql --entry something=else
|
|
298
|
+
- praetorian chariot add configuration --name "billing-status" --string PAID_MS
|
|
299
|
+
- praetorian chariot add configuration --name "request-timeout" --integer 60
|
|
300
|
+
- praetorian chariot add configuration --name "scoring-threshold" --float 0.85
|
|
287
301
|
"""
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
if '=' not in item:
|
|
291
|
-
click.echo(f"Error: Entry '{item}' is not in the format key=value")
|
|
292
|
-
return
|
|
293
|
-
|
|
294
|
-
if item.count('=') > 1:
|
|
295
|
-
click.echo(f"Error: Entry '{item}' contains multiple '=' characters. Format should be key=value")
|
|
296
|
-
return
|
|
297
|
-
|
|
298
|
-
key, value = item.split('=', 1)
|
|
299
|
-
|
|
300
|
-
if not key:
|
|
301
|
-
click.echo("Error: Key cannot be empty")
|
|
302
|
-
return
|
|
303
|
-
|
|
304
|
-
if not value:
|
|
305
|
-
click.echo("Error: Value cannot be empty")
|
|
306
|
-
return
|
|
307
|
-
|
|
308
|
-
config_dict[key] = value
|
|
309
|
-
|
|
310
|
-
sdk.configurations.add(name, config_dict)
|
|
302
|
+
config_value = parse_configuration_value(entry, string_value, integer_value, float_value)
|
|
303
|
+
sdk.configurations.add(name, config_value)
|
|
311
304
|
|
|
312
305
|
|
|
313
306
|
@add.command()
|
praetorian_cli/handlers/agent.py
CHANGED
|
@@ -69,3 +69,32 @@ def tools(sdk, allowed):
|
|
|
69
69
|
"""
|
|
70
70
|
for tool in dict.keys(sdk.agents.list_mcp_tools(allowed)):
|
|
71
71
|
click.echo(tool)
|
|
72
|
+
|
|
73
|
+
@agent.command()
|
|
74
|
+
@cli_handler
|
|
75
|
+
def conversation(sdk):
|
|
76
|
+
""" Interactive conversation with Chariot AI assistant
|
|
77
|
+
|
|
78
|
+
Start an interactive chat session with the Chariot AI assistant.
|
|
79
|
+
The AI can help you query security data, understand findings,
|
|
80
|
+
and provide insights about your attack surface.
|
|
81
|
+
|
|
82
|
+
\b
|
|
83
|
+
Commands within conversation:
|
|
84
|
+
- help Show available commands and query examples
|
|
85
|
+
- clear Clear the screen
|
|
86
|
+
- new Start a new conversation
|
|
87
|
+
- quit Exit the conversation
|
|
88
|
+
|
|
89
|
+
\b
|
|
90
|
+
Example queries:
|
|
91
|
+
- "Find all active assets"
|
|
92
|
+
- "Show me critical risks"
|
|
93
|
+
- "What assets do we have for example.com?"
|
|
94
|
+
|
|
95
|
+
\b
|
|
96
|
+
Usage:
|
|
97
|
+
praetorian chariot agent conversation
|
|
98
|
+
"""
|
|
99
|
+
from praetorian_cli.ui.conversation import run_textual_conversation
|
|
100
|
+
run_textual_conversation(sdk)
|
praetorian_cli/handlers/get.py
CHANGED
|
@@ -147,7 +147,8 @@ def file(chariot, name, path):
|
|
|
147
147
|
@cli_handler
|
|
148
148
|
@click.argument('name')
|
|
149
149
|
@click.option('-path', '--path', default=os.getcwd(), help='Download path. Default: save to current directory')
|
|
150
|
-
|
|
150
|
+
@click.option('--global', 'global_', is_flag=True, help='Fetch from global definitions instead of user-specific')
|
|
151
|
+
def definition(chariot, name, path, global_):
|
|
151
152
|
""" Download a definition using the risk name
|
|
152
153
|
|
|
153
154
|
\b
|
|
@@ -158,8 +159,9 @@ def definition(chariot, name, path):
|
|
|
158
159
|
Example usage:
|
|
159
160
|
- praetorian chariot get definition jira-unauthenticated-user-picker
|
|
160
161
|
- praetorian chariot get definition CVE-2024-23049
|
|
162
|
+
- praetorian chariot get definition CVE-2024-23049 --global
|
|
161
163
|
"""
|
|
162
|
-
downloaded_path = chariot.definitions.get(name, path)
|
|
164
|
+
downloaded_path = chariot.definitions.get(name, path, global_=global_)
|
|
163
165
|
click.echo(f'Saved definition at {downloaded_path}')
|
|
164
166
|
|
|
165
167
|
|
praetorian_cli/handlers/utils.py
CHANGED
|
@@ -3,6 +3,65 @@ import json
|
|
|
3
3
|
import click
|
|
4
4
|
|
|
5
5
|
|
|
6
|
+
def parse_configuration_value(
|
|
7
|
+
entries = [], s_val=None, i_val=None, f_val=None):
|
|
8
|
+
"""Return a configuration value derived from CLI inputs."""
|
|
9
|
+
has_entries = len(entries) > 0
|
|
10
|
+
typed_values = {
|
|
11
|
+
'string': s_val,
|
|
12
|
+
'integer': i_val,
|
|
13
|
+
'float': f_val,
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
if has_entries and any(value is not None for value in typed_values.values()):
|
|
17
|
+
error('--entry cannot be combined with --string, --integer, or --float')
|
|
18
|
+
|
|
19
|
+
if has_entries:
|
|
20
|
+
return _parse_entry_dict(entries)
|
|
21
|
+
|
|
22
|
+
provided = [(name, value) for name, value in typed_values.items() if value is not None]
|
|
23
|
+
|
|
24
|
+
if not provided:
|
|
25
|
+
error('Provide configuration data via --entry, --string, --integer, or --float')
|
|
26
|
+
if len(provided) > 1:
|
|
27
|
+
error('Specify only one of --string, --integer, or --float')
|
|
28
|
+
|
|
29
|
+
value_type, raw_value = provided[0]
|
|
30
|
+
return _cast_typed_value(value_type, raw_value)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def _parse_entry_dict(entries):
|
|
34
|
+
parsed = {}
|
|
35
|
+
|
|
36
|
+
for item in entries:
|
|
37
|
+
key, value = _split_entry(item)
|
|
38
|
+
if not key:
|
|
39
|
+
error(f'Key cannot be empty: {item}')
|
|
40
|
+
if not value:
|
|
41
|
+
error(f'Value cannot be empty: {item}')
|
|
42
|
+
parsed[key] = value
|
|
43
|
+
return parsed
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def _split_entry(item):
|
|
47
|
+
if '=' not in item:
|
|
48
|
+
error(f"Entry '{item}' is not in the format key=value")
|
|
49
|
+
if item.count('=') > 1:
|
|
50
|
+
error(f"Entry '{item}' contains multiple '=' characters. Format should be key=value")
|
|
51
|
+
return item.split('=', 1)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def _cast_typed_value(value_type, raw_value):
|
|
55
|
+
try:
|
|
56
|
+
if value_type == 'integer':
|
|
57
|
+
return int(raw_value)
|
|
58
|
+
if value_type == 'float':
|
|
59
|
+
return float(raw_value)
|
|
60
|
+
except ValueError:
|
|
61
|
+
error(f'{value_type} must be a valid {value_type}')
|
|
62
|
+
return raw_value
|
|
63
|
+
|
|
64
|
+
|
|
6
65
|
def render_list_results(list_results, details):
|
|
7
66
|
list_data, offset = list_results
|
|
8
67
|
if details:
|
|
@@ -29,7 +29,7 @@ class Definitions:
|
|
|
29
29
|
definition_name = os.path.basename(local_filepath)
|
|
30
30
|
return self.api.files.add(local_filepath, f'definitions/{definition_name}')
|
|
31
31
|
|
|
32
|
-
def get(self, definition_name, download_directory=os.getcwd()):
|
|
32
|
+
def get(self, definition_name, download_directory=os.getcwd(), global_=False):
|
|
33
33
|
"""
|
|
34
34
|
Download a risk definition file from the definitions folder.
|
|
35
35
|
|
|
@@ -37,15 +37,24 @@ class Definitions:
|
|
|
37
37
|
:type definition_name: str
|
|
38
38
|
:param download_directory: The directory to save the downloaded file (defaults to current working directory)
|
|
39
39
|
:type download_directory: str
|
|
40
|
+
:param global_: If True, fetch from global definitions instead of user-specific
|
|
41
|
+
:type global_: bool
|
|
40
42
|
:return: The local file path where the definition was saved
|
|
41
43
|
:rtype: str
|
|
42
44
|
"""
|
|
43
|
-
|
|
45
|
+
try:
|
|
46
|
+
content = self.api.files.get_utf8(f'definitions/{definition_name}', _global=global_)
|
|
47
|
+
except Exception as e:
|
|
48
|
+
if global_:
|
|
49
|
+
raise Exception(f'Global definition {definition_name} not found or inaccessible.')
|
|
50
|
+
else:
|
|
51
|
+
raise
|
|
44
52
|
download_path = os.path.join(download_directory, definition_name)
|
|
45
53
|
with open(download_path, 'w') as file:
|
|
46
54
|
file.write(content)
|
|
47
55
|
return download_path
|
|
48
56
|
|
|
57
|
+
|
|
49
58
|
def list(self, name_filter='', offset=None, pages=100000) -> tuple:
|
|
50
59
|
"""
|
|
51
60
|
List the definition names, optionally prefix-filtered by a definition name.
|
|
@@ -46,30 +46,35 @@ class Files:
|
|
|
46
46
|
|
|
47
47
|
return download_path
|
|
48
48
|
|
|
49
|
-
def get(self, chariot_filepath) -> bytes:
|
|
49
|
+
def get(self, chariot_filepath, _global=False) -> bytes:
|
|
50
50
|
"""
|
|
51
51
|
Download a file from Chariot storage into memory as bytes.
|
|
52
52
|
|
|
53
53
|
:param chariot_filepath: Path of the file in Chariot storage to download
|
|
54
54
|
:type chariot_filepath: str
|
|
55
|
+
:param _global: If True, fetch from global storage instead of user-specific
|
|
56
|
+
:type _global: bool
|
|
55
57
|
:return: The file content as bytes
|
|
56
58
|
:rtype: bytes
|
|
57
59
|
:raises Exception: If the file does not exist in Chariot storage
|
|
58
60
|
"""
|
|
59
|
-
|
|
60
|
-
|
|
61
|
+
if not _global:
|
|
62
|
+
self.raise_if_missing(chariot_filepath)
|
|
63
|
+
return self.api.download(chariot_filepath, global_=_global)
|
|
61
64
|
|
|
62
|
-
def get_utf8(self, chariot_filepath) -> str:
|
|
65
|
+
def get_utf8(self, chariot_filepath, _global=False) -> str:
|
|
63
66
|
"""
|
|
64
67
|
Download a file from Chariot storage into memory as a UTF-8 string.
|
|
65
68
|
|
|
66
69
|
:param chariot_filepath: Path of the file in Chariot storage to download
|
|
67
70
|
:type chariot_filepath: str
|
|
71
|
+
:param _global: If True, fetch from global storage instead of user-specific
|
|
72
|
+
:type _global: bool
|
|
68
73
|
:return: The file content as a UTF-8 decoded string
|
|
69
74
|
:rtype: str
|
|
70
75
|
:raises Exception: If the file does not exist in Chariot storage
|
|
71
76
|
"""
|
|
72
|
-
return self.get(chariot_filepath).decode('utf-8')
|
|
77
|
+
return self.get(chariot_filepath, _global=_global).decode('utf-8')
|
|
73
78
|
|
|
74
79
|
def list(self, prefix_filter='', offset=None, pages=100000) -> tuple:
|
|
75
80
|
"""
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
from praetorian_cli.sdk.model.globals import Kind
|
|
2
|
-
from praetorian_cli.sdk.model.query import Relationship, Node, Query, risk_of_key, ASSET_NODE, ATTRIBUTE_NODE
|
|
2
|
+
from praetorian_cli.sdk.model.query import Relationship, Node, Query, risk_of_key, ASSET_NODE, ATTRIBUTE_NODE, Filter
|
|
3
3
|
|
|
4
4
|
|
|
5
5
|
class Risks:
|
|
@@ -85,12 +85,12 @@ class Risks:
|
|
|
85
85
|
|
|
86
86
|
return self.api.delete_by_key('risk', key, body)
|
|
87
87
|
|
|
88
|
-
def list(self,
|
|
88
|
+
def list(self, contains_filter='', offset=None, pages=100000) -> tuple:
|
|
89
89
|
"""
|
|
90
90
|
List risks with optional filtering and pagination.
|
|
91
91
|
|
|
92
|
-
:param
|
|
93
|
-
:type
|
|
92
|
+
:param contains_filter: Filter to apply to the risk key. Ensure the risk's key contains the filter.
|
|
93
|
+
:type contains_filter: str
|
|
94
94
|
:param offset: The offset of the page you want to retrieve results. If not supplied, retrieves from the first page
|
|
95
95
|
:type offset: str or None
|
|
96
96
|
:param pages: The number of pages of results to retrieve. <mcp>Start with one page of results unless specifically requested.</mcp>
|
|
@@ -98,7 +98,12 @@ class Risks:
|
|
|
98
98
|
:return: A tuple containing (list of matching risks, next page offset)
|
|
99
99
|
:rtype: tuple
|
|
100
100
|
"""
|
|
101
|
-
|
|
101
|
+
filters = []
|
|
102
|
+
if contains_filter:
|
|
103
|
+
contains_filter_filter = Filter(field=Filter.Field.KEY, operator=Filter.Operator.CONTAINS, value=contains_filter)
|
|
104
|
+
filters.append(contains_filter_filter)
|
|
105
|
+
query = Query(Node([Node.Label.RISK], filters=filters))
|
|
106
|
+
return self.api.search.by_query(query, pages)
|
|
102
107
|
|
|
103
108
|
def attributes(self, key):
|
|
104
109
|
"""
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import json
|
|
2
2
|
from praetorian_cli.sdk.model.query import Query
|
|
3
|
-
from praetorian_cli.sdk.model.globals import EXACT_FLAG, DESCENDING_FLAG, GLOBAL_FLAG, Kind
|
|
3
|
+
from praetorian_cli.sdk.model.globals import EXACT_FLAG, DESCENDING_FLAG, GLOBAL_FLAG, USER_FLAG, Kind
|
|
4
4
|
class Search:
|
|
5
5
|
|
|
6
6
|
def __init__(self, api):
|
|
@@ -17,7 +17,7 @@ class Search:
|
|
|
17
17
|
"""
|
|
18
18
|
return self.api.count(dict(key=search_term))
|
|
19
19
|
|
|
20
|
-
def by_key_prefix(self, key_prefix, offset=None, pages=100000) -> tuple:
|
|
20
|
+
def by_key_prefix(self, key_prefix, offset=None, pages=100000, user=False) -> tuple:
|
|
21
21
|
"""
|
|
22
22
|
Search for entities by key prefix. <mcp>If the response is too large, make your query more specific.<mcp>
|
|
23
23
|
|
|
@@ -30,7 +30,7 @@ class Search:
|
|
|
30
30
|
:return: A tuple containing (list of matching entities, next page offset)
|
|
31
31
|
:rtype: tuple
|
|
32
32
|
"""
|
|
33
|
-
return self.by_term(key_prefix, None, offset, pages)
|
|
33
|
+
return self.by_term(key_prefix, None, offset, pages, user=user)
|
|
34
34
|
|
|
35
35
|
def by_exact_key(self, key, get_attributes=False) -> {}:
|
|
36
36
|
"""
|
|
@@ -119,7 +119,7 @@ class Search:
|
|
|
119
119
|
return self.by_term(f'dns:{dns_prefix}', kind, offset, pages)
|
|
120
120
|
|
|
121
121
|
def by_term(self, search_term, kind=None, offset=None, pages=100000, exact=False, descending=False,
|
|
122
|
-
global_=False) -> tuple:
|
|
122
|
+
global_=False, user=False) -> tuple:
|
|
123
123
|
"""
|
|
124
124
|
Search for a given kind by term.
|
|
125
125
|
|
|
@@ -151,6 +151,8 @@ class Search:
|
|
|
151
151
|
params |= DESCENDING_FLAG
|
|
152
152
|
if global_:
|
|
153
153
|
params |= GLOBAL_FLAG
|
|
154
|
+
if user:
|
|
155
|
+
params |= USER_FLAG
|
|
154
156
|
|
|
155
157
|
results = self.api.my(params, pages)
|
|
156
158
|
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import pytest
|
|
2
|
+
import json
|
|
3
|
+
import time
|
|
4
|
+
|
|
5
|
+
from praetorian_cli.sdk.test.utils import make_test_values, clean_test_entities, setup_chariot
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
@pytest.mark.coherence
|
|
9
|
+
class TestConversation:
|
|
10
|
+
"""Test conversation functionality with the Chariot agent"""
|
|
11
|
+
|
|
12
|
+
def setup_class(self):
|
|
13
|
+
self.sdk = setup_chariot()
|
|
14
|
+
make_test_values(self)
|
|
15
|
+
self.conversation_id = None
|
|
16
|
+
self.message_keys = []
|
|
17
|
+
|
|
18
|
+
def test_start_conversation(self):
|
|
19
|
+
"""Test starting a new conversation with the agent"""
|
|
20
|
+
url = self.sdk.url("/planner")
|
|
21
|
+
payload = {
|
|
22
|
+
"message": f"Test conversation started at {int(time.time())}",
|
|
23
|
+
"mode": "query"
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
response = self.sdk._make_request("POST", url, json=payload)
|
|
27
|
+
|
|
28
|
+
# Should get successful response
|
|
29
|
+
assert response.status_code == 200
|
|
30
|
+
|
|
31
|
+
result = response.json()
|
|
32
|
+
assert "conversation" in result
|
|
33
|
+
assert "uuid" in result["conversation"]
|
|
34
|
+
|
|
35
|
+
self.conversation_id = result["conversation"]["uuid"]
|
|
36
|
+
assert self.conversation_id is not None
|
|
37
|
+
assert len(self.conversation_id) > 0
|
|
38
|
+
|
|
39
|
+
def test_send_message_with_existing_conversation(self):
|
|
40
|
+
"""Test sending a message to an existing conversation"""
|
|
41
|
+
# First, start a new conversation
|
|
42
|
+
url = self.sdk.url("/planner")
|
|
43
|
+
initial_payload = {
|
|
44
|
+
"message": f"Initial message for follow-up test at {int(time.time())}",
|
|
45
|
+
"mode": "query"
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
response = self.sdk._make_request("POST", url, json=initial_payload)
|
|
49
|
+
assert response.status_code == 200
|
|
50
|
+
|
|
51
|
+
result = response.json()
|
|
52
|
+
conversation_id = result["conversation"]["uuid"]
|
|
53
|
+
|
|
54
|
+
# Now send a follow-up message to the existing conversation
|
|
55
|
+
follow_up_payload = {
|
|
56
|
+
"message": f"Follow-up message in existing conversation at {int(time.time())}",
|
|
57
|
+
"mode": "query",
|
|
58
|
+
"conversationId": conversation_id
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
response = self.sdk._make_request("POST", url, json=follow_up_payload)
|
|
62
|
+
|
|
63
|
+
# Should get successful response
|
|
64
|
+
assert response.status_code == 200
|
|
65
|
+
|
|
66
|
+
def test_read_conversation_messages(self):
|
|
67
|
+
"""Test reading messages from a conversation"""
|
|
68
|
+
# First, create a conversation and send a message
|
|
69
|
+
url = self.sdk.url("/planner")
|
|
70
|
+
payload = {
|
|
71
|
+
"message": f"Test message for reading at {int(time.time())}",
|
|
72
|
+
"mode": "query"
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
response = self.sdk._make_request("POST", url, json=payload)
|
|
76
|
+
assert response.status_code == 200
|
|
77
|
+
|
|
78
|
+
result = response.json()
|
|
79
|
+
conversation_id = result["conversation"]["uuid"]
|
|
80
|
+
|
|
81
|
+
# Wait a moment for message to be stored
|
|
82
|
+
time.sleep(2)
|
|
83
|
+
|
|
84
|
+
# Now try to read messages from this conversation
|
|
85
|
+
messages, offset = self.sdk.search.by_key_prefix(
|
|
86
|
+
f"#message#{conversation_id}#", user=True
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
# Should have at least the user message
|
|
90
|
+
assert isinstance(messages, list)
|
|
91
|
+
# Note: messages might be empty immediately after creation due to async processing
|
|
92
|
+
# This is still a valid test as it verifies the search functionality works
|
|
93
|
+
|
|
94
|
+
def test_read_conversations_list(self):
|
|
95
|
+
"""Test reading list of conversations"""
|
|
96
|
+
# First create a test conversation
|
|
97
|
+
url = self.sdk.url("/planner")
|
|
98
|
+
payload = {
|
|
99
|
+
"message": f"Test conversation for listing at {int(time.time())}",
|
|
100
|
+
"mode": "query"
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
response = self.sdk._make_request("POST", url, json=payload)
|
|
104
|
+
assert response.status_code == 200
|
|
105
|
+
|
|
106
|
+
# Wait a moment for conversation to be stored
|
|
107
|
+
time.sleep(1)
|
|
108
|
+
|
|
109
|
+
# Now search for conversations
|
|
110
|
+
conversations, offset = self.sdk.search.by_key_prefix(
|
|
111
|
+
"#conversation#", user=True
|
|
112
|
+
)
|
|
113
|
+
|
|
114
|
+
# Should be able to search for conversations (list might be empty or contain conversations)
|
|
115
|
+
assert isinstance(conversations, list)
|
|
116
|
+
|
|
117
|
+
def test_conversation_api_error_handling(self):
|
|
118
|
+
"""Test handling of malformed requests"""
|
|
119
|
+
url = self.sdk.url("/planner")
|
|
120
|
+
# Send malformed payload (missing required fields)
|
|
121
|
+
malformed_payload = {
|
|
122
|
+
"invalid_field": "should cause error"
|
|
123
|
+
# Missing required "message" field
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
response = self.sdk._make_request("POST", url, json=malformed_payload)
|
|
127
|
+
|
|
128
|
+
# Should get a 4xx error for bad request
|
|
129
|
+
assert response.status_code >= 400
|
|
130
|
+
|
|
131
|
+
def test_conversation_modes(self):
|
|
132
|
+
"""Test different conversation modes (query vs agent)"""
|
|
133
|
+
url = self.sdk.url("/planner")
|
|
134
|
+
|
|
135
|
+
# Test query mode
|
|
136
|
+
query_payload = {
|
|
137
|
+
"message": f"Test query mode at {int(time.time())}",
|
|
138
|
+
"mode": "query"
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
response = self.sdk._make_request("POST", url, json=query_payload)
|
|
142
|
+
assert response.status_code == 200
|
|
143
|
+
|
|
144
|
+
result = response.json()
|
|
145
|
+
assert "conversation" in result
|
|
146
|
+
|
|
147
|
+
# Test agent mode
|
|
148
|
+
agent_payload = {
|
|
149
|
+
"message": f"Test agent mode at {int(time.time())}",
|
|
150
|
+
"mode": "agent"
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
response = self.sdk._make_request("POST", url, json=agent_payload)
|
|
154
|
+
assert response.status_code == 200
|
|
155
|
+
|
|
156
|
+
result = response.json()
|
|
157
|
+
assert "conversation" in result
|
|
158
|
+
|
|
159
|
+
def test_message_polling_integration(self):
|
|
160
|
+
"""Test integration of message polling to check for new messages"""
|
|
161
|
+
# Create a conversation first
|
|
162
|
+
url = self.sdk.url("/planner")
|
|
163
|
+
payload = {
|
|
164
|
+
"message": f"Test message for polling at {int(time.time())}",
|
|
165
|
+
"mode": "query"
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
response = self.sdk._make_request("POST", url, json=payload)
|
|
169
|
+
assert response.status_code == 200
|
|
170
|
+
|
|
171
|
+
result = response.json()
|
|
172
|
+
conversation_id = result["conversation"]["uuid"]
|
|
173
|
+
|
|
174
|
+
# Test polling for messages (first call)
|
|
175
|
+
messages, offset = self.sdk.search.by_key_prefix(
|
|
176
|
+
f"#message#{conversation_id}#", user=True
|
|
177
|
+
)
|
|
178
|
+
|
|
179
|
+
initial_count = len(messages) if messages else 0
|
|
180
|
+
|
|
181
|
+
# Wait a moment and check again (simulating polling)
|
|
182
|
+
time.sleep(1)
|
|
183
|
+
|
|
184
|
+
messages, offset = self.sdk.search.by_key_prefix(
|
|
185
|
+
f"#message#{conversation_id}#", user=True
|
|
186
|
+
)
|
|
187
|
+
|
|
188
|
+
# Should still return valid result (messages may or may not have changed)
|
|
189
|
+
assert isinstance(messages, list)
|
|
190
|
+
|
|
191
|
+
def teardown_class(self):
|
|
192
|
+
"""Clean up test data"""
|
|
193
|
+
# Note: Conversations and messages are typically read-only in tests
|
|
194
|
+
# Real cleanup would depend on having delete methods for conversations
|
|
195
|
+
pass
|
|
@@ -5,6 +5,7 @@ from subprocess import run
|
|
|
5
5
|
import pytest
|
|
6
6
|
|
|
7
7
|
from praetorian_cli.sdk.model.globals import AddRisk, Asset, Risk, Seed, Preseed
|
|
8
|
+
from praetorian_cli.sdk.model.utils import configuration_key
|
|
8
9
|
from praetorian_cli.sdk.test.utils import epoch_micro, random_ip, make_test_values, clean_test_entities, setup_chariot
|
|
9
10
|
|
|
10
11
|
|
|
@@ -273,6 +274,24 @@ class TestZCli:
|
|
|
273
274
|
|
|
274
275
|
self.verify(f'delete configuration "{o.configuration_key}"', ignore_stdout=True)
|
|
275
276
|
|
|
277
|
+
string_name = f'{o.configuration_name}-string'
|
|
278
|
+
string_key = configuration_key(string_name)
|
|
279
|
+
self.verify(f'add configuration --name "{string_name}" --string PAID_MS')
|
|
280
|
+
self.verify(f'get configuration "{string_key}"', expected_stdout=[string_key, string_name, '"value": "PAID_MS"'])
|
|
281
|
+
self.verify(f'delete configuration "{string_key}"', ignore_stdout=True)
|
|
282
|
+
|
|
283
|
+
integer_name = f'{o.configuration_name}-integer'
|
|
284
|
+
integer_key = configuration_key(integer_name)
|
|
285
|
+
self.verify(f'add configuration --name "{integer_name}" --integer 123')
|
|
286
|
+
self.verify(f'get configuration "{integer_key}"', expected_stdout=[integer_key, integer_name, '"value": 123'])
|
|
287
|
+
self.verify(f'delete configuration "{integer_key}"', ignore_stdout=True)
|
|
288
|
+
|
|
289
|
+
float_name = f'{o.configuration_name}-float'
|
|
290
|
+
float_key = configuration_key(float_name)
|
|
291
|
+
self.verify(f'add configuration --name "{float_name}" --float 1.5')
|
|
292
|
+
self.verify(f'get configuration "{float_key}"', expected_stdout=[float_key, float_name, '"value": 1.5'])
|
|
293
|
+
self.verify(f'delete configuration "{float_key}"', ignore_stdout=True)
|
|
294
|
+
|
|
276
295
|
def test_webapplication_cli(self):
|
|
277
296
|
o = make_test_values(lambda: None)
|
|
278
297
|
self.verify(f'add asset --dns "{o.webapp_name}" --name "{o.webapp_url}" --type webapplication')
|
|
@@ -339,6 +358,11 @@ class TestZCli:
|
|
|
339
358
|
|
|
340
359
|
self.verify('link --help', ignore_stdout=True)
|
|
341
360
|
self.verify('link account --help', ignore_stdout=True)
|
|
361
|
+
|
|
362
|
+
# Agent commands
|
|
363
|
+
self.verify('agent --help', ignore_stdout=True)
|
|
364
|
+
self.verify('agent conversation --help', ignore_stdout=True)
|
|
365
|
+
self.verify('agent mcp --help', ignore_stdout=True)
|
|
342
366
|
self.verify('link webpage-source --help', ignore_stdout=True)
|
|
343
367
|
|
|
344
368
|
self.verify('unlink --help', ignore_stdout=True)
|