-
Notifications
You must be signed in to change notification settings - Fork 672
/
secret.go
143 lines (121 loc) · 3.93 KB
/
secret.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
// Copyright Project Contour Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package secret
import (
"context"
"fmt"
"strings"
"github.com/projectcontour/contour/internal/certgen"
"github.com/projectcontour/contour/internal/provisioner/model"
"github.com/projectcontour/contour/pkg/certs"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// generatedByVersionAnnotation is the key for the annotation that stores
// the version of Contour that the TLS secrets were generated by.
const generatedByVersionAnnotation = "projectcontour.io/generated-by-version"
// EnsureXDSSecrets ensures that mTLS secrets for Contour and Envoy exist
// and are up-to-date, i.e. have been generated with the current version.
func EnsureXDSSecrets(ctx context.Context, cli client.Client, contour *model.Contour, image string) error {
desiredVersion := tagFromImage(image)
if tlsSecretsExist(contour, cli, desiredVersion) {
return nil
}
certs, err := certs.GenerateCerts(
&certs.Configuration{
Lifetime: 365,
Namespace: contour.Namespace,
},
)
if err != nil {
return fmt.Errorf("error generating xDS TLS certificates: %w", err)
}
secrets, errs := certgen.AsSecrets(contour.Namespace, "-"+contour.Name, certs)
if len(errs) > 0 {
return utilerrors.NewAggregate(errs)
}
for _, secret := range secrets {
// Add owner labels.
if secret.Labels == nil {
secret.Labels = model.OwnerLabels(contour)
} else {
for k, v := range model.OwnerLabels(contour) {
secret.Labels[k] = v
}
}
// Add annotation indicating the version the secret was
// generated by, to ensure that we're rotating the secrets
// on upgrade.
if secret.Annotations == nil {
secret.Annotations = map[string]string{}
}
secret.Annotations[generatedByVersionAnnotation] = tagFromImage(image)
if err := cli.Create(ctx, secret); err != nil {
if !errors.IsAlreadyExists(err) {
return fmt.Errorf("error creating secret: %w", err)
}
if err := cli.Update(ctx, secret); err != nil {
return fmt.Errorf("error updating secret: %w", err)
}
}
}
return nil
}
// tagFromImage returns the tag from the provided image or an
// empty string if the image does not contain a tag.
func tagFromImage(image string) string {
if strings.Contains(image, ":") {
parsed := strings.Split(image, ":")
return parsed[1]
}
return ""
}
func tlsSecretsExist(contour *model.Contour, cli client.Client, generatedByVersion string) bool {
for _, secretName := range []string{
contour.ContourCertsSecretName(),
contour.EnvoyCertsSecretName(),
} {
s := &corev1.Secret{}
key := client.ObjectKey{
Namespace: contour.Namespace,
Name: secretName,
}
if err := cli.Get(context.Background(), key, s); err != nil {
return false
}
if s.Annotations[generatedByVersionAnnotation] != generatedByVersion {
return false
}
}
return true
}
func EnsureXDSSecretsDeleted(ctx context.Context, cli client.Client, contour *model.Contour) error {
for _, secretName := range []string{
contour.ContourCertsSecretName(),
contour.EnvoyCertsSecretName(),
} {
s := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Namespace: contour.Namespace,
Name: secretName,
},
}
if err := cli.Delete(context.Background(), s); err != nil && !errors.IsNotFound(err) {
return err
}
}
return nil
}