File size: 5,116 Bytes
df0899b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Verify referential templates include sync data

package main

import (
	"context"
	"flag"
	"fmt"
	"io/fs"
	"log"
	"os"
	"path/filepath"
	"strings"

	constraintclient "github.com/open-policy-agent/frameworks/constraint/pkg/client"
	"github.com/open-policy-agent/frameworks/constraint/pkg/client/drivers/rego"
	"github.com/open-policy-agent/frameworks/constraint/pkg/core/templates"
	gkapis "github.com/open-policy-agent/gatekeeper/v3/apis"
	"github.com/open-policy-agent/gatekeeper/v3/pkg/cachemanager/parser"
	"github.com/open-policy-agent/gatekeeper/v3/pkg/gator/reader"
	"github.com/open-policy-agent/gatekeeper/v3/pkg/target"
	"k8s.io/apimachinery/pkg/runtime"
)

const syncAnnotation string = "metadata.gatekeeper.sh/requires-sync-data"

var (
	pathFlag = flag.String("path", "", "Path to verify referential templates include sync data.")
	fileFlag = flag.Bool("sync-file", false, "When `true`, require a `sync.yaml` file for each referential template.")
)

var scheme *runtime.Scheme

func init() {
	scheme = runtime.NewScheme()
	err := gkapis.AddToScheme(scheme)
	if err != nil {
		panic(fmt.Errorf("adding gatekeeper apis to scheme: %w", err))
	}
}

func main() {
	flag.Parse()
	if *pathFlag == "" {
		log.Fatal("Missing `path` flag")
	}
	log.Printf("Verifying path: %s\n", *pathFlag)

	err := checkTemplates(*pathFlag)
	if err != nil {
		log.Fatal(err)
	}
}

func checkTemplates(libraryPath string) error {
	system := os.DirFS(libraryPath)

	rc, err := newRefChecker()
	if err != nil {
		return fmt.Errorf("creating referentialChecker: %w", err)
	}

	err = fs.WalkDir(system, ".", func(path string, d fs.DirEntry, err error) error {
		if err != nil {
			return err
		}

		if d.IsDir() {
			return nil
		}

		if d.Name() != "template.yaml" {
			return nil
		}

		// we'll use this in error messages
		absolutePath := filepath.Join(libraryPath, path)

		// read template
		tmpl, err := reader.ReadTemplate(scheme, system, path)
		if err != nil {
			return fmt.Errorf("reading template: %w", err)
		}

		// check if it's referential
		isRef, err := rc.isReferential(tmpl)
		if err != nil {
			return fmt.Errorf("detecting referential: %w", err)
		}

		// nothing to check for non referential templates
		if !isRef {
			return nil
		}

		log.Printf("Referential template: %s\n", absolutePath)

		// verify sync annotation is present and parsable by gatekeeper
		_, err = parser.ReadSyncRequirements(tmpl)
		if err != nil {
			return fmt.Errorf("template at path %q is missing valid %s annotation: %w", absolutePath, syncAnnotation, err)
		}

		// verify that the sync object is present in the same directory as the template
		if !*fileFlag {
			return nil
		}

		syncPath := filepath.Join(filepath.Dir(path), "sync.yaml")
		_, err = fs.Stat(system, syncPath)
		if err != nil {
			if os.IsNotExist(err) {
				return fmt.Errorf("`sync.yaml` not found in dir '%s'", filepath.Dir(absolutePath))
			}

			return fmt.Errorf("stat on '%s': %w", syncPath, err)
		}

		return nil
	})

	return err
}

type referentialChecker struct {
	refClient    *constraintclient.Client
	nonRefClient *constraintclient.Client
}

func newRefChecker() (*referentialChecker, error) {
	refClient, err := opaClient(true)
	if err != nil {
		return nil, fmt.Errorf("creating referential client: %w", err)
	}

	nonRefClient, err := opaClient(false)
	if err != nil {
		return nil, fmt.Errorf("creating non-referential client: %w", err)
	}

	return &referentialChecker{
		refClient:    refClient,
		nonRefClient: nonRefClient,
	}, nil
}

func (rc *referentialChecker) isReferential(ct *templates.ConstraintTemplate) (bool, error) {
	// Verify that we can add the template to a referential client.  This is a sanity
	// check for a malformed template.
	if _, err := rc.refClient.AddTemplate(context.Background(), ct); err != nil {
		return false, fmt.Errorf("adding template to referential client: %w", err)
	}

	// a referential template will fail when added to a client that does not
	// have the `inventory` field enabled.  Trying to add the template to the
	// non-referential client thus serves as an indication of it being
	// referential.
	_, err := rc.nonRefClient.AddTemplate(context.Background(), ct)
	if err == nil {
		// no error, template isn't referential
		return false, nil
	}

	// we got the specific error message that means referential
	if errTextIsReferential(err) {
		return true, nil
	}

	return false, fmt.Errorf("unrelated error when adding template to non-referential client: %w", err)
}

func errTextIsReferential(err error) bool {
	return strings.Contains(err.Error(), "check refs failed on module")
}

func opaClient(referential bool) (*constraintclient.Client, error) {
	externs := rego.Externs()
	if referential {
		externs = rego.Externs("inventory")
	}

	driver, err := rego.New(rego.Tracing(false), externs)
	if err != nil {
		return nil, fmt.Errorf("creating driver: %w", err)
	}

	client, err := constraintclient.NewClient(constraintclient.Targets(&target.K8sValidationTarget{}), constraintclient.Driver(driver))
	if err != nil {
		return nil, fmt.Errorf("creating client: %w", err)
	}

	return client, nil
}