Skip to content

Commit

Permalink
Issue #54, added debug to regoHandler. See examples/hexaRegoEmbedded
Browse files Browse the repository at this point in the history
Signed-off-by: Phil Hunt <[email protected]>
  • Loading branch information
independentid committed Sep 25, 2024
1 parent cf997dc commit 0cc726d
Show file tree
Hide file tree
Showing 7 changed files with 222 additions and 102 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM cgr.dev/chainguard/static:latest
FROM docker.io/chainguard/static:latest

LABEL org.opencontainers.image.authors="[email protected]"
LABEL org.opencontainers.image.source="https://github.com/hexa-org/policy-opa"
Expand Down
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ echo ""

echo "* building docker container image ($tag)..."
echo " - downloading latest chainguard platform image"
docker pull cgr.dev/chainguard/static:latest
docker pull docker.io/chainguard/static:latest

if [ "$multi" = 'Y' ];then
echo " - performing multi platform build"
Expand Down
107 changes: 107 additions & 0 deletions examples/hexaRegoEmbedded/handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package hexaRegoEmbedded

import (
"bytes"
"fmt"
"os"
"path/filepath"
"testing"

"github.com/hexa-org/policy-mapper/providers/openpolicyagent"
infoModel2 "github.com/hexa-org/policy-opa/api/infoModel"
opaTools "github.com/hexa-org/policy-opa/client/hexaOpaClient"
"github.com/hexa-org/policy-opa/cmd/hexaAuthZen/config"
"github.com/hexa-org/policy-opa/pkg/compressionsupport"
"github.com/hexa-org/policy-opa/pkg/decisionsupportproviders"
"github.com/hexa-org/policy-opa/server/opaHandler"
"github.com/open-policy-agent/opa/topdown"
assert "github.com/stretchr/testify/require"
)

const policyIDQL = `
{
"policies": [
{
"meta": {
"policyId": "GetOrganizationResource",
"version": "0.7",
"description": "A user must belong to an organization to access it's resources'"
},
"subjects": [
"role:my-organization-id"
],
"actions": [],
"object": "arn:maverics:us-west:organization-id"
}
]
}
`

func NewDecisionHandler() *opaHandler.RegoHandler {
bundlesDir := "/tmp/bundles"
_, err := os.Stat(filepath.Join(bundlesDir, "bundle"))
if os.IsNotExist(err) {
_ = os.Mkdir(bundlesDir, 0755)
createInitialBundle(bundlesDir)
} else {
// do this so we can edit the rego to test
os.WriteFile(filepath.Join(bundlesDir, "bundle", "data.json"), []byte(policyIDQL), 0644)
}

return opaHandler.NewRegoHandler(bundlesDir)
}

func createInitialBundle(bundlePath string) {
os.RemoveAll(bundlePath)
os.MkdirAll(bundlePath, 0755)
bundleBuffer, err := openpolicyagent.MakeHexaBundle([]byte(policyIDQL))
if err != nil {
config.ServerLog.Fatalf("unexpected error creating and initializing Hexa Bundle: %s", err)
}
gzip, _ := compressionsupport.UnGzip(bytes.NewReader(bundleBuffer.Bytes()))

_ = compressionsupport.UnTarToPath(bytes.NewReader(gzip), bundlePath)
}

func TestIDQL(t *testing.T) {
os.Setenv(decisionsupportproviders.EnvOpaDebug, "debug")
regoHandler := NewDecisionHandler()

claims := make(map[string]interface{})
claims["email"] = "[email protected]"
claims["picture"] = "https://www.topaz.sh/assets/templates/citadel/img/Rick%20Sanchez.jpg"
claims["name"] = "Rick Sanchez"
claims["id"] = "[email protected]"

subject := opaTools.SubjectInfo{
// These are the roles the user must have
Roles: []string{"admin", "my-organization-id"},
Sub: "CiRmZDA2MTRkMy1jMzlhLTQ3ODEtYjdiZC04Yjk2ZjVhNTEwMGQSBWxvY2Fs",
Claims: claims,
}

reqParams := opaTools.ReqParams{
ActionUris: []string{"viewApplication"},
ResourceIds: []string{"arn:maverics:us-west:organization-id:applications:application-id", "arn:maverics:us-west:organization-id"},
}

input := infoModel2.AzInfo{
Req: &reqParams,
Subject: &subject,
Resource: infoModel2.ResourceInfo{},
}

results, err := regoHandler.Evaluate(input)
assert.NoError(t, err)
assert.NotNil(t, results)

buffer := new(bytes.Buffer)
if regoHandler.Tracer != nil {
topdown.PrettyTraceWithLocation(buffer, *regoHandler.Tracer)
}
fmt.Println(buffer.String())

result := regoHandler.ProcessResults(results)
assert.NotNil(t, result)
assert.True(t, result.Allow)
}
61 changes: 32 additions & 29 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,49 @@ go 1.23

toolchain go1.23.1

// replace github.com/hexa-org/policy-mapper => ../policy-mapper

require (
github.com/golang-jwt/jwt/v5 v5.2.1
github.com/gorilla/mux v1.8.1
github.com/hexa-org/policy-mapper v0.7.0
github.com/hexa-org/policy-mapper v0.7.1
github.com/open-policy-agent/opa v0.68.0
github.com/stretchr/testify v1.9.0
github.com/tidwall/gjson v1.17.3
golang.org/x/net v0.29.0
)

require github.com/golang/glog v1.2.2 // indirect

require (
cloud.google.com/go/auth v0.9.4 // indirect
cloud.google.com/go/auth/oauth2adapt v0.2.4 // indirect
cloud.google.com/go/compute/metadata v0.5.1 // indirect
cloud.google.com/go/compute/metadata v0.5.2 // indirect
github.com/AdaLogics/go-fuzz-headers v0.0.0-20240806141605-e8a1dd7889d6 // indirect
github.com/MicahParks/jwkset v0.5.19 // indirect
github.com/MicahParks/jwkset v0.5.20 // indirect
github.com/MicahParks/keyfunc/v3 v3.3.5 // indirect
github.com/Microsoft/hcsshim v0.12.4 // indirect
github.com/OneOfOne/xxhash v1.2.8 // indirect
github.com/agnivade/levenshtein v1.1.1 // indirect
github.com/agnivade/levenshtein v1.2.0 // indirect
github.com/alexedwards/scs/v2 v2.8.0 // indirect
github.com/aws/aws-sdk-go-v2 v1.30.5 // indirect
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.4 // indirect
github.com/aws/aws-sdk-go-v2/config v1.27.33 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.17.32 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.13 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.17 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.17 // indirect
github.com/aws/aws-sdk-go-v2 v1.31.0 // indirect
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.5 // indirect
github.com/aws/aws-sdk-go-v2/config v1.27.38 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.17.36 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.14 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.18 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.18 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.1 // indirect
github.com/aws/aws-sdk-go-v2/internal/v4a v1.3.17 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.4 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.3.19 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.19 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.17.17 // indirect
github.com/aws/aws-sdk-go-v2/service/s3 v1.61.2 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.22.7 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.26.7 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.30.7 // indirect
github.com/aws/smithy-go v1.20.4 // indirect
github.com/aws/aws-sdk-go-v2/internal/v4a v1.3.18 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.5 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.3.20 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.20 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.17.18 // indirect
github.com/aws/aws-sdk-go-v2/service/s3 v1.63.2 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.23.2 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.27.2 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.31.2 // indirect
github.com/aws/smithy-go v1.21.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bytecodealliance/wasmtime-go/v3 v3.0.2 // indirect
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
Expand Down Expand Up @@ -70,7 +74,6 @@ require (
github.com/gobwas/glob v0.2.3 // indirect
github.com/gofrs/flock v0.12.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/glog v1.2.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/golang/snappy v0.0.4 // indirect
Expand All @@ -82,7 +85,7 @@ require (
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hhsnopek/etag v0.0.0-20171206181245-aea95f647346 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/klauspost/compress v1.17.10 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-runewidth v0.0.16 // indirect
Expand All @@ -96,7 +99,7 @@ require (
github.com/peterh/liner v1.2.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_golang v1.20.3 // indirect
github.com/prometheus/client_golang v1.20.4 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.59.1 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
Expand Down Expand Up @@ -129,7 +132,7 @@ require (
go.opentelemetry.io/otel/sdk v1.30.0 // indirect
go.opentelemetry.io/otel/trace v1.30.0 // indirect
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
go.uber.org/automaxprocs v1.5.3 // indirect
go.uber.org/automaxprocs v1.6.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/exp v0.0.0-20240909161429-701f63a606c0
Expand All @@ -138,10 +141,10 @@ require (
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.18.0 // indirect
golang.org/x/time v0.6.0 // indirect
google.golang.org/api v0.197.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240903143218-8af14fe29dc1 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240903143218-8af14fe29dc1 // indirect
google.golang.org/grpc v1.66.2 // indirect
google.golang.org/api v0.198.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240924160255-9d4c2d233b61 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240924160255-9d4c2d233b61 // indirect
google.golang.org/grpc v1.67.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
Loading

0 comments on commit 0cc726d

Please sign in to comment.