Documentation
¶
Overview ¶
Package cedar provides an implementation of the Cedar language authorizer.
Example ¶
package main import ( "encoding/json" "fmt" "github.com/cedar-policy/cedar-go" ) const policyCedar = `permit ( principal == User::"alice", action == Action::"view", resource in Album::"jane_vacation" ); ` const entitiesJSON = `[ { "uid": { "type": "User", "id": "alice" }, "attrs": { "age": 18 }, "parents": [] }, { "uid": { "type": "Photo", "id": "VacationPhoto94.jpg" }, "attrs": {}, "parents": [{ "type": "Album", "id": "jane_vacation" }] } ]` func main() { var policy cedar.Policy if err := policy.UnmarshalCedar([]byte(policyCedar)); err != nil { fmt.Println("policy unmarshal error:", err) return } ps := cedar.NewPolicySet() ps.Add("policy0", &policy) var entities cedar.EntityMap if err := json.Unmarshal([]byte(entitiesJSON), &entities); err != nil { fmt.Println("entity unmarshal error:", err) return } req := cedar.Request{ Principal: cedar.NewEntityUID("User", "alice"), Action: cedar.NewEntityUID("Action", "view"), Resource: cedar.NewEntityUID("Photo", "VacationPhoto94.jpg"), Context: cedar.NewRecord(cedar.RecordMap{ "demoRequest": cedar.True, }), } ok, _ := ps.IsAuthorized(entities, req) fmt.Println(ok) }
Output: allow
Index ¶
- Constants
- func Authorize(policies PolicyIterator, entities types.EntityGetter, req Request) (Decision, Diagnostic)
- type Annotations
- type Boolean
- type Datetime
- type Decimal
- type Decision
- type Diagnostic
- type DiagnosticError
- type DiagnosticReason
- type Duration
- type Effect
- type Entity
- type EntityGetter
- type EntityMap
- type EntityType
- type EntityUID
- type EntityUIDSet
- type IPAddr
- type Long
- type Pattern
- type Policy
- func (p *Policy) AST() *ast.Policy
- func (p *Policy) Annotations() Annotations
- func (p *Policy) Effect() Effect
- func (p *Policy) MarshalCedar() []byte
- func (p *Policy) MarshalJSON() ([]byte, error)
- func (p *Policy) Position() Position
- func (p *Policy) SetFilename(fileName string)
- func (p *Policy) UnmarshalCedar(b []byte) error
- func (p *Policy) UnmarshalJSON(b []byte) error
- type PolicyID
- type PolicyIterator
- type PolicyList
- type PolicyMap
- type PolicySet
- func (p *PolicySet) Add(policyID PolicyID, policy *Policy) bool
- func (p *PolicySet) All() iter.Seq2[PolicyID, *Policy]
- func (p *PolicySet) Get(policyID PolicyID) *Policy
- func (p *PolicySet) IsAuthorized(entities types.EntityGetter, req Request) (Decision, Diagnostic)deprecated
- func (p *PolicySet) Map() PolicyMapdeprecated
- func (p *PolicySet) MarshalCedar() []byte
- func (p *PolicySet) MarshalJSON() ([]byte, error)
- func (p *PolicySet) Remove(policyID PolicyID) bool
- func (p *PolicySet) UnmarshalJSON(b []byte) error
- type Position
- type Record
- type RecordMap
- type Request
- type Set
- type String
- type Value
- type Wildcard
Examples ¶
Constants ¶
const ( Allow = types.Allow Deny = types.Deny )
const ( Permit = types.Permit Forbid = types.Forbid )
const ( True = types.True False = types.False )
Variables ¶
This section is empty.
Functions ¶
func Authorize ¶ added in v1.2.0
func Authorize(policies PolicyIterator, entities types.EntityGetter, req Request) (Decision, Diagnostic)
Authorize uses the combination of the PolicySet and Entities to determine if the given Request to determine Decision and Diagnostic.
Types ¶
type Annotations ¶
type Annotations = types.Annotations
type Datetime ¶ added in v0.4.0
func NewDatetime ¶ added in v1.0.0
NewDatetime returns a Cedar Datetime from a Go time.Time value
func NewDatetimeFromMillis ¶ added in v1.0.0
NewDatetimeFromMillis returns a Datetime from milliseconds
type Decimal ¶
func NewDecimal ¶ added in v1.0.0
NewDecimal returns a Decimal value of i * 10^exponent.
func NewDecimalFromFloat ¶ added in v1.0.0
func NewDecimalFromFloat[T constraints.Float](f T) (Decimal, error)
NewDecimalFromFloat returns a Decimal that approximates the given floating point value. The value of the Decimal is calculated by multiplying it by 10^4, truncating it to an int64 representation to cut off any digits beyond the four allowed, and passing it as an integer to NewDecimal() with -4 as the exponent.
WARNING: decimal representations of more than 6 significant digits for float32s and 15 significant digits for float64s can be lossy in terms of precision. To create a precise Decimal above those sizes, use the NewDecimal constructor.
func NewDecimalFromInt ¶ added in v1.0.0
func NewDecimalFromInt[T constraints.Signed](i T) (Decimal, error)
NewDecimalFromInt returns a Decimal with the whole integer value provided
type Diagnostic ¶
type Diagnostic = types.Diagnostic
type DiagnosticError ¶ added in v0.3.1
type DiagnosticError = types.DiagnosticError
type DiagnosticReason ¶ added in v0.3.1
type DiagnosticReason = types.DiagnosticReason
type Duration ¶ added in v0.4.0
func NewDuration ¶ added in v1.0.0
NewDuration returns a Cedar Duration from a Go time.Duration
func NewDurationFromMillis ¶ added in v1.0.0
NewDurationFromMillis returns a Duration from milliseconds
type EntityGetter ¶ added in v1.0.6
type EntityGetter = types.EntityGetter
type EntityType ¶ added in v0.4.0
type EntityType = types.EntityType
type EntityUID ¶
func NewEntityUID ¶
func NewEntityUID(typ EntityType, id String) EntityUID
NewEntityUID returns an EntityUID given an EntityType and identifier
type EntityUIDSet ¶ added in v1.0.0
type EntityUIDSet = types.EntityUIDSet
func NewEntityUIDSet ¶ added in v1.0.0
func NewEntityUIDSet(args ...EntityUID) EntityUIDSet
NewEntityUIDSet returns an immutable EntityUIDSet ready for use.
type Pattern ¶ added in v0.4.0
func NewPattern ¶ added in v0.4.0
NewPattern permits for the programmatic construction of a Pattern out of a slice of pattern components. The pattern components may be one of string, cedar.String, or cedar.Wildcard. Any other types will cause a panic.
type Policy ¶
type Policy struct {
// contains filtered or unexported fields
}
A Policy is the parsed form of a single Cedar language policy statement.
func NewPolicyFromAST ¶ added in v0.2.0
NewPolicyFromAST lets you create a new policy statement from a programmatically created AST. Do not modify the *ast.Policy after passing it into NewPolicyFromAST.
func (*Policy) AST ¶ added in v0.3.1
AST retrieves the AST of this policy. Do not modify the AST, as the compiled policy will no longer be in sync with the AST.
func (*Policy) Annotations ¶
func (p *Policy) Annotations() Annotations
Annotations retrieves the annotations associated with this policy.
func (*Policy) MarshalCedar ¶ added in v0.2.0
MarshalCedar encodes a single Policy statement in the human-readable format specified by the Cedar documentation.
func (*Policy) MarshalJSON ¶ added in v0.2.0
MarshalJSON encodes a single Policy statement in the JSON format specified by the Cedar documentation.
func (*Policy) SetFilename ¶ added in v0.2.0
SetFilename sets the filename of this policy.
func (*Policy) UnmarshalCedar ¶ added in v0.2.0
UnmarshalCedar parses and compiles a single Policy statement in the human-readable format specified by the Cedar documentation.
func (*Policy) UnmarshalJSON ¶ added in v0.2.0
UnmarshalJSON parses and compiles a single Policy statement in the JSON format specified by the Cedar documentation.
type PolicyIterator ¶ added in v1.2.1
type PolicyIterator interface { // All returns an iterator over all the policies in the set All() iter.Seq2[PolicyID, *Policy] }
PolicyIterator is an interface which abstracts an iterable set of policies.
type PolicyList ¶ added in v0.2.0
type PolicyList []*Policy
PolicyList represents a list of un-named Policy's. Cedar documents, unlike the PolicySet form, don't have a means of naming individual policies.
func NewPolicyListFromBytes ¶ added in v0.2.0
func NewPolicyListFromBytes(fileName string, document []byte) (PolicyList, error)
NewPolicyListFromBytes will create a Policies from the given text document with the given file name used in Position data. If there is an error parsing the document, it will be returned.
func (PolicyList) MarshalCedar ¶ added in v0.2.0
func (p PolicyList) MarshalCedar() []byte
MarshalCedar emits a concatenated Cedar representation of the policies.
func (*PolicyList) UnmarshalCedar ¶ added in v0.2.0
func (p *PolicyList) UnmarshalCedar(b []byte) error
UnmarshalCedar parses a concatenation of un-named Cedar policy statements. Names can be assigned to these policies when adding them to a PolicySet.
type PolicySet ¶
type PolicySet struct {
// contains filtered or unexported fields
}
PolicySet is a set of named policies against which a request can be authorized.
func NewPolicySetFromBytes ¶ added in v0.2.0
NewPolicySetFromBytes will create a PolicySet from the given text document with the given file name used in Position data. If there is an error parsing the document, it will be returned.
NewPolicySetFromBytes assigns default PolicyIDs to the policies contained in fileName in the format "policy<n>" where <n> is incremented for each new policy found in the file.
func (*PolicySet) Add ¶ added in v1.0.0
Add inserts or updates a policy with the given ID. Returns true if a policy with the given ID did not already exist in the set.
func (*PolicySet) All ¶ added in v1.2.0
All returns an iterator over the (PolicyID, *Policy) tuples in the PolicySet
func (*PolicySet) Get ¶ added in v0.2.0
Get returns the Policy with the given ID. If a policy with the given ID does not exist, nil is returned.
func (*PolicySet) IsAuthorized
deprecated
func (p *PolicySet) IsAuthorized(entities types.EntityGetter, req Request) (Decision, Diagnostic)
IsAuthorized uses the combination of the PolicySet and Entities to determine if the given Request to determine Decision and Diagnostic.
Deprecated: Use the Authorize() function instead
func (*PolicySet) MarshalCedar ¶ added in v0.2.0
MarshalCedar emits a concatenated Cedar representation of a PolicySet. The policy names are stripped, but policies are emitted in lexicographical order by ID.
func (*PolicySet) MarshalJSON ¶ added in v0.2.0
MarshalJSON encodes a PolicySet in the JSON format specified by the Cedar documentation.
func (*PolicySet) Remove ¶ added in v1.0.0
Remove removes a policy from the PolicySet. Returns true if a policy with the given ID already existed in the set.
func (*PolicySet) UnmarshalJSON ¶ added in v0.2.0
UnmarshalJSON parses and compiles a PolicySet in the JSON format specified by the Cedar documentation.
Directories
¶
Path | Synopsis |
---|---|
Package ast provides functions for programmatically constructing a Cedar policy AST.
|
Package ast provides functions for programmatically constructing a Cedar policy AST. |
schema/ast
Package ast defines the structure for a Cedar schema file.
|
Package ast defines the structure for a Cedar schema file. |
schema/parser
Code generated by re2go 4.3 on Mon Jul 7 15:42:21 2025, DO NOT EDIT.
|
Code generated by re2go 4.3 on Mon Jul 7 15:42:21 2025, DO NOT EDIT. |
Package types contains primitive, plain-old-data types including:
|
Package types contains primitive, plain-old-data types including: |
x
|
|
exp/ast
Package ast exposes the internal AST used within cedar-go.
|
Package ast exposes the internal AST used within cedar-go. |
exp/batch
Package batch allows for performant batch evaluations of Cedar policy given a set of principals, actions, resources, and/or context as variables.
|
Package batch allows for performant batch evaluations of Cedar policy given a set of principals, actions, resources, and/or context as variables. |
exp/eval
Package eval provides a simple interface for evaluating or partially evaluating a policy node in a given environment.
|
Package eval provides a simple interface for evaluating or partially evaluating a policy node in a given environment. |