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
fd98622c
Commit
fd98622c
authored
2 years ago
by
Lyuben Penkovski
Browse files
Options
Downloads
Patches
Plain Diff
Unit tests for builtin functions for getting public keys
parent
3fbcd542
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!23
Create extension function to retrieve public keys from Signer service
Pipeline
#51817
passed with stages
in 1 minute and 20 seconds
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
internal/regofunc/did_resolver_test.go
+1
-0
1 addition, 0 deletions
internal/regofunc/did_resolver_test.go
internal/regofunc/pubkeys_test.go
+77
-0
77 additions, 0 deletions
internal/regofunc/pubkeys_test.go
internal/service/policy/service.go
+2
-1
2 additions, 1 deletion
internal/service/policy/service.go
with
80 additions
and
1 deletion
internal/regofunc/did_resolver_test.go
+
1
−
0
View file @
fd98622c
...
...
@@ -26,6 +26,7 @@ func TestResolveFunc(t *testing.T) {
r
:=
rego
.
New
(
rego
.
Query
(
`did.resolve("did:indy:idunion:BDrEcHc8Tb4Lb2VyQZWEDE")`
),
rego
.
Function1
(
DIDResolverFuncs
.
ResolveFunc
()),
rego
.
StrictBuiltinErrors
(
true
),
)
resultSet
,
err
:=
r
.
Eval
(
context
.
Background
())
assert
.
NoError
(
t
,
err
)
...
...
This diff is collapsed.
Click to expand it.
internal/regofunc/pubkeys_test.go
0 → 100644
+
77
−
0
View file @
fd98622c
package
regofunc_test
import
(
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/open-policy-agent/opa/rego"
"github.com/stretchr/testify/assert"
"code.vereign.com/gaiax/tsa/policy/internal/regofunc"
)
func
TestGetKeyFunc
(
t
*
testing
.
T
)
{
expected
:=
`{"key1":"key1 data"}`
signerSrv
:=
httptest
.
NewServer
(
http
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
_
,
_
=
fmt
.
Fprint
(
w
,
expected
)
}))
defer
signerSrv
.
Close
()
keysFuncs
:=
regofunc
.
NewPubkeyFuncs
(
signerSrv
.
URL
,
http
.
DefaultClient
)
r
:=
rego
.
New
(
rego
.
Query
(
`keys.get("key1")`
),
rego
.
Function1
(
keysFuncs
.
GetKeyFunc
()),
rego
.
StrictBuiltinErrors
(
true
),
)
resultSet
,
err
:=
r
.
Eval
(
context
.
Background
())
assert
.
NoError
(
t
,
err
)
resultBytes
,
err
:=
json
.
Marshal
(
resultSet
[
0
]
.
Expressions
[
0
]
.
Value
)
assert
.
NoError
(
t
,
err
)
assert
.
Equal
(
t
,
expected
,
string
(
resultBytes
))
}
func
TestGetKeyFuncError
(
t
*
testing
.
T
)
{
signerSrv
:=
httptest
.
NewServer
(
http
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
w
.
WriteHeader
(
http
.
StatusNotFound
)
}))
defer
signerSrv
.
Close
()
keysFuncs
:=
regofunc
.
NewPubkeyFuncs
(
signerSrv
.
URL
,
http
.
DefaultClient
)
r
:=
rego
.
New
(
rego
.
Query
(
`keys.get("key1")`
),
rego
.
Function1
(
keysFuncs
.
GetKeyFunc
()),
rego
.
StrictBuiltinErrors
(
true
),
)
resultSet
,
err
:=
r
.
Eval
(
context
.
Background
())
assert
.
Nil
(
t
,
resultSet
)
assert
.
Error
(
t
,
err
)
expectedError
:=
`keys.get("key1"): eval_builtin_error: keys.get: unexpected response from signer: 404 Not Found`
assert
.
Equal
(
t
,
expectedError
,
err
.
Error
())
}
func
TestGetAllKeysFunc
(
t
*
testing
.
T
)
{
expected
:=
`[{"key1":"key1 data"},{"key2":"key2 data"}]`
signerSrv
:=
httptest
.
NewServer
(
http
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
_
,
_
=
fmt
.
Fprint
(
w
,
expected
)
}))
defer
signerSrv
.
Close
()
keysFuncs
:=
regofunc
.
NewPubkeyFuncs
(
signerSrv
.
URL
,
http
.
DefaultClient
)
r
:=
rego
.
New
(
rego
.
Query
(
`keys.getAll()`
),
rego
.
FunctionDyn
(
keysFuncs
.
GetAllKeysFunc
()),
rego
.
StrictBuiltinErrors
(
true
),
)
resultSet
,
err
:=
r
.
Eval
(
context
.
Background
())
assert
.
NoError
(
t
,
err
)
resultBytes
,
err
:=
json
.
Marshal
(
resultSet
[
0
]
.
Expressions
[
0
]
.
Value
)
assert
.
NoError
(
t
,
err
)
assert
.
Equal
(
t
,
expected
,
string
(
resultBytes
))
}
This diff is collapsed.
Click to expand it.
internal/service/policy/service.go
+
2
−
1
View file @
fd98622c
...
...
@@ -224,9 +224,10 @@ func (s *Service) prepareQuery(ctx context.Context, group, policyName, version s
}
func
(
s
*
Service
)
buildRegoArgs
(
filename
,
regoPolicy
,
regoQuery
,
regoData
string
)
(
availableFuncs
[]
func
(
*
rego
.
Rego
),
err
error
)
{
availableFuncs
=
make
([]
func
(
*
rego
.
Rego
),
2
)
availableFuncs
=
make
([]
func
(
*
rego
.
Rego
),
3
)
availableFuncs
[
0
]
=
rego
.
Module
(
filename
,
regoPolicy
)
availableFuncs
[
1
]
=
rego
.
Query
(
regoQuery
)
availableFuncs
[
2
]
=
rego
.
StrictBuiltinErrors
(
true
)
extensions
:=
regofunc
.
List
()
for
i
:=
range
extensions
{
availableFuncs
=
append
(
availableFuncs
,
extensions
[
i
])
...
...
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