-
Notifications
You must be signed in to change notification settings - Fork 11
/
server.go
98 lines (87 loc) · 3.27 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
/* Copyright (c) 2019 Snowflake Inc. All rights reserved.
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 mtls
import (
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"github.com/Snowflake-Labs/sansshell/telemetry/metrics"
"github.com/go-logr/logr"
"google.golang.org/grpc/credentials"
)
// LoadServerCredentials returns transport credentials for a SansShell server as
// retrieved from the specified `loaderName`. This should be the most commonly
// used method to generate credentials as this will support reloadable credentials
// as the TransportCredentials returned are a WrappedTransportCredentials which
// will check at call time if new certificates are available.
func LoadServerCredentials(ctx context.Context, loaderName string) (credentials.TransportCredentials, error) {
logger := logr.FromContextOrDiscard(ctx)
recorder := metrics.RecorderFromContextOrNoop(ctx)
mtlsLoader, err := Loader(loaderName)
if err != nil {
return nil, err
}
creds, err := internalLoadServerCredentials(ctx, loaderName)
if err != nil {
return nil, err
}
wrapped := &WrappedTransportCredentials{
creds: creds,
loaderName: loaderName,
loader: internalLoadServerCredentials,
mtlsLoader: mtlsLoader,
logger: logger,
recorder: recorder,
}
return wrapped, nil
}
func internalLoadServerCredentials(ctx context.Context, loaderName string) (credentials.TransportCredentials, error) {
logger := logr.FromContextOrDiscard(ctx)
loader, err := Loader(loaderName)
if err != nil {
return nil, err
}
pool, err := loader.LoadClientCA(ctx)
if err != nil {
return nil, err
}
logger.Info("loading new server cert")
cert, err := loader.LoadServerCertificate(ctx)
if err != nil {
return nil, err
}
logger.Info("loaded new server cert", "error", err)
return NewServerCredentials(cert, pool), nil
}
// NewServerCredentials creates transport credentials for a SansShell server.
// NOTE: This doesn't support reloadable credentials.
func NewServerCredentials(cert tls.Certificate, CAPool *x509.CertPool) credentials.TransportCredentials {
return credentials.NewTLS(&tls.Config{
ClientAuth: tls.RequireAndVerifyClientCert,
Certificates: []tls.Certificate{cert},
ClientCAs: CAPool,
MinVersion: tls.VersionTLS13,
})
}
// LoadServerTLS reads the certificates and keys from disk at the supplied paths,
// and assembles them into a set of TransportCredentials for the gRPC server.
// NOTE: This doesn't support reloadable credentials.
func LoadServerTLS(clientCertFile, clientKeyFile string, CAPool *x509.CertPool) (credentials.TransportCredentials, error) {
// Read in client credentials
cert, err := tls.LoadX509KeyPair(clientCertFile, clientKeyFile)
if err != nil {
return nil, fmt.Errorf("reading client credentials: %w", err)
}
return NewServerCredentials(cert, CAPool), nil
}