A system for converting natural language access control policies into structured Rego policy code for Open Policy Agent (OPA).
prose2policy is a modular pipeline that processes natural language access control policies through several stages to produce valid Rego policy code. The system uses large language models (LLMs) with few-shot prompting to achieve high accuracy in policy extraction and code generation.
The system follows a modular pipeline architecture with 5 core modules:
- Pre-processing Module: Checks if text contains access control policy statements and simplifies them
- Generator Module: Extracts policy components (subject, action, resource, etc.) from preprocessed text
- Schema Validator Module: Validates extracted components against a schema
- Rego Generator Module: Generates Rego policy code from extracted components
- Rego Validator Module: Validates generated Rego code using the Regal linter
- Few-Shot Learning: All LLM-based modules use few-shot examples for improved accuracy
- Centralized Prompt Management: All prompts are stored in a single file for easy management
- LLM Provider Abstraction: A unified interface for different LLM providers (Anthropic, OpenAI)
- Configuration System: A flexible configuration system for LLM provider selection and other settings
- Batch Processing: Support for processing multiple policies at once with file upload/download
- Web Interface: A user-friendly web interface with tabs for single policy and batch processing
- Clone the repository:
git clone https://github.com/yourusername/prose2policy.git
cd prose2policy- Install dependencies:
pip install -r requirements.txt- Set up environment variables:
export OPENAI_API_KEY=your_openai_api_key
# Or for Anthropic
export ANTHROPIC_API_KEY=your_anthropic_api_keypython -m prose2policy.appThen open your browser and navigate to http://localhost:5000.
import requests
response = requests.post('http://localhost:5000/api/process', json={
'policy': 'Administrators can modify user accounts in the system.'
})
result = response.json()
print(result['rego_generator']['rego_policy'])import requests
response = requests.post('http://localhost:5000/api/batch', json={
'policies': [
'Administrators can modify user accounts in the system.',
'Employees must not access customer financial data unless required for support purposes.',
'Customers can view their own order history.'
]
})
results = response.json()['results']
for result in results:
print(result['rego_generator']['rego_policy'])from prose2policy.modules.preprocessor import Preprocessor
from prose2policy.modules.generator import Generator
from prose2policy.modules.rego_generator import RegoGenerator
from prose2policy.utils.config_manager import ConfigManager
# Initialize configuration manager
config_manager = ConfigManager()
# Initialize modules
preprocessor = Preprocessor(config_manager)
generator = Generator(config_manager)
rego_generator = RegoGenerator(config_manager)
# Process a policy
policy_text = "Administrators can modify user accounts in the system."
processed_text = preprocessor.process(policy_text)
components = generator.extract_components(processed_text)
rego_policy = rego_generator.generate_rego(components, policy_text)
print(rego_policy)Configuration can be managed through the web interface or by editing the config.json file directly.
{
"llm_provider": "anthropic",
"openai_api_key": "your_openai_api_key",
"openai_model": "gpt-4o",
"anthropic_api_key": "your_anthropic_api_key",
"anthropic_model": "claude-opus-4-5-20251101"
}Schemas are stored in the config/schemas directory. The default schema is default_schema.json.
{
"subjects": ["user", "admin", "guest"],
"actions": ["read", "write", "delete", "create", "update"],
"resources": ["document", "file", "database", "record", "system"],
"conditions": ["time", "location", "device", "network"],
"purposes": ["business", "personal", "security", "compliance"]
}Run the test script to test the entire pipeline with a simple example:
python -m prose2policy.test_pipelineFor batch processing test:
python -m prose2policy.test_pipeline --batchThe project includes a comprehensive test suite that covers all modules and components:
# Run all tests
python -m prose2policy.tests.run_tests
# Run tests for a specific module
python -m prose2policy.tests.run_tests preprocessorAvailable test modules:
preprocessor: Tests for the Preprocessor modulegenerator: Tests for the Generator moduleschema_validator: Tests for the Schema Validator modulerego_generator: Tests for the Rego Generator modulerego_validator: Tests for the Rego Validator modulellm_providers: Tests for the LLM providersconfig_manager: Tests for the Config Managerprompt_manager: Tests for the Prompt Managerapp: Tests for the main applicationintegration: Integration tests for the entire system
You can also use pytest to run the tests:
# Install pytest if you haven't already
pip install pytest
# Run all tests
pytest prose2policy/tests/
# Run a specific test file
pytest prose2policy/tests/test_preprocessor.pyFor more information about testing, see the tests README.
prose2policy/
├── app.py # Main Flask application
├── modules/ # Core modules
│ ├── __init__.py
│ ├── preprocessor.py # Pre-processing module
│ ├── generator.py # Generator module
│ └── rego_generator.py # Rego Generator module
├── prompts/ # Prompt management
│ └── prompts.py # Centralized prompt management
├── templates/ # HTML templates
│ └── index.html # Main UI template
├── utils/ # Utility functions and classes
│ ├── config_manager.py # Configuration management
│ ├── regal_validator.py # Rego validation using Regal
│ ├── schema_validator.py # Schema validation
│ └── llm_providers/ # LLM provider implementations
│ ├── __init__.py
│ ├── base.py # Base LLM provider interface
│ ├── openai.py # OpenAI provider
│ ├── anthropic.py # Anthropic provider
│ └── mock.py # Mock provider for testing
├── tests/ # Test suite
│ ├── __init__.py
│ ├── README.md # Testing documentation
│ ├── run_tests.py # Test runner
│ ├── test_app.py # Tests for the main application
│ ├── test_config_manager.py # Tests for the Config Manager
│ ├── test_generator.py # Tests for the Generator module
│ ├── test_integration.py # Integration tests
│ ├── test_llm_providers.py # Tests for the LLM providers
│ ├── test_preprocessor.py # Tests for the Preprocessor module
│ ├── test_prompt_manager.py # Tests for the Prompt Manager
│ ├── test_rego_generator.py # Tests for the Rego Generator module
│ ├── test_rego_validator.py # Tests for the Rego Validator module
│ └── test_schema_validator.py # Tests for the Schema Validator module
├── test_pipeline.py # Simple test script
└── config/ # Configuration files
└── schemas/ # Schema files
└── default_schema.json # Default schema
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.
This project builds upon the work of the agentv_nl2rego project, which provided the initial inspiration and some reusable components.
