Add connection close call from worker to controller (#387)

This commit is contained in:
Jeff Mitchell authored and GitHub committed 2020-09-21 00:09:50 -04:00
1 parent 7ff4b7f106
commit 9cbc7b5c3d
11 files changed
+671 -197

No files matched your search

+4 -8
View File
@@ -9,20 +9,16 @@ import (
// CloseWith defines the boundary data that is saved in the repo when the
// worker closes a connection between the client and the endpoint.
type CloseWith struct {
ConnectionId string
ConnectionVersion uint32
BytesUp uint64
BytesDown uint64
ClosedReason ClosedReason
ConnectionId string
BytesUp uint64
BytesDown uint64
ClosedReason ClosedReason
}
func (c CloseWith) validate() error {
if c.ConnectionId == "" {
return fmt.Errorf("missing connection id: %w", db.ErrInvalidParameter)
}
if c.ConnectionVersion == 0 {
return fmt.Errorf("missing connection version: %w", db.ErrInvalidParameter)
}
if c.ClosedReason.String() == "" {
return fmt.Errorf("missing closed reason: %w", db.ErrInvalidParameter)
}
+15 -30
View File
@@ -14,11 +14,10 @@ func TestClosedWith_validate(t *testing.T) {
session := TestDefaultSession(t, conn, wrapper, iamRepo)
sessionConnection := TestConnection(t, conn, session.PublicId, "127.0.0.1", 22, "127.0.0.1", 2222)
type fields struct {
ConnectionId string
ConnectionVersion uint32
BytesUp uint64
BytesDown uint64
ClosedReason ClosedReason
ConnectionId string
BytesUp uint64
BytesDown uint64
ClosedReason ClosedReason
}
tests := []struct {
name string
@@ -28,27 +27,15 @@ func TestClosedWith_validate(t *testing.T) {
{
name: "valid",
fields: fields{
ConnectionId: sessionConnection.PublicId,
ConnectionVersion: sessionConnection.Version,
BytesUp: 1,
BytesDown: 2,
ClosedReason: ConnectionClosedByUser,
ConnectionId: sessionConnection.PublicId,
BytesUp: 1,
BytesDown: 2,
ClosedReason: ConnectionClosedByUser,
},
},
{
name: "missing-ConnectionId",
fields: fields{
ConnectionVersion: sessionConnection.Version,
BytesUp: 1,
BytesDown: 2,
ClosedReason: ConnectionClosedByUser,
},
wantErr: true,
},
{
name: "missing-ConnectionVersion",
fields: fields{
ConnectionId: sessionConnection.PublicId,
BytesUp: 1,
BytesDown: 2,
ClosedReason: ConnectionClosedByUser,
@@ -58,10 +45,9 @@ func TestClosedWith_validate(t *testing.T) {
{
name: "missing-ClosedReason",
fields: fields{
ConnectionId: sessionConnection.PublicId,
ConnectionVersion: sessionConnection.Version,
BytesUp: 1,
BytesDown: 2,
ConnectionId: sessionConnection.PublicId,
BytesUp: 1,
BytesDown: 2,
},
wantErr: true,
},
@@ -69,11 +55,10 @@ func TestClosedWith_validate(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := CloseWith{
ConnectionId: tt.fields.ConnectionId,
ConnectionVersion: tt.fields.ConnectionVersion,
BytesUp: tt.fields.BytesUp,
BytesDown: tt.fields.BytesDown,
ClosedReason: tt.fields.ClosedReason,
ConnectionId: tt.fields.ConnectionId,
BytesUp: tt.fields.BytesUp,
BytesDown: tt.fields.BytesDown,
ClosedReason: tt.fields.ClosedReason,
}
if err := c.validate(); (err != nil) != tt.wantErr {
t.Errorf("ClosedWith.validate() error = %v, wantErr %v", err, tt.wantErr)
+2 -3
View File
@@ -409,8 +409,8 @@ func (r *Repository) sessionAuthzSummary(ctx context.Context, sessionId string)
return info, nil
}
// ConnectSession updates a connection in the repo with a state of "connected".
func (r *Repository) ConnectSession(ctx context.Context, c ConnectWith) (*Connection, []*ConnectionState, error) {
// ConnectConnection updates a connection in the repo with a state of "connected".
func (r *Repository) ConnectConnection(ctx context.Context, c ConnectWith) (*Connection, []*ConnectionState, error) {
// ConnectWith.validate will check all the fields...
if err := c.validate(); err != nil {
return nil, nil, fmt.Errorf("connect session: %w", err)
@@ -500,7 +500,6 @@ func (r *Repository) CloseConnections(ctx context.Context, closeWith []CloseWith
&updateConnection,
[]string{"BytesUp", "BytesDown", "ClosedReason"},
nil,
db.WithVersion(&cw.ConnectionVersion),
)
if err != nil {
return fmt.Errorf("unable to update connection %s: %w", cw.ConnectionId, err)
+6 -19
View File
@@ -491,7 +491,7 @@ func TestRepository_AuthorizeConnect(t *testing.T) {
}
}
func TestRepository_ConnectSession(t *testing.T) {
func TestRepository_ConnectConnection(t *testing.T) {
t.Parallel()
conn, _ := db.TestSetup(t, "postgres")
rw := db.New(conn)
@@ -581,7 +581,7 @@ func TestRepository_ConnectSession(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
c, cs, err := repo.ConnectSession(context.Background(), tt.connectWith)
c, cs, err := repo.ConnectConnection(context.Background(), tt.connectWith)
if tt.wantErr {
require.Error(err)
if tt.wantIsError != nil {
@@ -704,11 +704,10 @@ func TestRepository_CloseConnections(t *testing.T) {
c := TestConnection(t, conn, s.PublicId, "127.0.0.1", 22, "127.0.0.1", 2222)
require.NoError(t, err)
cw = append(cw, CloseWith{
ConnectionId: c.PublicId,
ConnectionVersion: c.Version,
BytesUp: 1,
BytesDown: 2,
ClosedReason: ConnectionClosedByUser,
ConnectionId: c.PublicId,
BytesUp: 1,
BytesDown: 2,
ClosedReason: ConnectionClosedByUser,
})
}
return cw
@@ -744,18 +743,6 @@ func TestRepository_CloseConnections(t *testing.T) {
wantErr: true,
wantIsError: db.ErrInvalidParameter,
},
{
name: "ConnectionVersion",
closeWith: func() []CloseWith {
cw := setupFn(2)
cw[1].ConnectionVersion = 0
return cw
}(),
reason: ClosedByUser,
wantErr: true,
wantIsError: db.ErrInvalidParameter,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {