Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: include metrics about number of custom rules [CFG-1133] #121

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 33 additions & 6 deletions internal/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package internal
import (
"bytes"
"context"
"fmt"
"io"
"os"
"path"
"strings"

"github.com/open-policy-agent/opa/ast"
Expand All @@ -30,6 +32,11 @@ type BuildCommandParams struct {
func RunBuild(args []string, params *BuildCommandParams) error {
buf := bytes.NewBuffer(nil)

metadataFile, err := createManifest(args)
if err == nil && metadataFile != "" {
defer os.Remove(metadataFile)
}

var capabilities *ast.Capabilities
// if capabilities are not provided as a cmd flag,
// then ast.CapabilitiesForThisVersion must be called
Expand All @@ -42,19 +49,20 @@ func RunBuild(args []string, params *BuildCommandParams) error {
compiler := compile.New().
WithCapabilities(capabilities).
WithTarget(params.Target.String()).
WithAsBundle(false).
WithAsBundle(true).
WithOptimizationLevel(0).
WithOutput(buf).
WithEntrypoints(params.Entrypoint.Strings()...).
WithPaths(args...).
WithFilter(buildCommandLoaderFilter(false, params.Ignore))
WithFilter(buildCommandLoaderFilter(true, params.Ignore))

err := compiler.Build(context.Background())
err = compiler.Build(context.Background())
if err != nil {
return err
}

out, err := os.Create(params.OutputFile)
defer out.Close()
if err != nil {
return err
}
Expand All @@ -64,14 +72,33 @@ func RunBuild(args []string, params *BuildCommandParams) error {
return err
}

return out.Close()
return nil
}

func buildCommandLoaderFilter(bundleMode bool, ignore []string) func(string, os.FileInfo, int) bool {
return func(abspath string, info os.FileInfo, depth int) bool {
if !info.IsDir() && strings.HasSuffix(abspath, ".tar.gz") {
return true
if !bundleMode {
if !info.IsDir() && strings.HasSuffix(abspath, ".tar.gz") {
return true
}
}
return util.LoaderFilter{Ignore: ignore}.Apply(abspath, info, depth)
}
}

func createManifest(inputPaths []string) (string, error) {
rules, err := util.RetrieveRules(inputPaths)
if err != nil {
return "", err
}

// choose either one of the locations for the metadata.json
// it will be included in the OPA generated data.json
metadataFile := path.Join(inputPaths[0], ".manifest")
err = os.WriteFile(metadataFile, []byte(fmt.Sprintf("{\"metadata\":{\"numberOfRules\":%d}}", len(rules))),
0644)
if err != nil {
return "", err
}
return metadataFile, nil
}
51 changes: 51 additions & 0 deletions internal/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package internal

import (
"archive/tar"
"bytes"
"compress/gzip"
"io"
"os"
Expand Down Expand Up @@ -268,3 +269,53 @@ func TestBuildRespectsCapabilitiesFailure(t *testing.T) {
assert.Contains(t, err.Error(), "undefined function is_foo")
})
}

func TestBuildProducesMetadata(t *testing.T) {
files := map[string]string{
"test.rego": `
package test
msg = {
"publicId":
"1"
}
`,
}

test.WithTempFS(files, func(root string) {
buildParams := mockBuildParams()
buildParams.OutputFile = path.Join(root, "bundle.tar.gz")
err := buildParams.Target.Set(TargetRego)
assert.Nil(t, err)

err = RunBuild([]string{root}, buildParams)
assert.Nil(t, err)

_, err = loader.NewFileLoader().AsBundle(buildParams.OutputFile)
assert.Nil(t, err)

// Check that manifest is not written given no input manifest and no other flags
f, err := os.Open(buildParams.OutputFile)
assert.Nil(t, err)
defer f.Close()

gr, err := gzip.NewReader(f)
assert.Nil(t, err)

tr := tar.NewReader(gr)

for {
f, err := tr.Next()
if err == io.EOF {
break
}
assert.Nil(t, err)

if f.Name == "/.manifest" {
data := new(bytes.Buffer)
_, err := data.ReadFrom(tr)
assert.Nil(t, err)
assert.Contains(t, data.String(), "{\"numberOfRules\":1}")
}
}
})
}