-
Notifications
You must be signed in to change notification settings - Fork 239
/
Copy pathcustom_resource_template_test.go
83 lines (70 loc) · 2.54 KB
/
custom_resource_template_test.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
73
74
75
76
77
78
79
80
81
82
83
package certlifetime
import (
"fmt"
"time"
cmv1 "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"
"golang.stackrox.io/kube-linter/pkg/check"
"golang.stackrox.io/kube-linter/pkg/config"
"golang.stackrox.io/kube-linter/pkg/diagnostic"
"golang.stackrox.io/kube-linter/pkg/k8sutil"
"golang.stackrox.io/kube-linter/pkg/lintcontext"
"golang.stackrox.io/kube-linter/pkg/objectkinds"
"golang.stackrox.io/kube-linter/pkg/templates"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)
// Define a constant that can be used to reference the objectKind in the check
const (
Certificate = cmv1.CertificateKind
)
// Define the GVK of the object that we want to match
var (
certificateGVK = cmv1.SchemeGroupVersion.WithKind(Certificate)
)
func init() {
// Register our matcher and objectkind with the global matcher registry.
// This function can contain any arbitrary logic to match objects we want to check with this lint check
objectkinds.RegisterObjectKind(Certificate, objectkinds.MatcherFunc(func(gvk schema.GroupVersionKind) bool {
return gvk == certificateGVK
}))
templates.Register(CertLifetime)
}
var CertLifetime = check.Template{
HumanName: "Certificate Lifetime",
Key: "cert-lifetime",
Description: "Flag certificates lasting longer than 1 year",
SupportedObjectKinds: config.ObjectKindsDesc{
ObjectKinds: []string{Certificate},
},
Parameters: nil,
ParseAndValidateParams: func(params map[string]interface{}) (interface{}, error) { return nil, nil },
Instantiate: func(_ interface{}) (check.Func, error) {
return func(_ lintcontext.LintContext, object lintcontext.Object) []diagnostic.Diagnostic {
cert, ok := object.K8sObject.(*cmv1.Certificate)
if !ok {
return []diagnostic.Diagnostic{{Message: "Invalid certificate"}}
}
if cert.Spec.Duration.Duration.Hours() > 8760 {
return []diagnostic.Diagnostic{{Message: "Certificates with lifetimes longer than one year are not allowed"}}
}
return nil
}, nil
},
}
func ExampleCertLifetime() {
dur, _ := time.ParseDuration("9001h")
cert := &cmv1.Certificate{
ObjectMeta: v1.ObjectMeta{
Name: "certificate-long",
},
Spec: cmv1.CertificateSpec{
Duration: &v1.Duration{Duration: dur},
},
}
checker, _ := CertLifetime.Instantiate(nil)
asK8sObj, _ := runtime.Object(cert).(k8sutil.Object)
result := checker(nil, lintcontext.Object{K8sObject: asK8sObj})
fmt.Println(result[0].Message)
// Output: Certificates with lifetimes longer than one year are not allowed
}