-
Notifications
You must be signed in to change notification settings - Fork 247
/
certfetcher.go
87 lines (70 loc) · 1.83 KB
/
certfetcher.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
package fetcher
import (
"context"
"fmt"
"io/ioutil"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
)
const (
baseCertPath = "/var/run/certs"
)
// CertFetcher is an interface for retreiving certs
type CertFetcher interface {
KeyBytes() ([]byte, error)
CertBytes() ([]byte, error)
}
// FileCertFetcher reads certs from files
type FileCertFetcher struct {
KeyFileName string
CertFileName string
}
// KeyBytes returns key bytes
func (f *FileCertFetcher) KeyBytes() ([]byte, error) {
return ioutil.ReadFile(f.KeyFileName)
}
// CertBytes returns cert bytes
func (f *FileCertFetcher) CertBytes() ([]byte, error) {
return ioutil.ReadFile(f.CertFileName)
}
// MemCertFetcher reads certs from files
type MemCertFetcher struct {
Cert, Key []byte
}
// KeyBytes returns key bytes
func (f *MemCertFetcher) KeyBytes() ([]byte, error) {
return f.Key, nil
}
// CertBytes returns cert bytes
func (f *MemCertFetcher) CertBytes() ([]byte, error) {
return f.Cert, nil
}
// CertBundleFetcher is an interface for retreiving CA Certbundles
type CertBundleFetcher interface {
BundleBytes() ([]byte, error)
}
// ConfigMapCertBundleFetcher reads bundles from ConfigMaps
type ConfigMapCertBundleFetcher struct {
Name string
Client corev1.ConfigMapInterface
}
// BundleBytes returns bundle bytes
func (f *ConfigMapCertBundleFetcher) BundleBytes() ([]byte, error) {
cm, err := f.Client.Get(context.TODO(), f.Name, metav1.GetOptions{})
if err != nil {
return nil, err
}
bundle, ok := cm.Data["ca-bundle.crt"]
if !ok {
return nil, fmt.Errorf("CA bundle missing")
}
return []byte(bundle), err
}
// MemCertBundleFetcher reads bundles
type MemCertBundleFetcher struct {
Bundle []byte
}
// BundleBytes returns bundle bytes
func (f *MemCertBundleFetcher) BundleBytes() ([]byte, error) {
return f.Bundle, nil
}