forked from grafana/loki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ca_configmap.go
38 lines (29 loc) · 831 Bytes
/
ca_configmap.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
package storage
import (
"crypto/sha1"
"fmt"
corev1 "k8s.io/api/core/v1"
)
type caKeyError string
func (e caKeyError) Error() string {
return fmt.Sprintf("key not present or data empty: %s", string(e))
}
// CheckCAConfigMap checks if the given CA configMap has an non-empty entry for the key used as CA certificate.
// If the key is present it will return a hash of the current key name and contents.
func CheckCAConfigMap(cm *corev1.ConfigMap, key string) (string, error) {
data := cm.Data[key]
if data == "" {
return "", caKeyError(key)
}
h := sha1.New()
if _, err := h.Write([]byte(key)); err != nil {
return "", err
}
if _, err := h.Write(hashSeparator); err != nil {
return "", err
}
if _, err := h.Write([]byte(data)); err != nil {
return "", err
}
return fmt.Sprintf("%x", h.Sum(nil)), nil
}