test(e2e): Add database migration test (#2627)
* test(e2e): Add database migration test * chore(enos): Add enos scenario for migration test * chore(enos): Update go test command to use count option * doc(e2e): Update readme with migration test details * Fix: Typo * CR: Use different library * CR: Add additional context in comment * CR: Move some code from test into functions * doc(e2e): Add instructions to more easily retrieve environment variables * refact(e2e): Create some more methods to increase test readability
This commit is contained in:
1 parent
49909a331c
commit
1596b02e53
15 files changed
+900
-51
No files matched your search
@@ -0,0 +1,23 @@
|
||||
package database_test
|
||||
|
||||
import "github.com/kelseyhightower/envconfig"
|
||||
|
||||
type config struct {
|
||||
TargetSshUser string `envconfig:"E2E_SSH_USER" required:"true"` // e.g. ubuntu
|
||||
TargetSshKeyPath string `envconfig:"E2E_SSH_KEY_PATH" required:"true"` // e.g. /Users/username/key.pem
|
||||
TargetPort string `envconfig:"E2E_SSH_PORT" default:"22"`
|
||||
VaultSecretPath string `envconfig:"E2E_VAULT_SECRET_PATH" default:"e2e_secrets"`
|
||||
AwsAccessKeyId string `envconfig:"E2E_AWS_ACCESS_KEY_ID" required:"true"`
|
||||
AwsSecretAccessKey string `envconfig:"E2E_AWS_SECRET_ACCESS_KEY" required:"true"`
|
||||
AwsHostSetFilter string `envconfig:"E2E_AWS_HOST_SET_FILTER" required:"true"` // e.g. "tag:testtag=true"
|
||||
}
|
||||
|
||||
func loadConfig() (*config, error) {
|
||||
var c config
|
||||
err := envconfig.Process("", &c)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &c, nil
|
||||
}
|
||||
@@ -0,0 +1,297 @@
|
||||
package database_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/boundary/api/credentiallibraries"
|
||||
"github.com/hashicorp/boundary/testing/internal/e2e"
|
||||
"github.com/hashicorp/boundary/testing/internal/e2e/boundary"
|
||||
"github.com/hashicorp/boundary/testing/internal/e2e/infra"
|
||||
"github.com/hashicorp/boundary/testing/internal/e2e/vault"
|
||||
_ "github.com/jackc/pgx/v4/stdlib"
|
||||
"github.com/ory/dockertest/v3"
|
||||
"github.com/ory/dockertest/v3/docker"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
type TestEnvironment struct {
|
||||
Pool *dockertest.Pool
|
||||
Network *dockertest.Network
|
||||
Boundary *infra.Container
|
||||
Database *infra.Container
|
||||
Target *infra.Container
|
||||
DbInitInfo boundary.DbInitInfo
|
||||
}
|
||||
|
||||
// TestDatabaseMigration tests migrating the Boundary database from the latest released version to
|
||||
// the version under test. It creates a database, populates the database with a number of resources,
|
||||
// and uses the Boundary version under test to migrate the database.
|
||||
func TestDatabaseMigration(t *testing.T) {
|
||||
e2e.MaybeSkipTest(t)
|
||||
c, err := loadConfig()
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := context.Background()
|
||||
te := setupEnvironment(t, ctx, c)
|
||||
populateBoundaryDatabase(t, ctx, c, te)
|
||||
|
||||
// Migrate database
|
||||
t.Log("Stopping boundary before migrating...")
|
||||
err = te.Pool.Client.StopContainer(te.Boundary.Resource.Container.ID, 10)
|
||||
require.NoError(t, err)
|
||||
|
||||
bonfigFilePath, err := filepath.Abs("testdata/boundary-config.hcl")
|
||||
require.NoError(t, err)
|
||||
|
||||
t.Log("Starting database migration...")
|
||||
output := e2e.RunCommand(ctx, "boundary",
|
||||
e2e.WithArgs("database", "migrate", "-config", bonfigFilePath),
|
||||
e2e.WithEnv("BOUNDARY_POSTGRES_URL", te.Database.UriLocalhost),
|
||||
)
|
||||
require.NoError(t, output.Err, string(output.Stderr))
|
||||
t.Logf("%s", output.Stdout)
|
||||
t.Logf("Migration Output: %s", output.Stderr)
|
||||
}
|
||||
|
||||
func setupEnvironment(t testing.TB, ctx context.Context, c *config) TestEnvironment {
|
||||
pool, err := dockertest.NewPool("")
|
||||
require.NoError(t, err)
|
||||
err = pool.Client.Ping()
|
||||
require.NoError(t, err)
|
||||
|
||||
// This ensures that the latest Boundary image is used
|
||||
err = pool.Client.PullImage(docker.PullImageOptions{
|
||||
Repository: "hashicorp/boundary",
|
||||
Tag: "latest",
|
||||
}, docker.AuthConfiguration{})
|
||||
require.NoError(t, err)
|
||||
|
||||
// Set up docker network
|
||||
network, err := pool.CreateNetwork(t.Name())
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() {
|
||||
require.NoError(t, pool.RemoveNetwork(network))
|
||||
})
|
||||
|
||||
// Start a Boundary database and wait until it's ready
|
||||
db := infra.StartBoundaryDatabase(t, pool, network)
|
||||
t.Cleanup(func() {
|
||||
pool.Purge(db.Resource)
|
||||
})
|
||||
|
||||
pool.MaxWait = 10 * time.Second
|
||||
t.Log("Waiting for database to load...")
|
||||
err = pool.Retry(func() error {
|
||||
db, err := sql.Open("pgx", db.UriLocalhost)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return db.Ping()
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
// Create a target
|
||||
target := infra.StartOpenSshServer(t, pool, network, c.TargetSshUser, c.TargetSshKeyPath)
|
||||
t.Cleanup(func() {
|
||||
pool.Purge(target.Resource)
|
||||
})
|
||||
|
||||
// Initialize the database and extract resulting information
|
||||
dbInit := infra.InitBoundaryDatabase(t, pool, network, db.UriNetwork)
|
||||
t.Cleanup(func() {
|
||||
pool.Purge(dbInit.Resource)
|
||||
})
|
||||
dbInitInfo := infra.GetDbInitInfoFromContainer(t, pool, dbInit)
|
||||
|
||||
// Start a Boundary server and wait until Boundary has finished loading
|
||||
b := infra.StartBoundary(t, pool, network, db.UriNetwork)
|
||||
t.Cleanup(func() {
|
||||
pool.Purge(b.Resource)
|
||||
})
|
||||
|
||||
buf := bytes.NewBuffer(nil)
|
||||
ebuf := bytes.NewBuffer(nil)
|
||||
_, err = b.Resource.Exec([]string{"boundary", "version"}, dockertest.ExecOptions{
|
||||
StdOut: buf,
|
||||
StdErr: ebuf,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.Empty(t, ebuf)
|
||||
t.Logf("Using Boundary Version: %s", buf.String())
|
||||
|
||||
t.Log("Waiting for Boundary to finish loading...")
|
||||
err = pool.Retry(func() error {
|
||||
response, err := http.Get(b.UriLocalhost)
|
||||
if err != nil {
|
||||
t.Logf("Could not access Boundary URL: %s. Retrying...", err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
if response.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("Could not connect to %s. Status Code: %d", b.UriLocalhost, response.StatusCode)
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
return TestEnvironment{
|
||||
Pool: pool,
|
||||
Network: network,
|
||||
Boundary: b,
|
||||
Database: db,
|
||||
Target: target,
|
||||
DbInitInfo: dbInitInfo,
|
||||
}
|
||||
}
|
||||
|
||||
func populateBoundaryDatabase(t testing.TB, ctx context.Context, c *config, te TestEnvironment) {
|
||||
os.Setenv("BOUNDARY_ADDR", te.Boundary.UriLocalhost)
|
||||
os.Setenv("E2E_PASSWORD_AUTH_METHOD_ID", te.DbInitInfo.AuthMethod.AuthMethodId)
|
||||
os.Setenv("E2E_PASSWORD_ADMIN_LOGIN_NAME", te.DbInitInfo.AuthMethod.LoginName)
|
||||
os.Setenv("E2E_PASSWORD_ADMIN_PASSWORD", te.DbInitInfo.AuthMethod.Password)
|
||||
|
||||
// Create resources for target. Uses the local CLI so that these methods can be reused.
|
||||
// While the CLI version used won't necessarily match the controller version, it should be (and is
|
||||
// supposed to be) backwards ompatible
|
||||
boundary.AuthenticateAdminCli(t, ctx)
|
||||
newOrgId := boundary.CreateNewOrgCli(t, ctx)
|
||||
newProjectId := boundary.CreateNewProjectCli(t, ctx, newOrgId)
|
||||
newHostCatalogId := boundary.CreateNewHostCatalogCli(t, ctx, newProjectId)
|
||||
newHostSetId := boundary.CreateNewHostSetCli(t, ctx, newHostCatalogId)
|
||||
newHostId := boundary.CreateNewHostCli(t, ctx, newHostCatalogId, te.Target.UriNetwork)
|
||||
boundary.AddHostToHostSetCli(t, ctx, newHostSetId, newHostId)
|
||||
newTargetId := boundary.CreateNewTargetCli(t, ctx, newProjectId, "2222") // openssh-server uses port 2222
|
||||
boundary.AddHostSourceToTargetCli(t, ctx, newTargetId, newHostSetId)
|
||||
|
||||
// Create AWS dynamic host catalog
|
||||
newAwsHostCatalogId := boundary.CreateNewAwsHostCatalogCli(t, ctx, newProjectId, c.AwsAccessKeyId, c.AwsSecretAccessKey)
|
||||
newAwsHostSetId := boundary.CreateNewAwsHostSetCli(t, ctx, newAwsHostCatalogId, c.AwsHostSetFilter)
|
||||
boundary.WaitForHostsInHostSetCli(t, ctx, newAwsHostSetId)
|
||||
|
||||
// Create static credentials
|
||||
newCredentialStoreId := boundary.CreateNewCredentialStoreStaticCli(t, ctx, newProjectId)
|
||||
boundary.CreateNewStaticCredentialPasswordCli(t, ctx, newCredentialStoreId, c.TargetSshUser, "password")
|
||||
boundary.CreateNewStaticCredentialJsonCli(t, ctx, newCredentialStoreId, "testdata/credential.json")
|
||||
newCredentialsId := boundary.CreateNewStaticCredentialPrivateKeyCli(t, ctx, newCredentialStoreId, c.TargetSshUser, c.TargetSshKeyPath)
|
||||
boundary.AddCredentialSourceToTargetCli(t, ctx, newTargetId, newCredentialsId)
|
||||
|
||||
// Create vault credentials
|
||||
vaultAddr, boundaryPolicyName, kvPolicyFilePath := vault.Setup(t)
|
||||
t.Cleanup(func() {
|
||||
output := e2e.RunCommand(ctx, "vault",
|
||||
e2e.WithArgs("policy", "delete", boundaryPolicyName),
|
||||
)
|
||||
require.NoError(t, output.Err, string(output.Stderr))
|
||||
})
|
||||
output := e2e.RunCommand(ctx, "vault",
|
||||
e2e.WithArgs("secrets", "enable", "-path="+c.VaultSecretPath, "kv-v2"),
|
||||
)
|
||||
require.NoError(t, output.Err, string(output.Stderr))
|
||||
t.Cleanup(func() {
|
||||
output := e2e.RunCommand(ctx, "vault",
|
||||
e2e.WithArgs("secrets", "disable", c.VaultSecretPath),
|
||||
)
|
||||
require.NoError(t, output.Err, string(output.Stderr))
|
||||
})
|
||||
privateKeySecretName := vault.CreateKvPrivateKeyCredential(t, c.VaultSecretPath, c.TargetSshUser, c.TargetSshKeyPath, kvPolicyFilePath)
|
||||
kvPolicyName := vault.WritePolicy(t, ctx, kvPolicyFilePath)
|
||||
t.Cleanup(func() {
|
||||
output := e2e.RunCommand(ctx, "vault",
|
||||
e2e.WithArgs("policy", "delete", kvPolicyName),
|
||||
)
|
||||
require.NoError(t, output.Err, string(output.Stderr))
|
||||
})
|
||||
t.Log("Created Vault Credential")
|
||||
output = e2e.RunCommand(ctx, "vault",
|
||||
e2e.WithArgs(
|
||||
"token", "create",
|
||||
"-no-default-policy=true",
|
||||
"-policy="+boundaryPolicyName,
|
||||
"-policy="+kvPolicyName,
|
||||
"-orphan=true",
|
||||
"-period=20m",
|
||||
"-renewable=true",
|
||||
"-format=json",
|
||||
),
|
||||
)
|
||||
require.NoError(t, output.Err, string(output.Stderr))
|
||||
var tokenCreateResult vault.CreateTokenResponse
|
||||
err := json.Unmarshal(output.Stdout, &tokenCreateResult)
|
||||
require.NoError(t, err)
|
||||
credStoreToken := tokenCreateResult.Auth.Client_Token
|
||||
t.Log("Created Vault Cred Store Token")
|
||||
newVaultCredentialStoreId := boundary.CreateNewCredentialStoreVaultCli(t, ctx, newProjectId, vaultAddr, credStoreToken)
|
||||
output = e2e.RunCommand(ctx, "boundary",
|
||||
e2e.WithArgs(
|
||||
"credential-libraries", "create", "vault",
|
||||
"-credential-store-id", newVaultCredentialStoreId,
|
||||
"-vault-path", c.VaultSecretPath+"/data/"+privateKeySecretName,
|
||||
"-name", "e2e Automated Test Vault Credential Library",
|
||||
"-credential-type", "ssh_private_key",
|
||||
"-format", "json",
|
||||
),
|
||||
)
|
||||
require.NoError(t, output.Err, string(output.Stderr))
|
||||
var newCredentialLibraryResult credentiallibraries.CredentialLibraryCreateResult
|
||||
err = json.Unmarshal(output.Stdout, &newCredentialLibraryResult)
|
||||
require.NoError(t, err)
|
||||
newCredentialLibraryId := newCredentialLibraryResult.Item.Id
|
||||
t.Logf("Created Credential Library: %s", newCredentialLibraryId)
|
||||
|
||||
// Create a session. Uses Boundary in a docker container to do the connect in order to avoid
|
||||
// modifying the runner's /etc/hosts file. Otherwise, you would need to add a `127.0.0.1
|
||||
// localhost boundary` entry into /etc/hosts.
|
||||
buf := bytes.NewBuffer(nil)
|
||||
ebuf := bytes.NewBuffer(nil)
|
||||
_, err = te.Boundary.Resource.Exec(
|
||||
[]string{
|
||||
"boundary", "authenticate", "password",
|
||||
"-addr", te.Boundary.UriNetwork,
|
||||
"-auth-method-id", te.DbInitInfo.AuthMethod.AuthMethodId,
|
||||
"-login-name", te.DbInitInfo.AuthMethod.LoginName,
|
||||
"-password", "env://E2E_TEST_BOUNDARY_PASSWORD",
|
||||
"-format", "json",
|
||||
},
|
||||
dockertest.ExecOptions{
|
||||
StdOut: buf,
|
||||
StdErr: ebuf,
|
||||
Env: []string{"E2E_TEST_BOUNDARY_PASSWORD=" + te.DbInitInfo.AuthMethod.Password},
|
||||
},
|
||||
)
|
||||
require.NoError(t, err)
|
||||
require.Empty(t, ebuf)
|
||||
var authenticationResult boundary.AuthenticateCliOutput
|
||||
err = json.Unmarshal(buf.Bytes(), &authenticationResult)
|
||||
require.NoError(t, err)
|
||||
auth_token := authenticationResult.Item.Attributes["token"].(string)
|
||||
|
||||
connectTarget := infra.ConnectToTarget(t, te.Pool, te.Network, te.Boundary.UriNetwork, auth_token, newTargetId)
|
||||
t.Cleanup(func() {
|
||||
te.Pool.Purge(connectTarget.Resource)
|
||||
})
|
||||
_, err = te.Pool.Client.WaitContainer(connectTarget.Resource.Container.ID)
|
||||
require.NoError(t, err)
|
||||
buf = bytes.NewBuffer(nil)
|
||||
ebuf = bytes.NewBuffer(nil)
|
||||
err = te.Pool.Client.Logs(docker.LogsOptions{
|
||||
Container: connectTarget.Resource.Container.ID,
|
||||
OutputStream: buf,
|
||||
ErrorStream: ebuf,
|
||||
Follow: true,
|
||||
Stdout: true,
|
||||
Stderr: true,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.Empty(t, ebuf)
|
||||
t.Log("Created session")
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
disable_mlock = true
|
||||
|
||||
controller {
|
||||
name = "docker-controller"
|
||||
description = "A controller for a docker demo!"
|
||||
|
||||
database {
|
||||
url = "env://BOUNDARY_POSTGRES_URL"
|
||||
}
|
||||
}
|
||||
|
||||
worker {
|
||||
name = "boundary-colocated-worker"
|
||||
description = "A worker that runs alongside the controller in the same process"
|
||||
address = "boundary:9201"
|
||||
}
|
||||
|
||||
listener "tcp" {
|
||||
address = "boundary:9200"
|
||||
purpose = "api"
|
||||
tls_disable = true
|
||||
}
|
||||
|
||||
listener "tcp" {
|
||||
address = "boundary:9201"
|
||||
purpose = "cluster"
|
||||
tls_disable = true
|
||||
}
|
||||
|
||||
listener "tcp" {
|
||||
address = "boundary:9202"
|
||||
purpose = "proxy"
|
||||
tls_disable = true
|
||||
}
|
||||
|
||||
listener "tcp" {
|
||||
address = "boundary:9203"
|
||||
purpose = "ops"
|
||||
tls_disable = true
|
||||
}
|
||||
|
||||
kms "aead" {
|
||||
purpose = "root"
|
||||
aead_type = "aes-gcm"
|
||||
key = "sP1fnF5Xz85RrXyELHFeZg9Ad2qt4Z4bgNHVGtD6ung="
|
||||
key_id = "global_root"
|
||||
}
|
||||
|
||||
kms "aead" {
|
||||
purpose = "worker-auth"
|
||||
aead_type = "aes-gcm"
|
||||
key = "8fZBjCUfN0TzjEGLQldGY4+iE9AkOvCfjh7+p0GtRBQ="
|
||||
key_id = "global_worker-auth"
|
||||
}
|
||||
|
||||
kms "aead" {
|
||||
purpose = "recovery"
|
||||
aead_type = "aes-gcm"
|
||||
key = "8fZBjCUfN0TzjEGLQldGY4+iE9AkOvCfjh7+p0GtRBQ="
|
||||
key_id = "global_recovery"
|
||||
}
|
||||
|
||||
events {
|
||||
audit_enabled = true
|
||||
observations_enabled = true
|
||||
sysevents_enabled = true
|
||||
|
||||
sink "stderr" {
|
||||
name = "all-events"
|
||||
description = "All events sent to stderr"
|
||||
event_types = ["*"]
|
||||
format = "cloudevents-json"
|
||||
|
||||
deny_filters = [
|
||||
"\"/data/request_info/method\" contains \"Status\"",
|
||||
"\"/data/request_info/path\" contains \"/health\"",
|
||||
]
|
||||
}
|
||||
|
||||
sink {
|
||||
name = "audit-sink"
|
||||
description = "Audit sent to a file"
|
||||
event_types = ["audit"]
|
||||
format = "cloudevents-json"
|
||||
|
||||
deny_filters = [
|
||||
"\"/data/request_info/method\" contains \"Status\"",
|
||||
]
|
||||
|
||||
file {
|
||||
path = "/logs"
|
||||
file_name = "audit.log"
|
||||
}
|
||||
|
||||
audit_config {
|
||||
audit_filter_overrides {
|
||||
secret = "encrypt"
|
||||
sensitive = "hmac-sha256"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
path "auth/token/lookup-self" {
|
||||
capabilities = ["read"]
|
||||
}
|
||||
|
||||
path "auth/token/renew-self" {
|
||||
capabilities = ["update"]
|
||||
}
|
||||
|
||||
path "auth/token/revoke-self" {
|
||||
capabilities = ["update"]
|
||||
}
|
||||
|
||||
path "sys/leases/renew" {
|
||||
capabilities = ["update"]
|
||||
}
|
||||
|
||||
path "sys/leases/revoke" {
|
||||
capabilities = ["update"]
|
||||
}
|
||||
|
||||
path "sys/capabilities-self" {
|
||||
capabilities = ["update"]
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"a": "test",
|
||||
"b": "foo"
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
listen_addresses = '*'
|
||||
max_connections = 100
|
||||
work_mem = 50000kB
|
||||
shared_buffers = 64MB
|
||||
log_destination = 'stderr'
|
||||
deadlock_timeout = 100
|
||||
log_min_messages = info
|
||||
log_min_error_statement = error
|
||||
log_error_verbosity = verbose
|
||||
log_lock_waits = true
|
||||
log_statement = all
|
||||
log_duration = on
|
||||
log_min_duration_statement = 0
|
||||
log_line_prefix = '%m [%p] (%x) %i '
|
||||
Reference in new issue
Block a user