-
Notifications
You must be signed in to change notification settings - Fork 57
/
client.go
180 lines (155 loc) · 4.93 KB
/
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
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
/*
Copyright 2021 The cert-manager Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package client
import (
"context"
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"strings"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/metadata"
securityapi "istio.io/api/security/v1alpha1"
"istio.io/istio/pkg/log"
"istio.io/istio/pkg/security"
)
const (
bearerTokenPrefix = "Bearer "
)
var (
certmanagerClientLog = log.RegisterScope("certmanagerclient", "cert-manager client debugging")
)
type certmanagerClient struct {
caEndpoint string
enableTLS bool
caTLSRootCert []byte
clusterID string
token string
client securityapi.IstioCertificateServiceClient
conn *grpc.ClientConn
}
// NewCertManagerClient create a CA client for cert-manager istio agent.
func NewCertManagerClient(endpoint, token string, tls bool, rootCert []byte, clusterID string) (security.Client, error) {
c := &certmanagerClient{
caEndpoint: endpoint,
token: token,
enableTLS: tls,
caTLSRootCert: rootCert,
clusterID: clusterID,
}
conn, err := c.buildConnection()
if err != nil {
certmanagerClientLog.Errorf("Failed to connect to endpoint %s: %v", endpoint, err)
return nil, fmt.Errorf("failed to connect to endpoint %s", endpoint)
}
c.conn = conn
c.client = securityapi.NewIstioCertificateServiceClient(conn)
return c, nil
}
// CSR Sign calls cert-manager istio-csr to sign a CSR.
func (c *certmanagerClient) CSRSign(csrPEM []byte, certValidTTLInSec int64) ([]string, error) {
req := &securityapi.IstioCertificateRequest{
Csr: string(csrPEM),
ValidityDuration: certValidTTLInSec,
}
if err := c.reconnect(); err != nil {
return nil, err
}
ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs("Authorization", bearerTokenPrefix+c.token, "ClusterID", c.clusterID))
resp, err := c.client.CreateCertificate(ctx, req)
if err != nil {
return nil, fmt.Errorf("create certificate: %v", err)
}
if len(resp.GetCertChain()) <= 1 {
return nil, errors.New("invalid empty CertChain")
}
return resp.GetCertChain(), nil
}
func (c *certmanagerClient) getTLSDialOption() (grpc.DialOption, error) {
// Load the TLS root certificate from the specified file.
// Create a certificate pool
var certPool *x509.CertPool
var err error
if c.caTLSRootCert == nil {
// No explicit certificate - assume the citadel-compatible server uses a public cert
certPool, err = x509.SystemCertPool()
if err != nil {
return nil, err
}
certmanagerClientLog.Infof("cert-manager client using public DNS: %s", c.caEndpoint)
} else {
certPool = x509.NewCertPool()
ok := certPool.AppendCertsFromPEM(c.caTLSRootCert)
if !ok {
return nil, fmt.Errorf("failed to append certificates")
}
certmanagerClientLog.Infof("cert-manager client using custom root: %s %s", c.caEndpoint, string(c.caTLSRootCert))
}
var certificate tls.Certificate
config := tls.Config{
MinVersion: tls.VersionTLS12,
Certificates: []tls.Certificate{certificate},
GetClientCertificate: func(*tls.CertificateRequestInfo) (*tls.Certificate, error) {
return &certificate, nil
},
}
config.RootCAs = certPool
// For debugging on localhost (with port forward)
if strings.Contains(c.caEndpoint, "localhost") {
config.ServerName = "istio-csr.cert-manager.svc"
}
transportCreds := credentials.NewTLS(&config)
return grpc.WithTransportCredentials(transportCreds), nil
}
func (c *certmanagerClient) buildConnection() (*grpc.ClientConn, error) {
var opts grpc.DialOption
var err error
if c.enableTLS {
opts, err = c.getTLSDialOption()
if err != nil {
return nil, err
}
} else {
opts = grpc.WithTransportCredentials(insecure.NewCredentials())
}
conn, err := grpc.Dial(c.caEndpoint, opts)
if err != nil {
certmanagerClientLog.Errorf("Failed to connect to endpoint %s: %v", c.caEndpoint, err)
return nil, fmt.Errorf("failed to connect to endpoint %s", c.caEndpoint)
}
return conn, nil
}
func (c *certmanagerClient) reconnect() error {
err := c.conn.Close()
if err != nil {
return fmt.Errorf("failed to close connection")
}
conn, err := c.buildConnection()
if err != nil {
return err
}
c.conn = conn
c.client = securityapi.NewIstioCertificateServiceClient(conn)
return err
}
func (c *certmanagerClient) Close() {
if c.conn != nil {
c.conn.Close()
}
}
func (c *certmanagerClient) GetRootCertBundle() ([]string, error) {
return []string{string(c.caTLSRootCert)}, nil
}