forked from aquasecurity/trivy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.go
72 lines (64 loc) · 1.32 KB
/
schema.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//go:build mage_schema
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"os"
"github.com/aquasecurity/trivy/pkg/iac/rego/schemas"
)
const (
schemaPath = "pkg/iac/rego/schemas/cloud.json"
)
func main() {
if len(os.Args) < 3 {
log.Fatalf("invalid schema command args: %s", os.Args)
}
switch os.Args[2] {
case "generate":
if err := GenSchema(); err != nil {
log.Fatalf(err.Error())
}
log.Println("schema generated")
case "verify":
if err := VerifySchema(); err != nil {
log.Fatalf(err.Error())
}
log.Println("schema valid")
}
}
// GenSchema generates the Trivy IaC schema
func GenSchema() error {
schema, err := schemas.Build()
if err != nil {
return err
}
data, err := json.MarshalIndent(schema, "", " ")
if err != nil {
return err
}
if err := os.WriteFile(schemaPath, data, 0600); err != nil {
return err
}
return nil
}
// VerifySchema verifies a generated schema for validity
func VerifySchema() error {
schema, err := schemas.Build()
if err != nil {
return err
}
data, err := json.MarshalIndent(schema, "", " ")
if err != nil {
return err
}
existing, err := os.ReadFile(schemaPath)
if err != nil {
return err
}
if !bytes.Equal(data, existing) {
return fmt.Errorf("schema is out of date:\n\nplease run 'mage schema:generate' and commit the changes\n")
}
return nil
}