forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parameters.go
134 lines (120 loc) · 4.47 KB
/
parameters.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
/*
Copyright 2017 The Kubernetes 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 controller
import (
"crypto/sha256"
"encoding/json"
"fmt"
"github.com/ghodss/yaml"
"github.com/kubernetes-incubator/service-catalog/pkg/apis/servicecatalog/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
)
// buildParameters generates the parameters JSON structure to be passed
// to the broker.
// The first return value is a map of parameters to send to the Broker, including
// secret values.
// The second return value is a map of parameters with secret values redacted,
// replaced with "<redacted>".
// The third return value is any error that caused the function to fail.
func buildParameters(kubeClient kubernetes.Interface, namespace string, parametersFrom []v1beta1.ParametersFromSource, parameters *runtime.RawExtension) (map[string]interface{}, map[string]interface{}, error) {
params := make(map[string]interface{})
paramsWithSecretsRedacted := make(map[string]interface{})
if parametersFrom != nil {
for _, p := range parametersFrom {
fps, err := fetchParametersFromSource(kubeClient, namespace, &p)
if err != nil {
return nil, nil, err
}
for k, v := range fps {
if _, ok := params[k]; ok {
return nil, nil, fmt.Errorf("conflict: duplicate entry for parameter %q", k)
}
params[k] = v
paramsWithSecretsRedacted[k] = "<redacted>"
}
}
}
if parameters != nil {
pp, err := UnmarshalRawParameters(parameters.Raw)
if err != nil {
return nil, nil, err
}
for k, v := range pp {
if _, ok := params[k]; ok {
return nil, nil, fmt.Errorf("conflict: duplicate entry for parameter %q", k)
}
params[k] = v
paramsWithSecretsRedacted[k] = v
}
}
return params, paramsWithSecretsRedacted, nil
}
// fetchParametersFromSource fetches data from a specified external source and
// represents it in the parameters map format
func fetchParametersFromSource(kubeClient kubernetes.Interface, namespace string, parametersFrom *v1beta1.ParametersFromSource) (map[string]interface{}, error) {
var params map[string]interface{}
if parametersFrom.SecretKeyRef != nil {
data, err := fetchSecretKeyValue(kubeClient, namespace, parametersFrom.SecretKeyRef)
if err != nil {
return nil, err
}
p, err := unmarshalJSON(data)
if err != nil {
return nil, err
}
params = p
}
return params, nil
}
// UnmarshalRawParameters produces a map structure from a given raw YAML/JSON input
func UnmarshalRawParameters(in []byte) (map[string]interface{}, error) {
parameters := make(map[string]interface{})
if len(in) > 0 {
if err := yaml.Unmarshal(in, ¶meters); err != nil {
return parameters, err
}
}
return parameters, nil
}
// MarshalRawParameters marshals the specified map of parameters into JSON
func MarshalRawParameters(in map[string]interface{}) ([]byte, error) {
return json.Marshal(in)
}
// unmarshalJSON produces a map structure from a given raw JSON input
func unmarshalJSON(in []byte) (map[string]interface{}, error) {
parameters := make(map[string]interface{})
if err := json.Unmarshal(in, ¶meters); err != nil {
return nil, fmt.Errorf("failed to unmarshal parameters as JSON object: %v", err)
}
return parameters, nil
}
// fetchSecretKeyValue requests and returns the contents of the given secret key
func fetchSecretKeyValue(kubeClient kubernetes.Interface, namespace string, secretKeyRef *v1beta1.SecretKeyReference) ([]byte, error) {
secret, err := kubeClient.CoreV1().Secrets(namespace).Get(secretKeyRef.Name, metav1.GetOptions{})
if err != nil {
return nil, err
}
return secret.Data[secretKeyRef.Key], nil
}
// generateChecksumOfParameters generates a checksum for the map of parameters.
// This checksum is used to determine if parameters have changed.
func generateChecksumOfParameters(params map[string]interface{}) (string, error) {
paramsAsJSON, err := json.Marshal(params)
if err != nil {
return "", err
}
hash := sha256.Sum256(paramsAsJSON)
return fmt.Sprintf("%x", hash), nil
}