Files
threshold/internal/servers/controller/worker_tls_config.go
T

89 lines
2.3 KiB
Go
Raw Normal View History

package controller
2020-03-22 20:17:23 -04:00
import (
"context"
"crypto/tls"
"crypto/x509"
"encoding/base64"
"encoding/json"
"errors"
2020-08-12 00:40:39 -04:00
"net"
2020-03-22 20:17:23 -04:00
"strings"
2020-08-10 17:15:02 -04:00
"github.com/hashicorp/boundary/internal/cmd/base"
2020-03-22 20:17:23 -04:00
wrapping "github.com/hashicorp/go-kms-wrapping"
"google.golang.org/protobuf/proto"
)
2020-08-12 00:40:39 -04:00
type workerAuthEntry struct {
*base.WorkerAuthInfo
conn net.Conn
}
2020-09-03 13:24:34 -04:00
func (c Controller) validateWorkerTls(hello *tls.ClientHelloInfo) (*tls.Config, error) {
2020-03-22 20:17:23 -04:00
for _, p := range hello.SupportedProtos {
switch {
case strings.HasPrefix(p, "v1workerauth-"):
2020-08-12 00:40:39 -04:00
tlsConf, workerInfo, err := c.v1WorkerAuthConfig(hello.SupportedProtos)
if err == nil {
// Set the info we need to prevent replays
c.workerAuthCache.Set(workerInfo.ConnectionNonce, &workerAuthEntry{
WorkerAuthInfo: workerInfo,
}, 0)
}
return tlsConf, err
2020-03-22 20:17:23 -04:00
}
}
return nil, nil
}
2020-03-22 20:17:23 -04:00
2020-08-12 00:40:39 -04:00
func (c Controller) v1WorkerAuthConfig(protos []string) (*tls.Config, *base.WorkerAuthInfo, error) {
2020-03-22 20:17:23 -04:00
var firstMatchProto string
var encString string
for _, p := range protos {
if strings.HasPrefix(p, "v1workerauth-") {
// Strip that and the number
encString += strings.TrimPrefix(p, "v1workerauth-")[3:]
if firstMatchProto == "" {
firstMatchProto = p
}
}
}
if firstMatchProto == "" {
2020-08-12 00:40:39 -04:00
return nil, nil, errors.New("no matching proto found")
2020-03-22 20:17:23 -04:00
}
marshaledEncInfo, err := base64.RawStdEncoding.DecodeString(encString)
if err != nil {
2020-08-12 00:40:39 -04:00
return nil, nil, err
2020-03-22 20:17:23 -04:00
}
encInfo := new(wrapping.EncryptedBlobInfo)
if err := proto.Unmarshal(marshaledEncInfo, encInfo); err != nil {
2020-08-12 00:40:39 -04:00
return nil, nil, err
2020-03-22 20:17:23 -04:00
}
2020-08-19 16:47:18 -04:00
marshaledInfo, err := c.conf.WorkerAuthKms.Decrypt(context.Background(), encInfo, nil)
2020-03-22 20:17:23 -04:00
if err != nil {
2020-08-12 00:40:39 -04:00
return nil, nil, err
2020-03-22 20:17:23 -04:00
}
2020-08-12 00:40:39 -04:00
info := new(base.WorkerAuthInfo)
2020-03-22 20:17:23 -04:00
if err := json.Unmarshal(marshaledInfo, info); err != nil {
2020-08-12 00:40:39 -04:00
return nil, nil, err
2020-03-22 20:17:23 -04:00
}
rootCAs := x509.NewCertPool()
if ok := rootCAs.AppendCertsFromPEM(info.CertPEM); !ok {
2020-08-12 00:40:39 -04:00
return nil, info, errors.New("unable to add ca cert to cert pool")
2020-03-22 20:17:23 -04:00
}
tlsCert, err := tls.X509KeyPair(info.CertPEM, info.KeyPEM)
if err != nil {
2020-08-12 00:40:39 -04:00
return nil, info, err
2020-03-22 20:17:23 -04:00
}
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{tlsCert},
ClientCAs: rootCAs,
ClientAuth: tls.RequireAndVerifyClientCert,
NextProtos: []string{firstMatchProto},
MinVersion: tls.VersionTLS13,
}
2020-08-12 00:40:39 -04:00
return tlsConfig, info, nil
2020-03-22 20:17:23 -04:00
}