make script stderr print into relay logs
This commit is contained in:
@@ -26,21 +26,19 @@ test-docker-policy/
|
||||
6. **Verifies** that `cs-policy.js` created `/home/orly/cs-policy-output.txt`
|
||||
7. **Reports** success or failure
|
||||
|
||||
## How cs-policy.js Works
|
||||
## How cs-policy-daemon.js Works
|
||||
|
||||
The policy script writes a timestamped message to `/home/orly/cs-policy-output.txt` each time it's executed:
|
||||
The policy script is a long-lived process that:
|
||||
1. Reads events from stdin (one JSON event per line)
|
||||
2. Processes each event and returns a JSON response to stdout
|
||||
3. Logs debug information to:
|
||||
- `/home/orly/cs-policy-output.txt` (file output)
|
||||
- stderr (appears in relay log with prefix `[policy script /path]`)
|
||||
|
||||
```javascript
|
||||
#!/usr/bin/env node
|
||||
const fs = require('fs')
|
||||
const filePath = '/home/orly/cs-policy-output.txt'
|
||||
|
||||
if (fs.existsSync(filePath)) {
|
||||
fs.appendFileSync(filePath, `${Date.now()}: Hey there!\n`)
|
||||
} else {
|
||||
fs.writeFileSync(filePath, `${Date.now()}: Hey there!\n`)
|
||||
}
|
||||
```
|
||||
**Key Features:**
|
||||
- Logs event details including kind, ID, and access type (read/write)
|
||||
- Writes debug output to stderr which appears in the relay log
|
||||
- Returns JSON responses to stdout for policy decisions
|
||||
|
||||
## Quick Start
|
||||
|
||||
|
||||
@@ -12,13 +12,14 @@ const rl = readline.createInterface({
|
||||
terminal: false
|
||||
});
|
||||
|
||||
// Log that script started
|
||||
// Log that script started - to both file and stderr
|
||||
fs.appendFileSync(filePath, `${Date.now()}: Policy script started\n`);
|
||||
console.error('[cs-policy] Policy script started');
|
||||
|
||||
// Process each line of input (policy events)
|
||||
rl.on('line', (line) => {
|
||||
try {
|
||||
// Log that we received an event
|
||||
// Log that we received an event (to file)
|
||||
fs.appendFileSync(filePath, `${Date.now()}: Received event: ${line.substring(0, 100)}...\n`);
|
||||
|
||||
// Parse the policy event
|
||||
@@ -27,7 +28,11 @@ rl.on('line', (line) => {
|
||||
// Log event details including access type
|
||||
const accessType = event.access_type || 'unknown';
|
||||
const eventKind = event.kind || 'unknown';
|
||||
fs.appendFileSync(filePath, `${Date.now()}: Event ID: ${event.id || 'unknown'}, Kind: ${eventKind}, Access: ${accessType}\n`);
|
||||
const eventId = event.id || 'unknown';
|
||||
|
||||
// Log to both file and stderr (stderr appears in relay log)
|
||||
fs.appendFileSync(filePath, `${Date.now()}: Event ID: ${eventId}, Kind: ${eventKind}, Access: ${accessType}\n`);
|
||||
console.error(`[cs-policy] Processing event ${eventId.substring(0, 8)}, kind: ${eventKind}, access: ${accessType}`);
|
||||
|
||||
// Respond with "accept" to allow the event
|
||||
const response = {
|
||||
@@ -38,8 +43,9 @@ rl.on('line', (line) => {
|
||||
|
||||
console.log(JSON.stringify(response));
|
||||
} catch (err) {
|
||||
// Log errors
|
||||
// Log errors to both file and stderr
|
||||
fs.appendFileSync(filePath, `${Date.now()}: Error: ${err.message}\n`);
|
||||
console.error(`[cs-policy] Error processing event: ${err.message}`);
|
||||
|
||||
// Reject on error
|
||||
console.log(JSON.stringify({
|
||||
@@ -51,4 +57,5 @@ rl.on('line', (line) => {
|
||||
|
||||
rl.on('close', () => {
|
||||
fs.appendFileSync(filePath, `${Date.now()}: Policy script stopped\n`);
|
||||
console.error('[cs-policy] Policy script stopped');
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user