Skip to content

Commit

Permalink
Chmod 600 keys.json on creation, refs #351
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Jan 26, 2024
1 parent 1a4853d commit 9119b03
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
1 change: 1 addition & 0 deletions llm/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,7 @@ def keys_set(name, value):
path.parent.mkdir(parents=True, exist_ok=True)
if not path.exists():
path.write_text(json.dumps(default))
path.chmod(0o600)
try:
current = json.loads(path.read_text())
except json.decoder.JSONDecodeError:
Expand Down
11 changes: 8 additions & 3 deletions tests/test_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,17 @@ def test_keys_in_user_path(monkeypatch, env, user_path):


def test_keys_set(monkeypatch, tmpdir):
user_path = str(tmpdir / "user/keys")
monkeypatch.setenv("LLM_USER_PATH", user_path)
user_path = tmpdir / "user/keys"
monkeypatch.setenv("LLM_USER_PATH", str(user_path))
keys_path = user_path / "keys.json"
assert not keys_path.exists()
runner = CliRunner()
result = runner.invoke(cli, ["keys", "set", "openai"], input="foo")
assert result.exit_code == 0
content = open(user_path + "/keys.json").read()
assert keys_path.exists()
# Should be chmod 600
assert oct(keys_path.stat().mode)[-3:] == "600"
content = keys_path.read_text("utf-8")
assert json.loads(content) == {
"// Note": "This file stores secret API credentials. Do not share!",
"openai": "foo",
Expand Down

0 comments on commit 9119b03

Please sign in to comment.