Skip to content

Writing your first Policy

This guide will walk you through creating your first Sentrie policy step by step. For full syntax, validation, and how multiple tag lines are indexed, see the Policy metadata reference.

A Sentrie policy file consists of exactly one namespace and at least one policy:

  • Namespace: A container for related policies.
  • Policy: A named collection of rules.

A policy consists of (in this order; see Policy metadata and Policies):

  • Metadata (optional): title, description, version, and one or more tag string literals for humans and tooling—they do not affect evaluation.
  • Facts: Input data for the policy (before any use if you import modules).
  • Rules: Individual decision logic.
  • Exports: Rules that are exported to make them available for external evaluation.
Terminal window
mkdir my-first-policy-pack
cd my-first-policy-pack
sentrie init my-first-policy-pack
first-policy.sentrie
namespace com/example/user_management
first-policy.sentrie
namespace com/example/user_management
policy user_access {
-- policy content goes here
}
first-policy.sentrie
namespace com/example/user_management
shape User {
role: string
status: string
}
policy user_access {
-- policy content goes here
}

You can document the policy for registries, search, and teammates with metadata lines at the top of the policy body (still inside policy { ... }). Values are plain string literals only; they are not used when rules run. You can repeat tag with different keys. See Policy metadata for ordering with fact, use, and the rest of the body.

first-policy.sentrie
namespace com/example/user_management
shape User {
role: string
status: string
}
policy user_access {
-- policy content goes here
title "User access"
description "Admin and active-user access for the user management example."
version "1.0.0"
tag "domain" = "user_management"
tag "tier" = "example"
-- facts and rules go below
}
first-policy.sentrie
namespace com/example/user_management
shape User {
role: string
status: string
}
policy user_access {
title "User access"
description "Admin and active-user access for the user management example."
version "1.0.0"
tag "domain" = "user_management"
tag "tier" = "example"
fact user: User as currentUser
fact context?: Context as ctx default {"environment": "production"}
}
first-policy.sentrie
namespace com/example/user_management
shape User {
role: string
status: string
}
policy user_access {
title "User access"
description "Admin and active-user access for the user management example."
version "1.0.0"
tag "domain" = "user_management"
tag "tier" = "example"
fact user: User as currentUser
fact context?: Context as ctx default {"environment": "production"}
rule allow_admin = {
yield user.role == "admin"
}
}
first-policy.sentrie
namespace com/example/user_management
shape User {
role: string
status: string
}
policy user_access {
title "User access"
description "Admin and active-user access for the user management example."
version "1.0.0"
tag "domain" = "user_management"
tag "tier" = "example"
fact user: User as currentUser
fact context?: Context as ctx default {"environment": "production"}
rule allow_admin = {
yield user.role == "admin"
}
rule allow_user = {
yield user.role == "user" and user.status == "active"
}
}

Lets use the output of the allow_admin rule to update the allow_user rule.

first-policy.sentrie
namespace com/example/user_management
shape User {
role: string
status: string
}
policy user_access {
title "User access"
description "Admin and active-user access for the user management example."
version "1.0.0"
tag "domain" = "user_management"
tag "tier" = "example"
fact user: User as currentUser
fact context?: Context as ctx default {"environment": "production"}
rule allow_admin = {
yield user.role == "admin"
}
rule allow_user = {
yield user.role == "user" and user.status == "active"
yield allow_admin or user.role == "user" and user.status == "active"
}
}
first-policy.sentrie
namespace com/example/user_management
shape User {
role: string
status: string
}
policy user_access {
title "User access"
description "Admin and active-user access for the user management example."
version "1.0.0"
tag "domain" = "user_management"
tag "tier" = "example"
fact user: User as currentUser
fact context?: Context as ctx default {"environment": "production"}
rule allow_admin = {
yield user.role == "admin"
}
rule allow_user = {
yield allow_admin or user.role == "user" and user.status == "active"
}
export decision of allow_admin
export decision of allow_user
}

Here’s a complete policy that checks user access:

first-policy.sentrie
namespace com/example/user_management
shape User {
role: string
status: string
}
policy user_access {
title "User access"
description "Admin and active-user access for the user management example."
version "1.0.0"
tag "domain" = "user_management"
tag "tier" = "example"
fact user: User as currentUser
fact context?: Context as ctx default {"environment": "production"}
rule allow_admin = {
yield user.role == "admin"
}
rule allow_user = {
yield allow_admin or user.role == "user" and user.status == "active"
}
export decision of allow_admin
export decision of allow_user
}

Now that you’ve written your first policy, learn how to run your policy to see it in action.