diff --git a/internal/session/session.go b/internal/session/session.go index 1924b14d88..bfa713d85b 100644 --- a/internal/session/session.go +++ b/internal/session/session.go @@ -3,6 +3,7 @@ package session import ( "context" "fmt" + "strconv" "strings" "github.com/hashicorp/boundary/internal/db" @@ -14,6 +15,18 @@ const ( DefaultSessionTableName = "session" ) +type ComposedOf struct { + UserId string + HostId string + ServerId string + ServerType string + TargetId string + HostSetId string + AuthTokenId string + ScopeId string + Address string + Port string +} type Session struct { *store.Session tableName string `gorm:"-"` @@ -24,30 +37,19 @@ var _ db.VetForWriter = (*Session)(nil) // New creates a new in memory session. No options // are currently supported. -func New( - userId, - hostId, - serverId, - serverType, - targetId, - hostSetId, - authTokenId, - scopeId, - address, - port string, - opt ...Option) (*Session, error) { +func New(c ComposedOf, opt ...Option) (*Session, error) { s := Session{ Session: &store.Session{ - UserId: userId, - HostId: hostId, - ServerId: serverId, - ServerType: serverType, - TargetId: targetId, - SetId: hostSetId, - AuthTokenId: authTokenId, - ScopeId: scopeId, - Address: address, - Port: port, + UserId: c.UserId, + HostId: c.HostId, + ServerId: c.ServerId, + ServerType: c.ServerType, + TargetId: c.TargetId, + SetId: c.HostSetId, + AuthTokenId: c.AuthTokenId, + ScopeId: c.ScopeId, + Address: c.Address, + Port: c.Port, }, } @@ -172,6 +174,9 @@ func (s *Session) validateNewSession(errorPrefix string) error { return fmt.Errorf("session vet for write: %w", db.ErrInvalidParameter) } } + if _, err := strconv.ParseUint(s.Port, 10, 16); err != nil { + return fmt.Errorf("%s invalid port %s: %w", errorPrefix, s.Port, db.ErrInvalidParameter) + } return nil } diff --git a/internal/session/session_test.go b/internal/session/session_test.go index 5439d8f229..fad0d1dd57 100644 --- a/internal/session/session_test.go +++ b/internal/session/session_test.go @@ -19,28 +19,10 @@ func TestSession_Create(t *testing.T) { wrapper := db.TestWrapper(t) iamRepo := iam.TestRepo(t, conn, wrapper) - userId, - hostId, - serverId, - serverType, - targetId, - hostSetId, - authTokenId, - scopeId, - address, - port := TestSessionParams(t, conn, wrapper, iamRepo) + composedOf := TestSessionParams(t, conn, wrapper, iamRepo) type args struct { - userId string - hostId string - serverId string - serverType string - targetId string - hostSetId string - authTokenId string - scopeId string - address string - port string + composedOf ComposedOf } tests := []struct { name string @@ -54,29 +36,20 @@ func TestSession_Create(t *testing.T) { { name: "valid", args: args{ - userId: userId, - hostId: hostId, - serverId: serverId, - serverType: serverType, - targetId: targetId, - hostSetId: hostSetId, - authTokenId: authTokenId, - scopeId: scopeId, - address: address, - port: port, + composedOf: composedOf, }, want: &Session{ Session: &store.Session{ - UserId: userId, - HostId: hostId, - ServerId: serverId, - ServerType: serverType, - TargetId: targetId, - SetId: hostSetId, - AuthTokenId: authTokenId, - ScopeId: scopeId, - Address: address, - Port: port, + UserId: composedOf.UserId, + HostId: composedOf.HostId, + ServerId: composedOf.ServerId, + ServerType: composedOf.ServerType, + TargetId: composedOf.TargetId, + SetId: composedOf.HostSetId, + AuthTokenId: composedOf.AuthTokenId, + ScopeId: composedOf.ScopeId, + Address: composedOf.Address, + Port: composedOf.Port, }, }, create: true, @@ -84,15 +57,11 @@ func TestSession_Create(t *testing.T) { { name: "empty-userId", args: args{ - hostId: hostId, - serverId: serverId, - serverType: serverType, - targetId: targetId, - hostSetId: hostSetId, - authTokenId: authTokenId, - scopeId: scopeId, - address: address, - port: port, + composedOf: func() ComposedOf { + c := composedOf + c.UserId = "" + return c + }(), }, wantErr: true, wantIsErr: db.ErrInvalidParameter, @@ -100,15 +69,11 @@ func TestSession_Create(t *testing.T) { { name: "empty-hostId", args: args{ - userId: userId, - serverId: serverId, - serverType: serverType, - targetId: targetId, - hostSetId: hostSetId, - authTokenId: authTokenId, - scopeId: scopeId, - address: address, - port: port, + composedOf: func() ComposedOf { + c := composedOf + c.HostId = "" + return c + }(), }, wantErr: true, wantIsErr: db.ErrInvalidParameter, @@ -116,15 +81,11 @@ func TestSession_Create(t *testing.T) { { name: "empty-serverId", args: args{ - userId: userId, - hostId: hostId, - serverType: serverType, - targetId: targetId, - hostSetId: hostSetId, - authTokenId: authTokenId, - scopeId: scopeId, - address: address, - port: port, + composedOf: func() ComposedOf { + c := composedOf + c.ServerId = "" + return c + }(), }, wantErr: true, wantIsErr: db.ErrInvalidParameter, @@ -132,15 +93,11 @@ func TestSession_Create(t *testing.T) { { name: "empty-serverType", args: args{ - userId: userId, - hostId: hostId, - serverId: serverId, - targetId: targetId, - hostSetId: hostSetId, - authTokenId: authTokenId, - scopeId: scopeId, - address: address, - port: port, + composedOf: func() ComposedOf { + c := composedOf + c.ServerType = "" + return c + }(), }, wantErr: true, wantIsErr: db.ErrInvalidParameter, @@ -148,15 +105,11 @@ func TestSession_Create(t *testing.T) { { name: "empty-targetId", args: args{ - userId: userId, - hostId: hostId, - serverId: serverId, - serverType: serverType, - hostSetId: hostSetId, - authTokenId: authTokenId, - scopeId: scopeId, - address: address, - port: port, + composedOf: func() ComposedOf { + c := composedOf + c.TargetId = "" + return c + }(), }, wantErr: true, wantIsErr: db.ErrInvalidParameter, @@ -164,15 +117,11 @@ func TestSession_Create(t *testing.T) { { name: "empty-hostSetId", args: args{ - userId: userId, - hostId: hostId, - serverId: serverId, - serverType: serverType, - targetId: targetId, - authTokenId: authTokenId, - scopeId: scopeId, - address: address, - port: port, + composedOf: func() ComposedOf { + c := composedOf + c.HostSetId = "" + return c + }(), }, wantErr: true, wantIsErr: db.ErrInvalidParameter, @@ -180,15 +129,11 @@ func TestSession_Create(t *testing.T) { { name: "empty-authTokenId", args: args{ - userId: userId, - hostId: hostId, - serverId: serverId, - serverType: serverType, - targetId: targetId, - hostSetId: hostSetId, - scopeId: scopeId, - address: address, - port: port, + composedOf: func() ComposedOf { + c := composedOf + c.AuthTokenId = "" + return c + }(), }, wantErr: true, wantIsErr: db.ErrInvalidParameter, @@ -196,15 +141,11 @@ func TestSession_Create(t *testing.T) { { name: "empty-scopeId", args: args{ - userId: userId, - hostId: hostId, - serverId: serverId, - serverType: serverType, - targetId: targetId, - hostSetId: hostSetId, - authTokenId: authTokenId, - address: address, - port: port, + composedOf: func() ComposedOf { + c := composedOf + c.ScopeId = "" + return c + }(), }, wantErr: true, wantIsErr: db.ErrInvalidParameter, @@ -212,15 +153,11 @@ func TestSession_Create(t *testing.T) { { name: "empty-address", args: args{ - userId: userId, - hostId: hostId, - serverId: serverId, - serverType: serverType, - targetId: targetId, - hostSetId: hostSetId, - authTokenId: authTokenId, - scopeId: scopeId, - port: port, + composedOf: func() ComposedOf { + c := composedOf + c.Address = "" + return c + }(), }, wantErr: true, wantIsErr: db.ErrInvalidParameter, @@ -228,15 +165,11 @@ func TestSession_Create(t *testing.T) { { name: "empty-port", args: args{ - userId: userId, - hostId: hostId, - serverId: serverId, - serverType: serverType, - targetId: targetId, - hostSetId: hostSetId, - authTokenId: authTokenId, - scopeId: scopeId, - address: address, + composedOf: func() ComposedOf { + c := composedOf + c.Port = "" + return c + }(), }, wantErr: true, wantIsErr: db.ErrInvalidParameter, @@ -245,18 +178,7 @@ func TestSession_Create(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { assert, require := assert.New(t), require.New(t) - got, err := New( - tt.args.userId, - tt.args.hostId, - tt.args.serverId, - tt.args.serverType, - tt.args.targetId, - tt.args.hostSetId, - tt.args.authTokenId, - tt.args.scopeId, - tt.args.address, - tt.args.port, - ) + got, err := New(tt.args.composedOf) if tt.wantErr { require.Error(err) assert.True(errors.Is(err, tt.wantIsErr)) diff --git a/internal/session/testing.go b/internal/session/testing.go index 6c459d84f4..3d253ee327 100644 --- a/internal/session/testing.go +++ b/internal/session/testing.go @@ -30,36 +30,11 @@ func TestState(t *testing.T, conn *gorm.DB, sessionId string, state Status) *Sta return s } -func TestSession( - t *testing.T, - conn *gorm.DB, - userId, - hostId, - serverId, - serverType, - targetId, - hostSetId, - authTokenId, - scopeId, - address, - port string, - opt ...Option) *Session { +func TestSession(t *testing.T, conn *gorm.DB, c ComposedOf, opt ...Option) *Session { t.Helper() require := require.New(t) rw := db.New(conn) - s, err := New( - userId, - hostId, - serverId, - serverType, - targetId, - hostSetId, - authTokenId, - scopeId, - address, - port, - opt..., - ) + s, err := New(c, opt...) require.NoError(err) id, err := newId() require.NoError(err) @@ -71,42 +46,11 @@ func TestSession( func TestDefaultSession(t *testing.T, conn *gorm.DB, wrapper wrapping.Wrapper, iamRepo *iam.Repository, opt ...Option) *Session { t.Helper() - userId, - hostId, - serverId, - serverType, - targetId, - hostSetId, - authTokenId, - scopeId, - address, - port := TestSessionParams(t, conn, wrapper, iamRepo) - return TestSession( - t, - conn, - userId, - hostId, - serverId, - serverType, - targetId, - hostSetId, - authTokenId, - scopeId, - address, - port) + composedOf := TestSessionParams(t, conn, wrapper, iamRepo) + return TestSession(t, conn, composedOf) } -func TestSessionParams(t *testing.T, conn *gorm.DB, wrapper wrapping.Wrapper, iamRepo *iam.Repository) ( - userId, - hostId, - serverId, - serverType, - targetId, - hostSetId, - authTokenId, - scopeId, - address, - port string) { +func TestSessionParams(t *testing.T, conn *gorm.DB, wrapper wrapping.Wrapper, iamRepo *iam.Repository) ComposedOf { t.Helper() ctx := context.Background() @@ -152,16 +96,18 @@ func TestSessionParams(t *testing.T, conn *gorm.DB, wrapper wrapping.Wrapper, ia _, _, err = serversRepo.UpsertServer(ctx, worker) require.NoError(err) - return user.PublicId, - hosts[0].PublicId, - worker.PrivateId, - worker.Type, - tcpTarget.PublicId, - sets[0].PublicId, - at.PublicId, - tcpTarget.ScopeId, - "127.0.0.1", - "22" + return ComposedOf{ + UserId: user.PublicId, + HostId: hosts[0].PublicId, + ServerId: worker.PrivateId, + ServerType: worker.Type, + TargetId: tcpTarget.PublicId, + HostSetId: sets[0].PublicId, + AuthTokenId: at.PublicId, + ScopeId: tcpTarget.ScopeId, + Address: "127.0.0.1", + Port: "22", + } } func testId(t *testing.T) string {