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
f837f2b7
Commit
f837f2b7
authored
2 years ago
by
Yordan Kinkov
Browse files
Options
Downloads
Patches
Plain Diff
Synchronize JSON snapshot configurations
parent
8d493f82
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
cmd/sync/main.go
+45
-19
45 additions, 19 deletions
cmd/sync/main.go
with
45 additions
and
19 deletions
cmd/sync/main.go
+
45
−
19
View file @
f837f2b7
...
...
@@ -22,11 +22,12 @@ import (
)
const
(
repoFolder
=
"policies"
policyFilename
=
"policy.rego"
dataFilename
=
"data.json"
policyDatabase
=
"policy"
policyCollection
=
"policies"
repoFolder
=
"policies"
policyFilename
=
"policy.rego"
dataFilename
=
"data.json"
dataConfigFilename
=
"data-config.json"
policyDatabase
=
"policy"
policyCollection
=
"policies"
)
type
Policy
struct
{
...
...
@@ -37,6 +38,7 @@ type Policy struct {
Rego
string
Locked
bool
Data
interface
{}
DataConfig
interface
{}
LastUpdate
time
.
Time
}
...
...
@@ -170,6 +172,9 @@ func createPolicy(p string, d os.DirEntry) (*Policy, error) {
}
regoSrc
:=
string
(
bytes
)
// generate policy filename for DB from pattern {group}/{name}/{version}/policy.rego
dbFilename
:=
group
+
"/"
+
name
+
"/"
+
version
+
"/"
+
policyFilename
// check if there is a data.json file in the same folder as the policy
dataBytes
,
err
:=
os
.
ReadFile
(
strings
.
TrimSuffix
(
p
,
policyFilename
)
+
dataFilename
)
if
err
!=
nil
&&
!
strings
.
Contains
(
err
.
Error
(),
"no such file or directory"
)
{
...
...
@@ -177,17 +182,27 @@ func createPolicy(p string, d os.DirEntry) (*Policy, error) {
}
data
:=
string
(
dataBytes
)
// generate filename for DB from pattern {group}/{name}/{version}/policy.rego
dbFilename
:=
group
+
"/"
+
name
+
"/"
+
version
+
"/"
+
policyFilename
// check if there is a data-config.json file in the same folder as the policy
configBytes
,
err
:=
os
.
ReadFile
(
strings
.
TrimSuffix
(
p
,
policyFilename
)
+
dataConfigFilename
)
if
err
!=
nil
&&
!
strings
.
Contains
(
err
.
Error
(),
"no such file or directory"
)
{
return
nil
,
err
}
dataConfig
:=
string
(
configBytes
)
// if both data.json and data-config.json files exist, log a warning message
if
len
(
dataBytes
)
>
0
&&
len
(
configBytes
)
>
0
{
fmt
.
Printf
(
"Policy data will be overwritten by a data configuration execution for policy '%s', group '%s' and version '%s'
\n
"
,
name
,
group
,
version
)
}
return
&
Policy
{
Filename
:
dbFilename
,
Name
:
name
,
Group
:
group
,
Version
:
version
,
Rego
:
regoSrc
,
Data
:
data
,
Locked
:
false
,
Filename
:
dbFilename
,
Name
:
name
,
Group
:
group
,
Version
:
version
,
Rego
:
regoSrc
,
Data
:
data
,
DataConfig
:
dataConfig
,
Locked
:
false
,
},
nil
}
...
...
@@ -275,11 +290,13 @@ func upsert(ctx context.Context, policies []*Policy, db *mongo.Collection) error
})
op
.
SetUpdate
(
bson
.
M
{
"$set"
:
bson
.
M
{
"filename"
:
policy
.
Filename
,
"locked"
:
policy
.
Locked
,
"rego"
:
policy
.
Rego
,
"data"
:
policy
.
Data
,
"lastUpdate"
:
time
.
Now
(),
"filename"
:
policy
.
Filename
,
"locked"
:
policy
.
Locked
,
"rego"
:
policy
.
Rego
,
"data"
:
policy
.
Data
,
"dataConfig"
:
policy
.
DataConfig
,
"lastUpdate"
:
time
.
Now
(),
"nextConfigExecution"
:
nextConfigExecution
(
policy
),
},
})
op
.
SetUpsert
(
true
)
...
...
@@ -294,9 +311,18 @@ func upsert(ctx context.Context, policies []*Policy, db *mongo.Collection) error
return
nil
}
func
nextConfigExecution
(
p
*
Policy
)
time
.
Time
{
if
p
.
DataConfig
!=
""
{
return
time
.
Now
()
}
return
time
.
Time
{}
}
func
(
p1
*
Policy
)
equals
(
p2
*
Policy
)
bool
{
if
p1
.
Rego
==
p2
.
Rego
&&
p1
.
Data
==
p2
.
Data
&&
p1
.
DataConfig
==
p2
.
DataConfig
&&
p1
.
Name
==
p2
.
Name
&&
p1
.
Version
==
p2
.
Version
&&
p1
.
Filename
==
p2
.
Filename
&&
...
...
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