forked from kyma-incubator/compass
/
clientset.go
197 lines (151 loc) · 4.36 KB
/
clientset.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package clientset
import (
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/base64"
"encoding/pem"
"strings"
"github.com/pkg/errors"
)
const (
keyLength = 4096
)
type ConnectorClientSet struct {
*clientsetOptions
}
type Option interface {
apply(*clientsetOptions)
}
type optionFunc func(*clientsetOptions)
func (f optionFunc) apply(o *clientsetOptions) {
f(o)
}
type clientsetOptions struct {
skipTLSVerify bool
}
func WithSkipTLSVerify(skipTLSVerify bool) Option {
return optionFunc(func(o *clientsetOptions) {
o.skipTLSVerify = skipTLSVerify
})
}
func NewConnectorClientSet(options ...Option) *ConnectorClientSet {
opts := &clientsetOptions{
skipTLSVerify: false,
}
for _, opt := range options {
opt.apply(opts)
}
return &ConnectorClientSet{
clientsetOptions: opts,
}
}
func (cs ConnectorClientSet) TokenSecuredClient(baseURL string) *TokenSecuredClient {
return newTokenSecuredClient(baseURL, cs.clientsetOptions.skipTLSVerify)
}
func (cs ConnectorClientSet) CertificateSecuredClient(baseURL string, certificate tls.Certificate) *CertificateSecuredClient {
return newCertificateSecuredConnectorClient(baseURL, certificate)
}
func (cs ConnectorClientSet) GenerateCertificateForToken(token, connectorURL string) (tls.Certificate, error) {
connectorClient := newTokenSecuredClient(connectorURL, cs.skipTLSVerify)
config, err := connectorClient.Configuration(token)
if err != nil {
return tls.Certificate{}, err
}
key, csr, err := NewCSR(config.CertificateSigningRequestInfo.Subject, nil)
if err != nil {
return tls.Certificate{}, err
}
encodedCSR := encodeCSR(csr)
certResult, err := connectorClient.SignCSR(encodedCSR, config.Token.Token)
if err != nil {
return tls.Certificate{}, err
}
pemCertChain, err := base64.StdEncoding.DecodeString(certResult.CertificateChain)
if err != nil {
return tls.Certificate{}, err
}
certs, err := decodeCertificates(pemCertChain)
if err != nil {
return tls.Certificate{}, err
}
return NewTLSCertificate(key, certs...), nil
}
func decodeCertificates(pemCertChain []byte) ([]*x509.Certificate, error) {
if pemCertChain == nil {
return nil, errors.New("Certificate data is empty")
}
var certificates []*x509.Certificate
for block, rest := pem.Decode(pemCertChain); block != nil && rest != nil; {
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return nil, errors.Wrap(err, "Failed to decode one of the pem blocks")
}
certificates = append(certificates, cert)
block, rest = pem.Decode(rest)
}
if len(certificates) == 0 {
return nil, errors.New("No certificates found in the pem block")
}
return certificates, nil
}
func encodeCSR(csr *x509.CertificateRequest) string {
pemCSR := pem.EncodeToMemory(&pem.Block{
Type: "CERTIFICATE REQUEST", Bytes: csr.Raw,
})
return base64.StdEncoding.EncodeToString(pemCSR)
}
func NewTLSCertificate(key *rsa.PrivateKey, certificates ...*x509.Certificate) tls.Certificate {
rawCerts := make([][]byte, len(certificates))
for i, c := range certificates {
rawCerts[i] = c.Raw
}
return tls.Certificate{
Certificate: rawCerts,
PrivateKey: key,
}
}
func NewCSR(subject string, key *rsa.PrivateKey) (*rsa.PrivateKey, *x509.CertificateRequest, error) {
var err error
if key == nil {
key, err = rsa.GenerateKey(rand.Reader, keyLength)
if err != nil {
return nil, nil, err
}
}
parsedSubject := parseSubject(subject)
var csrTemplate = x509.CertificateRequest{
Subject: parsedSubject,
}
csrCertificate, err := x509.CreateCertificateRequest(rand.Reader, &csrTemplate, key)
if err != nil {
return nil, nil, err
}
csr, err := x509.ParseCertificateRequest(csrCertificate)
if err != nil {
return nil, nil, err
}
return key, csr, nil
}
func parseSubject(subject string) pkix.Name {
subjectInfo := extractSubject(subject)
return pkix.Name{
CommonName: subjectInfo["CN"],
Country: []string{subjectInfo["C"]},
Organization: []string{subjectInfo["O"]},
OrganizationalUnit: []string{subjectInfo["OU"]},
Locality: []string{subjectInfo["L"]},
Province: []string{subjectInfo["ST"]},
}
}
func extractSubject(subject string) map[string]string {
result := map[string]string{}
segments := strings.Split(subject, ",")
for _, segment := range segments {
parts := strings.Split(segment, "=")
result[parts[0]] = parts[1]
}
return result
}