|
| 1 | +package migration |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "database/sql" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/hashicorp/boundary/internal/auth/oidc" |
| 9 | + "github.com/hashicorp/boundary/internal/authtoken" |
| 10 | + "github.com/hashicorp/boundary/internal/db" |
| 11 | + "github.com/hashicorp/boundary/internal/db/schema" |
| 12 | + "github.com/hashicorp/boundary/internal/docker" |
| 13 | + "github.com/hashicorp/boundary/internal/host/static" |
| 14 | + "github.com/hashicorp/boundary/internal/iam" |
| 15 | + "github.com/hashicorp/boundary/internal/kms" |
| 16 | + "github.com/hashicorp/boundary/internal/session" |
| 17 | + "github.com/hashicorp/boundary/internal/target" |
| 18 | + wrapping "github.com/hashicorp/go-kms-wrapping" |
| 19 | + "github.com/jinzhu/gorm" |
| 20 | + "github.com/stretchr/testify/assert" |
| 21 | + "github.com/stretchr/testify/require" |
| 22 | +) |
| 23 | + |
| 24 | +func TestMigrations_UserDimension(t *testing.T) { |
| 25 | + const ( |
| 26 | + priorMigration = 13001 |
| 27 | + currentMigration = 14001 |
| 28 | + ) |
| 29 | + |
| 30 | + t.Parallel() |
| 31 | + assert, require := assert.New(t), require.New(t) |
| 32 | + ctx := context.Background() |
| 33 | + dialect := "postgres" |
| 34 | + |
| 35 | + c, u, _, err := docker.StartDbInDocker(dialect) |
| 36 | + require.NoError(err) |
| 37 | + t.Cleanup(func() { |
| 38 | + require.NoError(c()) |
| 39 | + }) |
| 40 | + d, err := sql.Open(dialect, u) |
| 41 | + require.NoError(err) |
| 42 | + |
| 43 | + // migration to the prior migration (before the one we want to test) |
| 44 | + oState := schema.TestCloneMigrationStates(t) |
| 45 | + nState := schema.TestCreatePartialMigrationState(oState["postgres"], priorMigration) |
| 46 | + oState["postgres"] = nState |
| 47 | + |
| 48 | + m, err := schema.NewManager(ctx, dialect, d, schema.WithMigrationStates(oState)) |
| 49 | + require.NoError(err) |
| 50 | + |
| 51 | + assert.NoError(m.RollForward(ctx)) |
| 52 | + state, err := m.CurrentState(ctx) |
| 53 | + require.NoError(err) |
| 54 | + assert.Equal(priorMigration, state.DatabaseSchemaVersion) |
| 55 | + assert.False(state.Dirty) |
| 56 | + |
| 57 | + // okay, now we can seed the database with test data |
| 58 | + conn, err := gorm.Open(dialect, u) |
| 59 | + require.NoError(err) |
| 60 | + |
| 61 | + rw := db.New(conn) |
| 62 | + wrapper := db.TestWrapper(t) |
| 63 | + |
| 64 | + org, prj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper)) |
| 65 | + require.NotNil(prj) |
| 66 | + assert.NotEmpty(prj.GetPublicId()) |
| 67 | + |
| 68 | + hc := static.TestCatalogs(t, conn, prj.GetPublicId(), 1)[0] |
| 69 | + hs := static.TestSets(t, conn, hc.GetPublicId(), 1)[0] |
| 70 | + h := static.TestHosts(t, conn, hc.GetPublicId(), 1)[0] |
| 71 | + static.TestSetMembers(t, conn, hs.GetPublicId(), []*static.Host{h}) |
| 72 | + |
| 73 | + tar := target.TestTcpTarget(t, conn, prj.GetPublicId(), "test", target.WithHostSources([]string{hs.GetPublicId()})) |
| 74 | + var sessions []*session.Session |
| 75 | + |
| 76 | + kmsCache := kms.TestKms(t, conn, wrapper) |
| 77 | + databaseWrapper, err := kmsCache.GetWrapper(ctx, org.GetPublicId(), kms.KeyPurposeDatabase) |
| 78 | + require.NoError(err) |
| 79 | + |
| 80 | + { |
| 81 | + at := authtoken.TestAuthToken(t, conn, kmsCache, org.GetPublicId()) |
| 82 | + uId := at.GetIamUserId() |
| 83 | + |
| 84 | + sess := session.TestSession(t, conn, wrapper, session.ComposedOf{ |
| 85 | + UserId: uId, |
| 86 | + HostId: h.GetPublicId(), |
| 87 | + TargetId: tar.GetPublicId(), |
| 88 | + HostSetId: hs.GetPublicId(), |
| 89 | + AuthTokenId: at.GetPublicId(), |
| 90 | + ScopeId: prj.GetPublicId(), |
| 91 | + Endpoint: "tcp://127.0.0.1:22", |
| 92 | + }) |
| 93 | + sessions = append(sessions, sess) |
| 94 | + } |
| 95 | + |
| 96 | + { |
| 97 | + at := testOidcAuthToken(t, conn, kmsCache, databaseWrapper, org.GetPublicId()) |
| 98 | + uId := at.GetIamUserId() |
| 99 | + |
| 100 | + sess := session.TestSession(t, conn, wrapper, session.ComposedOf{ |
| 101 | + UserId: uId, |
| 102 | + HostId: h.GetPublicId(), |
| 103 | + TargetId: tar.GetPublicId(), |
| 104 | + HostSetId: hs.GetPublicId(), |
| 105 | + AuthTokenId: at.GetPublicId(), |
| 106 | + ScopeId: prj.GetPublicId(), |
| 107 | + Endpoint: "tcp://127.0.0.1:22", |
| 108 | + }) |
| 109 | + sessions = append(sessions, sess) |
| 110 | + } |
| 111 | + |
| 112 | + sessionRepo, err := session.NewRepository(rw, rw, kmsCache) |
| 113 | + require.NoError(err) |
| 114 | + |
| 115 | + count, err := sessionRepo.TerminateCompletedSessions(ctx) |
| 116 | + assert.NoError(err) |
| 117 | + assert.Zero(count) |
| 118 | + |
| 119 | + for _, sess := range sessions { |
| 120 | + // call TerminateSession |
| 121 | + _, err = sessionRepo.TerminateSession(ctx, sess.GetPublicId(), 1, session.ClosedByUser) |
| 122 | + assert.NoError(err) |
| 123 | + } |
| 124 | + |
| 125 | + // now we're ready for the migration we want to test. |
| 126 | + oState = schema.TestCloneMigrationStates(t) |
| 127 | + nState = schema.TestCreatePartialMigrationState(oState["postgres"], currentMigration) |
| 128 | + oState["postgres"] = nState |
| 129 | + |
| 130 | + m, err = schema.NewManager(ctx, dialect, d, schema.WithMigrationStates(oState)) |
| 131 | + require.NoError(err) |
| 132 | + |
| 133 | + assert.NoError(m.RollForward(ctx)) |
| 134 | + state, err = m.CurrentState(ctx) |
| 135 | + require.NoError(err) |
| 136 | + assert.Equal(currentMigration, state.DatabaseSchemaVersion) |
| 137 | + assert.False(state.Dirty) |
| 138 | +} |
| 139 | + |
| 140 | +func testOidcAuthToken(t *testing.T, conn *gorm.DB, kms *kms.Kms, wrapper wrapping.Wrapper, scopeId string) *authtoken.AuthToken { |
| 141 | + t.Helper() |
| 142 | + |
| 143 | + authMethod := oidc.TestAuthMethod( |
| 144 | + t, conn, wrapper, scopeId, oidc.ActivePrivateState, |
| 145 | + "alice-rp", "fido", |
| 146 | + oidc.WithIssuer(oidc.TestConvertToUrls(t, "https://www.alice.com")[0]), |
| 147 | + oidc.WithSigningAlgs(oidc.RS256), |
| 148 | + oidc.WithApiUrl(oidc.TestConvertToUrls(t, "https://www.alice.com/callback")[0]), |
| 149 | + ) |
| 150 | + acct := oidc.TestAccount(t, conn, authMethod, "test-subject") |
| 151 | + |
| 152 | + ctx := context.Background() |
| 153 | + rw := db.New(conn) |
| 154 | + iamRepo, err := iam.NewRepository(rw, rw, kms) |
| 155 | + require.NoError(t, err) |
| 156 | + |
| 157 | + u := iam.TestUser(t, iamRepo, scopeId, iam.WithAccountIds(acct.PublicId)) |
| 158 | + |
| 159 | + repo, err := authtoken.NewRepository(rw, rw, kms) |
| 160 | + require.NoError(t, err) |
| 161 | + |
| 162 | + at, err := repo.CreateAuthToken(ctx, u, acct.GetPublicId()) |
| 163 | + require.NoError(t, err) |
| 164 | + return at |
| 165 | +} |
0 commit comments