Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use predefined function for context policies #944

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/common/src/typegraph/runtimes/deno.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright Metatype OÜ, licensed under the Mozilla Public License Version 2.0.
// SPDX-License-Identifier: MPL-2.0

use anyhow::{anyhow, Context, Result};
use indexmap::IndexMap;
use serde::{Deserialize, Serialize};
use serde_json::Value;
Expand All @@ -11,6 +12,44 @@ pub struct FunctionMatData {
pub script: String,
}

#[derive(Serialize, Deserialize, Clone, Debug, Hash, PartialEq, Eq)]
#[serde(tag = "type", content = "value")]
#[serde(rename_all = "snake_case")]
pub enum ContextCheckX {
NonNull,
Value(String),
Pattern(String),
}

#[derive(PartialEq, Eq, Hash, Serialize, Deserialize, Clone, Debug)]
#[serde(rename_all = "snake_case")]
#[serde(tag = "name", content = "param")]
pub enum PredefinedFunctionMatData {
Identity,
True,
False,
InternalPolicy,
ContextCheck { key: String, value: ContextCheckX },
}

#[derive(Serialize)]
struct PredefinedFunctionMatDataRaw {
name: String,
param: Option<Value>,
}

impl PredefinedFunctionMatData {
pub fn from_raw(name: String, param: Option<String>) -> Result<Self> {
let param = param
.map(|p| serde_json::from_str(&p))
.transpose()
.context("invalid predefined function materializer parameter")?;
let value = serde_json::to_value(&PredefinedFunctionMatDataRaw { name, param })?;
serde_json::from_value(value)
.map_err(|e| anyhow!("invalid predefined function materializer: {e:?}"))
}
}

#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(rename_all = "camelCase")]
pub struct ModuleMatData {
Expand Down
40 changes: 34 additions & 6 deletions src/typegate/src/runtimes/deno/deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,39 @@ import { getLogger } from "../../log.ts";

const logger = getLogger(import.meta);

const predefinedFuncs: Record<string, Resolver<Record<string, unknown>>> = {
identity: ({ _, ...args }) => args,
true: () => true,
false: () => false,
internal_policy: ({ _: { context } }) => context.provider === "internal",
const predefinedFuncs: Record<
string,
(param: any) => Resolver<Record<string, unknown>>
> = {
identity: () => ({ _, ...args }) => args,
true: () => () => true,
false: () => () => false,
internal_policy: () => ({ _: { context } }) =>
context.provider === "internal",
context_check: ({ key, value }) => {
let check: (value: any) => boolean;
switch (value.type) {
case "not_null":
check = (v) => v != null;
break;
case "value":
check = (v) => v === value.value;
break;
case "pattern":
check = (v) => new RegExp(value.value).test(v);
break;
default:
throw new Error("unreachable");
}
const path = key.split(".");
return ({ _: { context } }) => {
let value: any = context;
for (const segment of path) {
value = value?.[segment];
}
return check(value);
};
},
};

export class DenoRuntime extends Runtime {
Expand Down Expand Up @@ -237,7 +265,7 @@ export class DenoRuntime extends Runtime {
if (!func) {
throw new Error(`predefined function ${mat.data.name} not found`);
}
return func;
return func(mat.data.param);
}

if (mat.name === "static") {
Expand Down
Loading
Loading