forked from kyma-project/kyma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
129 lines (102 loc) · 3.97 KB
/
service.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
package certificates
import (
"crypto/x509"
"encoding/base64"
"k8s.io/apimachinery/pkg/types"
"github.com/kyma-project/kyma/components/connector-service/internal/apperrors"
"github.com/kyma-project/kyma/components/connector-service/internal/secrets"
)
const (
caCertificateSecretKey = "ca.crt"
caKeySecretKey = "ca.key"
rootCACertificateSecretKey = "cacert"
)
type Service interface {
// SignCSR takes encoded CSR, validates subject and generates Certificate based on CA stored in secret
// returns base64 encoded certificate chain
SignCSR(encodedCSR []byte, subject CSRSubject) (EncodedCertificateChain, apperrors.AppError)
}
type certificateService struct {
secretsRepository secrets.Repository
certUtil CertificateUtility
caSecretName types.NamespacedName
rootCACertificateSecretName types.NamespacedName
}
func NewCertificateService(secretRepository secrets.Repository, certUtil CertificateUtility, caSecretName, rootCACertificateSecretName types.NamespacedName) Service {
return &certificateService{
secretsRepository: secretRepository,
certUtil: certUtil,
caSecretName: caSecretName,
rootCACertificateSecretName: rootCACertificateSecretName,
}
}
func (svc *certificateService) SignCSR(encodedCSR []byte, subject CSRSubject) (EncodedCertificateChain, apperrors.AppError) {
csr, err := svc.certUtil.LoadCSR(encodedCSR)
if err != nil {
return EncodedCertificateChain{}, err
}
err = svc.checkCSR(csr, subject)
if err != nil {
return EncodedCertificateChain{}, err
}
return svc.signCSR(csr)
}
func (svc *certificateService) signCSR(csr *x509.CertificateRequest) (EncodedCertificateChain, apperrors.AppError) {
secretData, err := svc.secretsRepository.Get(svc.caSecretName)
if err != nil {
return EncodedCertificateChain{}, err
}
caCrt, err := svc.certUtil.LoadCert(secretData[caCertificateSecretKey])
if err != nil {
return EncodedCertificateChain{}, err
}
caKey, err := svc.certUtil.LoadKey(secretData[caKeySecretKey])
if err != nil {
return EncodedCertificateChain{}, err
}
signedCrt, err := svc.certUtil.SignCSR(caCrt, csr, caKey)
if err != nil {
return EncodedCertificateChain{}, err
}
return svc.encodeCertificates(caCrt.Raw, signedCrt)
}
func (svc *certificateService) encodeCertificates(rawCaCertificate, rawClientCertificate []byte) (EncodedCertificateChain, apperrors.AppError) {
caCrtBytes := svc.certUtil.AddCertificateHeaderAndFooter(rawCaCertificate)
signedCrtBytes := svc.certUtil.AddCertificateHeaderAndFooter(rawClientCertificate)
if svc.rootCACertificateSecretName.Name != "" {
rootCABytes, err := svc.loadRootCACert()
if err != nil {
return EncodedCertificateChain{}, err
}
caCrtBytes = svc.createCertChain(rootCABytes, caCrtBytes)
}
certChain := svc.createCertChain(signedCrtBytes, caCrtBytes)
return encodeCertificateBase64(certChain, signedCrtBytes, caCrtBytes), nil
}
func (svc *certificateService) loadRootCACert() ([]byte, apperrors.AppError) {
secretData, err := svc.secretsRepository.Get(svc.rootCACertificateSecretName)
if err != nil {
return nil, err
}
rootCACrt, err := svc.certUtil.LoadCert(secretData[rootCACertificateSecretKey])
if err != nil {
return nil, err
}
return svc.certUtil.AddCertificateHeaderAndFooter(rootCACrt.Raw), nil
}
func (svc *certificateService) checkCSR(csr *x509.CertificateRequest, expectedSubject CSRSubject) apperrors.AppError {
return svc.certUtil.CheckCSRValues(csr, expectedSubject)
}
func (svc *certificateService) createCertChain(clientCrt, caCrt []byte) []byte {
return append(clientCrt, caCrt...)
}
func encodeCertificateBase64(certChain, clientCRT, caCRT []byte) EncodedCertificateChain {
return EncodedCertificateChain{
CertificateChain: encodeStringBase64(certChain),
ClientCertificate: encodeStringBase64(clientCRT),
CaCertificate: encodeStringBase64(caCRT),
}
}
func encodeStringBase64(bytes []byte) string {
return base64.StdEncoding.EncodeToString(bytes)
}