-
Notifications
You must be signed in to change notification settings - Fork 90
/
secretconfigobserver.go
262 lines (232 loc) · 6.81 KB
/
secretconfigobserver.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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package configobserver
import (
"context"
"encoding/json"
"fmt"
"reflect"
"strings"
"sync"
"time"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"k8s.io/klog/v2"
"github.com/openshift/insights-operator/pkg/config"
)
type ConfigReporter interface {
SetConfig(*config.Controller)
}
type Configurator interface {
Config() *config.Controller
ConfigChanged() (<-chan struct{}, func())
}
// Controller is responsible for periodically checking and (if necessary) updating the local configs/tokens
// according to the configs/tokens present on the cluster.
type Controller struct {
kubeClient kubernetes.Interface
lock sync.Mutex
defaultConfig config.Controller
tokenConfig *config.Controller
secretConfig *config.Controller
supportSecret *v1.Secret
config *config.Controller
checkPeriod time.Duration
listeners []chan struct{}
}
// New creates a new configobsever, the configs/tokens are updated from the configs/tokens present in the cluster if possible.
func New(defaultConfig config.Controller, kubeClient kubernetes.Interface) *Controller { //nolint: gocritic
c := &Controller{
kubeClient: kubeClient,
defaultConfig: defaultConfig,
checkPeriod: 5 * time.Minute,
}
c.mergeConfig()
if err := c.updateToken(context.TODO()); err != nil {
klog.Warningf("Unable to retrieve initial token config: %v", err)
}
if err := c.updateConfig(context.TODO()); err != nil {
klog.Warningf("Unable to retrieve initial config: %v", err)
}
return c
}
// Start is periodically invoking check and set of config and token
func (c *Controller) Start(ctx context.Context) {
wait.Until(func() {
if err := c.updateToken(ctx); err != nil {
klog.Warningf("Unable to retrieve token config: %v", err)
}
if err := c.updateConfig(ctx); err != nil {
klog.Warningf("Unable to retrieve config: %v", err)
}
}, c.checkPeriod, ctx.Done())
}
// Config provides the config in a thread-safe way.
func (c *Controller) Config() *config.Controller {
c.lock.Lock()
defer c.lock.Unlock()
return c.config
}
func (c *Controller) SupportSecret() *v1.Secret {
c.lock.Lock()
defer c.lock.Unlock()
return c.supportSecret
}
// ConfigChanged subscribe for config changes
// 1.Param: A channel where the listener is notified that the config has changed.
// 2.Param: A func which can be used to unsubscribe from the config changes.
func (c *Controller) ConfigChanged() (configCh <-chan struct{}, closeFn func()) {
c.lock.Lock()
defer c.lock.Unlock()
position := -1
for i := range c.listeners {
if c.listeners == nil {
position = i
break
}
}
if position == -1 {
c.listeners = append(c.listeners, nil)
position = len(c.listeners) - 1
}
ch := make(chan struct{}, 1)
c.listeners[position] = ch
return ch, func() {
c.lock.Lock()
defer c.lock.Unlock()
c.listeners[position] = nil
}
}
// Fetches the token from cluster secret key
func (c *Controller) fetchSecret(ctx context.Context, name string) (*v1.Secret, error) {
secret, err := c.kubeClient.CoreV1().Secrets("openshift-config").Get(ctx, name, metav1.GetOptions{})
if err != nil {
if errors.IsNotFound(err) {
klog.Infof("%s secret does not exist", name)
err = nil
secret = nil
} else if errors.IsForbidden(err) {
klog.V(2).Infof("Operator does not have permission to check %s: %v", name, err)
err = nil
secret = nil
} else {
err = fmt.Errorf("could not check %s: %v", name, err)
}
}
return secret, err
}
// Updates the stored tokens from the secrets in the cluster. (if present)
func (c *Controller) updateToken(ctx context.Context) error {
klog.V(2).Infof("Refreshing configuration from cluster pull secret")
secret, err := c.fetchSecret(ctx, "pull-secret")
if err != nil {
return err
}
var nextConfig config.Controller
if secret != nil {
var token string
token, err = tokenFromSecret(secret)
if err != nil {
return err
}
if len(token) > 0 {
nextConfig.Token = token
nextConfig.Report = true
}
}
c.setTokenConfig(&nextConfig)
return nil
}
// Updates the stored configs from the secrets in the cluster. (if present)
func (c *Controller) updateConfig(ctx context.Context) error {
klog.V(2).Infof("Refreshing configuration from cluster support secret")
secret, err := c.fetchSecret(ctx, "support")
if err != nil {
return err
}
c.supportSecret = secret
if secret == nil {
c.setSecretConfig(nil)
} else {
klog.Warning(`USING THE "SUPPORT" SECRET FOR OPERATOR CONFIGURATION IS DEPRECATED. PLEASE REFER TO THE OCP DOCUMENTATION FOR UPDATES.`) // nolint:lll
nextConfig, err := LoadConfigFromSecret(secret)
if err != nil {
return err
}
c.setSecretConfig(&nextConfig)
}
return nil
}
// Sets the token configuration to the observer
func (c *Controller) setTokenConfig(operatorConfig *config.Controller) {
c.lock.Lock()
defer c.lock.Unlock()
c.tokenConfig = operatorConfig
c.mergeConfig()
}
// Sets the secret configuration to the observer
func (c *Controller) setSecretConfig(operatorConfig *config.Controller) {
c.lock.Lock()
defer c.lock.Unlock()
c.secretConfig = operatorConfig
c.mergeConfig()
}
// Sets the operator configuration to the observer
func (c *Controller) setConfig(operatorConfig *config.Controller) {
if c.config != nil {
if !reflect.DeepEqual(c.config, operatorConfig) {
klog.Infof("Legacy configuration updated: %s", operatorConfig.ToString())
for _, ch := range c.listeners {
if ch == nil {
continue
}
select {
case ch <- struct{}{}:
default:
}
}
}
} else {
klog.Infof("Legacy configuration set: %s", operatorConfig.ToString())
}
c.config = operatorConfig
}
// Merges operator configuration to the observer
func (c *Controller) mergeConfig() {
cfg := c.defaultConfig
if c.secretConfig != nil {
cfg.MergeWith(c.secretConfig)
}
if c.tokenConfig != nil {
cfg.Token = c.tokenConfig.Token
}
cfg.Report = len(cfg.Endpoint) > 0 && len(cfg.Token) > 0
c.setConfig(&cfg)
}
// Parses the given secret to retrieve the token
func tokenFromSecret(secret *v1.Secret) (string, error) {
if data := secret.Data[".dockerconfigjson"]; len(data) > 0 {
var pullSecret serializedAuthMap
if err := json.Unmarshal(data, &pullSecret); err != nil {
klog.Errorf("Unable to unmarshal cluster pull-secret: %v", err)
}
if auth, ok := pullSecret.Auths["cloud.openshift.com"]; ok {
token := strings.TrimSpace(auth.Auth)
if strings.Contains(token, "\n") || strings.Contains(token, "\r") {
return "", fmt.Errorf("cluster authorization token is not valid: contains newlines")
}
if len(token) > 0 {
klog.Info("Found cloud.openshift.com token")
return token, nil
}
}
}
return "", nil
}
type serializedAuthMap struct {
Auths map[string]serializedAuth `json:"auths"`
}
type serializedAuth struct {
Auth string `json:"auth"`
}