2017-08-03 17:24:27 +00:00
|
|
|
package dynamodb
|
2015-12-19 16:34:36 +00:00
|
|
|
|
|
|
|
import (
|
2018-06-12 15:22:02 +00:00
|
|
|
"context"
|
2015-12-19 16:34:36 +00:00
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
2017-02-27 19:30:34 +00:00
|
|
|
"net/http"
|
2020-09-15 14:01:26 +00:00
|
|
|
"net/url"
|
2015-12-19 16:34:36 +00:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2018-06-16 22:21:33 +00:00
|
|
|
"github.com/go-test/deep"
|
2018-04-03 00:46:59 +00:00
|
|
|
log "github.com/hashicorp/go-hclog"
|
2019-04-22 16:26:10 +00:00
|
|
|
"github.com/hashicorp/vault/helper/testhelpers/docker"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/logging"
|
|
|
|
"github.com/hashicorp/vault/sdk/physical"
|
2016-08-19 20:45:17 +00:00
|
|
|
|
2015-12-19 16:34:36 +00:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/credentials"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/session"
|
|
|
|
"github.com/aws/aws-sdk-go/service/dynamodb"
|
2018-06-12 15:22:02 +00:00
|
|
|
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
|
2015-12-19 16:34:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDynamoDBBackend(t *testing.T) {
|
2020-09-15 14:01:26 +00:00
|
|
|
cleanup, svccfg := prepareDynamoDBTestContainer(t)
|
2017-02-27 19:30:34 +00:00
|
|
|
defer cleanup()
|
2015-12-19 16:34:36 +00:00
|
|
|
|
2020-09-15 14:01:26 +00:00
|
|
|
creds, err := svccfg.Credentials.Get()
|
2015-12-19 16:34:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
region := os.Getenv("AWS_DEFAULT_REGION")
|
|
|
|
if region == "" {
|
|
|
|
region = "us-east-1"
|
|
|
|
}
|
|
|
|
|
2018-05-10 12:25:51 +00:00
|
|
|
awsSession, err := session.NewSession(&aws.Config{
|
2020-09-15 14:01:26 +00:00
|
|
|
Credentials: svccfg.Credentials,
|
|
|
|
Endpoint: aws.String(svccfg.URL().String()),
|
2015-12-19 16:34:36 +00:00
|
|
|
Region: aws.String(region),
|
2018-05-10 12:25:51 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
conn := dynamodb.New(awsSession)
|
2015-12-19 16:34:36 +00:00
|
|
|
|
2021-04-08 16:43:39 +00:00
|
|
|
randInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
|
2015-12-19 16:34:36 +00:00
|
|
|
table := fmt.Sprintf("vault-dynamodb-testacc-%d", randInt)
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
conn.DeleteTable(&dynamodb.DeleteTableInput{
|
|
|
|
TableName: aws.String(table),
|
|
|
|
})
|
|
|
|
}()
|
|
|
|
|
2018-04-03 00:46:59 +00:00
|
|
|
logger := logging.NewVaultLogger(log.Debug)
|
2016-08-19 20:45:17 +00:00
|
|
|
|
2017-08-03 17:24:27 +00:00
|
|
|
b, err := NewDynamoDBBackend(map[string]string{
|
2015-12-19 16:34:36 +00:00
|
|
|
"access_key": creds.AccessKeyID,
|
|
|
|
"secret_key": creds.SecretAccessKey,
|
|
|
|
"session_token": creds.SessionToken,
|
|
|
|
"table": table,
|
2017-02-27 19:30:34 +00:00
|
|
|
"region": region,
|
2020-09-15 14:01:26 +00:00
|
|
|
"endpoint": svccfg.URL().String(),
|
2017-08-03 17:24:27 +00:00
|
|
|
}, logger)
|
2015-12-19 16:34:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2017-08-03 17:24:27 +00:00
|
|
|
physical.ExerciseBackend(t, b)
|
|
|
|
physical.ExerciseBackend_ListPrefix(t, b)
|
2018-06-12 15:22:02 +00:00
|
|
|
|
|
|
|
t.Run("Marshalling upgrade", func(t *testing.T) {
|
|
|
|
path := "test_key"
|
|
|
|
|
|
|
|
// Manually write to DynamoDB using the old ConvertTo function
|
|
|
|
// for marshalling data
|
|
|
|
inputEntry := &physical.Entry{
|
|
|
|
Key: path,
|
|
|
|
Value: []byte{0x0f, 0xcf, 0x4a, 0x0f, 0xba, 0x2b, 0x15, 0xf0, 0xaa, 0x75, 0x09},
|
|
|
|
}
|
|
|
|
|
|
|
|
record := DynamoDBRecord{
|
|
|
|
Path: recordPathForVaultKey(inputEntry.Key),
|
|
|
|
Key: recordKeyForVaultKey(inputEntry.Key),
|
|
|
|
Value: inputEntry.Value,
|
|
|
|
}
|
|
|
|
|
|
|
|
item, err := dynamodbattribute.ConvertToMap(record)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
request := &dynamodb.PutItemInput{
|
|
|
|
Item: item,
|
|
|
|
TableName: &table,
|
|
|
|
}
|
|
|
|
conn.PutItem(request)
|
|
|
|
|
|
|
|
// Read back the data using the normal interface which should
|
|
|
|
// handle the old marshalling format gracefully
|
|
|
|
entry, err := b.Get(context.Background(), path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2018-06-16 22:21:33 +00:00
|
|
|
if diff := deep.Equal(inputEntry, entry); diff != nil {
|
|
|
|
t.Fatal(diff)
|
2018-06-12 15:22:02 +00:00
|
|
|
}
|
|
|
|
})
|
2015-12-19 16:34:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDynamoDBHABackend(t *testing.T) {
|
2020-09-15 14:01:26 +00:00
|
|
|
cleanup, svccfg := prepareDynamoDBTestContainer(t)
|
2017-02-27 19:30:34 +00:00
|
|
|
defer cleanup()
|
2015-12-19 16:34:36 +00:00
|
|
|
|
2020-09-15 14:01:26 +00:00
|
|
|
creds, err := svccfg.Credentials.Get()
|
2015-12-19 16:34:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
region := os.Getenv("AWS_DEFAULT_REGION")
|
|
|
|
if region == "" {
|
|
|
|
region = "us-east-1"
|
|
|
|
}
|
|
|
|
|
2018-05-10 12:25:51 +00:00
|
|
|
awsSession, err := session.NewSession(&aws.Config{
|
2020-09-15 14:01:26 +00:00
|
|
|
Credentials: svccfg.Credentials,
|
|
|
|
Endpoint: aws.String(svccfg.URL().String()),
|
2015-12-19 16:34:36 +00:00
|
|
|
Region: aws.String(region),
|
2018-05-10 12:25:51 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
conn := dynamodb.New(awsSession)
|
2015-12-19 16:34:36 +00:00
|
|
|
|
2021-04-08 16:43:39 +00:00
|
|
|
randInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
|
2015-12-19 16:34:36 +00:00
|
|
|
table := fmt.Sprintf("vault-dynamodb-testacc-%d", randInt)
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
conn.DeleteTable(&dynamodb.DeleteTableInput{
|
|
|
|
TableName: aws.String(table),
|
|
|
|
})
|
|
|
|
}()
|
|
|
|
|
2018-04-03 00:46:59 +00:00
|
|
|
logger := logging.NewVaultLogger(log.Debug)
|
2018-11-01 17:31:09 +00:00
|
|
|
config := map[string]string{
|
2015-12-19 16:34:36 +00:00
|
|
|
"access_key": creds.AccessKeyID,
|
|
|
|
"secret_key": creds.SecretAccessKey,
|
|
|
|
"session_token": creds.SessionToken,
|
|
|
|
"table": table,
|
2017-02-27 19:30:34 +00:00
|
|
|
"region": region,
|
2020-09-15 14:01:26 +00:00
|
|
|
"endpoint": svccfg.URL().String(),
|
2018-11-01 17:31:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
b, err := NewDynamoDBBackend(config, logger)
|
2015-12-19 16:34:36 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-11-01 17:31:09 +00:00
|
|
|
b2, err := NewDynamoDBBackend(config, logger)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
2015-12-19 16:34:36 +00:00
|
|
|
}
|
2018-11-01 17:31:09 +00:00
|
|
|
|
|
|
|
physical.ExerciseHABackend(t, b.(physical.HABackend), b2.(physical.HABackend))
|
|
|
|
testDynamoDBLockTTL(t, b.(physical.HABackend))
|
2019-04-03 13:57:32 +00:00
|
|
|
testDynamoDBLockRenewal(t, b.(physical.HABackend))
|
2017-02-27 19:30:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Similar to testHABackend, but using internal implementation details to
|
|
|
|
// trigger the lock failure scenario by setting the lock renew period for one
|
|
|
|
// of the locks to a higher value than the lock TTL.
|
2017-08-03 17:24:27 +00:00
|
|
|
func testDynamoDBLockTTL(t *testing.T, ha physical.HABackend) {
|
2017-02-27 19:30:34 +00:00
|
|
|
// Set much smaller lock times to speed up the test.
|
|
|
|
lockTTL := time.Second * 3
|
|
|
|
renewInterval := time.Second * 1
|
|
|
|
watchInterval := time.Second * 1
|
|
|
|
|
|
|
|
// Get the lock
|
|
|
|
origLock, err := ha.LockWith("dynamodbttl", "bar")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
// set the first lock renew period to double the expected TTL.
|
|
|
|
lock := origLock.(*DynamoDBLock)
|
|
|
|
lock.renewInterval = lockTTL * 2
|
|
|
|
lock.ttl = lockTTL
|
|
|
|
lock.watchRetryInterval = watchInterval
|
|
|
|
|
|
|
|
// Attempt to lock
|
|
|
|
leaderCh, err := lock.Lock(nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if leaderCh == nil {
|
|
|
|
t.Fatalf("failed to get leader ch")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the value
|
|
|
|
held, val, err := lock.Value()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if !held {
|
|
|
|
t.Fatalf("should be held")
|
|
|
|
}
|
|
|
|
if val != "bar" {
|
|
|
|
t.Fatalf("bad value: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Second acquisition should succeed because the first lock should
|
|
|
|
// not renew within the 3 sec TTL.
|
|
|
|
origLock2, err := ha.LockWith("dynamodbttl", "baz")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
lock2 := origLock2.(*DynamoDBLock)
|
|
|
|
lock2.renewInterval = renewInterval
|
|
|
|
lock2.ttl = lockTTL
|
|
|
|
lock2.watchRetryInterval = watchInterval
|
|
|
|
|
2019-10-14 15:06:02 +00:00
|
|
|
// Cancel attempt eventually so as not to block unit tests forever
|
2017-02-27 19:30:34 +00:00
|
|
|
stopCh := make(chan struct{})
|
2019-10-14 15:06:02 +00:00
|
|
|
time.AfterFunc(lockTTL*10, func() {
|
2017-02-27 19:30:34 +00:00
|
|
|
close(stopCh)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Attempt to lock should work
|
|
|
|
leaderCh2, err := lock2.Lock(stopCh)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if leaderCh2 == nil {
|
|
|
|
t.Fatalf("should get leader ch")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the value
|
|
|
|
held, val, err = lock2.Value()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if !held {
|
|
|
|
t.Fatalf("should be held")
|
|
|
|
}
|
|
|
|
if val != "baz" {
|
|
|
|
t.Fatalf("bad value: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The first lock should have lost the leader channel
|
|
|
|
leaderChClosed := false
|
|
|
|
blocking := make(chan struct{})
|
|
|
|
// Attempt to read from the leader or the blocking channel, which ever one
|
|
|
|
// happens first.
|
|
|
|
go func() {
|
|
|
|
select {
|
2017-03-04 21:59:00 +00:00
|
|
|
case <-time.After(watchInterval * 3):
|
|
|
|
return
|
2017-02-27 19:30:34 +00:00
|
|
|
case <-leaderCh:
|
|
|
|
leaderChClosed = true
|
|
|
|
close(blocking)
|
|
|
|
case <-blocking:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
<-blocking
|
|
|
|
if !leaderChClosed {
|
|
|
|
t.Fatalf("original lock did not have its leader channel closed.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
lock2.Unlock()
|
|
|
|
}
|
|
|
|
|
2019-04-02 23:19:21 +00:00
|
|
|
// Similar to testHABackend, but using internal implementation details to
|
|
|
|
// trigger a renewal before a "watch" check, which has been a source of
|
|
|
|
// race conditions.
|
2019-04-03 13:57:32 +00:00
|
|
|
func testDynamoDBLockRenewal(t *testing.T, ha physical.HABackend) {
|
2019-04-02 23:19:21 +00:00
|
|
|
renewInterval := time.Second * 1
|
|
|
|
watchInterval := time.Second * 5
|
|
|
|
|
|
|
|
// Get the lock
|
|
|
|
origLock, err := ha.LockWith("dynamodbrenewal", "bar")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// customize the renewal and watch intervals
|
|
|
|
lock := origLock.(*DynamoDBLock)
|
|
|
|
lock.renewInterval = renewInterval
|
|
|
|
lock.watchRetryInterval = watchInterval
|
|
|
|
|
|
|
|
// Attempt to lock
|
|
|
|
leaderCh, err := lock.Lock(nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if leaderCh == nil {
|
|
|
|
t.Fatalf("failed to get leader ch")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the value
|
|
|
|
held, val, err := lock.Value()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if !held {
|
|
|
|
t.Fatalf("should be held")
|
|
|
|
}
|
|
|
|
if val != "bar" {
|
|
|
|
t.Fatalf("bad value: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Release the lock, which will delete the stored item
|
|
|
|
if err := lock.Unlock(); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait longer than the renewal time, but less than the watch time
|
|
|
|
time.Sleep(1500 * time.Millisecond)
|
|
|
|
|
|
|
|
// Attempt to lock with new lock
|
|
|
|
newLock, err := ha.LockWith("dynamodbrenewal", "baz")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cancel attempt in 6 sec so as not to block unit tests forever
|
|
|
|
stopCh := make(chan struct{})
|
|
|
|
time.AfterFunc(6*time.Second, func() {
|
|
|
|
close(stopCh)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Attempt to lock should work
|
|
|
|
leaderCh2, err := newLock.Lock(stopCh)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if leaderCh2 == nil {
|
|
|
|
t.Fatalf("should get leader ch")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the value
|
|
|
|
held, val, err = newLock.Value()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if !held {
|
|
|
|
t.Fatalf("should be held")
|
|
|
|
}
|
|
|
|
if val != "baz" {
|
|
|
|
t.Fatalf("bad value: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
newLock.Unlock()
|
|
|
|
}
|
|
|
|
|
2020-09-15 14:01:26 +00:00
|
|
|
type Config struct {
|
|
|
|
docker.ServiceURL
|
|
|
|
Credentials *credentials.Credentials
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ docker.ServiceConfig = &Config{}
|
|
|
|
|
|
|
|
func prepareDynamoDBTestContainer(t *testing.T) (func(), *Config) {
|
2017-02-27 19:30:34 +00:00
|
|
|
// If environment variable is set, assume caller wants to target a real
|
|
|
|
// DynamoDB.
|
2020-09-15 14:01:26 +00:00
|
|
|
if endpoint := os.Getenv("AWS_DYNAMODB_ENDPOINT"); endpoint != "" {
|
|
|
|
s, err := docker.NewServiceURLParse(endpoint)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return func() {}, &Config{*s, credentials.NewEnvCredentials()}
|
2017-02-27 19:30:34 +00:00
|
|
|
}
|
|
|
|
|
2020-09-15 14:01:26 +00:00
|
|
|
runner, err := docker.NewServiceRunner(docker.RunOptions{
|
2022-11-02 17:33:17 +00:00
|
|
|
ImageRepo: "docker.mirror.hashicorp.services/cnadiminti/dynamodb-local",
|
2020-09-15 14:01:26 +00:00
|
|
|
ImageTag: "latest",
|
|
|
|
ContainerName: "dynamodb",
|
|
|
|
Ports: []string{"8000/tcp"},
|
|
|
|
})
|
2017-02-27 19:30:34 +00:00
|
|
|
if err != nil {
|
2020-09-15 14:01:26 +00:00
|
|
|
t.Fatalf("Could not start local DynamoDB: %s", err)
|
2017-02-27 19:30:34 +00:00
|
|
|
}
|
|
|
|
|
2020-09-15 14:01:26 +00:00
|
|
|
svc, err := runner.StartService(context.Background(), connectDynamoDB)
|
2017-02-27 19:30:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not start local DynamoDB: %s", err)
|
|
|
|
}
|
|
|
|
|
2020-09-15 14:01:26 +00:00
|
|
|
return svc.Cleanup, svc.Config.(*Config)
|
|
|
|
}
|
2017-02-27 19:30:34 +00:00
|
|
|
|
2020-09-15 14:01:26 +00:00
|
|
|
func connectDynamoDB(ctx context.Context, host string, port int) (docker.ServiceConfig, error) {
|
|
|
|
u := url.URL{
|
|
|
|
Scheme: "http",
|
|
|
|
Host: fmt.Sprintf("%s:%d", host, port),
|
2017-02-27 19:30:34 +00:00
|
|
|
}
|
2020-09-15 14:01:26 +00:00
|
|
|
resp, err := http.Get(u.String())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if resp.StatusCode != 400 {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Config{
|
|
|
|
ServiceURL: *docker.NewServiceURL(u),
|
|
|
|
Credentials: credentials.NewStaticCredentials("fake", "fake", ""),
|
|
|
|
}, nil
|
2015-12-19 16:34:36 +00:00
|
|
|
}
|