Skip to content

Commit d82f998

Browse files
author
Juliya Smith
authored
Disallow TLS specific args for non-TLS (#225)
1 parent 8548e01 commit d82f998

File tree

5 files changed

+157
-0
lines changed

5 files changed

+157
-0
lines changed

src/code42cli/cmds/search/__init__.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from code42cli.errors import Code42CLIError
44
from code42cli.logger import get_logger_for_server
5+
from code42cli.logger.enums import ServerProtocol
56
from code42cli.output_formats import OutputFormat
67

78

@@ -21,10 +22,27 @@ def invoke(self, ctx):
2122
protocol = ctx.params.get("protocol")
2223
output_format = ctx.params.get("format", OutputFormat.RAW)
2324
ignore_cert_validation = ctx.params.get("ignore_cert_validation")
25+
_handle_incompatible_args(protocol, ignore_cert_validation, certs)
26+
2427
if ignore_cert_validation:
2528
certs = "ignore"
2629

2730
ctx.obj.logger = _try_get_logger_for_server(
2831
hostname, protocol, output_format, certs
2932
)
3033
return super().invoke(ctx)
34+
35+
36+
def _handle_incompatible_args(protocol, ignore_cert_validation, certs):
37+
if protocol == ServerProtocol.TLS_TCP:
38+
return
39+
40+
arg = None
41+
if ignore_cert_validation is not None:
42+
arg = "--ignore-cert-validation"
43+
elif certs is not None:
44+
arg = "--certs"
45+
if arg is not None:
46+
raise click.BadOptionUsage(
47+
arg, f"'{arg}' can only be used with '--protocol {ServerProtocol.TLS_TCP}'."
48+
)

src/code42cli/cmds/search/options.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ def server_options(f):
162162
help="Set to skip CA certificate validation. "
163163
"Incompatible with the 'certs' option.",
164164
is_flag=True,
165+
default=None,
165166
cls=incompatible_with(["certs"]),
166167
)
167168
f = hostname_arg(f)

tests/cmds/test_alerts.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,52 @@ def test_send_to_when_given_ignore_cert_validation_uses_certs_equal_to_ignore_st
789789
)
790790

791791

792+
@pytest.mark.parametrize("protocol", (ServerProtocol.UDP, ServerProtocol.TCP))
793+
def test_send_to_when_given_ignore_cert_validation_with_non_tls_protocol_fails_expectedly(
794+
cli_state, runner, protocol
795+
):
796+
res = runner.invoke(
797+
cli,
798+
[
799+
"alerts",
800+
"send-to",
801+
"0.0.0.0",
802+
"--begin",
803+
"1d",
804+
"--protocol",
805+
protocol,
806+
"--ignore-cert-validation",
807+
],
808+
obj=cli_state,
809+
)
810+
assert (
811+
"'--ignore-cert-validation' can only be used with '--protocol TLS-TCP'"
812+
in res.output
813+
)
814+
815+
816+
@pytest.mark.parametrize("protocol", (ServerProtocol.UDP, ServerProtocol.TCP))
817+
def test_send_to_when_given_certs_with_non_tls_protocol_fails_expectedly(
818+
cli_state, runner, protocol
819+
):
820+
res = runner.invoke(
821+
cli,
822+
[
823+
"alerts",
824+
"send-to",
825+
"0.0.0.0",
826+
"--begin",
827+
"1d",
828+
"--protocol",
829+
protocol,
830+
"--certs",
831+
"certs.pem",
832+
],
833+
obj=cli_state,
834+
)
835+
assert "'--certs' can only be used with '--protocol TLS-TCP'" in res.output
836+
837+
792838
def test_get_alert_details_batches_results_according_to_batch_size(sdk):
793839
extraction._ALERT_DETAIL_BATCH_SIZE = 2
794840
sdk.alerts.get_details.side_effect = ALERT_DETAIL_RESULT

tests/cmds/test_auditlogs.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,52 @@ def test_send_to_emits_events_in_chronological_order(
311311
)
312312

313313

314+
@pytest.mark.parametrize("protocol", (ServerProtocol.UDP, ServerProtocol.TCP))
315+
def test_send_to_when_given_ignore_cert_validation_with_non_tls_protocol_fails_expectedly(
316+
cli_state, runner, protocol
317+
):
318+
res = runner.invoke(
319+
cli,
320+
[
321+
"audit-logs",
322+
"send-to",
323+
"0.0.0.0",
324+
"--begin",
325+
"1d",
326+
"--protocol",
327+
protocol,
328+
"--ignore-cert-validation",
329+
],
330+
obj=cli_state,
331+
)
332+
assert (
333+
"'--ignore-cert-validation' can only be used with '--protocol TLS-TCP'"
334+
in res.output
335+
)
336+
337+
338+
@pytest.mark.parametrize("protocol", (ServerProtocol.UDP, ServerProtocol.TCP))
339+
def test_send_to_when_given_certs_with_non_tls_protocol_fails_expectedly(
340+
cli_state, runner, protocol
341+
):
342+
res = runner.invoke(
343+
cli,
344+
[
345+
"audit-logs",
346+
"send-to",
347+
"0.0.0.0",
348+
"--begin",
349+
"1d",
350+
"--protocol",
351+
protocol,
352+
"--certs",
353+
"certs.pem",
354+
],
355+
obj=cli_state,
356+
)
357+
assert "'--certs' can only be used with '--protocol TLS-TCP'" in res.output
358+
359+
314360
@search_and_send_to_test
315361
def test_search_and_send_to_with_checkpoint_saves_expected_cursor_timestamp(
316362
cli_state,

tests/cmds/test_securitydata.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,52 @@ def test_send_to_with_saved_search_and_incompatible_argument_errors(
298298
assert "{} can't be used with: --saved-search".format(arg[0]) in result.output
299299

300300

301+
@pytest.mark.parametrize("protocol", (ServerProtocol.UDP, ServerProtocol.TCP))
302+
def test_send_to_when_given_ignore_cert_validation_with_non_tls_protocol_fails_expectedly(
303+
cli_state, runner, protocol
304+
):
305+
res = runner.invoke(
306+
cli,
307+
[
308+
"security-data",
309+
"send-to",
310+
"0.0.0.0",
311+
"--begin",
312+
"1d",
313+
"--protocol",
314+
protocol,
315+
"--ignore-cert-validation",
316+
],
317+
obj=cli_state,
318+
)
319+
assert (
320+
"'--ignore-cert-validation' can only be used with '--protocol TLS-TCP'"
321+
in res.output
322+
)
323+
324+
325+
@pytest.mark.parametrize("protocol", (ServerProtocol.UDP, ServerProtocol.TCP))
326+
def test_send_to_when_given_certs_with_non_tls_protocol_fails_expectedly(
327+
cli_state, runner, protocol
328+
):
329+
res = runner.invoke(
330+
cli,
331+
[
332+
"security-data",
333+
"send-to",
334+
"0.0.0.0",
335+
"--begin",
336+
"1d",
337+
"--protocol",
338+
protocol,
339+
"--certs",
340+
"certs.pem",
341+
],
342+
obj=cli_state,
343+
)
344+
assert "'--certs' can only be used with '--protocol TLS-TCP'" in res.output
345+
346+
301347
@search_and_send_to_test
302348
def test_search_and_send_to_when_given_begin_and_end_dates_uses_expected_query(
303349
runner, cli_state, file_event_extractor, command

0 commit comments

Comments
 (0)