/
tls.go
213 lines (198 loc) 路 7.99 KB
/
tls.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
package v2
import (
envoy_auth "github.com/envoyproxy/go-control-plane/envoy/api/v2/auth"
envoy_core "github.com/envoyproxy/go-control-plane/envoy/api/v2/core"
envoy_grpc_credential "github.com/envoyproxy/go-control-plane/envoy/config/grpc_credential/v2alpha"
envoy_type_matcher "github.com/envoyproxy/go-control-plane/envoy/type/matcher"
"github.com/golang/protobuf/ptypes/wrappers"
core_xds "github.com/kumahq/kuma/pkg/core/xds"
"github.com/kumahq/kuma/pkg/util/proto"
util_xds "github.com/kumahq/kuma/pkg/util/xds"
xds_context "github.com/kumahq/kuma/pkg/xds/context"
"github.com/kumahq/kuma/pkg/xds/envoy/tls"
)
// CreateDownstreamTlsContext creates DownstreamTlsContext for incoming connections
// It verifies that incoming connection has TLS certificate signed by Mesh CA with URI SAN of prefix spiffe://{mesh_name}/
// It secures inbound listener with certificate of "identity_cert" that will be received from the SDS (it contains URI SANs of all inbounds).
// Access to SDS is secured by TLS certificate (set in config or autogenerated at CP start) and path to dataplane token
func CreateDownstreamTlsContext(ctx xds_context.Context, metadata *core_xds.DataplaneMetadata) (*envoy_auth.DownstreamTlsContext, error) {
if !ctx.Mesh.Resource.MTLSEnabled() {
return nil, nil
}
validationSANMatcher := MeshSpiffeIDPrefixMatcher(ctx.Mesh.Resource.Meta.GetName())
commonTlsContext, err := createCommonTlsContext(ctx, metadata, validationSANMatcher)
if err != nil {
return nil, err
}
return &envoy_auth.DownstreamTlsContext{
CommonTlsContext: commonTlsContext,
RequireClientCertificate: &wrappers.BoolValue{Value: true},
}, nil
}
// CreateUpstreamTlsContext creates UpstreamTlsContext for outgoing connections
// It verifies that the upstream server has TLS certificate signed by Mesh CA with URI SAN of spiffe://{mesh_name}/{upstream_service}
// The downstream client exposes for the upstream server cert with multiple URI SANs, which means that if DP has inbound with services "web" and "web-api" and communicates with "backend"
// the upstream server ("backend") will see that DP with TLS certificate of URIs of "web" and "web-api".
// There is no way to correlate incoming request to "web" or "web-api" with outgoing request to "backend" to expose only one URI SAN.
//
// Pass "*" for upstreamService to validate that upstream service is a service that is part of the mesh (but not specific one)
func CreateUpstreamTlsContext(ctx xds_context.Context, metadata *core_xds.DataplaneMetadata, upstreamService string, sni string) (*envoy_auth.UpstreamTlsContext, error) {
if !ctx.Mesh.Resource.MTLSEnabled() {
return nil, nil
}
var validationSANMatcher *envoy_type_matcher.StringMatcher
if upstreamService == "*" {
validationSANMatcher = MeshSpiffeIDPrefixMatcher(ctx.Mesh.Resource.Meta.GetName())
} else {
validationSANMatcher = ServiceSpiffeIDMatcher(ctx.Mesh.Resource.Meta.GetName(), upstreamService)
}
commonTlsContext, err := createCommonTlsContext(ctx, metadata, validationSANMatcher)
if err != nil {
return nil, err
}
return &envoy_auth.UpstreamTlsContext{
CommonTlsContext: commonTlsContext,
Sni: sni,
}, nil
}
func createCommonTlsContext(ctx xds_context.Context, metadata *core_xds.DataplaneMetadata, validationSANMatcher *envoy_type_matcher.StringMatcher) (*envoy_auth.CommonTlsContext, error) {
meshCaSecret, err := sdsSecretConfig(ctx, tls.MeshCaResource, metadata)
if err != nil {
return nil, err
}
identitySecret, err := sdsSecretConfig(ctx, tls.IdentityCertResource, metadata)
if err != nil {
return nil, err
}
return &envoy_auth.CommonTlsContext{
ValidationContextType: &envoy_auth.CommonTlsContext_CombinedValidationContext{
CombinedValidationContext: &envoy_auth.CommonTlsContext_CombinedCertificateValidationContext{
DefaultValidationContext: &envoy_auth.CertificateValidationContext{
MatchSubjectAltNames: []*envoy_type_matcher.StringMatcher{validationSANMatcher},
},
ValidationContextSdsSecretConfig: meshCaSecret,
},
},
TlsCertificateSdsSecretConfigs: []*envoy_auth.SdsSecretConfig{
identitySecret,
},
}, nil
}
func sdsSecretConfig(context xds_context.Context, name string, metadata *core_xds.DataplaneMetadata) (*envoy_auth.SdsSecretConfig, error) {
withCallCredentials := func(grpc *envoy_core.GrpcService_GoogleGrpc) (*envoy_core.GrpcService_GoogleGrpc, error) {
if metadata.GetDataplaneTokenPath() == "" {
return grpc, nil
}
config := &envoy_grpc_credential.FileBasedMetadataConfig{
SecretData: &envoy_core.DataSource{
Specifier: &envoy_core.DataSource_Filename{
Filename: metadata.GetDataplaneTokenPath(),
},
},
}
typedConfig, err := proto.MarshalAnyDeterministic(config)
if err != nil {
return nil, err
}
grpc.CallCredentials = append(grpc.CallCredentials, &envoy_core.GrpcService_GoogleGrpc_CallCredentials{
CredentialSpecifier: &envoy_core.GrpcService_GoogleGrpc_CallCredentials_FromPlugin{
FromPlugin: &envoy_core.GrpcService_GoogleGrpc_CallCredentials_MetadataCredentialsFromPlugin{
Name: "envoy.grpc_credentials.file_based_metadata",
ConfigType: &envoy_core.GrpcService_GoogleGrpc_CallCredentials_MetadataCredentialsFromPlugin_TypedConfig{
TypedConfig: typedConfig,
},
},
},
})
grpc.CredentialsFactoryName = "envoy.grpc_credentials.file_based_metadata"
return grpc, nil
}
googleGrpc, err := withCallCredentials(&envoy_core.GrpcService_GoogleGrpc{
TargetUri: context.SDSLocation(),
StatPrefix: util_xds.SanitizeMetric("sds_" + name),
ChannelCredentials: &envoy_core.GrpcService_GoogleGrpc_ChannelCredentials{
CredentialSpecifier: &envoy_core.GrpcService_GoogleGrpc_ChannelCredentials_SslCredentials{
SslCredentials: &envoy_core.GrpcService_GoogleGrpc_SslCredentials{
RootCerts: &envoy_core.DataSource{
Specifier: &envoy_core.DataSource_InlineBytes{
InlineBytes: context.ControlPlane.SdsTlsCert,
},
},
},
},
},
})
if err != nil {
return nil, err
}
return &envoy_auth.SdsSecretConfig{
Name: name,
SdsConfig: &envoy_core.ConfigSource{
ConfigSourceSpecifier: &envoy_core.ConfigSource_ApiConfigSource{
ApiConfigSource: &envoy_core.ApiConfigSource{
ApiType: envoy_core.ApiConfigSource_GRPC,
GrpcServices: []*envoy_core.GrpcService{
{
TargetSpecifier: &envoy_core.GrpcService_GoogleGrpc_{
GoogleGrpc: googleGrpc,
},
},
},
},
},
},
}, nil
}
func UpstreamTlsContextOutsideMesh(ca, cert, key []byte, hostname string) (*envoy_auth.UpstreamTlsContext, error) {
var tlsCertificates []*envoy_auth.TlsCertificate
if cert != nil && key != nil {
tlsCertificates = []*envoy_auth.TlsCertificate{
{
CertificateChain: dataSourceFromBytes(cert),
PrivateKey: dataSourceFromBytes(key),
},
}
}
var validationContextType *envoy_auth.CommonTlsContext_ValidationContext
if ca != nil {
validationContextType = &envoy_auth.CommonTlsContext_ValidationContext{
ValidationContext: &envoy_auth.CertificateValidationContext{
TrustedCa: dataSourceFromBytes(ca),
MatchSubjectAltNames: []*envoy_type_matcher.StringMatcher{
{
MatchPattern: &envoy_type_matcher.StringMatcher_Exact{
Exact: hostname,
},
},
},
},
}
}
return &envoy_auth.UpstreamTlsContext{
CommonTlsContext: &envoy_auth.CommonTlsContext{
TlsCertificates: tlsCertificates,
ValidationContextType: validationContextType,
},
}, nil
}
func dataSourceFromBytes(bytes []byte) *envoy_core.DataSource {
return &envoy_core.DataSource{
Specifier: &envoy_core.DataSource_InlineBytes{
InlineBytes: bytes,
},
}
}
func MeshSpiffeIDPrefixMatcher(mesh string) *envoy_type_matcher.StringMatcher {
return &envoy_type_matcher.StringMatcher{
MatchPattern: &envoy_type_matcher.StringMatcher_Prefix{
Prefix: tls.MeshSpiffeIDPrefix(mesh),
},
}
}
func ServiceSpiffeIDMatcher(mesh string, service string) *envoy_type_matcher.StringMatcher {
return &envoy_type_matcher.StringMatcher{
MatchPattern: &envoy_type_matcher.StringMatcher_Exact{
Exact: tls.ServiceSpiffeID(mesh, service),
},
}
}