-
Notifications
You must be signed in to change notification settings - Fork 0
/
cert.go
126 lines (115 loc) · 3.34 KB
/
cert.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
package cert
import (
"fmt"
"strings"
corev1 "k8s.io/api/core/v1"
)
const (
keyStartMarker = "-----BEGIN RSA PRIVATE KEY-----"
keyEndMarker = "-----END RSA PRIVATE KEY-----"
certStartMarker = "-----BEGIN CERTIFICATE-----"
certEndMarker = "-----END CERTIFICATE-----"
)
type CertData struct {
ProfileName string
CA string
Cert string
Key string
}
/*
func GetCertificateData(dir, profile string) (*CertData, error) {
certData := &CertData{
ProfileName: profile,
}
fmt.Printf("getCertificateData: %s\n", dir)
files, err := os.ReadDir(dir)
if err != nil {
fmt.Printf("getCertificateData error: %s\n", err.Error())
return nil, err
}
for _, f := range files {
fmt.Printf("filename: %s\n", f.Name())
if !f.IsDir() {
b, err := os.ReadFile(filepath.Join(dir, f.Name()))
if err != nil {
return nil, err
}
var found bool
if f.Name() == "ca.crt" {
certData.CA, found = getStringInBetween(string(b), caStartMarker, caEndMarker, true)
if !found {
return nil, fmt.Errorf("cannot get the ca string")
}
}
if f.Name() == "tls.crt" {
certData.Cert, found = getStringInBetween(string(b), certStartMarker, certEndMarker, true)
if !found {
return nil, fmt.Errorf("cannot get the cert string")
}
}
if f.Name() == "tls.key" {
certData.Key, found = getStringInBetween(string(b), keyStartMarker, keyEndMarker, false)
if !found {
return nil, fmt.Errorf("cannot get the key string")
}
certData.Key = strings.ReplaceAll(certData.Key, "\n", "")
}
}
}
return certData, nil
}
*/
func GetCertificateData(secret *corev1.Secret, profile string) (*CertData, error) {
certData := &CertData{
ProfileName: profile,
}
certFiles := []string{"ca.crt", "tls.crt", "tls.key"}
for _, certFile := range certFiles {
var found bool
switch certFile {
case "ca.crt":
certData.CA, found = getStringInBetween(string(secret.Data[certFile]), certStartMarker, certEndMarker, true)
if !found {
return nil, fmt.Errorf("cannot get the ca string")
}
certData.CA = strings.TrimSpace(certData.CA)
case "tls.crt":
certData.Cert, found = getStringInBetween(string(secret.Data[certFile]), certStartMarker, certEndMarker, true)
if !found {
return nil, fmt.Errorf("cannot get the tls cert string")
}
certData.Cert = strings.TrimSpace(certData.Cert)
case "tls.key":
fmt.Printf("tls.key:\n %s\n", secret.Data[certFile])
certData.Key, found = getStringInBetween(string(secret.Data[certFile]), keyStartMarker, keyEndMarker, true)
if !found {
return nil, fmt.Errorf("cannot get the tls key string")
}
certData.Key = strings.TrimSpace(certData.Key)
}
}
return certData, nil
}
// GetStringInBetween returns a string between the start/end markers with markers either included or excluded
func getStringInBetween(str, start, end string, include bool) (result string, found bool) {
// start index
sidx := strings.Index(str, start)
if sidx == -1 {
return "", false
}
// forward start index if we don't want to include the markers
if !include {
sidx += len(start)
}
newS := str[sidx:]
// end index
eidx := strings.Index(newS, end)
if eidx == -1 {
return "", false
}
// to include the end marker, increment the end index up till its length
if include {
eidx += len(end)
}
return newS[:eidx], true
}