2018-07-25 02:02:27 +00:00
|
|
|
package sink
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2021-06-02 13:22:31 +00:00
|
|
|
"fmt"
|
2018-07-25 02:02:27 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"math/rand"
|
|
|
|
"os"
|
2018-07-30 14:37:04 +00:00
|
|
|
"sync/atomic"
|
2018-07-25 02:02:27 +00:00
|
|
|
"time"
|
|
|
|
|
2019-01-09 00:48:57 +00:00
|
|
|
hclog "github.com/hashicorp/go-hclog"
|
2018-07-25 02:02:27 +00:00
|
|
|
"github.com/hashicorp/vault/api"
|
|
|
|
"github.com/hashicorp/vault/helper/dhutil"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/jsonutil"
|
2018-07-25 02:02:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Sink interface {
|
|
|
|
WriteToken(string) error
|
|
|
|
}
|
|
|
|
|
2019-02-20 07:11:05 +00:00
|
|
|
type SinkReader interface {
|
|
|
|
Token() string
|
|
|
|
}
|
|
|
|
|
2018-07-25 02:02:27 +00:00
|
|
|
type SinkConfig struct {
|
|
|
|
Sink
|
|
|
|
Logger hclog.Logger
|
|
|
|
Config map[string]interface{}
|
|
|
|
Client *api.Client
|
|
|
|
WrapTTL time.Duration
|
|
|
|
DHType string
|
|
|
|
DHPath string
|
2020-08-17 16:36:16 +00:00
|
|
|
DeriveKey bool
|
2018-07-25 02:02:27 +00:00
|
|
|
AAD string
|
|
|
|
cachedRemotePubKey []byte
|
|
|
|
cachedPubKey []byte
|
|
|
|
cachedPriKey []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
type SinkServerConfig struct {
|
2018-07-30 14:37:04 +00:00
|
|
|
Logger hclog.Logger
|
|
|
|
Client *api.Client
|
|
|
|
Context context.Context
|
|
|
|
ExitAfterAuth bool
|
2018-07-25 02:02:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SinkServer is responsible for pushing tokens to sinks
|
|
|
|
type SinkServer struct {
|
2018-07-30 14:37:04 +00:00
|
|
|
logger hclog.Logger
|
|
|
|
client *api.Client
|
|
|
|
random *rand.Rand
|
|
|
|
exitAfterAuth bool
|
|
|
|
remaining *int32
|
2018-07-25 02:02:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewSinkServer(conf *SinkServerConfig) *SinkServer {
|
|
|
|
ss := &SinkServer{
|
2018-07-30 14:37:04 +00:00
|
|
|
logger: conf.Logger,
|
|
|
|
client: conf.Client,
|
|
|
|
random: rand.New(rand.NewSource(int64(time.Now().Nanosecond()))),
|
|
|
|
exitAfterAuth: conf.ExitAfterAuth,
|
|
|
|
remaining: new(int32),
|
2018-07-25 02:02:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ss
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run executes the server's run loop, which is responsible for reading
|
|
|
|
// in new tokens and pushing them out to the various sinks.
|
2020-09-30 01:03:09 +00:00
|
|
|
func (ss *SinkServer) Run(ctx context.Context, incoming chan string, sinks []*SinkConfig) error {
|
2019-04-05 20:12:54 +00:00
|
|
|
latestToken := new(string)
|
|
|
|
writeSink := func(currSink *SinkConfig, currToken string) error {
|
|
|
|
if currToken != *latestToken {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if currSink.WrapTTL != 0 {
|
|
|
|
if currToken, err = currSink.wrapToken(ss.client, currSink.WrapTTL, currToken); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if currSink.DHType != "" {
|
|
|
|
if currToken, err = currSink.encryptToken(currToken); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return currSink.WriteToken(currToken)
|
|
|
|
}
|
|
|
|
|
2018-07-25 02:02:27 +00:00
|
|
|
if incoming == nil {
|
2020-09-30 01:03:09 +00:00
|
|
|
return errors.New("sink server: incoming channel is nil")
|
2018-07-25 02:02:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ss.logger.Info("starting sink server")
|
|
|
|
defer func() {
|
|
|
|
ss.logger.Info("sink server stopped")
|
|
|
|
}()
|
|
|
|
|
2019-04-05 20:12:54 +00:00
|
|
|
type sinkToken struct {
|
|
|
|
sink *SinkConfig
|
|
|
|
token string
|
|
|
|
}
|
|
|
|
sinkCh := make(chan sinkToken, len(sinks))
|
2018-07-25 02:02:27 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2020-09-30 01:03:09 +00:00
|
|
|
return nil
|
2018-07-25 02:02:27 +00:00
|
|
|
|
|
|
|
case token := <-incoming:
|
2020-05-26 17:52:14 +00:00
|
|
|
if len(sinks) > 0 {
|
|
|
|
if token != *latestToken {
|
|
|
|
|
|
|
|
// Drain the existing funcs
|
|
|
|
drainLoop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-sinkCh:
|
|
|
|
atomic.AddInt32(ss.remaining, -1)
|
|
|
|
default:
|
|
|
|
break drainLoop
|
|
|
|
}
|
2018-07-25 02:02:27 +00:00
|
|
|
}
|
|
|
|
|
2020-05-26 17:52:14 +00:00
|
|
|
*latestToken = token
|
2018-07-25 02:02:27 +00:00
|
|
|
|
2020-05-26 17:52:14 +00:00
|
|
|
for _, s := range sinks {
|
|
|
|
atomic.AddInt32(ss.remaining, 1)
|
|
|
|
sinkCh <- sinkToken{s, token}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ss.logger.Trace("no sinks, ignoring new token")
|
|
|
|
if ss.exitAfterAuth {
|
|
|
|
ss.logger.Trace("no sinks, exitAfterAuth, bye")
|
2020-09-30 01:03:09 +00:00
|
|
|
return nil
|
2018-07-25 02:02:27 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-05 20:12:54 +00:00
|
|
|
case st := <-sinkCh:
|
2018-07-30 14:37:04 +00:00
|
|
|
atomic.AddInt32(ss.remaining, -1)
|
2018-07-25 02:02:27 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2020-09-30 01:03:09 +00:00
|
|
|
return nil
|
2018-07-25 02:02:27 +00:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2019-04-05 20:12:54 +00:00
|
|
|
if err := writeSink(st.sink, st.token); err != nil {
|
2018-07-25 02:02:27 +00:00
|
|
|
backoff := 2*time.Second + time.Duration(ss.random.Int63()%int64(time.Second*2)-int64(time.Second))
|
|
|
|
ss.logger.Error("error returned by sink function, retrying", "error", err, "backoff", backoff.String())
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2020-09-30 01:03:09 +00:00
|
|
|
return nil
|
2018-07-25 02:02:27 +00:00
|
|
|
case <-time.After(backoff):
|
2018-07-30 14:37:04 +00:00
|
|
|
atomic.AddInt32(ss.remaining, 1)
|
2019-04-05 20:12:54 +00:00
|
|
|
sinkCh <- st
|
2018-07-25 02:02:27 +00:00
|
|
|
}
|
2018-07-30 14:37:04 +00:00
|
|
|
} else {
|
|
|
|
if atomic.LoadInt32(ss.remaining) == 0 && ss.exitAfterAuth {
|
2020-09-30 01:03:09 +00:00
|
|
|
return nil
|
2018-07-30 14:37:04 +00:00
|
|
|
}
|
2018-07-25 02:02:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SinkConfig) encryptToken(token string) (string, error) {
|
|
|
|
var aesKey []byte
|
|
|
|
var err error
|
|
|
|
resp := new(dhutil.Envelope)
|
|
|
|
switch s.DHType {
|
|
|
|
case "curve25519":
|
|
|
|
if len(s.cachedRemotePubKey) == 0 {
|
|
|
|
_, err = os.Lstat(s.DHPath)
|
|
|
|
if err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
2021-06-02 13:22:31 +00:00
|
|
|
return "", fmt.Errorf("error stat-ing dh parameters file: %w", err)
|
2018-07-25 02:02:27 +00:00
|
|
|
}
|
|
|
|
return "", errors.New("no dh parameters file found, and no cached pub key")
|
|
|
|
}
|
|
|
|
fileBytes, err := ioutil.ReadFile(s.DHPath)
|
|
|
|
if err != nil {
|
2021-06-02 13:22:31 +00:00
|
|
|
return "", fmt.Errorf("error reading file for dh parameters: %w", err)
|
2018-07-25 02:02:27 +00:00
|
|
|
}
|
|
|
|
theirPubKey := new(dhutil.PublicKeyInfo)
|
|
|
|
if err := jsonutil.DecodeJSON(fileBytes, theirPubKey); err != nil {
|
2021-06-02 13:22:31 +00:00
|
|
|
return "", fmt.Errorf("error decoding public key: %w", err)
|
2018-07-25 02:02:27 +00:00
|
|
|
}
|
|
|
|
if len(theirPubKey.Curve25519PublicKey) == 0 {
|
|
|
|
return "", errors.New("public key is nil")
|
|
|
|
}
|
|
|
|
s.cachedRemotePubKey = theirPubKey.Curve25519PublicKey
|
|
|
|
}
|
|
|
|
if len(s.cachedPubKey) == 0 {
|
|
|
|
s.cachedPubKey, s.cachedPriKey, err = dhutil.GeneratePublicPrivateKey()
|
|
|
|
if err != nil {
|
2021-06-02 13:22:31 +00:00
|
|
|
return "", fmt.Errorf("error generating pub/pri curve25519 keys: %w", err)
|
2018-07-25 02:02:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
resp.Curve25519PublicKey = s.cachedPubKey
|
|
|
|
}
|
|
|
|
|
2020-08-17 16:36:16 +00:00
|
|
|
secret, err := dhutil.GenerateSharedSecret(s.cachedPriKey, s.cachedRemotePubKey)
|
|
|
|
if err != nil {
|
2021-06-02 13:22:31 +00:00
|
|
|
return "", fmt.Errorf("error calculating shared key: %w", err)
|
2020-08-17 16:36:16 +00:00
|
|
|
}
|
|
|
|
if s.DeriveKey {
|
|
|
|
aesKey, err = dhutil.DeriveSharedKey(secret, s.cachedPubKey, s.cachedRemotePubKey)
|
|
|
|
} else {
|
|
|
|
aesKey = secret
|
|
|
|
}
|
|
|
|
|
2018-07-25 02:02:27 +00:00
|
|
|
if err != nil {
|
2021-06-02 13:22:31 +00:00
|
|
|
return "", fmt.Errorf("error deriving shared key: %w", err)
|
2018-07-25 02:02:27 +00:00
|
|
|
}
|
|
|
|
if len(aesKey) == 0 {
|
|
|
|
return "", errors.New("derived AES key is empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.EncryptedPayload, resp.Nonce, err = dhutil.EncryptAES(aesKey, []byte(token), []byte(s.AAD))
|
|
|
|
if err != nil {
|
2021-06-02 13:22:31 +00:00
|
|
|
return "", fmt.Errorf("error encrypting with shared key: %w", err)
|
2018-07-25 02:02:27 +00:00
|
|
|
}
|
|
|
|
m, err := jsonutil.EncodeJSON(resp)
|
|
|
|
if err != nil {
|
2021-06-02 13:22:31 +00:00
|
|
|
return "", fmt.Errorf("error encoding encrypted payload: %w", err)
|
2018-07-25 02:02:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return string(m), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SinkConfig) wrapToken(client *api.Client, wrapTTL time.Duration, token string) (string, error) {
|
2022-01-19 17:43:12 +00:00
|
|
|
wrapClient, err := client.CloneWithHeaders()
|
2018-07-25 02:02:27 +00:00
|
|
|
if err != nil {
|
2021-06-02 13:22:31 +00:00
|
|
|
return "", fmt.Errorf("error deriving client for wrapping, not writing out to sink: %w)", err)
|
2018-07-25 02:02:27 +00:00
|
|
|
}
|
2022-01-19 17:43:12 +00:00
|
|
|
|
2018-07-25 02:02:27 +00:00
|
|
|
wrapClient.SetToken(token)
|
|
|
|
wrapClient.SetWrappingLookupFunc(func(string, string) string {
|
|
|
|
return wrapTTL.String()
|
|
|
|
})
|
2022-01-19 17:43:12 +00:00
|
|
|
|
2018-07-25 02:02:27 +00:00
|
|
|
secret, err := wrapClient.Logical().Write("sys/wrapping/wrap", map[string]interface{}{
|
|
|
|
"token": token,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-06-02 13:22:31 +00:00
|
|
|
return "", fmt.Errorf("error wrapping token, not writing out to sink: %w)", err)
|
2018-07-25 02:02:27 +00:00
|
|
|
}
|
|
|
|
if secret == nil {
|
|
|
|
return "", errors.New("nil secret returned, not writing out to sink")
|
|
|
|
}
|
|
|
|
if secret.WrapInfo == nil {
|
|
|
|
return "", errors.New("nil wrap info returned, not writing out to sink")
|
|
|
|
}
|
|
|
|
|
|
|
|
m, err := jsonutil.EncodeJSON(secret.WrapInfo)
|
|
|
|
if err != nil {
|
2021-06-02 13:22:31 +00:00
|
|
|
return "", fmt.Errorf("error marshaling token, not writing out to sink: %w)", err)
|
2018-07-25 02:02:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return string(m), nil
|
|
|
|
}
|