2019-02-20 07:11:05 +00:00
|
|
|
package inmem
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
hclog "github.com/hashicorp/go-hclog"
|
2019-02-27 21:14:58 +00:00
|
|
|
"github.com/hashicorp/vault/command/agent/cache"
|
2019-02-20 07:11:05 +00:00
|
|
|
"github.com/hashicorp/vault/command/agent/sink"
|
2019-10-22 17:42:56 +00:00
|
|
|
"go.uber.org/atomic"
|
2019-02-20 07:11:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// inmemSink retains the auto-auth token in memory and exposes it via
|
|
|
|
// sink.SinkReader interface.
|
|
|
|
type inmemSink struct {
|
2019-02-27 21:14:58 +00:00
|
|
|
logger hclog.Logger
|
2019-10-22 17:42:56 +00:00
|
|
|
token *atomic.String
|
2019-02-27 21:14:58 +00:00
|
|
|
leaseCache *cache.LeaseCache
|
2019-02-20 07:11:05 +00:00
|
|
|
}
|
|
|
|
|
2019-02-27 21:14:58 +00:00
|
|
|
// New creates a new instance of inmemSink.
|
|
|
|
func New(conf *sink.SinkConfig, leaseCache *cache.LeaseCache) (sink.Sink, error) {
|
2019-02-20 07:11:05 +00:00
|
|
|
if conf.Logger == nil {
|
|
|
|
return nil, errors.New("nil logger provided")
|
|
|
|
}
|
2019-02-27 21:14:58 +00:00
|
|
|
|
2019-02-20 07:11:05 +00:00
|
|
|
return &inmemSink{
|
2019-02-27 21:14:58 +00:00
|
|
|
logger: conf.Logger,
|
|
|
|
leaseCache: leaseCache,
|
2019-10-22 17:42:56 +00:00
|
|
|
token: atomic.NewString(""),
|
2019-02-20 07:11:05 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *inmemSink) WriteToken(token string) error {
|
2019-10-22 17:42:56 +00:00
|
|
|
s.token.Store(token)
|
2019-02-27 21:14:58 +00:00
|
|
|
|
|
|
|
if s.leaseCache != nil {
|
|
|
|
s.leaseCache.RegisterAutoAuthToken(token)
|
|
|
|
}
|
|
|
|
|
2019-02-20 07:11:05 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *inmemSink) Token() string {
|
2019-10-22 17:42:56 +00:00
|
|
|
return s.token.Load()
|
2019-02-20 07:11:05 +00:00
|
|
|
}
|