Skip to content

Writing your first Policy

This guide will walk you through creating your first Sentrie policy step by step.

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:

  • Rules: Individual decision logic.
  • Facts: Input data for the policy.
  • Exports: Rules that are exported to make them available for external evaluation.
Terminal window
mkdir my-first-policy-pack
cd example-policy-pack
sentrie init example-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
}
first-policy.sentrie
namespace com/example/user_management
shape User {
role: string
status: string
}
policy user_access {
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 {
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 {
fact user: User as currentUser
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 {
fact user: User as currentUser
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 {
fact user: User as currentUser
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 {
fact user: User as currentUser
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.