This repository has been archived by the owner on Apr 7, 2020. It is now read-only.
forked from istio/istio
/
server.go
254 lines (210 loc) · 7.43 KB
/
server.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// Copyright 2018 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 sds
import (
"fmt"
"net"
"os"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"istio.io/istio/pkg/log"
"istio.io/istio/security/pkg/nodeagent/cache"
"istio.io/istio/security/pkg/nodeagent/plugin"
"istio.io/istio/security/pkg/nodeagent/plugin/providers/google/stsclient"
)
const maxStreams = 100000
// Options provides all of the configuration parameters for secret discovery service.
type Options struct {
// EnableWorkloadSDS indicates whether node agent works as SDS server for workload proxies.
EnableWorkloadSDS bool
// WorkloadUDSPath is the unix domain socket through which SDS server communicates with workload proxies.
WorkloadUDSPath string
// EnableIngressGatewaySDS indicates whether node agent works as ingress gateway agent.
EnableIngressGatewaySDS bool
// IngressGatewayUDSPath is the unix domain socket through which SDS server communicates with
// ingress gateway proxies.
IngressGatewayUDSPath string
// CertFile is the path of Cert File for gRPC server TLS settings.
CertFile string
// KeyFile is the path of Key File for gRPC server TLS settings.
KeyFile string
// CAEndpoint is the CA endpoint to which node agent sends CSR request.
CAEndpoint string
// The CA provider name.
CAProviderName string
// TrustDomain corresponds to the trust root of a system.
// https://github.com/spiffe/spiffe/blob/master/standards/SPIFFE-ID.md#21-trust-domain
TrustDomain string
// PluginNames is plugins' name for certain authentication provider.
PluginNames []string
// The Vault CA address.
VaultAddress string
// The Vault auth path.
VaultAuthPath string
// The Vault role.
VaultRole string
// The Vault sign CSR path.
VaultSignCsrPath string
// The Vault TLS root certificate.
VaultTLSRootCert string
// AlwaysValidTokenFlag is set to true for if token used is always valid(ex, normal k8s JWT)
AlwaysValidTokenFlag bool
}
// Server is the gPRC server that exposes SDS through UDS.
type Server struct {
workloadSds *sdsservice
gatewaySds *sdsservice
grpcWorkloadListener net.Listener
grpcGatewayListener net.Listener
grpcWorkloadServer *grpc.Server
grpcGatewayServer *grpc.Server
}
// NewServer creates and starts the Grpc server for SDS.
func NewServer(options Options, workloadSecretCache, gatewaySecretCache cache.SecretManager) (*Server, error) {
s := &Server{
workloadSds: newSDSService(workloadSecretCache, false),
gatewaySds: newSDSService(gatewaySecretCache, true),
}
if options.EnableWorkloadSDS {
if err := s.initWorkloadSdsService(&options); err != nil {
log.Errorf("Failed to initialize secret discovery service for workload proxies: %v", err)
return nil, err
}
log.Infof("SDS gRPC server start, listen %q \n", options.WorkloadUDSPath)
}
if options.EnableIngressGatewaySDS {
if err := s.initGatewaySdsService(&options); err != nil {
log.Errorf("Failed to initialize secret discovery service for ingress gateway: %v", err)
return nil, err
}
log.Infof("SDS gRPC server start, listen %q \n", options.IngressGatewayUDSPath)
}
return s, nil
}
// Stop closes the gRPC server.
func (s *Server) Stop() {
if s == nil {
return
}
if s.grpcWorkloadListener != nil {
s.grpcWorkloadListener.Close()
}
if s.grpcGatewayListener != nil {
s.grpcGatewayListener.Close()
}
if s.grpcWorkloadServer != nil {
s.grpcWorkloadServer.Stop()
}
if s.grpcGatewayServer != nil {
s.grpcGatewayServer.Stop()
}
}
// NewPlugins returns a slice of default Plugins.
func NewPlugins(in []string) []plugin.Plugin {
var availablePlugins = map[string]plugin.Plugin{
plugin.GoogleTokenExchange: stsclient.NewPlugin(),
}
var plugins []plugin.Plugin
for _, pl := range in {
if p, exist := availablePlugins[pl]; exist {
plugins = append(plugins, p)
}
}
return plugins
}
func (s *Server) initWorkloadSdsService(options *Options) error {
s.grpcWorkloadServer = grpc.NewServer(s.grpcServerOptions(options)...)
s.workloadSds.register(s.grpcWorkloadServer)
var err error
s.grpcWorkloadListener, err = setUpUds(options.WorkloadUDSPath)
if err != nil {
log.Errorf("SDS grpc server for workload proxies failed to start: %v", err)
}
go func() {
for {
// Retry if Serve() fails
log.Info("Start SDS grpc server")
if err = s.grpcWorkloadServer.Serve(s.grpcWorkloadListener); err != nil {
log.Errorf("SDS grpc server for workload proxies failed to start: %v", err)
}
s.grpcWorkloadListener, err = setUpUds(options.WorkloadUDSPath)
if err != nil {
log.Errorf("SDS grpc server for workload proxies failed to set up UDS: %v", err)
}
}
}()
return nil
}
func (s *Server) initGatewaySdsService(options *Options) error {
s.grpcGatewayServer = grpc.NewServer(s.grpcServerOptions(options)...)
s.gatewaySds.register(s.grpcGatewayServer)
var err error
s.grpcGatewayListener, err = setUpUds(options.IngressGatewayUDSPath)
if err != nil {
log.Errorf("SDS grpc server for ingress gateway proxy failed to start: %v", err)
return fmt.Errorf("SDS grpc server for ingress gateway proxy failed to start: %v", err)
}
go func() {
for {
// Retry if Serve() fails
log.Info("Start SDS grpc server for ingress gateway proxy")
if err = s.grpcGatewayServer.Serve(s.grpcGatewayListener); err != nil {
log.Errorf("SDS grpc server for ingress gateway proxy failed to start: %v", err)
}
s.grpcGatewayListener, err = setUpUds(options.IngressGatewayUDSPath)
if err != nil {
log.Errorf("SDS grpc server for ingress gateway proxy failed to set up UDS: %v", err)
}
}
}()
return nil
}
func setUpUds(udsPath string) (net.Listener, error) {
// Remove unix socket before use.
if err := os.Remove(udsPath); err != nil && !os.IsNotExist(err) {
// Anything other than "file not found" is an error.
log.Errorf("Failed to remove unix://%s: %v", udsPath, err)
return nil, fmt.Errorf("failed to remove unix://%s", udsPath)
}
var err error
udsListener, err := net.Listen("unix", udsPath)
if err != nil {
log.Errorf("Failed to listen on unix socket %q: %v", udsPath, err)
return nil, err
}
// Update SDS UDS file permission so that istio-proxy has permission to access it.
if _, err := os.Stat(udsPath); err != nil {
log.Errorf("SDS uds file %q doesn't exist", udsPath)
return nil, fmt.Errorf("sds uds file %q doesn't exist", udsPath)
}
if err := os.Chmod(udsPath, 0666); err != nil {
log.Errorf("Failed to update %q permission", udsPath)
return nil, fmt.Errorf("failed to update %q permission", udsPath)
}
return udsListener, nil
}
func (s *Server) grpcServerOptions(options *Options) []grpc.ServerOption {
grpcOptions := []grpc.ServerOption{
grpc.MaxConcurrentStreams(uint32(maxStreams)),
}
if options.CertFile != "" && options.KeyFile != "" {
creds, err := credentials.NewServerTLSFromFile(options.CertFile, options.KeyFile)
if err != nil {
log.Errorf("Failed to load TLS keys: %s", err)
return nil
}
grpcOptions = append(grpcOptions, grpc.Creds(creds))
}
return grpcOptions
}