mirror of
https://github.com/microsoft/regorus.git
synced 2026-08-05 02:16:11 +00:00
Updated readme. Added bundle support. (#61)
Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>
This commit is contained in:
committed by
GitHub
parent
70bf371ebf
commit
8a73b4bef9
26
examples/example.rego
Normal file
26
examples/example.rego
Normal file
@@ -0,0 +1,26 @@
|
||||
package example
|
||||
|
||||
default allow := false # unless otherwise defined, allow is false
|
||||
|
||||
allow := true { # allow is true if...
|
||||
count(violation) == 0 # there are zero violations.
|
||||
}
|
||||
|
||||
violation[server.id] { # a server is in the violation set if...
|
||||
some server
|
||||
public_server[server] # it exists in the 'public_server' set and...
|
||||
server.protocols[_] == "http" # it contains the insecure "http" protocol.
|
||||
}
|
||||
|
||||
violation[server.id] { # a server is in the violation set if...
|
||||
server := input.servers[_] # it exists in the input.servers collection and...
|
||||
server.protocols[_] == "telnet" # it contains the "telnet" protocol.
|
||||
}
|
||||
|
||||
public_server[server] { # a server exists in the public_server set if...
|
||||
some i, j
|
||||
server := input.servers[_] # it exists in the input.servers collection and...
|
||||
server.ports[_] == input.ports[i].id # it references a port in the input.ports collection and...
|
||||
input.ports[i].network == input.networks[j].id # the port references a network in the input.networks collection and...
|
||||
input.networks[j].public # the network is public.
|
||||
}
|
||||
20
examples/input.json
Normal file
20
examples/input.json
Normal file
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"servers": [
|
||||
{"id": "app", "protocols": ["https", "ssh"], "ports": ["p1", "p2", "p3"]},
|
||||
{"id": "db", "protocols": ["mysql"], "ports": ["p3"]},
|
||||
{"id": "cache", "protocols": ["memcache"], "ports": ["p3"]},
|
||||
{"id": "ci", "protocols": ["http"], "ports": ["p1", "p2"]},
|
||||
{"id": "busybox", "protocols": ["telnet"], "ports": ["p1"]}
|
||||
],
|
||||
"networks": [
|
||||
{"id": "net1", "public": false},
|
||||
{"id": "net2", "public": false},
|
||||
{"id": "net3", "public": true},
|
||||
{"id": "net4", "public": true}
|
||||
],
|
||||
"ports": [
|
||||
{"id": "p1", "network": "net1"},
|
||||
{"id": "p2", "network": "net3"},
|
||||
{"id": "p3", "network": "net2"}
|
||||
]
|
||||
}
|
||||
@@ -5,6 +5,7 @@ use anyhow::{bail, Result};
|
||||
use clap::{Parser, Subcommand};
|
||||
|
||||
fn rego_eval(
|
||||
bundles: &[String],
|
||||
files: &[String],
|
||||
input: Option<String>,
|
||||
query: String,
|
||||
@@ -13,11 +14,30 @@ fn rego_eval(
|
||||
// Create engine.
|
||||
let mut engine = regorus::Engine::new();
|
||||
|
||||
// Load files from given bundles.
|
||||
for dir in bundles.iter() {
|
||||
let entries =
|
||||
std::fs::read_dir(dir).or_else(|e| bail!("failed to read bundle {dir}.\n{e}"))?;
|
||||
// Loop through each entry in the bundle folder.
|
||||
for entry in entries {
|
||||
let entry = entry.or_else(|e| bail!("failed to unwrap entry. {e}"))?;
|
||||
let path = entry.path();
|
||||
|
||||
// Process only .rego files.
|
||||
match (path.is_file(), path.extension()) {
|
||||
(true, Some(ext)) if ext == "rego" => {}
|
||||
_ => continue,
|
||||
}
|
||||
|
||||
engine.add_policy_from_file(entry.path())?;
|
||||
}
|
||||
}
|
||||
|
||||
// Load given files.
|
||||
for file in files.iter() {
|
||||
if file.ends_with(".rego") {
|
||||
// Read policy file.
|
||||
engine.add_policy_from_file(file.to_string())?;
|
||||
engine.add_policy_from_file(file)?;
|
||||
} else {
|
||||
// Read data file.
|
||||
let data = if file.ends_with(".json") {
|
||||
@@ -92,6 +112,10 @@ fn rego_parse(file: String) -> Result<()> {
|
||||
enum RegorusCommand {
|
||||
/// Evaluate a Rego Query.
|
||||
Eval {
|
||||
/// Directories containing Rego files.
|
||||
#[arg(long, short, value_name = "bundle")]
|
||||
bundles: Vec<String>,
|
||||
|
||||
/// Policy or data files. Rego, json or yaml.
|
||||
#[arg(long, short, value_name = "policy.rego|data.json|data.yaml")]
|
||||
data: Vec<String>,
|
||||
@@ -137,11 +161,12 @@ fn main() -> Result<()> {
|
||||
let cli = Cli::parse();
|
||||
match cli.command {
|
||||
RegorusCommand::Eval {
|
||||
bundles,
|
||||
data,
|
||||
input,
|
||||
query,
|
||||
trace,
|
||||
} => rego_eval(&data, input, query, trace),
|
||||
} => rego_eval(&bundles, &data, input, query, trace),
|
||||
RegorusCommand::Lex { file, verbose } => rego_lex(file, verbose),
|
||||
RegorusCommand::Parse { file } => rego_parse(file),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user