feat: add initial migration hook

This commit is contained in:
Damian Debkowski committed 2022-08-22 12:15:20 -07:00
1 parent 79866a287f
commit 27e9f775be
174 files changed
+4234 -2180

No files matched your search

+4 -4
View File
@@ -23,7 +23,7 @@ type Option func(*options)
type options struct {
withLimit int
withOrderByCreateTime db.OrderBy
withScopeIds []string
withProjectIds []string
withUserId string
withExpirationTime *timestamp.Timestamp
withTestTofu []byte
@@ -57,10 +57,10 @@ func WithOrderByCreateTime(orderBy db.OrderBy) Option {
}
}
// WithScopeIds allows specifying a scope ID criteria for the function.
func WithScopeIds(scopeIds []string) Option {
// WithProjectIds allows specifying a project ID criteria for the function.
func WithProjectIds(projectIds []string) Option {
return func(o *options) {
o.withScopeIds = scopeIds
o.withProjectIds = projectIds
}
}
+3 -3
View File
@@ -30,11 +30,11 @@ func Test_GetOpts(t *testing.T) {
testOpts.withLimit = 1
assert.Equal(opts, testOpts)
})
t.Run("WithScopeId", func(t *testing.T) {
t.Run("WithProjectIds", func(t *testing.T) {
assert := assert.New(t)
opts := getOpts(WithScopeIds([]string{"o_1234"}))
opts := getOpts(WithProjectIds([]string{"o_1234"}))
testOpts := getDefaultOptions()
testOpts.withScopeIds = []string{"o_1234"}
testOpts.withProjectIds = []string{"o_1234"}
assert.Equal(opts, testOpts)
})
t.Run("WithOrderByCreateTime", func(t *testing.T) {
+4 -4
View File
@@ -119,18 +119,18 @@ from
session_connection_limit, session_connection_count;
`
nonTerminatedSessionPublicIdList = `
select public_id, scope_id, user_id from session
select public_id, project_id, user_id from session
where
session.termination_reason is null
and
scope_id = any(@scope_ids)
project_id = any(@project_ids)
;
`
sessionPublicIdList = `
select public_id, scope_id, user_id from session
select public_id, project_id, user_id from session
where
scope_id = any(@scope_ids)
project_id = any(@project_ids)
;
`
+2 -2
View File
@@ -107,7 +107,7 @@ func (r *Repository) convertToSessions(ctx context.Context, sessionList []*sessi
TargetId: sv.TargetId,
HostSetId: sv.HostSetId,
AuthTokenId: sv.AuthTokenId,
ScopeId: sv.ScopeId,
ProjectId: sv.ProjectId,
Certificate: sv.Certificate,
ExpirationTime: sv.ExpirationTime,
CtTofuToken: sv.CtTofuToken,
@@ -126,7 +126,7 @@ func (r *Repository) convertToSessions(ctx context.Context, sessionList []*sessi
workingSession.KeyId = "" // KeyId should not be returned in lists
} else {
if len(workingSession.CtTofuToken) > 0 {
databaseWrapper, err := r.kms.GetWrapper(ctx, workingSession.ScopeId, kms.KeyPurposeDatabase)
databaseWrapper, err := r.kms.GetWrapper(ctx, workingSession.ProjectId, kms.KeyPurposeDatabase)
if err != nil {
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("unable to get database wrapper"))
}
+9 -9
View File
@@ -9,22 +9,22 @@ import (
)
// AddSessionCredentials encrypts the credData and adds the credentials to the repository. The credentials are linked
// to the sessionID provided, and encrypted using the sessScopeId. Session credentials are only valid for pending and
// to the sessionID provided, and encrypted using the sessProjectId. Session credentials are only valid for pending and
// active sessions, once a session ends, all session credentials are deleted.
// All options are ignored.
func (r *Repository) AddSessionCredentials(ctx context.Context, sessScopeId, sessionId string, credData []Credential, _ ...Option) error {
func (r *Repository) AddSessionCredentials(ctx context.Context, sessProjectId, sessionId string, credData []Credential, _ ...Option) error {
const op = "session.(Repository).AddSessionCredentials"
if sessionId == "" {
return errors.New(ctx, errors.InvalidParameter, op, "missing session id")
}
if sessScopeId == "" {
return errors.New(ctx, errors.InvalidParameter, op, "missing session scope id")
if sessProjectId == "" {
return errors.New(ctx, errors.InvalidParameter, op, "missing session project id")
}
if len(credData) == 0 {
return errors.New(ctx, errors.InvalidParameter, op, "missing credentials")
}
databaseWrapper, err := r.kms.GetWrapper(ctx, sessScopeId, kms.KeyPurposeDatabase)
databaseWrapper, err := r.kms.GetWrapper(ctx, sessProjectId, kms.KeyPurposeDatabase)
if err != nil {
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to get database wrapper"))
}
@@ -59,16 +59,16 @@ func (r *Repository) AddSessionCredentials(ctx context.Context, sessScopeId, ses
// ListSessionCredentials returns all Credential attached to the sessionId.
// All options are ignored.
func (r *Repository) ListSessionCredentials(ctx context.Context, sessScopeId, sessionId string, _ ...Option) ([]Credential, error) {
func (r *Repository) ListSessionCredentials(ctx context.Context, sessProjectId, sessionId string, _ ...Option) ([]Credential, error) {
const op = "session.(Repository).ListSessionCredentials"
if sessionId == "" {
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing session id")
}
if sessScopeId == "" {
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing session scope id")
if sessProjectId == "" {
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing session project id")
}
databaseWrapper, err := r.kms.GetWrapper(ctx, sessScopeId, kms.KeyPurposeDatabase)
databaseWrapper, err := r.kms.GetWrapper(ctx, sessProjectId, kms.KeyPurposeDatabase)
if err != nil {
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("unable to get database wrapper"))
}
+30 -30
View File
@@ -24,9 +24,9 @@ func TestRepository_AddSessionCredentials(t *testing.T) {
s := TestSession(t, conn, wrapper, TestSessionParams(t, conn, wrapper, iamRepo))
type args struct {
creds []Credential
sessionId string
sessionScopeId string
creds []Credential
sessionId string
sessionProjectId string
}
tests := []struct {
name string
@@ -38,15 +38,15 @@ func TestRepository_AddSessionCredentials(t *testing.T) {
{
name: "invalid-missing-credentials",
args: args{
sessionId: s.PublicId,
sessionScopeId: s.ScopeId,
sessionId: s.PublicId,
sessionProjectId: s.ProjectId,
},
wantErr: true,
wantErrCode: errors.InvalidParameter,
wantErrMsg: "session.(Repository).AddSessionCredentials: missing credentials: parameter violation: error #100",
},
{
name: "invalid-missing-scope-id",
name: "invalid-missing-project-id",
args: args{
sessionId: s.PublicId,
creds: []Credential{
@@ -55,12 +55,12 @@ func TestRepository_AddSessionCredentials(t *testing.T) {
},
wantErr: true,
wantErrCode: errors.InvalidParameter,
wantErrMsg: "session.(Repository).AddSessionCredentials: missing session scope id: parameter violation: error #100",
wantErrMsg: "session.(Repository).AddSessionCredentials: missing session project id: parameter violation: error #100",
},
{
name: "invalid-missing-session-id",
args: args{
sessionScopeId: s.ScopeId,
sessionProjectId: s.ProjectId,
creds: []Credential{
Credential("test-cred"),
},
@@ -72,8 +72,8 @@ func TestRepository_AddSessionCredentials(t *testing.T) {
{
name: "invalid-empty-cred",
args: args{
sessionId: s.PublicId,
sessionScopeId: s.ScopeId,
sessionId: s.PublicId,
sessionProjectId: s.ProjectId,
creds: []Credential{
Credential(""),
},
@@ -85,8 +85,8 @@ func TestRepository_AddSessionCredentials(t *testing.T) {
{
name: "invalid-empty-cred-with-valid",
args: args{
sessionId: s.PublicId,
sessionScopeId: s.ScopeId,
sessionId: s.PublicId,
sessionProjectId: s.ProjectId,
creds: []Credential{
Credential("test-cred"),
Credential(""),
@@ -100,8 +100,8 @@ func TestRepository_AddSessionCredentials(t *testing.T) {
{
name: "valid",
args: args{
sessionId: s.PublicId,
sessionScopeId: s.ScopeId,
sessionId: s.PublicId,
sessionProjectId: s.ProjectId,
creds: []Credential{
Credential("test-cred"),
Credential("test-cred1"),
@@ -114,7 +114,7 @@ func TestRepository_AddSessionCredentials(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
err := repo.AddSessionCredentials(context.Background(), tt.args.sessionScopeId, tt.args.sessionId, tt.args.creds)
err := repo.AddSessionCredentials(context.Background(), tt.args.sessionProjectId, tt.args.sessionId, tt.args.creds)
if tt.wantErr {
require.Error(err)
assert.Truef(errors.Match(errors.T(tt.wantErrCode), err), "Unexpected error %s", err)
@@ -123,7 +123,7 @@ func TestRepository_AddSessionCredentials(t *testing.T) {
}
require.NoError(err)
creds, err := repo.ListSessionCredentials(context.Background(), s.ScopeId, s.PublicId)
creds, err := repo.ListSessionCredentials(context.Background(), s.ProjectId, s.PublicId)
require.NoError(err)
assert.ElementsMatch(creds, tt.args.creds)
})
@@ -152,15 +152,15 @@ func TestRepository_ListSessionCredentials(t *testing.T) {
Credential("cred1"),
}
err = repo.AddSessionCredentials(context.Background(), s1.ScopeId, s1.PublicId, s1Creds)
err = repo.AddSessionCredentials(context.Background(), s1.ProjectId, s1.PublicId, s1Creds)
require.NoError(t, err)
err = repo.AddSessionCredentials(context.Background(), s2.ScopeId, s2.PublicId, s2Creds)
err = repo.AddSessionCredentials(context.Background(), s2.ProjectId, s2.PublicId, s2Creds)
require.NoError(t, err)
type args struct {
sessionId string
sessionScopeId string
sessionId string
sessionProjectId string
}
tests := []struct {
name string
@@ -171,18 +171,18 @@ func TestRepository_ListSessionCredentials(t *testing.T) {
wantErrMsg string
}{
{
name: "invalid-missing-scope-id",
name: "invalid-missing-project-id",
args: args{
sessionId: s1.PublicId,
},
wantErr: true,
wantErrCode: errors.InvalidParameter,
wantErrMsg: "session.(Repository).ListSessionCredentials: missing session scope id: parameter violation: error #100",
wantErrMsg: "session.(Repository).ListSessionCredentials: missing session project id: parameter violation: error #100",
},
{
name: "invalid-missing-session-id",
args: args{
sessionScopeId: s1.ScopeId,
sessionProjectId: s1.ProjectId,
},
wantErr: true,
wantErrCode: errors.InvalidParameter,
@@ -191,24 +191,24 @@ func TestRepository_ListSessionCredentials(t *testing.T) {
{
name: "valid-s1",
args: args{
sessionId: s1.PublicId,
sessionScopeId: s1.ScopeId,
sessionId: s1.PublicId,
sessionProjectId: s1.ProjectId,
},
wantCreds: s1Creds,
},
{
name: "valid-s2",
args: args{
sessionId: s2.PublicId,
sessionScopeId: s2.ScopeId,
sessionId: s2.PublicId,
sessionProjectId: s2.ProjectId,
},
wantCreds: s2Creds,
},
{
name: "valid-s3-no-creds",
args: args{
sessionId: s3.PublicId,
sessionScopeId: s3.ScopeId,
sessionId: s3.PublicId,
sessionProjectId: s3.ProjectId,
},
wantCreds: nil,
},
@@ -217,7 +217,7 @@ func TestRepository_ListSessionCredentials(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
creds, err := repo.ListSessionCredentials(context.Background(), tt.args.sessionScopeId, tt.args.sessionId)
creds, err := repo.ListSessionCredentials(context.Background(), tt.args.sessionProjectId, tt.args.sessionId)
if tt.wantErr {
require.Error(err)
assert.Truef(errors.Match(errors.T(tt.wantErrCode), err), "Unexpected error %s", err)
+19 -19
View File
@@ -47,8 +47,8 @@ func (r *Repository) CreateSession(ctx context.Context, sessionWrapper wrapping.
if newSession.AuthTokenId == "" {
return nil, nil, errors.New(ctx, errors.InvalidParameter, op, "missing auth token id")
}
if newSession.ScopeId == "" {
return nil, nil, errors.New(ctx, errors.InvalidParameter, op, "missing scope id")
if newSession.ProjectId == "" {
return nil, nil, errors.New(ctx, errors.InvalidParameter, op, "missing project id")
}
if newSession.CtTofuToken != nil {
return nil, nil, errors.New(ctx, errors.InvalidParameter, op, "ct is not empty")
@@ -210,7 +210,7 @@ func (r *Repository) LookupSession(ctx context.Context, sessionId string, _ ...O
return nil, nil, errors.Wrap(ctx, err, op)
}
if len(session.CtTofuToken) > 0 {
databaseWrapper, err := r.kms.GetWrapper(ctx, session.ScopeId, kms.KeyPurposeDatabase)
databaseWrapper, err := r.kms.GetWrapper(ctx, session.ProjectId, kms.KeyPurposeDatabase)
if err != nil {
return nil, nil, errors.Wrap(ctx, err, op, errors.WithMsg("unable to get database wrapper"))
}
@@ -229,23 +229,23 @@ func (r *Repository) LookupSession(ctx context.Context, sessionId string, _ ...O
return &session, authzSummary, nil
}
// fetchAuthzProtectedSessionsByScope fetches sessions for the given scopes.
// fetchAuthzProtectedSessionsByProject fetches sessions for the given projects.
// Note that the sessions are not fully populated, and only contain the
// necessary information to implement the boundary.AuthzProtectedEntity
// interface. Supports the WithTerminated option.
func (r *Repository) fetchAuthzProtectedSessionsByScope(
ctx context.Context, scopeIds []string, opt ...Option,
func (r *Repository) fetchAuthzProtectedSessionsByProject(
ctx context.Context, projectIds []string, opt ...Option,
) (map[string][]boundary.AuthzProtectedEntity, error) {
const op = "session.(Repository).fetchAuthzProtectedSessionsByScope"
const op = "session.(Repository).fetchAuthzProtectedSessionsByProject"
opts := getOpts(opt...)
if len(scopeIds) == 0 {
return nil, errors.New(ctx, errors.InvalidParameter, op, "no scopes given")
if len(projectIds) == 0 {
return nil, errors.New(ctx, errors.InvalidParameter, op, "no project ids given")
}
args := []interface{}{
sql.Named("scope_ids", "{"+strings.Join(scopeIds, ",")+"}"),
sql.Named("project_ids", "{"+strings.Join(projectIds, ",")+"}"),
}
var query string
@@ -267,13 +267,13 @@ func (r *Repository) fetchAuthzProtectedSessionsByScope(
if err := r.reader.ScanRows(ctx, rows, &ses); err != nil {
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("scan row failed"))
}
sessionsMap[ses.GetScopeId()] = append(sessionsMap[ses.GetScopeId()], ses)
sessionsMap[ses.GetProjectId()] = append(sessionsMap[ses.GetProjectId()], ses)
}
return sessionsMap, nil
}
// ListSessions lists sessions. Supports the WithLimit, WithScopeId, and WithSessionIds options.
// ListSessions lists sessions. Supports the WithLimit, WithProjectId, and WithSessionIds options.
func (r *Repository) ListSessions(ctx context.Context, opt ...Option) ([]*Session, error) {
const op = "session.(Repository).ListSessions"
opts := getOpts(opt...)
@@ -281,18 +281,18 @@ func (r *Repository) ListSessions(ctx context.Context, opt ...Option) ([]*Sessio
var args []interface{}
inClauseCnt := 0
switch len(opts.withScopeIds) {
switch len(opts.withProjectIds) {
case 0:
case 1:
inClauseCnt += 1
where, args = append(where, fmt.Sprintf("scope_id = @%d", inClauseCnt)), append(args, sql.Named(fmt.Sprintf("%d", inClauseCnt), opts.withScopeIds[0]))
where, args = append(where, fmt.Sprintf("project_id = @%d", inClauseCnt)), append(args, sql.Named(fmt.Sprintf("%d", inClauseCnt), opts.withProjectIds[0]))
default:
idsInClause := make([]string, 0, len(opts.withScopeIds))
for _, id := range opts.withScopeIds {
idsInClause := make([]string, 0, len(opts.withProjectIds))
for _, id := range opts.withProjectIds {
inClauseCnt += 1
idsInClause, args = append(idsInClause, fmt.Sprintf("@%d", inClauseCnt)), append(args, sql.Named(fmt.Sprintf("%d", inClauseCnt), id))
}
where = append(where, fmt.Sprintf("scope_id in (%s)", strings.Join(idsInClause, ",")))
where = append(where, fmt.Sprintf("project_id in (%s)", strings.Join(idsInClause, ",")))
}
if opts.withUserId != "" {
@@ -547,7 +547,7 @@ func (r *Repository) ActivateSession(ctx context.Context, sessionId string, sess
if err := reader.LookupById(ctx, &foundSession); err != nil {
return errors.Wrap(ctx, err, op, errors.WithMsg(fmt.Sprintf("failed for %s", sessionId)))
}
databaseWrapper, err := r.kms.GetWrapper(ctx, foundSession.ScopeId, kms.KeyPurposeDatabase)
databaseWrapper, err := r.kms.GetWrapper(ctx, foundSession.ProjectId, kms.KeyPurposeDatabase)
if err != nil {
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to get database wrapper"))
}
@@ -619,7 +619,7 @@ func (r *Repository) updateState(ctx context.Context, sessionId string, sessionV
return errors.New(ctx, errors.MultipleRecords, op, fmt.Sprintf("updated session and %d rows updated", rowsUpdated))
}
if len(updatedSession.CtTofuToken) > 0 {
databaseWrapper, err := r.kms.GetWrapper(ctx, updatedSession.ScopeId, kms.KeyPurposeDatabase)
databaseWrapper, err := r.kms.GetWrapper(ctx, updatedSession.ProjectId, kms.KeyPurposeDatabase)
if err != nil {
return errors.Wrap(ctx, err, op, errors.WithMsg("unable to get database wrapper"))
}
+40 -40
View File
@@ -80,19 +80,19 @@ func TestRepository_ListSession(t *testing.T) {
wantErr: false,
},
{
name: "withScopeId",
name: "withProjectIds",
createCnt: repo.defaultLimit + 1,
args: args{
opt: []Option{WithScopeIds([]string{composedOf.ScopeId})},
opt: []Option{WithProjectIds([]string{composedOf.ProjectId})},
},
wantCnt: repo.defaultLimit,
wantErr: false,
},
{
name: "bad-withScopeId",
name: "bad-withProjectId",
createCnt: repo.defaultLimit + 1,
args: args{
opt: []Option{WithScopeIds([]string{"o_thisIsNotValid"})},
opt: []Option{WithProjectIds([]string{"o_thisIsNotValid"})},
},
wantCnt: 0,
wantErr: false,
@@ -190,11 +190,11 @@ func TestRepository_ListSession(t *testing.T) {
coDiffUser.UserId = s.UserId
wantS := TestSession(t, conn, wrapper, coDiffUser)
got, err := repo.ListSessions(context.Background(), WithUserId(coDiffUser.UserId), WithScopeIds([]string{coDiffUser.ScopeId}))
got, err := repo.ListSessions(context.Background(), WithUserId(coDiffUser.UserId), WithProjectIds([]string{coDiffUser.ProjectId}))
require.NoError(err)
assert.Equal(1, len(got))
assert.Equal(wantS.UserId, got[0].UserId)
assert.Equal(wantS.ScopeId, got[0].ScopeId)
assert.Equal(wantS.ProjectId, got[0].ProjectId)
})
t.Run("WithSessionIds", func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
@@ -231,12 +231,12 @@ func TestRepository_ListSessions_Multiple_Scopes(t *testing.T) {
var projs []string
for i := 0; i < numPerScope; i++ {
composedOf := TestSessionParams(t, conn, wrapper, iamRepo)
projs = append(projs, composedOf.ScopeId)
projs = append(projs, composedOf.ProjectId)
s := TestSession(t, conn, wrapper, composedOf)
_ = TestState(t, conn, s.PublicId, StatusActive)
}
got, err := repo.ListSessions(context.Background(), WithScopeIds(projs))
got, err := repo.ListSessions(context.Background(), WithProjectIds(projs))
require.NoError(t, err)
assert.Equal(t, len(projs), len(got))
}
@@ -344,11 +344,11 @@ func TestRepository_CreateSession(t *testing.T) {
wantIsError: errors.InvalidParameter,
},
{
name: "empty-scopeId",
name: "empty-projectId",
args: args{
composedOf: func() ComposedOf {
c := TestSessionParams(t, conn, wrapper, iamRepo)
c.ScopeId = ""
c.ProjectId = ""
return c
}(),
workerAddresses: workerAddresses,
@@ -377,7 +377,7 @@ func TestRepository_CreateSession(t *testing.T) {
TargetId: tt.args.composedOf.TargetId,
HostSetId: tt.args.composedOf.HostSetId,
AuthTokenId: tt.args.composedOf.AuthTokenId,
ScopeId: tt.args.composedOf.ScopeId,
ProjectId: tt.args.composedOf.ProjectId,
Endpoint: "tcp://127.0.0.1:22",
ExpirationTime: tt.args.composedOf.ExpirationTime,
ConnectionLimit: tt.args.composedOf.ConnectionLimit,
@@ -954,7 +954,7 @@ func TestRepository_CancelSession(t *testing.T) {
TargetId: tcpTarget.GetPublicId(),
HostSetId: sets[0].PublicId,
AuthTokenId: at.PublicId,
ScopeId: tcpTarget.GetScopeId(),
ProjectId: tcpTarget.GetProjectId(),
Endpoint: "tcp://127.0.0.1:22",
ExpirationTime: &timestamp.Timestamp{Timestamp: expTime},
ConnectionLimit: tcpTarget.GetSessionConnectionLimit(),
@@ -1175,7 +1175,7 @@ func TestRepository_CancelSessionViaFKNull(t *testing.T) {
t := &iam.Scope{
Scope: &iamStore.Scope{
PublicId: s.ScopeId,
PublicId: s.ProjectId,
},
}
return cancelFk{
@@ -1463,11 +1463,11 @@ func testSessionCredentialParams(t *testing.T, conn *db.DB, wrapper wrapping.Wra
require.NoError(err)
require.NotNil(tar)
vaultStore := vault.TestCredentialStores(t, conn, wrapper, params.ScopeId, 1)[0]
vaultStore := vault.TestCredentialStores(t, conn, wrapper, params.ProjectId, 1)[0]
libIds := vault.TestCredentialLibraries(t, conn, wrapper, vaultStore.GetPublicId(), 2)
staticStore := credstatic.TestCredentialStore(t, conn, wrapper, params.ScopeId)
upCreds := credstatic.TestUsernamePasswordCredentials(t, conn, wrapper, "u", "p", staticStore.GetPublicId(), params.ScopeId, 2)
staticStore := credstatic.TestCredentialStore(t, conn, wrapper, params.ProjectId)
upCreds := credstatic.TestUsernamePasswordCredentials(t, conn, wrapper, "u", "p", staticStore.GetPublicId(), params.ProjectId, 2)
ids := target.CredentialSources{
BrokeredCredentialIds: []string{libIds[0].GetPublicId(), libIds[1].GetPublicId(), upCreds[0].GetPublicId(), upCreds[1].GetPublicId()},
@@ -1649,7 +1649,7 @@ func TestFetchAuthzProtectedSessionsByScopes(t *testing.T) {
TargetId: tarOther.GetPublicId(),
HostSetId: hsOther.GetPublicId(),
AuthTokenId: composedOf.AuthTokenId,
ScopeId: pWithOtherSessions.GetPublicId(),
ProjectId: pWithOtherSessions.GetPublicId(),
Endpoint: "tcp://127.0.0.1:22",
}
@@ -1670,21 +1670,21 @@ func TestFetchAuthzProtectedSessionsByScopes(t *testing.T) {
{
name: "NonTerminated/none",
createCnt: 0,
reqScopes: []string{composedOf.ScopeId},
reqScopes: []string{composedOf.ProjectId},
wantCnt: 0,
wantErr: false,
},
{
name: "NonTerminated/one",
createCnt: 1,
reqScopes: []string{composedOf.ScopeId},
reqScopes: []string{composedOf.ProjectId},
wantCnt: 1,
wantErr: false,
},
{
name: "NonTerminated/many",
createCnt: 5,
reqScopes: []string{composedOf.ScopeId},
reqScopes: []string{composedOf.ProjectId},
wantCnt: 5,
wantErr: false,
},
@@ -1692,7 +1692,7 @@ func TestFetchAuthzProtectedSessionsByScopes(t *testing.T) {
name: "NonTerminated/many one terminated",
createCnt: 5,
terminateCnt: 1,
reqScopes: []string{composedOf.ScopeId},
reqScopes: []string{composedOf.ProjectId},
wantCnt: 4,
wantErr: false,
},
@@ -1700,31 +1700,31 @@ func TestFetchAuthzProtectedSessionsByScopes(t *testing.T) {
name: "NonTerminated/many terminated",
createCnt: 5,
terminateCnt: 3,
reqScopes: []string{composedOf.ScopeId},
reqScopes: []string{composedOf.ProjectId},
wantCnt: 2,
wantErr: false,
},
{
name: "NonTerminated/many multiple scopes",
name: "NonTerminated/many multiple projects",
createCnt: 5,
terminateCnt: 3,
reqScopes: []string{composedOf.ScopeId, composedOfOther.ScopeId},
reqScopes: []string{composedOf.ProjectId, composedOfOther.ProjectId},
wantCnt: 2,
wantErr: false,
},
{
name: "NonTerminated/many multiple scopes",
name: "NonTerminated/many multiple projects",
createCnt: 5,
terminateCnt: 3,
otherCnt: 3,
otherTerminateCnt: 1,
reqScopes: []string{composedOf.ScopeId, composedOfOther.ScopeId},
reqScopes: []string{composedOf.ProjectId, composedOfOther.ProjectId},
wantCnt: 2,
wantOtherCnt: 2,
wantErr: false,
},
{
name: "NonTerminated/no scopes",
name: "NonTerminated/no projects",
createCnt: 2,
terminateCnt: 1,
otherCnt: 2,
@@ -1736,7 +1736,7 @@ func TestFetchAuthzProtectedSessionsByScopes(t *testing.T) {
name: "none",
opts: []Option{WithTerminated(true)},
createCnt: 0,
reqScopes: []string{composedOf.ScopeId},
reqScopes: []string{composedOf.ProjectId},
wantCnt: 0,
wantErr: false,
},
@@ -1744,7 +1744,7 @@ func TestFetchAuthzProtectedSessionsByScopes(t *testing.T) {
name: "one",
opts: []Option{WithTerminated(true)},
createCnt: 1,
reqScopes: []string{composedOf.ScopeId},
reqScopes: []string{composedOf.ProjectId},
wantCnt: 1,
wantErr: false,
},
@@ -1752,7 +1752,7 @@ func TestFetchAuthzProtectedSessionsByScopes(t *testing.T) {
name: "many",
opts: []Option{WithTerminated(true)},
createCnt: 5,
reqScopes: []string{composedOf.ScopeId},
reqScopes: []string{composedOf.ProjectId},
wantCnt: 5,
wantErr: false,
},
@@ -1761,7 +1761,7 @@ func TestFetchAuthzProtectedSessionsByScopes(t *testing.T) {
opts: []Option{WithTerminated(true)},
createCnt: 5,
terminateCnt: 1,
reqScopes: []string{composedOf.ScopeId},
reqScopes: []string{composedOf.ProjectId},
wantCnt: 5,
wantErr: false,
},
@@ -1770,33 +1770,33 @@ func TestFetchAuthzProtectedSessionsByScopes(t *testing.T) {
opts: []Option{WithTerminated(true)},
createCnt: 5,
terminateCnt: 3,
reqScopes: []string{composedOf.ScopeId},
reqScopes: []string{composedOf.ProjectId},
wantCnt: 5,
wantErr: false,
},
{
name: "many multiple scopes",
name: "many multiple projects",
opts: []Option{WithTerminated(true)},
createCnt: 5,
terminateCnt: 3,
reqScopes: []string{composedOf.ScopeId, composedOfOther.ScopeId},
reqScopes: []string{composedOf.ProjectId, composedOfOther.ProjectId},
wantCnt: 5,
wantErr: false,
},
{
name: "many multiple scopes",
name: "many multiple projects",
opts: []Option{WithTerminated(true)},
createCnt: 5,
terminateCnt: 3,
otherCnt: 3,
otherTerminateCnt: 1,
reqScopes: []string{composedOf.ScopeId, composedOfOther.ScopeId},
reqScopes: []string{composedOf.ProjectId, composedOfOther.ProjectId},
wantCnt: 5,
wantOtherCnt: 3,
wantErr: false,
},
{
name: "no scopes",
name: "no projects",
opts: []Option{WithTerminated(true)},
createCnt: 2,
terminateCnt: 1,
@@ -1841,14 +1841,14 @@ func TestFetchAuthzProtectedSessionsByScopes(t *testing.T) {
assert.Equal(tt.otherCnt, len(otherTestSessions))
got, err := repo.fetchAuthzProtectedSessionsByScope(ctx, tt.reqScopes, tt.opts...)
got, err := repo.fetchAuthzProtectedSessionsByProject(ctx, tt.reqScopes, tt.opts...)
if tt.wantErr {
require.Error(err)
return
}
require.NoError(err)
assert.Equal(tt.wantCnt, len(got[composedOf.ScopeId]))
assert.Equal(tt.wantOtherCnt, len(got[composedOfOther.ScopeId]))
assert.Equal(tt.wantCnt, len(got[composedOf.ProjectId]))
assert.Equal(tt.wantOtherCnt, len(got[composedOfOther.ProjectId]))
})
}
}
@@ -112,7 +112,7 @@ func TestService_AuthorizeConnection(t *testing.T) {
TargetId: tcpTarget.GetPublicId(),
HostSetId: sets[0].PublicId,
AuthTokenId: at.PublicId,
ScopeId: tcpTarget.GetScopeId(),
ProjectId: tcpTarget.GetProjectId(),
Endpoint: "tcp://127.0.0.1:22",
ExpirationTime: &timestamp.Timestamp{Timestamp: expTime},
ConnectionLimit: tcpTarget.GetSessionConnectionLimit(),
@@ -73,7 +73,7 @@ func TestServiceCloseConnections(t *testing.T) {
TargetId: tcpTarget.GetPublicId(),
HostSetId: sets[0].PublicId,
AuthTokenId: at.PublicId,
ScopeId: tcpTarget.GetScopeId(),
ProjectId: tcpTarget.GetProjectId(),
Endpoint: "tcp://127.0.0.1:22",
ExpirationTime: &timestamp.Timestamp{Timestamp: expTime},
ConnectionLimit: tcpTarget.GetSessionConnectionLimit(),
@@ -6,18 +6,18 @@ import (
"github.com/hashicorp/boundary/internal/boundary"
)
type listForAuthzCheckFunc func(ctx context.Context, scopeIds []string) (map[string][]boundary.AuthzProtectedEntity, error)
type listForAuthzCheckFunc func(ctx context.Context, projectIds []string) (map[string][]boundary.AuthzProtectedEntity, error)
func (a listForAuthzCheckFunc) FetchAuthzProtectedEntitiesByScope(ctx context.Context, scopeIds []string) (map[string][]boundary.AuthzProtectedEntity, error) {
return a(ctx, scopeIds)
func (a listForAuthzCheckFunc) FetchAuthzProtectedEntitiesByScope(ctx context.Context, projectIds []string) (map[string][]boundary.AuthzProtectedEntity, error) {
return a(ctx, projectIds)
}
// ListForAuthzCheck returns a functions that fetches sessions for the given
// scopes. Note that the sessions are not fully populated, and only contain the
// projects. Note that the sessions are not fully populated, and only contain the
// necessary information to implement the boundary.AuthzProtectedEntity
// interface. Supports the WithTerminated option.
func ListForAuthzCheck(repo *Repository, opt ...Option) listForAuthzCheckFunc {
return func(ctx context.Context, scopeIds []string) (map[string][]boundary.AuthzProtectedEntity, error) {
return repo.fetchAuthzProtectedSessionsByScope(ctx, scopeIds, opt...)
return func(ctx context.Context, projectIds []string) (map[string][]boundary.AuthzProtectedEntity, error) {
return repo.fetchAuthzProtectedSessionsByProject(ctx, projectIds, opt...)
}
}
@@ -82,7 +82,7 @@ func TestWorkerStatusReport(t *testing.T) {
TargetId: tar.GetPublicId(),
HostSetId: hs.GetPublicId(),
AuthTokenId: at.GetPublicId(),
ScopeId: prj.GetPublicId(),
ProjectId: prj.GetPublicId(),
Endpoint: "tcp://127.0.0.1:22",
ConnectionLimit: 10,
})
@@ -114,7 +114,7 @@ func TestWorkerStatusReport(t *testing.T) {
TargetId: tar.GetPublicId(),
HostSetId: hs.GetPublicId(),
AuthTokenId: at.GetPublicId(),
ScopeId: prj.GetPublicId(),
ProjectId: prj.GetPublicId(),
Endpoint: "tcp://127.0.0.1:22",
ConnectionLimit: 10,
})
@@ -148,7 +148,7 @@ func TestWorkerStatusReport(t *testing.T) {
TargetId: tar.GetPublicId(),
HostSetId: hs.GetPublicId(),
AuthTokenId: at.GetPublicId(),
ScopeId: prj.GetPublicId(),
ProjectId: prj.GetPublicId(),
Endpoint: "tcp://127.0.0.1:22",
ConnectionLimit: 10,
})
@@ -188,7 +188,7 @@ func TestWorkerStatusReport(t *testing.T) {
TargetId: tar.GetPublicId(),
HostSetId: hs.GetPublicId(),
AuthTokenId: at.GetPublicId(),
ScopeId: prj.GetPublicId(),
ProjectId: prj.GetPublicId(),
Endpoint: "tcp://127.0.0.1:22",
ConnectionLimit: 10,
})
@@ -206,7 +206,7 @@ func TestWorkerStatusReport(t *testing.T) {
TargetId: tar.GetPublicId(),
HostSetId: hs.GetPublicId(),
AuthTokenId: at.GetPublicId(),
ScopeId: prj.GetPublicId(),
ProjectId: prj.GetPublicId(),
Endpoint: "tcp://127.0.0.1:22",
ConnectionLimit: 10,
})
@@ -255,7 +255,7 @@ func TestWorkerStatusReport(t *testing.T) {
TargetId: tar.GetPublicId(),
HostSetId: hs.GetPublicId(),
AuthTokenId: at.GetPublicId(),
ScopeId: prj.GetPublicId(),
ProjectId: prj.GetPublicId(),
Endpoint: "tcp://127.0.0.1:22",
ConnectionLimit: 10,
})
@@ -271,7 +271,7 @@ func TestWorkerStatusReport(t *testing.T) {
TargetId: tar.GetPublicId(),
HostSetId: hs.GetPublicId(),
AuthTokenId: at.GetPublicId(),
ScopeId: prj.GetPublicId(),
ProjectId: prj.GetPublicId(),
Endpoint: "tcp://127.0.0.1:22",
ConnectionLimit: 10,
})
@@ -306,7 +306,7 @@ func TestWorkerStatusReport(t *testing.T) {
TargetId: tar.GetPublicId(),
HostSetId: hs.GetPublicId(),
AuthTokenId: at.GetPublicId(),
ScopeId: prj.GetPublicId(),
ProjectId: prj.GetPublicId(),
Endpoint: "tcp://127.0.0.1:22",
ConnectionLimit: 10,
})
@@ -324,7 +324,7 @@ func TestWorkerStatusReport(t *testing.T) {
TargetId: tar.GetPublicId(),
HostSetId: hs.GetPublicId(),
AuthTokenId: at.GetPublicId(),
ScopeId: prj.GetPublicId(),
ProjectId: prj.GetPublicId(),
Endpoint: "tcp://127.0.0.1:22",
ConnectionLimit: 10,
})
+11 -11
View File
@@ -36,8 +36,8 @@ type ComposedOf struct {
HostSetId string
// AuthTokenId of the session
AuthTokenId string
// ScopeId of the session
ScopeId string
// ProjectId of the session
ProjectId string
// Endpoint. This is generated by the target, but is not stored in the
// warehouse as the worker may need to e.g. resolve DNS. This is to round
// trip the information to the worker when it validates a session.
@@ -73,8 +73,8 @@ type Session struct {
HostSetId string `json:"host_set_id,omitempty" gorm:"default:null"`
// AuthTokenId for the session
AuthTokenId string `json:"auth_token_id,omitempty" gorm:"default:null"`
// ScopeId for the session
ScopeId string `json:"scope_id,omitempty" gorm:"default:null"`
// ProjectId for the session
ProjectId string `json:"project_id,omitempty" gorm:"default:null"`
// Certificate to use when connecting (or if using custom certs, to
// serve as the "login"). Raw DER bytes. Private key is not, and should not be
// stored in the database.
@@ -126,8 +126,8 @@ func (s Session) GetPublicId() string {
return s.PublicId
}
func (s Session) GetScopeId() string {
return s.ScopeId
func (s Session) GetProjectId() string {
return s.ProjectId
}
func (s Session) GetUserId() string {
@@ -149,7 +149,7 @@ func New(c ComposedOf, _ ...Option) (*Session, error) {
TargetId: c.TargetId,
HostSetId: c.HostSetId,
AuthTokenId: c.AuthTokenId,
ScopeId: c.ScopeId,
ProjectId: c.ProjectId,
Endpoint: c.Endpoint,
ExpirationTime: c.ExpirationTime,
ConnectionLimit: c.ConnectionLimit,
@@ -177,7 +177,7 @@ func (s *Session) Clone() interface{} {
TargetId: s.TargetId,
HostSetId: s.HostSetId,
AuthTokenId: s.AuthTokenId,
ScopeId: s.ScopeId,
ProjectId: s.ProjectId,
TerminationReason: s.TerminationReason,
Version: s.Version,
Endpoint: s.Endpoint,
@@ -335,8 +335,8 @@ func (s *Session) validateNewSession() error {
if s.AuthTokenId == "" {
return errors.NewDeprecated(errors.InvalidParameter, op, "missing auth token id")
}
if s.ScopeId == "" {
return errors.NewDeprecated(errors.InvalidParameter, op, "missing scope id")
if s.ProjectId == "" {
return errors.NewDeprecated(errors.InvalidParameter, op, "missing project id")
}
if s.Endpoint == "" {
return errors.NewDeprecated(errors.InvalidParameter, op, "missing endpoint")
@@ -456,7 +456,7 @@ type sessionListView struct {
TargetId string `json:"target_id,omitempty" gorm:"default:null"`
HostSetId string `json:"host_set_id,omitempty" gorm:"default:null"`
AuthTokenId string `json:"auth_token_id,omitempty" gorm:"default:null"`
ScopeId string `json:"scope_id,omitempty" gorm:"default:null"`
ProjectId string `json:"project_id,omitempty" gorm:"default:null"`
Certificate []byte `json:"certificate,omitempty" gorm:"default:null"`
ExpirationTime *timestamp.Timestamp `json:"expiration_time,omitempty" gorm:"default:null"`
CtTofuToken []byte `json:"ct_tofu_token,omitempty" gorm:"column:tofu_token;default:null" wrapping:"ct,tofu_token"`
+4 -4
View File
@@ -53,7 +53,7 @@ func TestSession_Create(t *testing.T) {
TargetId: composedOf.TargetId,
HostSetId: composedOf.HostSetId,
AuthTokenId: composedOf.AuthTokenId,
ScopeId: composedOf.ScopeId,
ProjectId: composedOf.ProjectId,
Endpoint: "tcp://127.0.0.1:22",
ExpirationTime: composedOf.ExpirationTime,
ConnectionLimit: composedOf.ConnectionLimit,
@@ -128,11 +128,11 @@ func TestSession_Create(t *testing.T) {
wantIsErr: errors.InvalidParameter,
},
{
name: "empty-scopeId",
name: "empty-projectId",
args: args{
composedOf: func() ComposedOf {
c := composedOf
c.ScopeId = ""
c.ProjectId = ""
return c
}(),
addresses: defaultAddresses,
@@ -154,7 +154,7 @@ func TestSession_Create(t *testing.T) {
TargetId: composedOf.TargetId,
HostSetId: composedOf.HostSetId,
AuthTokenId: composedOf.AuthTokenId,
ScopeId: composedOf.ScopeId,
ProjectId: composedOf.ProjectId,
Endpoint: "tcp://127.0.0.1:22",
ExpirationTime: composedOf.ExpirationTime,
ConnectionLimit: composedOf.ConnectionLimit,
+1 -1
View File
@@ -166,7 +166,7 @@ func TestSessionParams(t testing.TB, conn *db.DB, wrapper wrapping.Wrapper, iamR
TargetId: tcpTarget.GetPublicId(),
HostSetId: sets[0].PublicId,
AuthTokenId: at.PublicId,
ScopeId: tcpTarget.GetScopeId(),
ProjectId: tcpTarget.GetProjectId(),
Endpoint: "tcp://127.0.0.1:22",
ExpirationTime: &timestamp.Timestamp{Timestamp: expTime},
ConnectionLimit: tcpTarget.GetSessionConnectionLimit(),
+1 -1
View File
@@ -9,7 +9,7 @@ import (
wrapping "github.com/hashicorp/go-kms-wrapping/v2"
)
// DeriveED25519Key generates a key based on the scope's session DEK, the
// DeriveED25519Key generates a key based on the project's session DEK, the
// requesting user, and the generated job ID.
func DeriveED25519Key(ctx context.Context, wrapper wrapping.Wrapper, userId, jobId string) (ed25519.PublicKey, ed25519.PrivateKey, error) {
const op = "session.DeriveED25519Key"