seaweedfs/weed/security/tls.go

188 lines
6 KiB
Go
Raw Normal View History

2019-02-18 20:11:52 +00:00
package security
import (
"context"
2019-02-18 20:11:52 +00:00
"crypto/tls"
"crypto/x509"
2022-06-23 18:32:15 +00:00
"google.golang.org/grpc/credentials/tls/certprovider/pemfile"
"google.golang.org/grpc/security/advancedtls"
"io/ioutil"
"strings"
2022-06-23 18:32:15 +00:00
"time"
2019-02-18 20:11:52 +00:00
grpc_auth "github.com/grpc-ecosystem/go-grpc-middleware/auth"
2019-02-18 20:11:52 +00:00
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
2019-02-18 20:11:52 +00:00
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/peer"
"google.golang.org/grpc/status"
2020-02-23 05:23:30 +00:00
"github.com/chrislusf/seaweedfs/weed/glog"
"github.com/chrislusf/seaweedfs/weed/util"
2019-02-18 20:11:52 +00:00
)
2022-06-23 19:26:56 +00:00
const credRefreshingInterval = time.Duration(5) * time.Hour
2022-06-23 18:32:15 +00:00
type Authenticator struct {
2021-03-10 09:02:13 +00:00
AllowedWildcardDomain string
AllowedCommonNames map[string]bool
}
func LoadServerTLS(config *util.ViperProxy, component string) (grpc.ServerOption, grpc.ServerOption) {
2019-02-18 20:11:52 +00:00
if config == nil {
return nil, nil
2019-02-18 20:11:52 +00:00
}
2022-06-23 18:32:15 +00:00
serverOptions := pemfile.Options{
CertFile: config.GetString(component + ".cert"),
KeyFile: config.GetString(component + ".key"),
RefreshDuration: credRefreshingInterval,
}
serverIdentityProvider, err := pemfile.NewProvider(serverOptions)
2019-02-18 20:11:52 +00:00
if err != nil {
2022-06-23 18:32:15 +00:00
glog.Warningf("pemfile.NewProvider(%v) failed: %v", serverOptions, err)
return nil, nil
2019-02-18 20:11:52 +00:00
}
2022-06-23 18:32:15 +00:00
defer serverIdentityProvider.Close()
serverRootOptions := pemfile.Options{
RootFile: config.GetString("grpc.ca"),
RefreshDuration: credRefreshingInterval,
}
serverRootProvider, err := pemfile.NewProvider(serverRootOptions)
2019-02-18 20:11:52 +00:00
if err != nil {
2022-06-23 18:32:15 +00:00
glog.Warningf("pemfile.NewProvider(%v) failed: %v", serverRootOptions, err)
return nil, nil
}
defer serverIdentityProvider.Close()
// Start a server and create a client using advancedtls API with Provider.
options := &advancedtls.ServerOptions{
IdentityOptions: advancedtls.IdentityCertificateOptions{
IdentityProvider: serverIdentityProvider,
},
RootOptions: advancedtls.RootCertificateOptions{
RootProvider: serverRootProvider,
},
2022-06-23 19:26:56 +00:00
RequireClientCert: false,
2022-06-23 18:32:15 +00:00
VerifyPeer: func(params *advancedtls.VerificationFuncParams) (*advancedtls.VerificationResults, error) {
glog.V(0).Infof("Client common name: %s.\n", params.Leaf.Subject.CommonName)
return &advancedtls.VerificationResults{}, nil
},
2022-06-23 19:26:56 +00:00
VType: advancedtls.SkipVerification,
2022-06-23 18:32:15 +00:00
}
ta, err := advancedtls.NewServerCreds(options)
if err != nil {
glog.Warningf("advancedtls.NewServerCreds(%v) failed: %v", options, err)
return nil, nil
2019-02-18 20:11:52 +00:00
}
2021-03-10 09:42:39 +00:00
allowedCommonNames := config.GetString(component + ".allowed_commonNames")
2021-03-10 09:02:13 +00:00
allowedWildcardDomain := config.GetString("grpc.allowed_wildcard_domain")
2021-03-10 09:42:39 +00:00
if allowedCommonNames != "" || allowedWildcardDomain != "" {
2021-03-10 09:02:13 +00:00
allowedCommonNamesMap := make(map[string]bool)
2021-03-10 09:42:39 +00:00
for _, s := range strings.Split(allowedCommonNames, ",") {
2021-03-10 09:02:13 +00:00
allowedCommonNamesMap[s] = true
}
auther := Authenticator{
2021-03-10 09:02:13 +00:00
AllowedCommonNames: allowedCommonNamesMap,
AllowedWildcardDomain: allowedWildcardDomain,
}
return grpc.Creds(ta), grpc.UnaryInterceptor(grpc_auth.UnaryServerInterceptor(auther.Authenticate))
}
return grpc.Creds(ta), nil
2019-02-18 20:11:52 +00:00
}
2021-01-12 10:28:13 +00:00
func LoadClientTLS(config *util.ViperProxy, component string) grpc.DialOption {
2019-02-18 20:11:52 +00:00
if config == nil {
return grpc.WithInsecure()
}
2020-11-22 12:27:15 +00:00
certFileName, keyFileName, caFileName := config.GetString(component+".cert"), config.GetString(component+".key"), config.GetString("grpc.ca")
2020-09-20 22:40:49 +00:00
if certFileName == "" || keyFileName == "" || caFileName == "" {
2020-09-20 22:38:59 +00:00
return grpc.WithInsecure()
}
2022-06-23 18:32:15 +00:00
// Initialize credential struct using reloading API.
clientOptions := pemfile.Options{
CertFile: certFileName,
KeyFile: keyFileName,
RefreshDuration: credRefreshingInterval,
}
clientProvider, err := pemfile.NewProvider(clientOptions)
2019-02-18 20:11:52 +00:00
if err != nil {
2022-06-23 18:32:15 +00:00
glog.Warningf("pemfile.NewProvider(%v) failed %v", clientOptions, err)
2019-02-18 20:11:52 +00:00
return grpc.WithInsecure()
}
2022-06-23 18:32:15 +00:00
defer clientProvider.Close()
2022-06-23 19:26:56 +00:00
clientRootOptions := pemfile.Options{
RootFile: config.GetString("grpc.ca"),
RefreshDuration: credRefreshingInterval,
}
clientRootProvider, err := pemfile.NewProvider(clientRootOptions)
if err != nil {
glog.Warningf("pemfile.NewProvider(%v) failed: %v", clientRootOptions, err)
return grpc.WithInsecure()
}
defer clientRootProvider.Close()
2022-06-23 18:32:15 +00:00
options := &advancedtls.ClientOptions{
2022-06-23 19:26:56 +00:00
IdentityOptions: advancedtls.IdentityCertificateOptions{
IdentityProvider: clientProvider,
},
2022-06-23 18:32:15 +00:00
VerifyPeer: func(params *advancedtls.VerificationFuncParams) (*advancedtls.VerificationResults, error) {
return &advancedtls.VerificationResults{}, nil
},
RootOptions: advancedtls.RootCertificateOptions{
2022-06-23 19:26:56 +00:00
RootProvider: clientRootProvider,
2022-06-23 18:32:15 +00:00
},
2022-06-23 19:26:56 +00:00
VType: advancedtls.SkipVerification,
2022-06-23 18:32:15 +00:00
}
ta, err := advancedtls.NewClientCreds(options)
2019-02-18 20:11:52 +00:00
if err != nil {
2022-06-23 18:32:15 +00:00
glog.Warningf("advancedtls.NewClientCreds(%v) failed: %v", options, err)
2019-02-18 20:11:52 +00:00
return grpc.WithInsecure()
}
return grpc.WithTransportCredentials(ta)
}
func LoadClientTLSHTTP(clientCertFile string) *tls.Config {
clientCerts, err := ioutil.ReadFile(clientCertFile)
if err != nil {
glog.Fatal(err)
}
certPool := x509.NewCertPool()
ok := certPool.AppendCertsFromPEM(clientCerts)
if !ok {
glog.Fatalf("Error processing client certificate in %s\n", clientCertFile)
}
return &tls.Config{
ClientCAs: certPool,
ClientAuth: tls.RequireAndVerifyClientCert,
}
}
func (a Authenticator) Authenticate(ctx context.Context) (newCtx context.Context, err error) {
p, ok := peer.FromContext(ctx)
if !ok {
return ctx, status.Error(codes.Unauthenticated, "no peer found")
}
tlsAuth, ok := p.AuthInfo.(credentials.TLSInfo)
if !ok {
return ctx, status.Error(codes.Unauthenticated, "unexpected peer transport credentials")
}
if len(tlsAuth.State.VerifiedChains) == 0 || len(tlsAuth.State.VerifiedChains[0]) == 0 {
return ctx, status.Error(codes.Unauthenticated, "could not verify peer certificate")
}
2021-03-10 09:42:39 +00:00
2021-03-10 09:02:13 +00:00
commonName := tlsAuth.State.VerifiedChains[0][0].Subject.CommonName
if a.AllowedWildcardDomain != "" && strings.HasSuffix(commonName, a.AllowedWildcardDomain) {
return ctx, nil
}
if _, ok := a.AllowedCommonNames[commonName]; ok {
return ctx, nil
}
2021-03-10 09:42:39 +00:00
return ctx, status.Errorf(codes.Unauthenticated, "invalid subject common name: %s", commonName)
}