Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions limacharlie/commands/hive.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,37 @@ def validate(ctx, hive_name, key, input_file) -> None:
_output(ctx, result)


# ---------------------------------------------------------------------------
# schema
# ---------------------------------------------------------------------------

_EXPLAIN_SCHEMA = """\
Get the JSON Schema describing the record type for a given hive. The
schema documents the structure, fields, and types accepted by records
in that hive — useful for tooling, code generation, or validating
records before calling 'set'.

Not all hives expose a typed schema. Hives without a typed record
format (e.g., dr-general, dr-managed, dr-service, fp,
extension_config) return an error indicating no schema is available.

Example:
limacharlie hive schema --hive-name secret
limacharlie hive schema --hive-name lookup
"""
register_explain("hive.schema", _EXPLAIN_SCHEMA)


@group.command()
@click.option("--hive-name", required=True, help="Hive name (e.g., secret, lookup, yara).")
@pass_context
def schema(ctx, hive_name) -> None:
org = _get_org(ctx)
hive = Hive(org, hive_name)
data = hive.get_schema()
_output(ctx, data)


# ---------------------------------------------------------------------------
# rename
# ---------------------------------------------------------------------------
Expand Down
11 changes: 11 additions & 0 deletions limacharlie/sdk/hive.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,17 @@ def validate(self, record: HiveRecord) -> dict[str, Any]:
params=req,
)

def get_schema(self) -> dict[str, Any]:
"""Get the JSON Schema describing this hive's record type.

Returns:
dict: API response with a 'schema' field containing the JSON Schema.
"""
return self.client.request(
"GET",
f"hive/{urlescape(self._hive_name, safe='')}/schema",
)

def rename(self, record_name: str, new_name: str) -> dict[str, Any]:
"""Rename a record.

Expand Down
12 changes: 8 additions & 4 deletions tests/integration/test_org_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ def test_v2_org_list_accessible_orgs(oid, key):
assert "orgs" in result, f"Expected 'orgs' key, got keys: {list(result.keys())}"
except LimaCharlieError as e:
# list_accessible_orgs requires a user-scoped JWT (oid="-") which
# API keys cannot obtain.
assert "unknown api key" in str(e).lower() or "unauthorized" in str(e).lower(), (
f"Unexpected error (expected auth failure): {e}"
)
# API keys cannot obtain — the JWT service rejects with one of
# several auth-failure phrasings depending on backend version.
msg = str(e).lower()
assert (
"unknown api key" in msg
or "unauthorized" in msg
or "user not found" in msg
), f"Unexpected error (expected auth failure): {e}"
2 changes: 1 addition & 1 deletion tests/unit/test_cli_lazy_loading_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@
"help": frozenset({"cheatsheet", "discover", "topic"}),
"hive": frozenset({
"delete", "disable", "enable", "export", "get", "import",
"list", "list-types", "rename", "set", "validate",
"list", "list-types", "rename", "schema", "set", "validate",
}),
"ingestion-key": frozenset({"create", "delete", "list"}),
"installation-key": frozenset({"create", "delete", "get", "list"}),
Expand Down
19 changes: 19 additions & 0 deletions tests/unit/test_sdk_hive.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,25 @@ def test_set_url_escapes_record_name(self, hive, mock_org):
assert "rule%2Fwith%2Fslashes" in call_args[0][1]


class TestHiveGetSchema:
def test_get_schema(self, hive, mock_org):
mock_org.client.request.return_value = {
"schema": {"$ref": "#/$defs/SecretRecord"},
}
result = hive.get_schema()
assert result == {"schema": {"$ref": "#/$defs/SecretRecord"}}
mock_org.client.request.assert_called_once_with(
"GET", "hive/dr-general/schema"
)

def test_get_schema_url_escapes_hive_name(self, mock_org):
h = Hive(mock_org, "weird/name")
mock_org.client.request.return_value = {"schema": {}}
h.get_schema()
call_args = mock_org.client.request.call_args
assert call_args[0][1] == "hive/weird%2Fname/schema"


class TestHiveDelete:
def test_delete(self, hive, mock_org):
hive.delete("old-rule")
Expand Down