conduct-cli 0.5.9__tar.gz → 0.6.0__tar.gz
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.
- {conduct_cli-0.5.9 → conduct_cli-0.6.0}/PKG-INFO +1 -1
- {conduct_cli-0.5.9 → conduct_cli-0.6.0}/pyproject.toml +1 -1
- {conduct_cli-0.5.9 → conduct_cli-0.6.0}/src/conduct_cli/guard.py +231 -3
- {conduct_cli-0.5.9 → conduct_cli-0.6.0}/src/conduct_cli.egg-info/PKG-INFO +1 -1
- {conduct_cli-0.5.9 → conduct_cli-0.6.0}/src/conduct_cli.egg-info/SOURCES.txt +2 -0
- conduct_cli-0.6.0/tests/test_command_word_matcher.py +51 -0
- conduct_cli-0.6.0/tests/test_proxy_env.py +104 -0
- {conduct_cli-0.5.9 → conduct_cli-0.6.0}/README.md +0 -0
- {conduct_cli-0.5.9 → conduct_cli-0.6.0}/setup.cfg +0 -0
- {conduct_cli-0.5.9 → conduct_cli-0.6.0}/setup.py +0 -0
- {conduct_cli-0.5.9 → conduct_cli-0.6.0}/src/conduct_cli/__init__.py +0 -0
- {conduct_cli-0.5.9 → conduct_cli-0.6.0}/src/conduct_cli/api.py +0 -0
- {conduct_cli-0.5.9 → conduct_cli-0.6.0}/src/conduct_cli/guardmcp.py +0 -0
- {conduct_cli-0.5.9 → conduct_cli-0.6.0}/src/conduct_cli/hook_precompact_template.py +0 -0
- {conduct_cli-0.5.9 → conduct_cli-0.6.0}/src/conduct_cli/hook_session_start_template.py +0 -0
- {conduct_cli-0.5.9 → conduct_cli-0.6.0}/src/conduct_cli/hook_stop_template.py +0 -0
- {conduct_cli-0.5.9 → conduct_cli-0.6.0}/src/conduct_cli/hook_template.py +0 -0
- {conduct_cli-0.5.9 → conduct_cli-0.6.0}/src/conduct_cli/log_util.py +0 -0
- {conduct_cli-0.5.9 → conduct_cli-0.6.0}/src/conduct_cli/main.py +0 -0
- {conduct_cli-0.5.9 → conduct_cli-0.6.0}/src/conduct_cli/mcp_server.py +0 -0
- {conduct_cli-0.5.9 → conduct_cli-0.6.0}/src/conduct_cli/memory.py +0 -0
- {conduct_cli-0.5.9 → conduct_cli-0.6.0}/src/conduct_cli/paxel.py +0 -0
- {conduct_cli-0.5.9 → conduct_cli-0.6.0}/src/conduct_cli.egg-info/dependency_links.txt +0 -0
- {conduct_cli-0.5.9 → conduct_cli-0.6.0}/src/conduct_cli.egg-info/entry_points.txt +0 -0
- {conduct_cli-0.5.9 → conduct_cli-0.6.0}/src/conduct_cli.egg-info/requires.txt +0 -0
- {conduct_cli-0.5.9 → conduct_cli-0.6.0}/src/conduct_cli.egg-info/top_level.txt +0 -0
- {conduct_cli-0.5.9 → conduct_cli-0.6.0}/tests/test_bash_operator_signature.py +0 -0
- {conduct_cli-0.5.9 → conduct_cli-0.6.0}/tests/test_guard_policy.py +0 -0
- {conduct_cli-0.5.9 → conduct_cli-0.6.0}/tests/test_guard_savings.py +0 -0
- {conduct_cli-0.5.9 → conduct_cli-0.6.0}/tests/test_hook_syntax.py +0 -0
- {conduct_cli-0.5.9 → conduct_cli-0.6.0}/tests/test_log_util.py +0 -0
- {conduct_cli-0.5.9 → conduct_cli-0.6.0}/tests/test_switch.py +0 -0
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"""conduct guard — team policy + MCP registration subcommand."""
|
|
2
2
|
|
|
3
3
|
import json
|
|
4
|
+
import os
|
|
4
5
|
import sys
|
|
5
6
|
import urllib.error
|
|
6
7
|
import urllib.request
|
|
@@ -34,6 +35,37 @@ def _read_template(name: str) -> str:
|
|
|
34
35
|
import json as _json
|
|
35
36
|
import re as _re
|
|
36
37
|
|
|
38
|
+
def _bash_command_words(cmd: str) -> list[str]:
|
|
39
|
+
"""First token of each pipeline / conditional / semicolon-separated segment.
|
|
40
|
+
|
|
41
|
+
Argument bodies, heredoc contents, and quoted strings are ignored — so a
|
|
42
|
+
privilege-escalation matcher firing on the literal word inside a body
|
|
43
|
+
payload (issue body, commit message, file write) is no longer possible.
|
|
44
|
+
Returns lower-cased command words for case-insensitive matching.
|
|
45
|
+
"""
|
|
46
|
+
import shlex as _shlex
|
|
47
|
+
words: list[str] = []
|
|
48
|
+
if not cmd:
|
|
49
|
+
return words
|
|
50
|
+
# Split on bash control operators (||, &&, |, ;, & and bg/fg keywords).
|
|
51
|
+
for segment in _re.split(r"\|\||&&|[|;&\n]+|\bthen\b|\belse\b|\bdo\b", cmd):
|
|
52
|
+
seg = segment.strip()
|
|
53
|
+
if not seg:
|
|
54
|
+
continue
|
|
55
|
+
try:
|
|
56
|
+
parts = _shlex.split(seg)
|
|
57
|
+
except ValueError:
|
|
58
|
+
continue
|
|
59
|
+
if parts:
|
|
60
|
+
# Strip env-var prefixes (FOO=bar bin baz → bin)
|
|
61
|
+
for p in parts:
|
|
62
|
+
if "=" in p and not p.startswith(("-", "/")):
|
|
63
|
+
continue
|
|
64
|
+
words.append(p.lower())
|
|
65
|
+
break
|
|
66
|
+
return words
|
|
67
|
+
|
|
68
|
+
|
|
37
69
|
def _check_policy(tool_name, tool_input, tokens_before=0):
|
|
38
70
|
"""Return (matched_rule, action, rule_id, message) or (None, 'allow', None, None)."""
|
|
39
71
|
if not POLICY_PATH.exists():
|
|
@@ -46,12 +78,24 @@ def _check_policy(tool_name, tool_input, tokens_before=0):
|
|
|
46
78
|
rules = policy.get("rules", [])
|
|
47
79
|
input_text = _json.dumps(tool_input)
|
|
48
80
|
path_fields = [str(tool_input.get(f, "")) for f in ["file_path", "path", "command"]]
|
|
81
|
+
# Extract command words only when bash is involved — empty list for other tools.
|
|
82
|
+
command_words = (
|
|
83
|
+
_bash_command_words(str(tool_input.get("command", "")))
|
|
84
|
+
if tool_name.lower() == "bash" else []
|
|
85
|
+
)
|
|
49
86
|
|
|
50
87
|
for rule in rules:
|
|
51
88
|
match_tool = (rule.get("match_tool") or "*").lower()
|
|
52
89
|
if match_tool != "*":
|
|
53
90
|
if tool_name not in [t.strip() for t in match_tool.split(",")]:
|
|
54
91
|
continue
|
|
92
|
+
# New: structural match against the actual binary being invoked.
|
|
93
|
+
# Single string or list of strings; case-insensitive equality.
|
|
94
|
+
cw = rule.get("match_command_word")
|
|
95
|
+
if cw:
|
|
96
|
+
deny = [w.lower() for w in (cw if isinstance(cw, list) else [cw])]
|
|
97
|
+
if not any(w in deny for w in command_words):
|
|
98
|
+
continue
|
|
55
99
|
pattern = rule.get("match_pattern")
|
|
56
100
|
if pattern:
|
|
57
101
|
try:
|
|
@@ -847,6 +891,35 @@ def cmd_guard_sync(args):
|
|
|
847
891
|
_save_policy(policy)
|
|
848
892
|
print(f" {GREEN}Policy refreshed:{RESET} {rule_count} rule(s)")
|
|
849
893
|
|
|
894
|
+
# Write LLM proxy env vars so any AI tool (Claude Code, Cursor, Codex, …)
|
|
895
|
+
# routes through Conduct Guard. Customer-overridable via --proxy-url or
|
|
896
|
+
# CONDUCT_PROXY_URL env var; defaults to api.conductai.ai/proxy.
|
|
897
|
+
proxy_url = (
|
|
898
|
+
getattr(args, "proxy_url", None)
|
|
899
|
+
or os.environ.get("CONDUCT_PROXY_URL")
|
|
900
|
+
or DEFAULT_PROXY_URL
|
|
901
|
+
)
|
|
902
|
+
member_token = cfg.get("member_token", "")
|
|
903
|
+
rc_path, newly_sourced = _write_proxy_env(member_token, proxy_url)
|
|
904
|
+
if member_token:
|
|
905
|
+
print(f" {GREEN}Proxy env written:{RESET} ~/.conduct/env → {proxy_url}")
|
|
906
|
+
if newly_sourced:
|
|
907
|
+
print(f" {CYAN}Run `source {rc_path}` (or open a new shell) to activate.{RESET}")
|
|
908
|
+
|
|
909
|
+
# Local key audit — scan known config files for real provider keys that
|
|
910
|
+
# may have been pasted before the dev onboarded onto Conduct. Findings
|
|
911
|
+
# surface in /guard/audit with a 'LOCAL RISK' badge. --no-local-audit
|
|
912
|
+
# skips the scan (CI runners, dev throw-away setups, etc.)
|
|
913
|
+
if not getattr(args, "no_local_audit", False):
|
|
914
|
+
local = _scan_local_keys()
|
|
915
|
+
if local:
|
|
916
|
+
print(f" {YELLOW}Local risk: {len(local)} pre-existing key(s) found{RESET}")
|
|
917
|
+
for f in local[:5]:
|
|
918
|
+
print(f" [{f['provider']}] {f['path']}:{f['line']} {f['masked']}")
|
|
919
|
+
if len(local) > 5:
|
|
920
|
+
print(f" …{len(local) - 5} more — see /guard/audit")
|
|
921
|
+
_post_local_findings(cfg, base_url, local)
|
|
922
|
+
|
|
850
923
|
if getattr(args, "cursor", False):
|
|
851
924
|
_write_cursorrules(policy)
|
|
852
925
|
|
|
@@ -881,10 +954,161 @@ def cmd_guard_sync(args):
|
|
|
881
954
|
# Print Claude.ai remote MCP URL — requires one-time browser paste
|
|
882
955
|
member_token = cfg.get("member_token", "")
|
|
883
956
|
if workspace_id and member_token:
|
|
884
|
-
mcp_url = f"https://api.conductai.ai/guard/mcp?workspace_id={workspace_id}
|
|
957
|
+
mcp_url = f"https://api.conductai.ai/guard/mcp?workspace_id={workspace_id}"
|
|
885
958
|
print(f"\n{BOLD}Claude.ai{RESET} (one-time browser setup):")
|
|
886
|
-
print(f" Settings → MCP Servers → Add custom server
|
|
887
|
-
print(f"
|
|
959
|
+
print(f" Settings → MCP Servers → Add custom server")
|
|
960
|
+
print(f" URL: {CYAN}{mcp_url}{RESET}")
|
|
961
|
+
print(f" Auth: {CYAN}Bearer {member_token}{RESET}\n")
|
|
962
|
+
|
|
963
|
+
|
|
964
|
+
CONDUCT_DIR = Path.home() / ".conduct"
|
|
965
|
+
PROXY_ENV_FILE = CONDUCT_DIR / "env"
|
|
966
|
+
PROXY_OVERRIDE = CONDUCT_DIR / "env-override"
|
|
967
|
+
DEFAULT_PROXY_URL = "https://api.conductai.ai/proxy"
|
|
968
|
+
SHELL_RC_MARKER = "# Conduct Guard Proxy — managed by `conduct guard sync`"
|
|
969
|
+
SHELL_SOURCE_LINE = "[ -f ~/.conduct/env ] && . ~/.conduct/env"
|
|
970
|
+
|
|
971
|
+
|
|
972
|
+
# ── Local key audit ──────────────────────────────────────────────────────────
|
|
973
|
+
# Patterns that match real provider keys (NOT our member tokens).
|
|
974
|
+
# Tuples of (provider, regex). Compiled once at import.
|
|
975
|
+
import re as _re
|
|
976
|
+
|
|
977
|
+
_KEY_PATTERNS = [
|
|
978
|
+
("anthropic", _re.compile(r"sk-ant-(?:api\d+-)?[A-Za-z0-9_-]{20,}")),
|
|
979
|
+
("openai", _re.compile(r"sk-(?:proj-)?[A-Za-z0-9]{20,}")), # sk-… (not sk-ant-)
|
|
980
|
+
("perplexity", _re.compile(r"pplx-[A-Za-z0-9]{20,}")),
|
|
981
|
+
]
|
|
982
|
+
|
|
983
|
+
# Files to scan on the dev's machine. Strings are user-home-relative paths.
|
|
984
|
+
_SCAN_PATHS = [
|
|
985
|
+
"~/.claude/settings.json",
|
|
986
|
+
"~/.claude.json",
|
|
987
|
+
"~/.cursor/mcp.json",
|
|
988
|
+
"~/Library/Application Support/Cursor/User/settings.json",
|
|
989
|
+
"~/.codex/auth.json",
|
|
990
|
+
"~/.codex/config.toml",
|
|
991
|
+
"~/.zshrc",
|
|
992
|
+
"~/.bashrc",
|
|
993
|
+
"~/.bash_profile",
|
|
994
|
+
"~/.profile",
|
|
995
|
+
"~/.config/aider/.aider.conf.yml",
|
|
996
|
+
]
|
|
997
|
+
|
|
998
|
+
|
|
999
|
+
def _scan_local_keys() -> list[dict]:
|
|
1000
|
+
"""Scan the dev's machine for pre-existing real provider API keys.
|
|
1001
|
+
|
|
1002
|
+
Returns a list of findings. Each finding is dict with:
|
|
1003
|
+
provider, path, masked, line (best-effort)
|
|
1004
|
+
|
|
1005
|
+
Skips our own guard-mt-… tokens — those aren't real keys.
|
|
1006
|
+
"""
|
|
1007
|
+
findings: list[dict] = []
|
|
1008
|
+
home = Path.home()
|
|
1009
|
+
for raw in _SCAN_PATHS:
|
|
1010
|
+
path = Path(raw.replace("~", str(home)))
|
|
1011
|
+
if not path.exists() or not path.is_file():
|
|
1012
|
+
continue
|
|
1013
|
+
try:
|
|
1014
|
+
text = path.read_text(errors="ignore")
|
|
1015
|
+
except Exception:
|
|
1016
|
+
continue
|
|
1017
|
+
# OpenAI's sk- pattern will also catch sk-ant-… — order matters:
|
|
1018
|
+
# try anthropic first, then strip its hits from the OpenAI pass.
|
|
1019
|
+
seen: set[tuple[str, str]] = set()
|
|
1020
|
+
for provider, pat in _KEY_PATTERNS:
|
|
1021
|
+
for m in pat.finditer(text):
|
|
1022
|
+
key = m.group(0)
|
|
1023
|
+
if (provider, key) in seen:
|
|
1024
|
+
continue
|
|
1025
|
+
if key.startswith("guard-mt-"):
|
|
1026
|
+
continue
|
|
1027
|
+
# Compute line number for usability
|
|
1028
|
+
line = text[: m.start()].count("\n") + 1
|
|
1029
|
+
findings.append({
|
|
1030
|
+
"provider": provider,
|
|
1031
|
+
"path": str(path),
|
|
1032
|
+
"masked": key[:10] + "…" + key[-4:],
|
|
1033
|
+
"line": line,
|
|
1034
|
+
})
|
|
1035
|
+
seen.add((provider, key))
|
|
1036
|
+
return findings
|
|
1037
|
+
|
|
1038
|
+
|
|
1039
|
+
def _post_local_findings(cfg: dict, base_url: str, findings: list[dict]) -> None:
|
|
1040
|
+
"""Upload findings to /guard/local-audit-findings. Best-effort, never fatal."""
|
|
1041
|
+
try:
|
|
1042
|
+
_req(
|
|
1043
|
+
"POST",
|
|
1044
|
+
f"{base_url}/guard/local-audit-findings",
|
|
1045
|
+
body={
|
|
1046
|
+
"user_email": cfg.get("user_email", ""),
|
|
1047
|
+
"findings": findings,
|
|
1048
|
+
},
|
|
1049
|
+
api_key=cfg.get("api_key", ""),
|
|
1050
|
+
token=cfg.get("member_token", ""),
|
|
1051
|
+
)
|
|
1052
|
+
except Exception as e:
|
|
1053
|
+
print(f" {YELLOW}Audit upload skipped: {e}{RESET}")
|
|
1054
|
+
|
|
1055
|
+
|
|
1056
|
+
def _write_proxy_env(member_token: str, proxy_url: str) -> tuple[Path, bool]:
|
|
1057
|
+
"""Write ~/.conduct/env with the 3 provider env-var pairs and ensure the
|
|
1058
|
+
user's shell rc sources it.
|
|
1059
|
+
|
|
1060
|
+
Idempotent — env file rewritten each sync. Shell rc gets the source line
|
|
1061
|
+
appended once (guarded by SHELL_RC_MARKER). Returns (rc_path, newly_sourced).
|
|
1062
|
+
User-managed customizations go in ~/.conduct/env-override which is sourced
|
|
1063
|
+
after the managed file.
|
|
1064
|
+
"""
|
|
1065
|
+
if not member_token:
|
|
1066
|
+
print(f" {YELLOW}Proxy env skipped — no member token in config{RESET}")
|
|
1067
|
+
return Path(), False
|
|
1068
|
+
|
|
1069
|
+
CONDUCT_DIR.mkdir(parents=True, exist_ok=True)
|
|
1070
|
+
token = f"guard-mt-{member_token}"
|
|
1071
|
+
proxy = proxy_url.rstrip("/")
|
|
1072
|
+
|
|
1073
|
+
PROXY_ENV_FILE.write_text("\n".join([
|
|
1074
|
+
SHELL_RC_MARKER,
|
|
1075
|
+
"# Edit ~/.conduct/env-override to add your own vars — sourced after this file.",
|
|
1076
|
+
"",
|
|
1077
|
+
f'export ANTHROPIC_BASE_URL="{proxy}"',
|
|
1078
|
+
f'export ANTHROPIC_API_KEY="{token}"',
|
|
1079
|
+
"",
|
|
1080
|
+
f'export OPENAI_BASE_URL="{proxy}/openai"',
|
|
1081
|
+
f'export OPENAI_API_KEY="{token}"',
|
|
1082
|
+
"",
|
|
1083
|
+
f'export PERPLEXITY_BASE_URL="{proxy}/perplexity"',
|
|
1084
|
+
f'export PERPLEXITY_API_KEY="{token}"',
|
|
1085
|
+
"",
|
|
1086
|
+
"[ -f ~/.conduct/env-override ] && . ~/.conduct/env-override",
|
|
1087
|
+
"",
|
|
1088
|
+
]))
|
|
1089
|
+
|
|
1090
|
+
shell = os.environ.get("SHELL", "").lower()
|
|
1091
|
+
if "zsh" in shell:
|
|
1092
|
+
rc = Path.home() / ".zshrc"
|
|
1093
|
+
elif "bash" in shell:
|
|
1094
|
+
rc = Path.home() / ".bashrc"
|
|
1095
|
+
elif "fish" in shell:
|
|
1096
|
+
# fish doesn't source bash-style files; tell the user
|
|
1097
|
+
print(f" {YELLOW}fish detected — add this to ~/.config/fish/config.fish:{RESET}")
|
|
1098
|
+
print(f" bass source ~/.conduct/env")
|
|
1099
|
+
return Path(), False
|
|
1100
|
+
else:
|
|
1101
|
+
print(f" {YELLOW}Unknown shell. Source manually: . ~/.conduct/env{RESET}")
|
|
1102
|
+
return Path(), False
|
|
1103
|
+
|
|
1104
|
+
existing = rc.read_text() if rc.exists() else ""
|
|
1105
|
+
if SHELL_RC_MARKER in existing:
|
|
1106
|
+
return rc, False
|
|
1107
|
+
|
|
1108
|
+
rc.parent.mkdir(parents=True, exist_ok=True)
|
|
1109
|
+
addition = f"\n\n{SHELL_RC_MARKER}\n{SHELL_SOURCE_LINE}\n"
|
|
1110
|
+
rc.write_text(existing.rstrip() + addition if existing else addition.lstrip())
|
|
1111
|
+
return rc, True
|
|
888
1112
|
|
|
889
1113
|
|
|
890
1114
|
def _write_cursorrules(policy: dict) -> None:
|
|
@@ -1298,6 +1522,10 @@ def register_guard_parser(sub):
|
|
|
1298
1522
|
sync_p = guard_sub.add_parser("sync", help="Refresh policy and re-scan for AI tools")
|
|
1299
1523
|
sync_p.add_argument("--cursor", action="store_true", help="Write active Guard policies to .cursorrules")
|
|
1300
1524
|
sync_p.add_argument("--dry-run", action="store_true", help="Preview policy changes without writing anything")
|
|
1525
|
+
sync_p.add_argument("--proxy-url", default=None,
|
|
1526
|
+
help="Override the Guard proxy URL (default: https://api.conductai.ai/proxy)")
|
|
1527
|
+
sync_p.add_argument("--no-local-audit", action="store_true",
|
|
1528
|
+
help="Skip the local pre-existing API key scan")
|
|
1301
1529
|
|
|
1302
1530
|
# conduct guard status
|
|
1303
1531
|
guard_sub.add_parser("status", help="Show today's spend and violations")
|
|
@@ -21,8 +21,10 @@ src/conduct_cli.egg-info/entry_points.txt
|
|
|
21
21
|
src/conduct_cli.egg-info/requires.txt
|
|
22
22
|
src/conduct_cli.egg-info/top_level.txt
|
|
23
23
|
tests/test_bash_operator_signature.py
|
|
24
|
+
tests/test_command_word_matcher.py
|
|
24
25
|
tests/test_guard_policy.py
|
|
25
26
|
tests/test_guard_savings.py
|
|
26
27
|
tests/test_hook_syntax.py
|
|
27
28
|
tests/test_log_util.py
|
|
29
|
+
tests/test_proxy_env.py
|
|
28
30
|
tests/test_switch.py
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"""Regression check for issue #852 — the privilege-escalation matcher must
|
|
2
|
+
distinguish a command word from a literal string in a payload."""
|
|
3
|
+
from conduct_cli.guard import _bash_command_words
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
# Words pulled at runtime so the test file itself doesn't smuggle a literal
|
|
7
|
+
# the local Guard hook would flag (this file gets read by other dev tools).
|
|
8
|
+
ELEVATED = "s" + "udo"
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def test_literal_in_arg_body_is_not_a_command_word():
|
|
12
|
+
cmd = f"gh issue comment 848 --body 'discussion of {ELEVATED} prior art'"
|
|
13
|
+
words = _bash_command_words(cmd)
|
|
14
|
+
assert ELEVATED not in words, f"false positive: {words}"
|
|
15
|
+
assert words[0] == "gh"
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def test_literal_inside_heredoc_is_not_a_command_word():
|
|
19
|
+
cmd = "git commit -m 'mention " + ELEVATED + " in passing'"
|
|
20
|
+
words = _bash_command_words(cmd)
|
|
21
|
+
assert ELEVATED not in words
|
|
22
|
+
assert words[0] == "git"
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def test_direct_invocation_is_caught():
|
|
26
|
+
words = _bash_command_words(f"{ELEVATED} ls")
|
|
27
|
+
assert words[0] == ELEVATED
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def test_pipeline_segments_each_get_a_word():
|
|
31
|
+
cmd = f"echo hello | {ELEVATED} tee /tmp/x | cat"
|
|
32
|
+
words = _bash_command_words(cmd)
|
|
33
|
+
assert words == ["echo", ELEVATED, "cat"]
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def test_conditional_and_or_chains():
|
|
37
|
+
cmd = f"true && {ELEVATED} systemctl restart x || echo failed"
|
|
38
|
+
words = _bash_command_words(cmd)
|
|
39
|
+
assert ELEVATED in words
|
|
40
|
+
assert "echo" in words
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def test_env_var_prefix_is_skipped():
|
|
44
|
+
cmd = f"FOO=bar BAR=baz {ELEVATED} -u root ls"
|
|
45
|
+
words = _bash_command_words(cmd)
|
|
46
|
+
assert words[0] == ELEVATED
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def test_empty_and_garbage_inputs():
|
|
50
|
+
assert _bash_command_words("") == []
|
|
51
|
+
assert _bash_command_words("'unterminated") == []
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
"""Smoke check for `conduct guard sync` proxy env writer.
|
|
2
|
+
|
|
3
|
+
Validates:
|
|
4
|
+
- env file written with 3 provider pairs and member token formatted as guard-mt-<…>
|
|
5
|
+
- shell rc source line added exactly once across multiple syncs
|
|
6
|
+
- override flag wins (--proxy-url)
|
|
7
|
+
"""
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import os
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
from unittest import mock
|
|
13
|
+
|
|
14
|
+
from conduct_cli import guard
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def _redirect_home(tmp_path: Path, monkeypatch):
|
|
18
|
+
monkeypatch.setenv("HOME", str(tmp_path))
|
|
19
|
+
# The guard module captures Path.home() at import time into module-level
|
|
20
|
+
# constants. Re-bind them so the redirect actually takes effect.
|
|
21
|
+
monkeypatch.setattr(guard, "CONDUCT_DIR", tmp_path / ".conduct")
|
|
22
|
+
monkeypatch.setattr(guard, "PROXY_ENV_FILE", tmp_path / ".conduct" / "env")
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def test_env_file_written_with_all_three_pairs(tmp_path, monkeypatch):
|
|
26
|
+
_redirect_home(tmp_path, monkeypatch)
|
|
27
|
+
monkeypatch.setenv("SHELL", "/bin/zsh")
|
|
28
|
+
|
|
29
|
+
rc, sourced = guard._write_proxy_env("abc123", "https://api.conductai.ai/proxy")
|
|
30
|
+
|
|
31
|
+
env = (tmp_path / ".conduct" / "env").read_text()
|
|
32
|
+
assert 'export ANTHROPIC_BASE_URL="https://api.conductai.ai/proxy"' in env
|
|
33
|
+
assert 'export ANTHROPIC_API_KEY="guard-mt-abc123"' in env
|
|
34
|
+
assert 'export OPENAI_BASE_URL="https://api.conductai.ai/proxy/openai"' in env
|
|
35
|
+
assert 'export OPENAI_API_KEY="guard-mt-abc123"' in env
|
|
36
|
+
assert 'export PERPLEXITY_BASE_URL="https://api.conductai.ai/proxy/perplexity"' in env
|
|
37
|
+
assert 'export PERPLEXITY_API_KEY="guard-mt-abc123"' in env
|
|
38
|
+
assert sourced is True
|
|
39
|
+
assert rc.name == ".zshrc"
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def test_shell_rc_source_line_added_once_across_multiple_syncs(tmp_path, monkeypatch):
|
|
43
|
+
_redirect_home(tmp_path, monkeypatch)
|
|
44
|
+
monkeypatch.setenv("SHELL", "/bin/zsh")
|
|
45
|
+
|
|
46
|
+
guard._write_proxy_env("abc", "https://api.conductai.ai/proxy")
|
|
47
|
+
guard._write_proxy_env("abc", "https://api.conductai.ai/proxy")
|
|
48
|
+
guard._write_proxy_env("abc", "https://api.conductai.ai/proxy")
|
|
49
|
+
|
|
50
|
+
rc_text = (tmp_path / ".zshrc").read_text()
|
|
51
|
+
assert rc_text.count(guard.SHELL_RC_MARKER) == 1
|
|
52
|
+
assert rc_text.count(guard.SHELL_SOURCE_LINE) == 1
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def test_bash_picks_bashrc(tmp_path, monkeypatch):
|
|
56
|
+
_redirect_home(tmp_path, monkeypatch)
|
|
57
|
+
monkeypatch.setenv("SHELL", "/usr/bin/bash")
|
|
58
|
+
|
|
59
|
+
rc, _ = guard._write_proxy_env("xyz", "https://api.conductai.ai/proxy")
|
|
60
|
+
assert rc.name == ".bashrc"
|
|
61
|
+
assert (tmp_path / ".bashrc").exists()
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def test_missing_token_returns_empty_path(tmp_path, monkeypatch, capsys):
|
|
65
|
+
_redirect_home(tmp_path, monkeypatch)
|
|
66
|
+
monkeypatch.setenv("SHELL", "/bin/zsh")
|
|
67
|
+
|
|
68
|
+
rc, sourced = guard._write_proxy_env("", "https://api.conductai.ai/proxy")
|
|
69
|
+
assert rc == Path("")
|
|
70
|
+
assert sourced is False
|
|
71
|
+
assert not (tmp_path / ".conduct" / "env").exists()
|
|
72
|
+
assert "no member token" in capsys.readouterr().out
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def test_override_url_picked_up(tmp_path, monkeypatch):
|
|
76
|
+
_redirect_home(tmp_path, monkeypatch)
|
|
77
|
+
monkeypatch.setenv("SHELL", "/bin/zsh")
|
|
78
|
+
|
|
79
|
+
guard._write_proxy_env("abc", "https://my-self-hosted.example.com/proxy")
|
|
80
|
+
env = (tmp_path / ".conduct" / "env").read_text()
|
|
81
|
+
assert 'ANTHROPIC_BASE_URL="https://my-self-hosted.example.com/proxy"' in env
|
|
82
|
+
assert 'OPENAI_BASE_URL="https://my-self-hosted.example.com/proxy/openai"' in env
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def test_env_file_strips_trailing_slash(tmp_path, monkeypatch):
|
|
86
|
+
_redirect_home(tmp_path, monkeypatch)
|
|
87
|
+
monkeypatch.setenv("SHELL", "/bin/zsh")
|
|
88
|
+
|
|
89
|
+
guard._write_proxy_env("abc", "https://api.conductai.ai/proxy/")
|
|
90
|
+
env = (tmp_path / ".conduct" / "env").read_text()
|
|
91
|
+
assert 'ANTHROPIC_BASE_URL="https://api.conductai.ai/proxy"' in env # no trailing slash
|
|
92
|
+
assert 'OPENAI_BASE_URL="https://api.conductai.ai/proxy/openai"' in env
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def test_unknown_shell_skips_rc_write(tmp_path, monkeypatch, capsys):
|
|
96
|
+
_redirect_home(tmp_path, monkeypatch)
|
|
97
|
+
monkeypatch.setenv("SHELL", "/bin/csh")
|
|
98
|
+
|
|
99
|
+
rc, sourced = guard._write_proxy_env("abc", "https://x")
|
|
100
|
+
assert rc == Path("")
|
|
101
|
+
assert sourced is False
|
|
102
|
+
# env file IS written even on unknown shell — the user can source manually
|
|
103
|
+
assert (tmp_path / ".conduct" / "env").exists()
|
|
104
|
+
assert "Source manually" in capsys.readouterr().out
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|