regula-wasi 3.2.4 → 3.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -0
- package/index.d.ts +25 -0
- package/index.js +51 -10
- package/package.json +1 -1
- package/regula.wasm +0 -0
package/README.md
CHANGED
|
@@ -89,6 +89,16 @@ if (result.summary.rule_results.FAIL > 0) {
|
|
|
89
89
|
console.error('Security violations found!');
|
|
90
90
|
process.exit(1);
|
|
91
91
|
}
|
|
92
|
+
|
|
93
|
+
// Error handling with detailed error information
|
|
94
|
+
try {
|
|
95
|
+
const result = await runRegula('./main.tf');
|
|
96
|
+
} catch (error) {
|
|
97
|
+
// RegulaError includes stdout, stderr, exitCode, and command
|
|
98
|
+
console.error('Error:', error.message);
|
|
99
|
+
console.error('Stderr:', error.stderr);
|
|
100
|
+
console.error('Exit code:', error.exitCode);
|
|
101
|
+
}
|
|
92
102
|
```
|
|
93
103
|
|
|
94
104
|
#### API Options
|
|
@@ -103,6 +113,16 @@ if (result.summary.rule_results.FAIL > 0) {
|
|
|
103
113
|
| `noIgnore` | boolean | Disable .gitignore filtering |
|
|
104
114
|
| `varFiles` | string[] | Terraform variable files (.tfvars) to use |
|
|
105
115
|
|
|
116
|
+
#### Error Handling
|
|
117
|
+
|
|
118
|
+
When an error occurs, a `RegulaError` is thrown with the following properties:
|
|
119
|
+
|
|
120
|
+
- `message` - Error description
|
|
121
|
+
- `stdout` - Standard output from regula command
|
|
122
|
+
- `stderr` - Standard error output (includes OPA errors and warnings)
|
|
123
|
+
- `exitCode` - Exit code from regula process
|
|
124
|
+
- `command` - The command that was executed
|
|
125
|
+
|
|
106
126
|
### Prebuilt Binary
|
|
107
127
|
|
|
108
128
|
Download from [Releases](https://github.com/nonfx/regula/releases) for your platform.
|
package/index.d.ts
CHANGED
|
@@ -1,3 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custom error class for Regula execution errors
|
|
3
|
+
*/
|
|
4
|
+
export class RegulaError extends Error {
|
|
5
|
+
/** Standard output from regula command */
|
|
6
|
+
stdout: string;
|
|
7
|
+
/** Standard error output from regula command */
|
|
8
|
+
stderr: string;
|
|
9
|
+
/** Exit code from regula process */
|
|
10
|
+
exitCode: number;
|
|
11
|
+
/** The command that was executed */
|
|
12
|
+
command: string;
|
|
13
|
+
|
|
14
|
+
constructor(
|
|
15
|
+
message: string,
|
|
16
|
+
details: {
|
|
17
|
+
stdout: string;
|
|
18
|
+
stderr: string;
|
|
19
|
+
exitCode: number;
|
|
20
|
+
command: string;
|
|
21
|
+
}
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
|
|
1
25
|
export interface RegulaOptions {
|
|
2
26
|
/** Input type: auto, tf, tf-plan, cfn, k8s, arm */
|
|
3
27
|
inputType?: "auto" | "tf" | "tf-plan" | "cfn" | "k8s" | "arm";
|
|
@@ -89,6 +113,7 @@ export function validate(
|
|
|
89
113
|
declare const _default: {
|
|
90
114
|
runRegula: typeof runRegula;
|
|
91
115
|
validate: typeof validate;
|
|
116
|
+
RegulaError: typeof RegulaError;
|
|
92
117
|
};
|
|
93
118
|
|
|
94
119
|
export default _default;
|
package/index.js
CHANGED
|
@@ -5,6 +5,20 @@ import { dirname, join } from "path";
|
|
|
5
5
|
const __filename = fileURLToPath(import.meta.url);
|
|
6
6
|
const __dirname = dirname(__filename);
|
|
7
7
|
|
|
8
|
+
/**
|
|
9
|
+
* Custom error class for Regula execution errors
|
|
10
|
+
*/
|
|
11
|
+
export class RegulaError extends Error {
|
|
12
|
+
constructor(message, { stdout, stderr, exitCode, command }) {
|
|
13
|
+
super(message);
|
|
14
|
+
this.name = "RegulaError";
|
|
15
|
+
this.stdout = stdout;
|
|
16
|
+
this.stderr = stderr;
|
|
17
|
+
this.exitCode = exitCode;
|
|
18
|
+
this.command = command;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
8
22
|
/**
|
|
9
23
|
* Run regula on the specified path(s) and return parsed JSON results.
|
|
10
24
|
* @param {string|string[]} paths - Path(s) to IaC files or directories
|
|
@@ -17,6 +31,7 @@ const __dirname = dirname(__filename);
|
|
|
17
31
|
* @param {boolean} options.noIgnore - Disable .gitignore filtering
|
|
18
32
|
* @param {string[]} options.varFiles - Terraform variable files to use
|
|
19
33
|
* @returns {Promise<Object>} - Parsed regula output with rule_results and summary
|
|
34
|
+
* @throws {RegulaError} - Throws RegulaError with stdout, stderr, exitCode, and command properties
|
|
20
35
|
*/
|
|
21
36
|
export async function runRegula(paths, options = {}) {
|
|
22
37
|
const pathArray = Array.isArray(paths) ? paths : [paths];
|
|
@@ -68,23 +83,48 @@ export async function runRegula(paths, options = {}) {
|
|
|
68
83
|
|
|
69
84
|
return new Promise((resolve, reject) => {
|
|
70
85
|
const cliPath = join(__dirname, "cli.js");
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
86
|
+
const command = `node ${cliPath} ${args.join(" ")}`;
|
|
87
|
+
|
|
88
|
+
const child = execFile("node", [cliPath, ...args], { maxBuffer: 50 * 1024 * 1024 }, (error, stdout, stderr) => {
|
|
89
|
+
const exitCode = error?.code || 0;
|
|
90
|
+
|
|
91
|
+
// If there was an execution error (not just a non-zero exit), reject with details
|
|
92
|
+
if (error && error.code !== 1) {
|
|
93
|
+
// Exit code 1 is expected for security violations, so we don't treat it as an error
|
|
94
|
+
reject(
|
|
95
|
+
new RegulaError(`Regula execution failed: ${error.message}`, {
|
|
96
|
+
stdout,
|
|
97
|
+
stderr,
|
|
98
|
+
exitCode,
|
|
99
|
+
command,
|
|
100
|
+
})
|
|
101
|
+
);
|
|
102
|
+
return;
|
|
75
103
|
}
|
|
76
104
|
|
|
105
|
+
// Try to parse the output
|
|
77
106
|
try {
|
|
78
107
|
const result = JSON.parse(stdout);
|
|
79
108
|
resolve(result);
|
|
80
109
|
} catch (parseError) {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
110
|
+
reject(
|
|
111
|
+
new RegulaError(`Failed to parse regula output: ${parseError.message}`, {
|
|
112
|
+
stdout,
|
|
113
|
+
stderr,
|
|
114
|
+
exitCode,
|
|
115
|
+
command,
|
|
116
|
+
})
|
|
117
|
+
);
|
|
86
118
|
}
|
|
87
119
|
});
|
|
120
|
+
|
|
121
|
+
// With no path arguments the regula CLI falls back to reading stdin. execFile
|
|
122
|
+
// always hands the child an open stdin pipe, so without an explicit EOF the
|
|
123
|
+
// WASI process blocks on read forever and this promise never settles.
|
|
124
|
+
// Note: execFile ignores the `stdio` option (it owns the pipes to build the
|
|
125
|
+
// callback's stdout/stderr), and `input` is execFileSync-only — closing the
|
|
126
|
+
// stream on the returned handle is what actually delivers EOF.
|
|
127
|
+
child.stdin.end();
|
|
88
128
|
});
|
|
89
129
|
}
|
|
90
130
|
|
|
@@ -94,9 +134,10 @@ export async function runRegula(paths, options = {}) {
|
|
|
94
134
|
* @param {string|string[]} paths - Path(s) to IaC files or directories
|
|
95
135
|
* @param {Object} options - Optional configuration
|
|
96
136
|
* @returns {Promise<Object>} - Object with rule_results and summary
|
|
137
|
+
* @throws {RegulaError} - Throws RegulaError with stdout, stderr, exitCode, and command properties
|
|
97
138
|
*/
|
|
98
139
|
export async function validate(paths, options = {}) {
|
|
99
140
|
return runRegula(paths, options);
|
|
100
141
|
}
|
|
101
142
|
|
|
102
|
-
export default { runRegula, validate };
|
|
143
|
+
export default { runRegula, validate, RegulaError };
|
package/package.json
CHANGED
package/regula.wasm
CHANGED
|
Binary file
|