mirror of https://github.com/hashicorp/boundary
refact(cred-libs): Update user_password to username_password (#2154)
* refact(cred-libs): Update user_password to username_passwordpull/2183/head
parent
06a2f4ce9c
commit
b6ca61a7f6
@ -0,0 +1,23 @@
|
||||
begin;
|
||||
|
||||
-- drop constraint so we can migrate user_password to username_password
|
||||
alter table credential_type_enm
|
||||
drop constraint only_predefined_credential_types_allowed;
|
||||
|
||||
-- Next: we will update user_password to username_password
|
||||
update credential_type_enm
|
||||
set name = 'username_password'
|
||||
where name = 'user_password';
|
||||
|
||||
-- Add new constraint that only allows unspecified and new username_password
|
||||
-- This replaces the constraint defined in 2/02_credential_type.up.sql
|
||||
alter table credential_type_enm
|
||||
add constraint only_predefined_credential_types_allowed
|
||||
check (
|
||||
name in (
|
||||
'unspecified',
|
||||
'username_password'
|
||||
)
|
||||
);
|
||||
|
||||
commit;
|
||||
@ -0,0 +1,111 @@
|
||||
package oss_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/boundary/internal/credential/vault"
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
"github.com/hashicorp/boundary/internal/db/common"
|
||||
"github.com/hashicorp/boundary/internal/db/schema"
|
||||
"github.com/hashicorp/boundary/internal/iam"
|
||||
"github.com/hashicorp/boundary/testing/dbtest"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestMigrations_user_password_Migration(t *testing.T) {
|
||||
const (
|
||||
priorMigration = 31002
|
||||
currentMigration = 32001
|
||||
)
|
||||
|
||||
t.Parallel()
|
||||
ctx := context.Background()
|
||||
dialect := dbtest.Postgres
|
||||
|
||||
c, u, _, err := dbtest.StartUsingTemplate(dialect, dbtest.WithTemplate(dbtest.Template1))
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() {
|
||||
require.NoError(t, c())
|
||||
})
|
||||
d, err := common.SqlOpen(dialect, u)
|
||||
require.NoError(t, err)
|
||||
|
||||
// migration to the prior migration (before the one we want to test)
|
||||
m, err := schema.NewManager(ctx, schema.Dialect(dialect), d, schema.WithEditions(
|
||||
schema.TestCreatePartialEditions(schema.Dialect(dialect), schema.PartialEditions{"oss": priorMigration}),
|
||||
))
|
||||
require.NoError(t, err)
|
||||
|
||||
require.NoError(t, m.ApplyMigrations(ctx))
|
||||
state, err := m.CurrentState(ctx)
|
||||
require.NoError(t, err)
|
||||
want := &schema.State{
|
||||
Initialized: true,
|
||||
Editions: []schema.EditionState{
|
||||
{
|
||||
Name: "oss",
|
||||
BinarySchemaVersion: priorMigration,
|
||||
DatabaseSchemaVersion: priorMigration,
|
||||
DatabaseSchemaState: schema.Equal,
|
||||
},
|
||||
},
|
||||
}
|
||||
require.Equal(t, want, state)
|
||||
|
||||
// get a connection
|
||||
dbType, err := db.StringToDbType(dialect)
|
||||
require.NoError(t, err)
|
||||
conn, err := db.Open(dbType, u)
|
||||
require.NoError(t, err)
|
||||
rw := db.New(conn)
|
||||
|
||||
rootWrapper := db.TestWrapper(t)
|
||||
iamRepo := iam.TestRepo(t, conn, rootWrapper)
|
||||
_, prj := iam.TestScopes(t, iamRepo)
|
||||
|
||||
cs, err := vault.NewCredentialStore(prj.PublicId, "https://vault", []byte("token"))
|
||||
cs.PublicId = "csvlt_test1234"
|
||||
require.NoError(t, rw.Create(context.Background(), cs))
|
||||
|
||||
upLib, err := vault.NewCredentialLibrary(cs.PublicId, "vault_path", vault.WithMethod("GET"), vault.WithCredentialType("user_password"))
|
||||
upLib.PublicId = "clvlt_testuplib"
|
||||
require.NoError(t, rw.Create(context.Background(), upLib))
|
||||
|
||||
lib, err := vault.NewCredentialLibrary(cs.PublicId, "vault_path", vault.WithMethod("GET"))
|
||||
lib.PublicId = "clvlt_testlib"
|
||||
require.NoError(t, rw.Create(context.Background(), lib))
|
||||
|
||||
// now we're ready for the migration we want to test.
|
||||
m, err = schema.NewManager(ctx, schema.Dialect(dialect), d, schema.WithEditions(
|
||||
schema.TestCreatePartialEditions(schema.Dialect(dialect), schema.PartialEditions{"oss": currentMigration}),
|
||||
))
|
||||
require.NoError(t, err)
|
||||
|
||||
require.NoError(t, m.ApplyMigrations(ctx))
|
||||
state, err = m.CurrentState(ctx)
|
||||
require.NoError(t, err)
|
||||
want = &schema.State{
|
||||
Initialized: true,
|
||||
Editions: []schema.EditionState{
|
||||
{
|
||||
Name: "oss",
|
||||
BinarySchemaVersion: currentMigration,
|
||||
DatabaseSchemaVersion: currentMigration,
|
||||
DatabaseSchemaState: schema.Equal,
|
||||
},
|
||||
},
|
||||
}
|
||||
require.Equal(t, want, state)
|
||||
|
||||
// Validate uplib was migrated to username_password
|
||||
err = rw.LookupByPublicId(context.Background(), upLib)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "username_password", upLib.GetCredentialType())
|
||||
|
||||
// Validate lib was left as unspecified
|
||||
err = rw.LookupByPublicId(context.Background(), lib)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "unspecified", lib.GetCredentialType())
|
||||
}
|
||||
Loading…
Reference in new issue