Skip to content

Commit dc43901

Browse files
committed
fix: key parameter added
Signed-off-by: Leonid Kaganov <lleo@lleo.me>
1 parent fa94b15 commit dc43901

File tree

8 files changed

+26
-13
lines changed

8 files changed

+26
-13
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ DROP_DB.sh
1515
TODO.txt
1616
DOCKER.sh
1717
/lleo
18+
/client
19+
/scripts

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "hulypulse"
3-
version = "0.1.31"
3+
version = "0.1.32"
44
edition = "2024"
55

66
[dependencies]

permit.repo renamed to policy.repo

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ default permit = true
22

33
#permit if {
44
# input.command == "Get"
5+
# contains(input.key, "/typing/")
56
# input.claim.workspace == "00000000-0000-0000-0000-000000000001"
67
#}

src/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ pub struct Config {
5959

6060
pub heartbeat_timeout: u64,
6161

62-
pub permit_file: Option<String>,
62+
pub policy_file: Option<String>,
6363
}
6464

6565
pub static CONFIG: LazyLock<Config> = LazyLock::new(|| {

src/handlers_http.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ pub async fn list(
6868
let key = format!("{}/{}", &params.workspace, &params.key);
6969
trace!(key, "list request");
7070

71-
if !CONFIG.no_authorization && !test_rego_http(req, "List") {
71+
if !CONFIG.no_authorization && !test_rego_http(req, "List", &key) {
7272
return Err(actix_web::error::ErrorForbidden("forbidden"));
7373
}
7474

@@ -86,7 +86,7 @@ pub async fn get(
8686
let key = format!("{}/{}", &params.workspace, &params.key);
8787
trace!(key, "get request");
8888

89-
if !CONFIG.no_authorization && !test_rego_http(req, "Get") {
89+
if !CONFIG.no_authorization && !test_rego_http(req, "Get", &key) {
9090
return Err(actix_web::error::ErrorForbidden("forbidden"));
9191
}
9292

@@ -119,7 +119,7 @@ pub async fn put(
119119
let key = format!("{}/{}", &params.workspace, &params.key);
120120
trace!(key, "put request");
121121

122-
if !CONFIG.no_authorization && !test_rego_http(req, "Put") {
122+
if !CONFIG.no_authorization && !test_rego_http(req, "Put", &key) {
123123
return Err(actix_web::error::ErrorForbidden("forbidden"));
124124
}
125125

@@ -171,7 +171,7 @@ pub async fn delete(
171171
let key = format!("{}/{}", &params.workspace, &params.key);
172172
trace!(key, "delete request");
173173

174-
if !CONFIG.no_authorization && !test_rego_http(req, "Delete") {
174+
if !CONFIG.no_authorization && !test_rego_http(req, "Delete", &key) {
175175
return Err(actix_web::error::ErrorForbidden("forbidden"));
176176
}
177177

src/handlers_ws.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,17 @@ impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for WsSession {
217217
// let x = self.claims.unwrap().as_ref();
218218
// );
219219
if !CONFIG.no_authorization {
220+
let key = match &cmd {
221+
WsCommand::Put { key, .. }
222+
| WsCommand::Delete { key, .. }
223+
| WsCommand::Get { key, .. }
224+
| WsCommand::List { key, .. }
225+
| WsCommand::Sub { key, .. }
226+
| WsCommand::Unsub { key, .. } => key.as_str(),
227+
_ => "",
228+
};
220229
if let Some(ref claim) = self.claims {
221-
if !test_rego_claims(claim, cmd.as_ref()) {
230+
if !test_rego_claims(claim, cmd.as_ref(), &key) {
222231
ctx.text("Unauthorized: Rego policy");
223232
ctx.stop();
224233
return;

src/workspace_owner.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,33 +57,34 @@ pub fn check_workspace_core(claims_opt: Option<Claims>, key: &str) -> Result<(),
5757
Ok(())
5858
}
5959

60-
pub fn test_rego_claims(claim: &Claims, command: &str) -> bool {
60+
pub fn test_rego_claims(claim: &Claims, command: &str, key: &str) -> bool {
6161
let data = serde_json::to_value(&claim).unwrap_or_default();
6262
let mut rego = REGORUS_ENGINE.clone();
6363

6464
rego.set_input(regorus::Value::from(json!({
6565
"command": command,
6666
"claim": data,
67+
"key": key,
6768
})));
6869
let result = rego.eval_rule(String::from("data.main.permit")).unwrap();
6970

7071
result == regorus::Value::Bool(true)
7172
}
7273

73-
pub fn test_rego_http(req: HttpRequest, command: &str) -> bool {
74+
pub fn test_rego_http(req: HttpRequest, command: &str, key: &str) -> bool {
7475
let claims = req
7576
.extensions()
7677
.get::<Claims>()
7778
.expect("Missing claims")
7879
.to_owned();
79-
test_rego_claims(&claims, command)
80+
test_rego_claims(&claims, command, key)
8081
}
8182

8283
pub static POLICY_TEXT: LazyLock<String> = LazyLock::new(|| {
83-
let Some(permit_file) = CONFIG.permit_file.as_ref() else {
84+
let Some(policy_file) = CONFIG.policy_file.as_ref() else {
8485
return "package main\n\ndefault permit = true\n".to_string();
8586
};
86-
let path = Path::new(permit_file);
87+
let path = Path::new(policy_file);
8788
if !path.exists() {
8889
panic!("Policy file not found: {}", path.display());
8990
}

0 commit comments

Comments
 (0)