-
Notifications
You must be signed in to change notification settings - Fork 12
/
secret.go
144 lines (121 loc) · 3.68 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
144
/*
* Copyright 2019 SAP SE or an SAP affiliate company. All rights reserved.
* This file is licensed under the Apache Software License, v. 2 except as noted
* otherwise in the LICENSE file
*
* 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
*
*/
package secret
import (
"fmt"
"k8s.io/apimachinery/pkg/runtime/schema"
"github.com/gardener/controller-manager-library/pkg/certmgmt"
"github.com/gardener/controller-manager-library/pkg/controllermanager/cluster"
"github.com/gardener/controller-manager-library/pkg/fieldpath"
"github.com/gardener/controller-manager-library/pkg/logger"
"github.com/gardener/controller-manager-library/pkg/resources"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
)
////////////////////////////////////////////////////////////////////////////////
var dataField = fieldpath.RequiredField(&corev1.Secret{}, ".Data")
type secretCertificateAccess struct {
cluster cluster.Interface
name resources.ObjectName
keys []Keys
}
var _ certmgmt.CertificateAccess = &secretCertificateAccess{}
func NewSecret(cluster cluster.Interface, name resources.ObjectName, keys ...Keys) certmgmt.CertificateAccess {
if len(keys) == 0 {
keys = []Keys{DefaultKeys()}
}
return &secretCertificateAccess{
cluster: cluster,
name: name,
keys: keys,
}
}
func (this *secretCertificateAccess) String() string {
return fmt.Sprintf("{cluster: %s, secret: %s}", this.cluster.GetName(), this.name)
}
func (this *secretCertificateAccess) Get(logger logger.LogContext) (certmgmt.CertificateInfo, error) {
secret, err := resources.GetSecret(this.cluster, this.name.Namespace(), this.name.Name())
if err != nil {
if !apierrors.IsNotFound(err) {
return nil, err
}
return nil, nil
}
return dataToCertInfo(secret.GetData(), this.keys), nil
}
func (this *secretCertificateAccess) Set(logger logger.LogContext, cert certmgmt.CertificateInfo) error {
r, _ := this.cluster.GetResource(schema.GroupKind{corev1.GroupName, "Secret"})
o := r.New(this.name)
data := certInfoToData(cert, this.keys[0])
mod, err := resources.CreateOrModify(o, func(mod *resources.ModificationState) error {
mod.Set(dataField, data)
return nil
})
if mod {
logger.Infof("certs in secret %q[%s] are updated", this.name, this.cluster.GetName())
}
return err
}
func dataToCertInfo(data map[string][]byte, keys []Keys) certmgmt.CertificateInfo {
if data == nil {
return nil
}
var ok bool
var _cert []byte
for _, k := range keys {
_cert, ok = data[k.CertName]
if ok {
break
}
}
var _key []byte
for _, k := range keys {
_key, ok = data[k.KeyName]
if ok {
break
}
}
var _cacert []byte
for _, k := range keys {
_cacert, ok = data[k.CACertName]
if ok {
break
}
}
var _cakey []byte
for _, k := range keys {
_cakey, ok = data[k.CAKeyName]
if ok {
break
}
}
return certmgmt.NewCertInfo(_cert, _key, _cacert, _cakey)
}
func certInfoToData(cert certmgmt.CertificateInfo, keys Keys) map[string][]byte {
m := map[string][]byte{}
add(m, keys.CACertName, cert.CACert())
add(m, keys.CAKeyName, cert.CAKey())
add(m, keys.CertName, cert.Cert())
add(m, keys.KeyName, cert.Key())
return m
}
func add(m map[string][]byte, key string, data []byte) {
if len(data) > 0 {
m[key] = data
}
}