OCI Lexer Parser
Overview
Note this SDK is used in "OCInferno", a tool to be released within the NetSPI repo by end of March for OCI enumeration and mapping.
In the spirit of full transparency, the development of the script was done with the help of LLM coding assistants. The assistant did most of the heavy lifting. As with any open-source tool, make sure that you review the code to understand what its doing before you run it. That said, we've reviewed the code for any potential issues and welcome any changes via PR requests. See
Contributing.mdat the repo root.
OCI Lexer Parser converts human-readable OCI IAM statements and dynamic group rules into normalized JSON for analysis, testing, or transformation. It is built with built with ANTLR4 and Python. See Credits below for the original groundwork in the area by Gordon Trevorrow.
For additional information regarding the JSON schema and different options offered by the SDK, review the corresponding Github wiki:
At a Glance
| Area | Details |
|---|---|
| Statement types | ALLOW, DENY, DEFINE, ADMIT, ENDORSE |
| Verbs and permissions | manage, use, read, inspect, plus {PERMISSION} lists |
| Subjects | group, dynamic-group, service, any-user, any-group |
| Locations | tenancy, compartment name, compartment path, compartment OCID |
| Conditions | ANY / ALL clauses, nested groups, same-mode flattening |
| Dynamic group rules | ALL / ANY groups, nested structures, strict LHS paths |
| Diagnostics | raise, report, or ignore error handling |
| Output normalization | DEFINE substitutions, identity domain enrichment, spans |
Some features offered by the SDK include:
- Auto-simplification of large complex conditionals in policies or dynamic groups
- Auto-substitutaiton of values for DEFINE statements
- Data enrichment by supplying information like tenancy data
- Different reporting modes depending on your specific use case
Installation
Requires Python 3.10+.
Option A: pip
pip install oci-lexer-parser
Option B: Git clone
git clone git@github.com:NetSPI/oci-lexer-parser.git
cd oci-lexer-parser
virtualenv .venv && source .venv/bin/activate
pip install -U pip
pip install .
Import name in Python:
import oci_lexer_parser
Verify the CLI:
oci-lexer-parse --help
SDK Examples
Parse Policy Statements
Input:
Allow service faas to read keys in compartment f_compartment where request.operation='GetKeyVersion'
SDK:
from oci_lexer_parser import parse_policy_statements
text = "Allow service faas to read keys in compartment f_compartment where request.operation='GetKeyVersion'"
payload, diagnostics = parse_policy_statements(text, error_mode="report")
print(payload)
Output:
{
"schema_version": "1.0",
"statements": [
{
"kind": "allow",
"subject": {"type": "service", "values": [{"label": "faas"}]},
"actions": {"type": "verbs", "values": ["read"]},
"resources": {"type": "specific", "values": ["keys"]},
"location": {"type": "compartment_name", "values": ["f_compartment"]},
"conditions": {
"type": "group",
"mode": "all",
"items": [
{
"type": "clause",
"node": {
"lhs": "request.operation",
"op": "eq",
"rhs": {"type": "literal", "value": "GetKeyVersion"}
}
}
]
}
}
]
}
Parse Dynamic Group Matching Rules
Input:
ALL { instance.compartment.id = 'ocid1.compartment.oc1..example', resource.type = 'instance' }
SDK:
from oci_lexer_parser import parse_dynamic_group_matching_rules
payload = parse_dynamic_group_matching_rules(
"ALL { instance.compartment.id = 'ocid1.compartment.oc1..example', resource.type = 'instance' }"
)
print(payload)
Output:
{
"schema_version": "1.0",
"rules": [
{
"level": 1,
"expr": {
"type": "group",
"mode": "all",
"items": [
{
"type": "clause",
"node": {
"lhs": "instance.compartment.id",
"op": "eq",
"rhs": {"type": "ocid", "value": "ocid1.compartment.oc1..example"}
}
},
{
"type": "clause",
"node": {
"lhs": "resource.type",
"op": "eq",
"rhs": {"type": "literal", "value": "instance"}
}
}
]
}
}
]
}
CLI Examples
Parse from stdin:
echo "Allow service faas to read keys in compartment f_compartment" | oci-lexer-parse --pretty
Parse a file with diagnostics:
oci-lexer-parse --error-mode report ./policy.txt --pretty
Stream JSON Lines:
oci-lexer-parse ./policy.txt --jsonl
Dependencies
Runtime dependency:
antlr4-python3-runtime>=4.13.2,<4.14
Contributing
See Contributing.md.
Credits
- Built with ANTLR4 and the Python runtime
- Based on: Unlocking the Power of ANTLR for Oracle Cloud IAM Policy Automation
Author: WebbinRoot
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file oci_lexer_parser-0.5.0.tar.gz.
File metadata
- Download URL: oci_lexer_parser-0.5.0.tar.gz
- Upload date:
- Size: 58.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a9efdb28bc42b9f93b35cd76dae3b2fe2f297b7e1e1c792bbeb359223b7da8c0
|
|
| MD5 |
76b546d9cddbb914d818267e96a057b9
|
|
| BLAKE2b-256 |
852b5f4621e9375ca0498d8795fa211b426e5d225ac2fcd77ed23bc7eb08394a
|
Provenance
The following attestation bundles were made for oci_lexer_parser-0.5.0.tar.gz:
Publisher:
publish.yml on NetSPI/oci-lexer-parser
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
oci_lexer_parser-0.5.0.tar.gz -
Subject digest:
a9efdb28bc42b9f93b35cd76dae3b2fe2f297b7e1e1c792bbeb359223b7da8c0 - Sigstore transparency entry: 2437993085
- Sigstore integration time:
-
Permalink:
NetSPI/oci-lexer-parser@ce47d15c8d00c0451983c438bc2ea3f082f6fa44 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/NetSPI
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@ce47d15c8d00c0451983c438bc2ea3f082f6fa44 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file oci_lexer_parser-0.5.0-py3-none-any.whl.
File metadata
- Download URL: oci_lexer_parser-0.5.0-py3-none-any.whl
- Upload date:
- Size: 63.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1d2b793ec3adf597bbe98a03875ada6e9b592c7f1140ca2b26b11d1934fba998
|
|
| MD5 |
b7856b05cbad5be04ade12ea65313ec2
|
|
| BLAKE2b-256 |
2d871a9d1b7e4ed2cf5a878ab2b86830bdb48a5a8b32b34f0267a8c2a5d2b3a7
|
Provenance
The following attestation bundles were made for oci_lexer_parser-0.5.0-py3-none-any.whl:
Publisher:
publish.yml on NetSPI/oci-lexer-parser
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
oci_lexer_parser-0.5.0-py3-none-any.whl -
Subject digest:
1d2b793ec3adf597bbe98a03875ada6e9b592c7f1140ca2b26b11d1934fba998 - Sigstore transparency entry: 2437993150
- Sigstore integration time:
-
Permalink:
NetSPI/oci-lexer-parser@ce47d15c8d00c0451983c438bc2ea3f082f6fa44 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/NetSPI
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@ce47d15c8d00c0451983c438bc2ea3f082f6fa44 -
Trigger Event:
workflow_dispatch
-
Statement type: