-
Notifications
You must be signed in to change notification settings - Fork 7.6k
/
onprem.go
148 lines (127 loc) · 4.25 KB
/
onprem.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
// Copyright 2017 Istio 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 platform
import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"os"
"istio.io/istio/pkg/spiffe"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"strings"
"istio.io/istio/security/pkg/pki/util"
)
// OnPremClientImpl is the implementation of on premise metadata client.
type OnPremClientImpl struct {
// Root CA cert file to validate the gRPC service in CA.
rootCertFile string
// The private key file
keyFile string
// The cert chain file
certChainFile string
}
// CitadelDNSSan is the hardcoded DNS SAN used to identify citadel server.
// The user may use an IP address to connect to the mesh.
const CitadelDNSSan = "istio-citadel"
// NewOnPremClientImpl creates a new OnPremClientImpl.
func NewOnPremClientImpl(rootCert, key, certChain string) (*OnPremClientImpl, error) {
if _, err := os.Stat(rootCert); err != nil {
return nil, fmt.Errorf("failed to create onprem client root cert file %v error %v", rootCert, err)
}
if _, err := os.Stat(key); err != nil {
return nil, fmt.Errorf("failed to create onprem client key file %v", err)
}
if _, err := os.Stat(certChain); err != nil {
return nil, fmt.Errorf("failed to create onprem client certChain file %v", err)
}
return &OnPremClientImpl{rootCert, key, certChain}, nil
}
// GetDialOptions returns the GRPC dial options to connect to the CA.
func (ci *OnPremClientImpl) GetDialOptions() ([]grpc.DialOption, error) {
transportCreds, err := getTLSCredentials(ci.rootCertFile,
ci.keyFile, ci.certChainFile)
if err != nil {
return nil, err
}
var options []grpc.DialOption
options = append(options, grpc.WithTransportCredentials(transportCreds))
return options, nil
}
// IsProperPlatform returns whether the platform is on premise.
func (ci *OnPremClientImpl) IsProperPlatform() bool {
return true
}
// GetServiceIdentity gets the service account from the cert SAN field.
func (ci *OnPremClientImpl) GetServiceIdentity() (string, error) {
certBytes, err := ioutil.ReadFile(ci.certChainFile)
if err != nil {
return "", err
}
cert, err := util.ParsePemEncodedCertificate(certBytes)
if err != nil {
return "", err
}
serviceIDs, err := util.ExtractIDs(cert.Extensions)
if err != nil {
return "", err
}
if len(serviceIDs) != 1 {
for _, s := range serviceIDs {
if strings.HasPrefix(s, spiffe.Scheme+"://") {
return s, nil
}
}
return "", fmt.Errorf("cert does not have siffe:// SAN fields")
}
return serviceIDs[0], nil
}
// GetAgentCredential passes the certificate to control plane to authenticate
func (ci *OnPremClientImpl) GetAgentCredential() ([]byte, error) {
certBytes, err := ioutil.ReadFile(ci.certChainFile)
if err != nil {
return nil, fmt.Errorf("failed to read cert file: %s", ci.certChainFile)
}
return certBytes, nil
}
// GetCredentialType returns "onprem".
func (ci *OnPremClientImpl) GetCredentialType() string {
return "onprem"
}
// getTLSCredentials creates transport credentials that are common to
// node agent and CA.
func getTLSCredentials(rootCertFile, keyFile, certChainFile string) (credentials.TransportCredentials, error) {
// Load the certificate from disk
certificate, err := tls.LoadX509KeyPair(certChainFile, keyFile)
if err != nil {
return nil, fmt.Errorf("cannot load key pair: %s", err)
}
// Create a certificate pool
certPool := x509.NewCertPool()
bs, err := ioutil.ReadFile(rootCertFile)
if err != nil {
return nil, fmt.Errorf("failed to read CA cert: %s", err)
}
ok := certPool.AppendCertsFromPEM(bs)
if !ok {
return nil, fmt.Errorf("failed to append certificates")
}
config := tls.Config{
Certificates: []tls.Certificate{certificate},
}
config.RootCAs = certPool
config.ServerName = CitadelDNSSan
return credentials.NewTLS(&config), nil
}