Skip to content

Commit c754024

Browse files
committed
build: build cuevalidator as a binary
Signed-off-by: behnazh-w <[email protected]>
1 parent 219903d commit c754024

File tree

18 files changed

+374
-222
lines changed

18 files changed

+374
-222
lines changed

Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ setup: force-upgrade setup-go setup-binaries setup-schemastore
9393
go install github.com/CycloneDX/cyclonedx-gomod/cmd/[email protected]
9494
setup-go:
9595
go build -o $(PACKAGE_PATH)/bin/ $(REPO_PATH)/golang/cmd/...
96-
go build -o $(PACKAGE_PATH)/bin/cuevalidate.so -buildmode=c-shared $(REPO_PATH)/golang/internal/cue_validator/cue_validator.go
9796
setup-binaries: $(PACKAGE_PATH)/bin/slsa-verifier $(PACKAGE_PATH)/resources/mvnw $(PACKAGE_PATH)/resources/gradlew souffle gnu-sed
9897
$(PACKAGE_PATH)/bin/slsa-verifier:
9998
git clone --depth 1 https://github.com/slsa-framework/slsa-verifier.git -b v2.6.0

golang/README.md

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
# Go module documentation
22
## Quick start
33
Prerequisites
4-
- Go (tested on `go1.17.8 linux/amd64`). Installation instructions [here](https://go.dev/doc/install).
4+
- Go (tested on `go 1.23.0 linux/amd64`). Installation instructions [here](https://go.dev/doc/install).
55

66
- Prepare the required libraries by running this command from the root dir of this repository:
7-
```
7+
```bash
88
go mod download
99
```
1010
This command will download all packages as defined in [go.mod](../../../go.mod) and [go.sum](../../../go.sum).
1111

1212
### Project layout
1313
This go module follows the Golang project layout as specified in [golang-standards/project-layout](https://github.com/golang-standards/project-layout).
1414

15-
```
15+
```bash
1616
macaron
1717
├── golang
18-
├── cmd
19-
│ └── bashparser
20-
── internal
21-
├── bashparser
22-
├── cue_validator
23-
│ └── filewriter
24-
── pkg
25-
└── README.md
18+
   ├── cmd
19+
   │   ├── bashparser
20+
   │   └── cuevalidator
21+
   ├── internal
22+
   │   ├── bashparser
23+
   │   ├── cuevalidator
24+
   │   └── filewriter
25+
   └── README.md
2626
├── go.mod
2727
├── go.sum
2828
└── <other files in the root repository ...>
@@ -36,32 +36,39 @@ macaron
3636

3737
### Run the application code directly using Go
3838
To run an application (in the `cmd` dir), from the root dir of this repository:
39-
```
39+
```bash
4040
go run ./golang/cmd/<app_name>/<app_name>.go [ARGS]
4141
```
4242

43-
For example, to run the [actionparser](./cmd/actionparser/README.md) application:
44-
```
45-
go run ./golang/cmd/actionparser/actionparser.go -file ./golang/internal/actionparser/resources/valid.yaml
46-
```
4743
### Run the Go tests
4844

4945
To run all the tests, from the root dir of this repository:
46+
```bash
47+
make test
5048
```
49+
50+
To just run the Go tests:
51+
```bash
5152
go test ./golang/...
5253
```
5354

5455
To run the tests and record the code coverage, from the root dir of this repository:
55-
```
56+
```bash
5657
go test -cover ./golang/...
5758
```
5859

5960
### Build the executable
6061
To build an executable of an application in this module:
62+
63+
```bash
64+
make setup-go
6165
```
66+
67+
Alternatively you can run:
68+
```bash
6269
go build ./golang/cmd/<app_name>/<app_name>.go
6370
```
6471
This will generate an executable `app_name` in the current directory. We can also change the path of the output executable by using:
65-
```
72+
```bash
6673
go build -o <output_path> ./golang/cmd/<app_name>/<app_name>.go
6774
```

golang/cmd/cuevalidator/README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# CUE Validator
2+
3+
This Go module validates CUE provenance against a policy and extracts analysis targets using [CUE](https://cuelang.org/).
4+
5+
### Run the CUE Validator directly
6+
7+
To run the validator, from the root directory of this repository:
8+
9+
```bash
10+
go run ./golang/cmd/cuevalidator/cuevalidator.go -h
11+
```
12+
13+
14+
#### Commands:
15+
16+
- `-target-policy <cue-policy-path>`: The CUE policy path from which to extract the target.
17+
- `-validate-policy <cue-policy-path>`: The CUE policy path to validate the provenance against.
18+
- `-validate-provenance <provenance-path>`: The provenance payload path to validate.
19+
20+
### Examples:
21+
22+
1. **Extract Target from Policy**
23+
To extract the target from a CUE policy, use the following command:
24+
25+
```bash
26+
go run ./golang/cmd/cuevalidator/cuevalidator.go -target-policy <path-to-cue-policy>
27+
```
28+
29+
Output:
30+
31+
```bash
32+
pkg:maven/io.micronaut/micronaut-core
33+
```
34+
35+
2. **Validate Provenance Against Policy**
36+
To validate provenance against a policy, use the following command:
37+
38+
```bash
39+
go run ./golang/cmd/cuevalidator/cuevalidator.go -validate-policy <path-to-cue-policy> -validate-provenance <path-to-provenance-payload>
40+
```
41+
42+
### Error Handling:
43+
44+
- If required arguments are missing or invalid, the program will print an error message to `stderr` and exit with a non-zero status code.
45+
- If the validation fails, an error message will be printed, and the program will exit with an appropriate error code.
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/* Copyright (c) 2025 - 2025, Oracle and/or its affiliates. All rights reserved. */
2+
/* Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/. */
3+
4+
package main
5+
6+
import (
7+
"flag"
8+
"fmt"
9+
"os"
10+
11+
"github.com/oracle/macaron/golang/internal/cuevalidator"
12+
)
13+
14+
// Utility function to handle file reading and errors.
15+
func readFile(path string) ([]byte, error) {
16+
content, err := os.ReadFile(path)
17+
if err != nil {
18+
return nil, fmt.Errorf("failed to read file '%s': %w", path, err)
19+
}
20+
return content, nil
21+
}
22+
23+
// Handle validation errors.
24+
func handleError(message string, code int) {
25+
fmt.Fprintln(os.Stderr, message)
26+
os.Exit(code)
27+
}
28+
29+
// Main entry point for the CUE Validator tool.
30+
// This function processes command-line flags to execute one of the following commands:
31+
// - Extract a target from a CUE policy (using -target-policy flag).
32+
// - Validate provenance against a CUE policy (using -validate-policy and -validate-provenance flags).
33+
//
34+
// Params:
35+
//
36+
// -target-policy <CUE_POLICY>: the CUE policy to extract the target from.
37+
// -validate-policy <CUE_POLICY>: the CUE policy to validate the provenance against.
38+
// -validate-provenance <PROVENANCE_DATA>: the provenance data to validate.
39+
//
40+
// Return code:
41+
//
42+
// 0 - If the target is successfully extracted or the provenance validation finishes with no errors.
43+
// 1 - If there is a missing required argument or invalid command usage.
44+
// 2 - If an error occurs during validation (e.g., invalid provenance or policy).
45+
//
46+
// Usage:
47+
//
48+
// 1. To extract the target from a policy:
49+
// go run cuevalidator.go -target-policy <CUE_POLICY>
50+
// Output: The extracted target will be printed to stdout.
51+
//
52+
// 2. To validate provenance against a policy:
53+
// go run cuevalidator.go -validate-policy <CUE_POLICY> -validate-provenance <PROVENANCE_DATA>
54+
// Output: A success or failure message will be printed based on the validation result.
55+
func main() {
56+
// Define flags for the target command.
57+
targetPolicy := flag.String("target-policy", "", "Path to CUE policy to extract the target from.")
58+
59+
// Define flags for the validate command
60+
validatePolicy := flag.String("validate-policy", "", "Path to CUE policy to validate against.")
61+
validateProvenance := flag.String("validate-provenance", "", "Path to provenance data to validate.")
62+
63+
// Parse flags
64+
flag.Parse()
65+
66+
// Handle 'target-policy' command.
67+
if *targetPolicy != "" {
68+
policyContent, err := readFile(*targetPolicy)
69+
if err != nil {
70+
handleError(err.Error(), 2)
71+
}
72+
73+
result := cuevalidator.Target(string(policyContent))
74+
if result == "" {
75+
handleError("Error: Unable to extract target from policy.", 2)
76+
}
77+
78+
fmt.Print(result)
79+
return
80+
}
81+
82+
// Handle 'validate' command.
83+
if *validatePolicy != "" && *validateProvenance != "" {
84+
policyContent, err := readFile(*validatePolicy)
85+
if err != nil {
86+
handleError(err.Error(), 2)
87+
}
88+
89+
provenanceContent, err := readFile(*validateProvenance)
90+
if err != nil {
91+
handleError(err.Error(), 2)
92+
}
93+
94+
result := cuevalidator.Validate(string(policyContent), string(provenanceContent))
95+
switch result {
96+
case 1:
97+
fmt.Print("True")
98+
os.Exit(0)
99+
case 0:
100+
fmt.Print("False")
101+
os.Exit(0)
102+
default:
103+
handleError("Error: Validation encountered an issue.", 2)
104+
}
105+
return
106+
}
107+
108+
// If no valid command was given, print usage message
109+
handleError("Error: Missing required arguments for target or validate command.", 1)
110+
}

golang/internal/cue_validator/cgo_helper.go

Lines changed: 0 additions & 36 deletions
This file was deleted.

golang/internal/cue_validator/cue_validator.go

Lines changed: 0 additions & 80 deletions
This file was deleted.

0 commit comments

Comments
 (0)