cs-policy-daemon.js raw
1 #!/usr/bin/env node
2
3 const fs = require('fs');
4 const readline = require('readline');
5
6 const filePath = '/home/orly/cs-policy-output.txt';
7
8 // Create readline interface to read from stdin
9 const rl = readline.createInterface({
10 input: process.stdin,
11 output: process.stdout,
12 terminal: false
13 });
14
15 // Log that script started - to both file and stderr
16 fs.appendFileSync(filePath, `${Date.now()}: Policy script started\n`);
17 console.error('[cs-policy] Policy script started');
18
19 // Process each line of input (policy events)
20 rl.on('line', (line) => {
21 try {
22 // Log that we received an event (to file)
23 fs.appendFileSync(filePath, `${Date.now()}: Received event: ${line.substring(0, 100)}...\n`);
24
25 // Parse the policy event
26 const event = JSON.parse(line);
27
28 // Log event details including access type
29 const accessType = event.access_type || 'unknown';
30 const eventKind = event.kind || 'unknown';
31 const eventId = event.id || 'unknown';
32
33 // Log to both file and stderr (stderr appears in relay log)
34 fs.appendFileSync(filePath, `${Date.now()}: Event ID: ${eventId}, Kind: ${eventKind}, Access: ${accessType}\n`);
35 console.error(`[cs-policy] Processing event ${eventId.substring(0, 8)}, kind: ${eventKind}, access: ${accessType}`);
36
37 // Respond with "accept" to allow the event
38 const response = {
39 id: event.id,
40 action: "accept",
41 msg: ""
42 };
43
44 console.log(JSON.stringify(response));
45 } catch (err) {
46 // Log errors to both file and stderr
47 fs.appendFileSync(filePath, `${Date.now()}: Error: ${err.message}\n`);
48 console.error(`[cs-policy] Error processing event: ${err.message}`);
49
50 // Reject on error
51 console.log(JSON.stringify({
52 action: "reject",
53 msg: "Policy script error"
54 }));
55 }
56 });
57
58 rl.on('close', () => {
59 fs.appendFileSync(filePath, `${Date.now()}: Policy script stopped\n`);
60 console.error('[cs-policy] Policy script stopped');
61 });
62