-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
stringsource.go
61 lines (52 loc) · 1.22 KB
/
stringsource.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
package api
import (
"crypto/x509"
"fmt"
"io/ioutil"
"os"
pemutil "github.com/openshift/origin/pkg/cmd/util/pem"
)
func GetStringSourceFileReferences(s *StringSource) []*string {
if s == nil {
return nil
}
return []*string{
&s.File,
&s.KeyFile,
}
}
func ResolveStringValue(s StringSource) (string, error) {
var value string
switch {
case len(s.Value) > 0:
value = s.Value
case len(s.Env) > 0:
value = os.Getenv(s.Env)
case len(s.File) > 0:
data, err := ioutil.ReadFile(s.File)
if err != nil {
return "", err
}
value = string(data)
default:
value = ""
}
if len(s.KeyFile) == 0 {
// value is cleartext, return
return value, nil
}
keyData, err := ioutil.ReadFile(s.KeyFile)
if err != nil {
return "", err
}
secretBlock, ok := pemutil.BlockFromBytes([]byte(value), StringSourceEncryptedBlockType)
if !ok {
return "", fmt.Errorf("no valid PEM block of type %q found in data", StringSourceEncryptedBlockType)
}
keyBlock, ok := pemutil.BlockFromBytes(keyData, StringSourceKeyBlockType)
if !ok {
return "", fmt.Errorf("no valid PEM block of type %q found in key", StringSourceKeyBlockType)
}
data, err := x509.DecryptPEMBlock(secretBlock, keyBlock.Bytes)
return string(data), err
}