Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
policy
Manage
Activity
Members
Labels
Plan
Issues
4
Issue boards
Milestones
Wiki
Code
Merge requests
2
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Gaia-X
Trust Services API
policy
Commits
a6f6993a
Commit
a6f6993a
authored
2 years ago
by
Yordan Kinkov
Browse files
Options
Downloads
Patches
Plain Diff
pass static data for policy evaluation
parent
429b1c57
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!19
Use policy data field during execution
Pipeline
#51566
passed with stage
Stage:
in 50 seconds
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
README.md
+24
-9
24 additions, 9 deletions
README.md
internal/service/policy/service.go
+23
-3
23 additions, 3 deletions
internal/service/policy/service.go
internal/storage/storage.go
+1
-0
1 addition, 0 deletions
internal/storage/storage.go
with
48 additions
and
12 deletions
README.md
+
24
−
9
View file @
a6f6993a
...
...
@@ -95,9 +95,9 @@ code files in an external Git server.
flowchart LR
A[Policy\nDeveloper] --git push/merge--> B[Git branch]
subgraph Git Server
B --> C[example
_
1.0.rego]
B --> D[example
_
2.0.rego]
B --> G[example
_
3.0.rego]
B --> C[example
/
1.0
/policy
.rego]
B --> D[example
/
2.0
/policy
.rego]
B --> G[example
/
3.0
/policy
.rego]
end
C --> E[Sync]
D --> E[Sync]
...
...
@@ -117,9 +117,11 @@ for detailed overview of Rego and OPA capabilities.
**
Some conventions
*must*
be followed when writing policies.
**
1.
The filename of the policy
*must*
follow rules for the naming and directory structure:
the
`group`
is a directory inside the Git repo, while the policy name and version are used
for naming the file. For example:
`/gaiax/example_1.0.rego`
.
2.
The policy package name inside the policy source code file
*must*
exactly match
the
`group`
,
`policy name`
and
`version`
are directories inside the Git repo and policy file
*must*
be named
`policy.rego`
. For example:
`/gaiax/example/1.0/policy.rego`
.
2.
In the same directory there could be a data file containing static json, which is later passed to the
policy evaluation runtime. The file
*must*
be named
`data.json`
. Example:
`/gaiax/example/1.0/data.json`
3.
The policy package name inside the policy source code file
*must*
exactly match
the
`group`
and
`policy`
(name) of the policy.
*What does all this mean?*
...
...
@@ -133,19 +135,32 @@ allow {
}
```
Next, the filename must be
`/gaiax/example
_
1.0.rego`
. When such file is synchronized
Next, the filename must be
`/gaiax/example
/
1.0
/policy
.rego`
. When such file is synchronized
with the policy service (storage), the naming convention allows the service to understand
which part is the policy group, which part is policy name and which part is version.
If we create the above policy and store it in the Git repo as
`/gaiax/example
_
1.0.rego`
,
If we create the above policy and store it in the Git repo as
`/gaiax/example
/
1.0
/policy
.rego`
,
after the Git server is synchronized with the Policy Storage, the policy service will
automatically expose URLs for working with the policy at:
```
http://localhost:8081/policy/gaiax/example/1.0/evaluation
http://localhost:8081/policy/gaiax/example/1.0/lock
```
The 2nd rule for static data file naming is to make sure that file
`/gaiax/example/1.0/data.json`
is passed and is available to the evaluation runtime when a policy is evaluated at URL:
```
http://localhost:8081/policy/gaiax/example/1.0/evaluation
```
Static data is accessed within the Rego policy with
`data.someKey`
.
Example: If the
`/gaiax/example/1.0/data.json`
file is:
```
json
{
"name"
:
"some name"
}
```
one could access the data using
`data.name`
within the Rego source code.
The
2n
d rule for package naming is needed so that a generic evaluation function
The
3r
d rule for package naming is needed so that a generic evaluation function
can be mapped and used for evaluating all kinds of different policies. Without a
package naming rule, there's no way the service can automatically generate HTTP
endpoints for working with arbitrary dynamically uploaded policies.
...
...
This diff is collapsed.
Click to expand it.
internal/service/policy/service.go
+
23
−
3
View file @
a6f6993a
...
...
@@ -7,6 +7,7 @@ import (
"github.com/google/uuid"
"github.com/open-policy-agent/opa/rego"
"github.com/open-policy-agent/opa/storage/inmem"
"go.uber.org/zap"
"code.vereign.com/gaiax/tsa/golib/errors"
...
...
@@ -204,8 +205,14 @@ func (s *Service) prepareQuery(ctx context.Context, group, policyName, version s
// and the group and policy name.
regoQuery
:=
fmt
.
Sprintf
(
"data.%s.%s"
,
group
,
policyName
)
// regoArgs contains all rego functions passed to evaluation runtime
regoArgs
,
err
:=
s
.
buildRegoArgs
(
pol
.
Filename
,
pol
.
Rego
,
regoQuery
,
pol
.
Data
)
if
err
!=
nil
{
return
nil
,
errors
.
New
(
"error building rego runtime functions"
,
err
)
}
newQuery
,
err
:=
rego
.
New
(
buildR
egoArgs
(
pol
.
Filename
,
pol
.
Rego
,
regoQuery
)
...
,
r
egoArgs
...
,
)
.
PrepareForEval
(
ctx
)
if
err
!=
nil
{
return
nil
,
errors
.
New
(
"error preparing rego query"
,
err
)
...
...
@@ -216,7 +223,7 @@ func (s *Service) prepareQuery(ctx context.Context, group, policyName, version s
return
&
newQuery
,
nil
}
func
buildRegoArgs
(
filename
,
regoPolicy
,
regoQuery
string
)
(
availableFuncs
[]
func
(
*
rego
.
Rego
))
{
func
(
s
*
Service
)
buildRegoArgs
(
filename
,
regoPolicy
,
regoQuery
,
regoData
string
)
(
availableFuncs
[]
func
(
*
rego
.
Rego
)
,
err
error
)
{
availableFuncs
=
make
([]
func
(
*
rego
.
Rego
),
2
)
availableFuncs
[
0
]
=
rego
.
Module
(
filename
,
regoPolicy
)
availableFuncs
[
1
]
=
rego
.
Query
(
regoQuery
)
...
...
@@ -224,7 +231,20 @@ func buildRegoArgs(filename, regoPolicy, regoQuery string) (availableFuncs []fun
for
i
:=
range
extensions
{
availableFuncs
=
append
(
availableFuncs
,
extensions
[
i
])
}
return
// add static data to evaluation runtime
if
regoData
!=
""
{
var
data
map
[
string
]
interface
{}
err
:=
json
.
Unmarshal
([]
byte
(
regoData
),
&
data
)
if
err
!=
nil
{
return
nil
,
err
}
store
:=
inmem
.
NewFromObject
(
data
)
availableFuncs
=
append
(
availableFuncs
,
rego
.
Store
(
store
))
}
return
availableFuncs
,
nil
}
func
(
s
*
Service
)
queryCacheKey
(
group
,
policyName
,
version
string
)
string
{
...
...
This diff is collapsed.
Click to expand it.
internal/storage/storage.go
+
1
−
0
View file @
a6f6993a
...
...
@@ -22,6 +22,7 @@ type Policy struct {
Group
string
Version
string
Rego
string
Data
string
Locked
bool
LastUpdate
time
.
Time
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment