-
Notifications
You must be signed in to change notification settings - Fork 84
/
cert_secured_client.go
76 lines (62 loc) · 2.25 KB
/
cert_secured_client.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
package clientset
import (
"context"
"crypto/tls"
"net/http"
"github.com/kyma-incubator/compass/components/connector/pkg/graphql/externalschema"
httputil "github.com/kyma-incubator/compass/components/director/pkg/http"
gcli "github.com/machinebox/graphql"
"github.com/pkg/errors"
)
type CertificateSecuredClient struct {
graphQlClient *gcli.Client
queryProvider queryProvider
}
func newCertificateSecuredConnectorClient(endpoint string, tlsCert tls.Certificate, opts *clientsetOptions) *CertificateSecuredClient {
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{tlsCert},
InsecureSkipVerify: true,
}
tr := httputil.NewCorrelationIDTransport(httputil.NewHTTPTransportWrapper(&http.Transport{
TLSClientConfig: tlsConfig,
}))
httpClient := &http.Client{
Transport: tr,
Timeout: opts.timeout,
}
graphQlClient := gcli.NewClient(endpoint, gcli.WithHTTPClient(httpClient))
return &CertificateSecuredClient{
graphQlClient: graphQlClient,
queryProvider: queryProvider{},
}
}
func (c CertificateSecuredClient) Configuration(ctx context.Context, headers ...http.Header) (externalschema.Configuration, error) {
query := c.queryProvider.configuration()
req := newRequest(query, headers...)
var response ConfigurationResponse
err := c.graphQlClient.Run(ctx, req, &response)
if err != nil {
return externalschema.Configuration{}, errors.Wrap(err, "Failed to get configuration")
}
return response.Result, nil
}
func (c CertificateSecuredClient) SignCSR(ctx context.Context, csr string, headers ...http.Header) (externalschema.CertificationResult, error) {
query := c.queryProvider.signCSR(csr)
req := newRequest(query, headers...)
var response CertificationResponse
err := c.graphQlClient.Run(ctx, req, &response)
if err != nil {
return externalschema.CertificationResult{}, errors.Wrap(err, "Failed to generate certificate")
}
return response.Result, nil
}
func (c CertificateSecuredClient) RevokeCertificate(ctx context.Context, headers ...http.Header) (bool, error) {
query := c.queryProvider.revokeCert()
req := newRequest(query, headers...)
var response RevokeResult
err := c.graphQlClient.Run(ctx, req, &response)
if err != nil {
return false, errors.Wrap(err, "Failed to revoke certificate")
}
return response.Result, nil
}