|
| 1 | +""" |
| 2 | +Example: Secure OpenClaw agent with authorization and verification. |
| 3 | +
|
| 4 | +This example demonstrates how to wrap an OpenClaw CLI agent with predicate-secure |
| 5 | +to add pre-action authorization and audit logging for browser automation tasks. |
| 6 | +
|
| 7 | +Prerequisites: |
| 8 | + 1. OpenClaw CLI installed (npm install -g openclaw) |
| 9 | + 2. predicate-snapshot skill installed in OpenClaw |
| 10 | + 3. Policy file (policies/openclaw_browser.yaml) |
| 11 | +""" |
| 12 | + |
| 13 | +from pathlib import Path |
| 14 | + |
| 15 | +from predicate_secure import SecureAgent |
| 16 | +from predicate_secure.openclaw_adapter import OpenClawConfig |
| 17 | + |
| 18 | +# Example policy file content (create this as policies/openclaw_browser.yaml): |
| 19 | +EXAMPLE_POLICY = """ |
| 20 | +rules: |
| 21 | + # Allow OpenClaw snapshot skill |
| 22 | + - action: "openclaw.skill.predicate-snapshot" |
| 23 | + resource: "*" |
| 24 | + effect: allow |
| 25 | +
|
| 26 | + # Allow clicking on known safe domains |
| 27 | + - action: "openclaw.skill.predicate-act.click" |
| 28 | + resource: "element:*" |
| 29 | + effect: allow |
| 30 | + conditions: |
| 31 | + # Only allow on safe domains (you'd check current URL in real policy) |
| 32 | + - domain_matches: ["*.example.com", "*.trusted-site.com"] |
| 33 | +
|
| 34 | + # Allow typing in form fields (with restrictions) |
| 35 | + - action: "openclaw.skill.predicate-act.type" |
| 36 | + resource: "element:*" |
| 37 | + effect: allow |
| 38 | + conditions: |
| 39 | + # Prevent entering sensitive data |
| 40 | + - not_contains: ["password", "ssn", "credit"] |
| 41 | +
|
| 42 | + # Block scrolling to prevent UI confusion |
| 43 | + - action: "openclaw.skill.predicate-act.scroll" |
| 44 | + resource: "*" |
| 45 | + effect: deny |
| 46 | +
|
| 47 | + # Default deny for safety |
| 48 | + - action: "*" |
| 49 | + resource: "*" |
| 50 | + effect: deny |
| 51 | +""" |
| 52 | + |
| 53 | + |
| 54 | +def main(): |
| 55 | + """Run OpenClaw agent with secure authorization.""" |
| 56 | + # Create OpenClaw configuration |
| 57 | + openclaw_config = OpenClawConfig( |
| 58 | + cli_path="/usr/local/bin/openclaw", # Or None to use PATH |
| 59 | + skill_proxy_port=8788, # Port for HTTP proxy |
| 60 | + skill_name="predicate-snapshot", |
| 61 | + working_dir=str(Path.home() / ".openclaw"), |
| 62 | + ) |
| 63 | + |
| 64 | + # You could also use a dict instead of OpenClawConfig: |
| 65 | + # openclaw_config = { |
| 66 | + # "openclaw_cli_path": "/usr/local/bin/openclaw", |
| 67 | + # "skill_proxy_port": 8788, |
| 68 | + # "skill_name": "predicate-snapshot", |
| 69 | + # } |
| 70 | + |
| 71 | + # Create policy file |
| 72 | + policy_dir = Path("policies") |
| 73 | + policy_dir.mkdir(exist_ok=True) |
| 74 | + policy_file = policy_dir / "openclaw_browser.yaml" |
| 75 | + |
| 76 | + if not policy_file.exists(): |
| 77 | + policy_file.write_text(EXAMPLE_POLICY) |
| 78 | + print(f"Created example policy at {policy_file}") |
| 79 | + |
| 80 | + # Wrap OpenClaw with SecureAgent |
| 81 | + secure_agent = SecureAgent( |
| 82 | + agent=openclaw_config, |
| 83 | + policy=str(policy_file), |
| 84 | + mode="strict", # Fail-closed mode |
| 85 | + principal_id="openclaw-agent-01", |
| 86 | + trace_format="console", |
| 87 | + ) |
| 88 | + |
| 89 | + print(f"[predicate-secure] Detected framework: {secure_agent.framework.value}") |
| 90 | + print(f"[predicate-secure] Mode: {secure_agent.config.mode}") |
| 91 | + print(f"[predicate-secure] Policy: {secure_agent.config.effective_policy_path}") |
| 92 | + |
| 93 | + # Example task |
| 94 | + task = "Navigate to example.com and take a snapshot" |
| 95 | + |
| 96 | + print(f"\n[OpenClaw] Running task: {task}") |
| 97 | + print("[predicate-secure] Starting HTTP proxy for skill interception...") |
| 98 | + |
| 99 | + try: |
| 100 | + # Run the OpenClaw task with authorization |
| 101 | + result = secure_agent.run(task=task) |
| 102 | + print("\n[OpenClaw] Task completed successfully") |
| 103 | + print(f"Return code: {result.get('returncode', 'N/A')}") |
| 104 | + print(f"Output: {result.get('stdout', '')[:200]}...") |
| 105 | + except Exception as e: |
| 106 | + print(f"\n[predicate-secure] Task failed: {e}") |
| 107 | + |
| 108 | + |
| 109 | +def example_with_debug_mode(): |
| 110 | + """Run OpenClaw agent in debug mode for troubleshooting.""" |
| 111 | + openclaw_config = OpenClawConfig(skill_proxy_port=8789) |
| 112 | + |
| 113 | + secure_agent = SecureAgent( |
| 114 | + agent=openclaw_config, |
| 115 | + mode="debug", # Human-readable trace output |
| 116 | + trace_format="console", |
| 117 | + trace_colors=True, |
| 118 | + ) |
| 119 | + |
| 120 | + print("\n[Debug Mode] Running OpenClaw agent with full tracing...") |
| 121 | + |
| 122 | + task = "Check if example.com loads correctly" |
| 123 | + |
| 124 | + try: |
| 125 | + result = secure_agent.run(task=task) |
| 126 | + print("\n[Debug] Task trace complete") |
| 127 | + print(f"Result: {result}") |
| 128 | + except Exception as e: |
| 129 | + print(f"\n[Debug] Error occurred: {e}") |
| 130 | + |
| 131 | + |
| 132 | +def example_with_manual_proxy(): |
| 133 | + """ |
| 134 | + Example showing how to manually control the proxy lifecycle. |
| 135 | +
|
| 136 | + Useful when you want to keep the proxy running across multiple tasks. |
| 137 | + """ |
| 138 | + from predicate_secure.openclaw_adapter import create_openclaw_adapter |
| 139 | + |
| 140 | + openclaw_config = OpenClawConfig(skill_proxy_port=8790) |
| 141 | + |
| 142 | + # Create adapter manually |
| 143 | + def authorizer(action: str, context: dict) -> bool: |
| 144 | + """Simple authorizer that allows snapshot but blocks act.""" |
| 145 | + if "snapshot" in action: |
| 146 | + return True |
| 147 | + print(f"[Authorizer] Blocked action: {action}") |
| 148 | + return False |
| 149 | + |
| 150 | + adapter = create_openclaw_adapter(openclaw_config, authorizer=authorizer) |
| 151 | + |
| 152 | + try: |
| 153 | + # Start proxy (stays running) |
| 154 | + adapter.start_proxy() |
| 155 | + print("[Proxy] Started on http://localhost:8790") |
| 156 | + |
| 157 | + # Run multiple tasks with same proxy |
| 158 | + tasks = [ |
| 159 | + "Take snapshot of example.com", |
| 160 | + "Take snapshot of httpbin.org", |
| 161 | + ] |
| 162 | + |
| 163 | + for task in tasks: |
| 164 | + print(f"\n[Task] {task}") |
| 165 | + adapter.start_cli(task) |
| 166 | + # Process would run in background |
| 167 | + # In real usage, you'd wait for completion |
| 168 | + |
| 169 | + finally: |
| 170 | + # Clean up |
| 171 | + adapter.cleanup() |
| 172 | + print("\n[Proxy] Stopped") |
| 173 | + |
| 174 | + |
| 175 | +if __name__ == "__main__": |
| 176 | + print("=" * 60) |
| 177 | + print("predicate-secure: OpenClaw Agent Example") |
| 178 | + print("=" * 60) |
| 179 | + |
| 180 | + # Uncomment the example you want to run: |
| 181 | + |
| 182 | + # Example 1: Basic usage with policy file |
| 183 | + # main() |
| 184 | + |
| 185 | + # Example 2: Debug mode with full tracing |
| 186 | + # example_with_debug_mode() |
| 187 | + |
| 188 | + # Example 3: Manual proxy control |
| 189 | + # example_with_manual_proxy() |
| 190 | + |
| 191 | + print("\nNote: Uncomment one of the example functions in __main__ to run") |
| 192 | + print("Make sure OpenClaw CLI is installed and in your PATH") |
0 commit comments