-
Notifications
You must be signed in to change notification settings - Fork 407
/
connector.go
197 lines (159 loc) · 6.65 KB
/
connector.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 compassconnection
import (
"context"
"crypto/x509/pkix"
"strings"
"github.com/kyma-incubator/compass/components/director/pkg/correlation"
gqlschema "github.com/kyma-incubator/compass/components/connector/pkg/graphql/externalschema"
"github.com/kyma-project/kyma/components/compass-runtime-agent/internal/certificates"
"github.com/kyma-project/kyma/components/compass-runtime-agent/internal/compass"
"github.com/kyma-project/kyma/components/compass-runtime-agent/pkg/apis/compass/v1alpha1"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
type EstablishedConnection struct {
Credentials certificates.Credentials
ManagementInfo v1alpha1.ManagementInfo
}
const (
ConnectorTokenHeader = "Connector-Token"
)
//go:generate mockery --name=Connector
type Connector interface {
EstablishConnection(ctx context.Context, connectorURL, token string) (EstablishedConnection, error)
MaintainConnection(ctx context.Context, renewCert bool, credentialsExist bool) (*certificates.Credentials, v1alpha1.ManagementInfo, error)
}
func NewCompassConnector(
csrProvider certificates.CSRProvider,
clientsProvider compass.ClientsProvider,
) Connector {
return &compassConnector{
csrProvider: csrProvider,
clientsProvider: clientsProvider,
}
}
type compassConnector struct {
csrProvider certificates.CSRProvider
clientsProvider compass.ClientsProvider
}
func (cc *compassConnector) EstablishConnection(ctx context.Context, connectorURL, token string) (EstablishedConnection, error) {
connection, err := cc.establishConnection(ctx, connectorURL, token)
requestID := correlation.HeadersFromContext(ctx)[correlation.RequestIDHeaderKey]
return connection, errors.Wrapf(err, "x-request-id: %s failed", requestID)
}
func (cc *compassConnector) establishConnection(ctx context.Context, connectorURL, token string) (EstablishedConnection, error) {
requestID := correlation.HeadersFromContext(ctx)[correlation.RequestIDHeaderKey]
logger := logrus.WithFields(logrus.Fields{"x-request-id": requestID})
if connectorURL == "" {
return EstablishedConnection{}, errors.New("Failed to establish connection. Connector URL is empty")
}
tokenSecuredConnectorClient, err := cc.clientsProvider.GetConnectorTokensClient(connectorURL)
if err != nil {
return EstablishedConnection{}, errors.Wrap(err, "Failed to prepare Connector Token-secured client")
}
logger.Infof("Fetching configuration")
configuration, err := tokenSecuredConnectorClient.Configuration(ctx, connectorTokenHeader(token))
if err != nil {
return EstablishedConnection{}, errors.Wrap(err, "Failed to fetch configuration")
}
subject := parseSubject(configuration.CertificateSigningRequestInfo.Subject)
csr, key, err := cc.csrProvider.CreateCSR(subject)
if err != nil {
return EstablishedConnection{}, errors.Wrap(err, "Failed to generate CSR")
}
logger.Infof("Signing CSR")
certResponse, err := tokenSecuredConnectorClient.SignCSR(ctx, csr, connectorTokenHeader(configuration.Token.Token))
if err != nil {
return EstablishedConnection{}, errors.Wrap(err, "Failed to sign CSR")
}
credentials, err := certificates.NewCredentials(key, certResponse)
if err != nil {
return EstablishedConnection{}, errors.Wrap(err, "Failed to parse certification response to credentials")
}
return EstablishedConnection{
Credentials: credentials,
ManagementInfo: toManagementInfo(configuration.ManagementPlaneInfo),
}, nil
}
func (cc *compassConnector) MaintainConnection(ctx context.Context, renewCert bool, credentialsExist bool) (*certificates.Credentials, v1alpha1.ManagementInfo, error) {
certSecuredClient, err := cc.clientsProvider.GetConnectorCertSecuredClient()
if err != nil {
return nil, v1alpha1.ManagementInfo{}, errors.Wrap(err, "Failed to prepare Certificate-secured Connector client while checking connection")
}
configuration, err := certSecuredClient.Configuration(ctx, nil)
if err != nil {
return nil, v1alpha1.ManagementInfo{}, errors.Wrap(err, "Failed to query Connection Configuration while checking connection")
}
// Due to bug in Graphql client the response may not result in error but be empty
if err := validateConfigInfo(configuration.ManagementPlaneInfo); err != nil {
return nil, v1alpha1.ManagementInfo{}, err
}
if !renewCert && credentialsExist {
return nil, toManagementInfo(configuration.ManagementPlaneInfo), nil
}
subject := parseSubject(configuration.CertificateSigningRequestInfo.Subject)
csr, key, err := cc.csrProvider.CreateCSR(subject)
if err != nil {
return nil, v1alpha1.ManagementInfo{}, errors.Wrap(err, "Failed to create CSR while renewing connection")
}
certResponse, err := certSecuredClient.SignCSR(ctx, csr, nil)
if err != nil {
return nil, v1alpha1.ManagementInfo{}, errors.Wrap(err, "Failed to sign CSR while renewing connection")
}
renewedCredentials, err := certificates.NewCredentials(key, certResponse)
if err != nil {
return nil, v1alpha1.ManagementInfo{}, errors.Wrap(err, "Failed to parse certification response to credentials while renewing connection")
}
return &renewedCredentials, toManagementInfo(configuration.ManagementPlaneInfo), nil
}
func validateConfigInfo(configInfo *gqlschema.ManagementPlaneInfo) error {
if configInfo == nil {
return errors.New("Management info is empty")
}
if configInfo.CertificateSecuredConnectorURL == nil || configInfo.DirectorURL == nil {
return errors.New("Connector URL or Director URL is empty")
}
return nil
}
func toManagementInfo(configInfo *gqlschema.ManagementPlaneInfo) v1alpha1.ManagementInfo {
if configInfo == nil {
return v1alpha1.ManagementInfo{}
}
var directorURL = ""
if configInfo.DirectorURL != nil {
directorURL = *configInfo.DirectorURL
}
var certSecuredConnectorURL = ""
if configInfo.CertificateSecuredConnectorURL != nil {
certSecuredConnectorURL = *configInfo.CertificateSecuredConnectorURL
}
return v1alpha1.ManagementInfo{
DirectorURL: directorURL,
ConnectorURL: certSecuredConnectorURL,
}
}
func connectorTokenHeader(token string) map[string]string {
return map[string]string{
ConnectorTokenHeader: token,
}
}
func parseSubject(plainSubject string) pkix.Name {
subjectInfo := extractSubject(plainSubject)
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(plainSubject string) map[string]string {
result := map[string]string{}
segments := strings.Split(plainSubject, ",")
for _, segment := range segments {
parts := strings.Split(segment, "=")
result[parts[0]] = parts[1]
}
return result
}