|
1 | 1 | # Audit |
| 2 | + |
| 3 | +The Audit system in Polykey provides a way to track and retrieve events that occur within the Polykey agent. This feature is essential for security monitoring, troubleshooting, and compliance purposes. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The Audit system records various events that occur during the operation of a Polykey node. These events are stored in a structured format and can be retrieved for analysis. The system is designed to be efficient and secure, with events stored in the node's database. |
| 8 | + |
| 9 | +## Core Components |
| 10 | + |
| 11 | +### Audit Class |
| 12 | + |
| 13 | +The `Audit` class is the main component of the audit system. It provides methods for: |
| 14 | + |
| 15 | +- Recording audit events |
| 16 | +- Retrieving audit events |
| 17 | +- Managing the audit event lifecycle |
| 18 | + |
| 19 | +### Audit Events |
| 20 | + |
| 21 | +Audit events have the following structure: |
| 22 | + |
| 23 | +- `id`: A unique identifier for the event (AuditEventId) |
| 24 | +- `path`: An array of strings representing the event category/path |
| 25 | +- `data`: The event data, which can contain any relevant information about the event |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +### Retrieving Audit Events |
| 30 | + |
| 31 | +Audit events can be retrieved from a node using the `nodesAuditEventsGet` RPC method. This method supports: |
| 32 | + |
| 33 | +- Pagination through `seek` and `seekEnd` parameters |
| 34 | +- Limiting the number of results with the `limit` parameter |
| 35 | +- Ordering results in ascending or descending order |
| 36 | + |
| 37 | +Example usage through the node connection: |
| 38 | + |
| 39 | +```typescript |
| 40 | +// Retrieve audit events from a connected node |
| 41 | +const response = await nodeConnection.getClient().methods.nodesAuditEventsGet({ |
| 42 | + seek: 0, // Start from the beginning or specific audit event ID |
| 43 | + seekEnd: Date.now(), // End at current time or specific audit event ID |
| 44 | + limit: 100, // Limit the number of results |
| 45 | + order: 'asc' // Order results (asc or desc) |
| 46 | +}); |
| 47 | + |
| 48 | +// Process the audit events |
| 49 | +for await (const auditEvent of response) { |
| 50 | + console.log(`Event ID: ${auditEvent.id}`); |
| 51 | + console.log(`Event Path: ${auditEvent.path.join('/')}`); |
| 52 | + console.log(`Event Data:`, auditEvent.data); |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +### Event Types |
| 57 | + |
| 58 | +The audit system can record various types of events, including but not limited to: |
| 59 | + |
| 60 | +- Node connection events (e.g., `['node', 'connection', 'forward']`) |
| 61 | +- Authentication events (e.g., `['auth', 'success']`, `['auth', 'failure']`) |
| 62 | +- Vault operations (e.g., `['vault', 'create']`, `['vault', 'delete']`) |
| 63 | +- Secret access events (e.g., `['secret', 'read']`, `['secret', 'write']`) |
| 64 | +- Permission changes (e.g., `['permission', 'grant']`, `['permission', 'revoke']`) |
| 65 | + |
| 66 | +Each event type has a specific path structure and data format. The path is an array of strings that categorizes the event, while the data contains relevant information specific to that event type. |
| 67 | + |
| 68 | +## Security Considerations |
| 69 | + |
| 70 | +Audit events are stored locally on the node and are only accessible to authorized users with appropriate permissions. When retrieving audit events from another node, proper authentication and authorization are required. |
| 71 | + |
| 72 | +The audit system is designed to be secure and tamper-resistant, ensuring that audit events cannot be modified or deleted without proper authorization. |
| 73 | + |
| 74 | +## Integration with Other Components |
| 75 | + |
| 76 | +The audit system is integrated with various components of the Polykey system: |
| 77 | + |
| 78 | +- The `PolykeyAgent` includes the audit system in its initialization |
| 79 | +- The agent service exposes audit functionality through RPC methods |
| 80 | +- Node connections can access audit events from connected nodes |
| 81 | + |
| 82 | +Example of how the audit system is integrated with the PolykeyAgent: |
| 83 | + |
| 84 | +```typescript |
| 85 | +// In PolykeyAgent.ts |
| 86 | +const agentService = agentServerManifest({ |
| 87 | + audit: this.audit, |
| 88 | + acl: this.acl, |
| 89 | + db: this.db, |
| 90 | + keyRing: this.keyRing, |
| 91 | + // ... other components |
| 92 | +}); |
| 93 | +``` |
| 94 | + |
| 95 | +## Implementation Details |
| 96 | + |
| 97 | +The audit system is implemented using the following key files: |
| 98 | + |
| 99 | +- `src/audit/Audit.ts`: The main Audit class implementation |
| 100 | +- `src/audit/types.ts`: Type definitions for audit events |
| 101 | +- `src/audit/utils.ts`: Utility functions for audit operations |
| 102 | +- `src/nodes/agent/handlers/NodesAuditEventsGet.ts`: Handler for retrieving audit events |
| 103 | +- `src/nodes/agent/callers/nodesAuditEventsGet.ts`: Caller for the audit events RPC method |
| 104 | + |
| 105 | +## Future Enhancements |
| 106 | + |
| 107 | +Future versions of the audit system may include: |
| 108 | + |
| 109 | +- Additional event types for more comprehensive auditing |
| 110 | +- Enhanced filtering capabilities based on event paths and data |
| 111 | +- Export functionality for audit logs to common formats (CSV, JSON) |
| 112 | +- Integration with external logging systems (Syslog, ELK stack) |
| 113 | +- Real-time audit event notifications |
0 commit comments