-
Notifications
You must be signed in to change notification settings - Fork 107
/
trivy.go
169 lines (137 loc) · 5.4 KB
/
trivy.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
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
package harbor
import (
"context"
goharborv1 "github.com/goharbor/harbor-operator/apis/goharbor.io/v1beta1"
harbormetav1 "github.com/goharbor/harbor-operator/apis/meta/v1alpha1"
"github.com/goharbor/harbor-operator/controllers"
"github.com/goharbor/harbor-operator/pkg/config"
serrors "github.com/goharbor/harbor-operator/pkg/controller/errors"
"github.com/goharbor/harbor-operator/pkg/graph"
"github.com/goharbor/harbor-operator/pkg/version"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func (r *Reconciler) AddTrivyConfigurations(ctx context.Context, harbor *goharborv1.Harbor, tlsIssuer InternalTLSIssuer) (TrivyInternalCertificate, TrivyUpdateSecret, error) {
if harbor.Spec.Trivy == nil {
return nil, nil, nil
}
certificate, err := r.AddTrivyInternalCertificate(ctx, harbor, tlsIssuer)
if err != nil {
return nil, nil, errors.Wrap(err, "certificate")
}
var secret TrivyUpdateSecret
if harbor.Spec.Trivy.GithubTokenRef == "" {
secret, err = r.AddTrivyUpdateSecret(ctx, harbor)
if err != nil {
return nil, nil, errors.Wrap(err, "update secret")
}
}
return certificate, secret, nil
}
type TrivyInternalCertificate graph.Resource
func (r *Reconciler) AddTrivyInternalCertificate(ctx context.Context, harbor *goharborv1.Harbor, tlsIssuer InternalTLSIssuer) (TrivyInternalCertificate, error) {
cert, err := r.GetInternalTLSCertificate(ctx, harbor, harbormetav1.TrivyTLS)
if err != nil {
return nil, errors.Wrap(err, "get")
}
certRes, err := r.Controller.AddCertificateToManage(ctx, cert, tlsIssuer)
if err != nil {
return nil, errors.Wrap(err, "add")
}
return TrivyInternalCertificate(certRes), nil
}
type TrivyUpdateSecret graph.Resource
func (r *Reconciler) AddTrivyUpdateSecret(ctx context.Context, harbor *goharborv1.Harbor) (TrivyUpdateSecret, error) {
authSecret, err := r.GetTrivyUpdateSecret(ctx, harbor)
if err != nil {
return nil, errors.Wrap(err, "get")
}
authSecretRes, err := r.AddSecretToManage(ctx, authSecret)
if err != nil {
return nil, errors.Wrap(err, "add")
}
return TrivyUpdateSecret(authSecretRes), nil
}
const TrivyGithubCredentialsConfigKey = "trivy-github-credentials"
func (r *Reconciler) GetTrivyUpdateSecret(ctx context.Context, harbor *goharborv1.Harbor) (*corev1.Secret, error) {
name := r.NormalizeName(ctx, harbor.GetName(), controllers.Trivy.String(), "github")
namespace := harbor.GetNamespace()
token, err := r.GetGithubToken(TrivyGithubCredentialsConfigKey)
if err != nil {
if config.IsNotFound(err, TrivyGithubCredentialsConfigKey) {
return nil, nil
}
if config.IsNotFound(err, GithubCredentialsConfigKey) {
return nil, nil
}
return nil, serrors.UnrecoverrableError(err, serrors.OperatorReason, "cannot get github credentials")
}
return &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
Immutable: &varFalse,
Type: harbormetav1.SecretTypeGithubToken,
StringData: map[string]string{
harbormetav1.GithubTokenKey: token,
},
}, nil
}
type Trivy graph.Resource
func (r *Reconciler) AddTrivy(ctx context.Context, harbor *goharborv1.Harbor, certificate TrivyInternalCertificate, secretUpdate TrivyUpdateSecret) (Trivy, error) {
if harbor.Spec.Trivy == nil {
return nil, nil
}
trivy, err := r.GetTrivy(ctx, harbor, secretUpdate != nil)
if err != nil {
return nil, errors.Wrap(err, "get")
}
trivyRes, err := r.AddBasicResource(ctx, trivy, certificate, secretUpdate)
return Trivy(trivyRes), errors.Wrap(err, "add")
}
func (r *Reconciler) GetTrivy(ctx context.Context, harbor *goharborv1.Harbor, hasUpdateSecret bool) (*goharborv1.Trivy, error) {
name := r.NormalizeName(ctx, harbor.GetName())
namespace := harbor.GetNamespace()
redis := harbor.Spec.RedisConnection(harbormetav1.TrivyRedis)
githubTokenRef := harbor.Spec.Trivy.GithubTokenRef
if githubTokenRef == "" && hasUpdateSecret {
githubTokenRef = r.NormalizeName(ctx, harbor.GetName(), controllers.Trivy.String(), "github")
}
tls := harbor.Spec.InternalTLS.GetComponentTLSSpec(r.GetInternalTLSCertificateSecretName(ctx, harbor, harbormetav1.TrivyTLS))
var tokenServiceCertificateAuthorityRefs []string
if harbor.Spec.Expose.Core.TLS != nil {
tokenServiceCertificateAuthorityRefs = append(tokenServiceCertificateAuthorityRefs, harbor.Spec.Expose.Core.TLS.CertificateRef)
}
return &goharborv1.Trivy{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Annotations: version.SetVersion(map[string]string{
harbormetav1.NetworkPoliciesAnnotationName: harbormetav1.NetworkPoliciesAnnotationDisabled,
}, harbor.Spec.Version),
},
Spec: goharborv1.TrivySpec{
ComponentSpec: harbor.GetComponentSpec(ctx, harbormetav1.TrivyComponent),
Redis: goharborv1.TrivyRedisSpec{
RedisConnection: redis,
},
Storage: goharborv1.TrivyStorageSpec{
Reports: r.TrivyReportsStorage(ctx, harbor),
Cache: r.TrivyCacheStorage(ctx, harbor),
},
Server: goharborv1.TrivyServerSpec{
TLS: tls,
TokenServiceCertificateAuthorityRefs: tokenServiceCertificateAuthorityRefs,
},
Update: goharborv1.TrivyUpdateSpec{
Skip: harbor.Spec.Trivy.SkipUpdate,
GithubTokenRef: githubTokenRef,
},
CertificateInjection: harbor.Spec.Trivy.CertificateInjection,
Proxy: harbor.GetComponentProxySpec(harbormetav1.TrivyComponent),
Network: harbor.Spec.Network,
},
}, nil
}