* Adding explicit MPL license for sub-package. This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * Update copyright file headers to BUS-1.1 * Rerun make gen This will pick up the last of the license changes * Revert "Rerun make gen" This reverts commit dbe0968bffd92745d8663c6940debae098395a6b. * Remove copywrite from generation script The current version of copywrite is not sophisticated enough to only apply the copywrite changes to certain subdirectories, so to avoid accidentally relicensing our api and sdk modules, we will stop running the copywrite script until such time that it can do the right thing. * License protobuf API as MPL The protobuf API files are used to generate the SDK, since we want the SDK to be MPL, the protobuf files must also be MPL. This extends to all protobuf files whose sources end up in the SDK directory, including: - internal/proto/controller/api - internal/proto/controller/custom_options - internal/proto/plugin It also includes the generated service directory in internal/gen/controller/api/services, since they are generated from the protobuf sources used for the SDK. * Change website license file name This makes the naming scheme consistent with the rest of the repository. * Add note on licensing to README * Set copywrite license to BUSL * Revert "Add note on licensing to README" This reverts commit 4d865a91a7a4f11605f0dc4609355d7e3c3cfffe. --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com> Co-authored-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
332 lines
8.9 KiB
Go
332 lines
8.9 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package session
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/boundary/internal/db"
|
|
"github.com/hashicorp/boundary/internal/db/timestamp"
|
|
"github.com/hashicorp/boundary/internal/errors"
|
|
"github.com/hashicorp/boundary/internal/iam"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
|
)
|
|
|
|
func TestSession_ImmutableFields(t *testing.T) {
|
|
t.Parallel()
|
|
conn, _ := db.TestSetup(t, "postgres")
|
|
wrapper := db.TestWrapper(t)
|
|
rw := db.New(conn)
|
|
iamRepo := iam.TestRepo(t, conn, wrapper)
|
|
|
|
ts := timestamp.Timestamp{Timestamp: ×tamppb.Timestamp{Seconds: 0, Nanos: 0}}
|
|
|
|
_, _ = iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
|
new := TestDefaultSession(t, conn, wrapper, iamRepo)
|
|
|
|
tests := []struct {
|
|
name string
|
|
update *Session
|
|
fieldMask []string
|
|
}{
|
|
{
|
|
name: "public_id",
|
|
update: func() *Session {
|
|
s := new.Clone().(*Session)
|
|
s.PublicId = "o_thisIsNotAValidId"
|
|
return s
|
|
}(),
|
|
fieldMask: []string{"PublicId"},
|
|
},
|
|
{
|
|
name: "certificate",
|
|
update: func() *Session {
|
|
s := new.Clone().(*Session)
|
|
s.Certificate = []byte("fake cert for test")
|
|
return s
|
|
}(),
|
|
fieldMask: []string{"Certificate"},
|
|
},
|
|
{
|
|
name: "expiration time",
|
|
update: func() *Session {
|
|
s := new.Clone().(*Session)
|
|
s.ExpirationTime = &ts
|
|
return s
|
|
}(),
|
|
fieldMask: []string{"ExpirationTime"},
|
|
},
|
|
{
|
|
name: "create time",
|
|
update: func() *Session {
|
|
s := new.Clone().(*Session)
|
|
s.CreateTime = &ts
|
|
return s
|
|
}(),
|
|
fieldMask: []string{"CreateTime"},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
tt := tt
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
assert, require := assert.New(t), require.New(t)
|
|
orig := new.Clone()
|
|
err := rw.LookupById(context.Background(), orig)
|
|
require.NoError(err)
|
|
|
|
rowsUpdated, err := rw.Update(context.Background(), tt.update, tt.fieldMask, nil, db.WithSkipVetForWrite(true))
|
|
require.Error(err)
|
|
assert.Equal(0, rowsUpdated)
|
|
|
|
after := new.Clone()
|
|
err = rw.LookupById(context.Background(), after)
|
|
require.NoError(err)
|
|
|
|
assert.Equal(orig.(*Session), after)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestState_ImmutableFields(t *testing.T) {
|
|
t.Parallel()
|
|
conn, _ := db.TestSetup(t, "postgres")
|
|
rw := db.New(conn)
|
|
wrapper := db.TestWrapper(t)
|
|
iamRepo := iam.TestRepo(t, conn, wrapper)
|
|
|
|
ts := timestamp.Timestamp{Timestamp: ×tamppb.Timestamp{Seconds: 0, Nanos: 0}}
|
|
|
|
_, _ = iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
|
session := TestDefaultSession(t, conn, wrapper, iamRepo)
|
|
state := TestState(t, conn, session.PublicId, StatusActive)
|
|
|
|
var new State
|
|
err := rw.LookupWhere(context.Background(), &new, "session_id = ? and state = ?", []any{state.SessionId, state.Status})
|
|
require.NoError(t, err)
|
|
|
|
tests := []struct {
|
|
name string
|
|
update *State
|
|
fieldMask []string
|
|
}{
|
|
{
|
|
name: "session_id",
|
|
update: func() *State {
|
|
s := new.Clone().(*State)
|
|
s.SessionId = "s_thisIsNotAValidId"
|
|
return s
|
|
}(),
|
|
fieldMask: []string{"PublicId"},
|
|
},
|
|
{
|
|
name: "status",
|
|
update: func() *State {
|
|
s := new.Clone().(*State)
|
|
s.Status = "canceling"
|
|
return s
|
|
}(),
|
|
fieldMask: []string{"Status"},
|
|
},
|
|
{
|
|
name: "start time",
|
|
update: func() *State {
|
|
s := new.Clone().(*State)
|
|
s.StartTime = &ts
|
|
return s
|
|
}(),
|
|
fieldMask: []string{"StartTime"},
|
|
},
|
|
{
|
|
name: "previous_end_time",
|
|
update: func() *State {
|
|
s := new.Clone().(*State)
|
|
s.PreviousEndTime = &ts
|
|
return s
|
|
}(),
|
|
fieldMask: []string{"PreviousEndTime"},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
tt := tt
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
assert, require := assert.New(t), require.New(t)
|
|
orig := new.Clone()
|
|
err := rw.LookupWhere(context.Background(), orig, "session_id = ? and start_time = ?", []any{new.SessionId, new.StartTime})
|
|
require.NoError(err)
|
|
|
|
rowsUpdated, err := rw.Update(context.Background(), tt.update, tt.fieldMask, nil, db.WithSkipVetForWrite(true))
|
|
require.Error(err)
|
|
assert.Equal(0, rowsUpdated)
|
|
|
|
after := new.Clone()
|
|
err = rw.LookupWhere(context.Background(), after, "session_id = ? and start_time = ?", []any{new.SessionId, new.StartTime})
|
|
require.NoError(err)
|
|
assert.Equal(orig.(*State), after)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestConnection_ImmutableFields(t *testing.T) {
|
|
t.Parallel()
|
|
conn, _ := db.TestSetup(t, "postgres")
|
|
wrapper := db.TestWrapper(t)
|
|
rw := db.New(conn)
|
|
iamRepo := iam.TestRepo(t, conn, wrapper)
|
|
|
|
ts := timestamp.Timestamp{Timestamp: ×tamppb.Timestamp{Seconds: 0, Nanos: 0}}
|
|
|
|
_, _ = iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
|
session := TestDefaultSession(t, conn, wrapper, iamRepo)
|
|
new := TestConnection(t, conn, session.PublicId, "127.0.0.1", 22, "127.0.0.1", 2222, "127.0.0.1")
|
|
|
|
tests := []struct {
|
|
name string
|
|
update *Connection
|
|
fieldMask []string
|
|
}{
|
|
{
|
|
name: "public_id",
|
|
update: func() *Connection {
|
|
c := new.Clone().(*Connection)
|
|
c.PublicId = "sc_thisIsNotAValidId"
|
|
return c
|
|
}(),
|
|
fieldMask: []string{"PublicId"},
|
|
},
|
|
{
|
|
name: "session_id",
|
|
update: func() *Connection {
|
|
c := new.Clone().(*Connection)
|
|
c.SessionId = "s_thisIsNotAValidId"
|
|
return c
|
|
}(),
|
|
fieldMask: []string{"SessionId"},
|
|
},
|
|
{
|
|
name: "create time",
|
|
update: func() *Connection {
|
|
s := new.Clone().(*Connection)
|
|
s.CreateTime = &ts
|
|
return s
|
|
}(),
|
|
fieldMask: []string{"CreateTime"},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
tt := tt
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
assert, require := assert.New(t), require.New(t)
|
|
orig := new.Clone()
|
|
err := rw.LookupById(context.Background(), orig)
|
|
require.NoError(err)
|
|
|
|
rowsUpdated, err := rw.Update(context.Background(), tt.update, tt.fieldMask, nil, db.WithSkipVetForWrite(true))
|
|
require.Error(err)
|
|
assert.Equal(0, rowsUpdated)
|
|
|
|
after := new.Clone()
|
|
err = rw.LookupById(context.Background(), after)
|
|
require.NoError(err)
|
|
|
|
assert.Equal(orig.(*Connection), after)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestConnectionState_ImmutableFields(t *testing.T) {
|
|
t.Parallel()
|
|
conn, _ := db.TestSetup(t, "postgres")
|
|
rw := db.New(conn)
|
|
wrapper := db.TestWrapper(t)
|
|
iamRepo := iam.TestRepo(t, conn, wrapper)
|
|
|
|
ts := timestamp.Timestamp{Timestamp: ×tamppb.Timestamp{Seconds: 0, Nanos: 0}}
|
|
|
|
_, _ = iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
|
|
session := TestDefaultSession(t, conn, wrapper, iamRepo)
|
|
connection := TestConnection(t, conn, session.PublicId, "127.0.0.1", 22, "127.0.0.1", 2222, "127.0.0.1")
|
|
state := TestConnectionState(t, conn, connection.PublicId, StatusConnected)
|
|
|
|
var new ConnectionState
|
|
err := rw.LookupWhere(context.Background(), &new, "connection_id = ? and state = ?", []any{state.ConnectionId, state.Status})
|
|
require.NoError(t, err)
|
|
|
|
tests := []struct {
|
|
name string
|
|
update *ConnectionState
|
|
fieldMask []string
|
|
wantErrMatch *errors.Template
|
|
wantErrContains string
|
|
}{
|
|
{
|
|
name: "session_id",
|
|
update: func() *ConnectionState {
|
|
s := new.Clone().(*ConnectionState)
|
|
s.ConnectionId = "sc_thisIsNotAValidId"
|
|
return s
|
|
}(),
|
|
fieldMask: []string{"PublicId"},
|
|
},
|
|
{
|
|
name: "status",
|
|
update: func() *ConnectionState {
|
|
s := new.Clone().(*ConnectionState)
|
|
s.Status = "closed"
|
|
return s
|
|
}(),
|
|
fieldMask: []string{"Status"},
|
|
wantErrMatch: errors.T(errors.NotSpecificIntegrity),
|
|
wantErrContains: "immutable column",
|
|
},
|
|
{
|
|
name: "start time",
|
|
update: func() *ConnectionState {
|
|
s := new.Clone().(*ConnectionState)
|
|
s.StartTime = &ts
|
|
return s
|
|
}(),
|
|
fieldMask: []string{"StartTime"},
|
|
wantErrMatch: errors.T(errors.InvalidFieldMask),
|
|
wantErrContains: "parameter violation",
|
|
},
|
|
{
|
|
name: "previous_end_time",
|
|
update: func() *ConnectionState {
|
|
s := new.Clone().(*ConnectionState)
|
|
s.PreviousEndTime = &ts
|
|
return s
|
|
}(),
|
|
fieldMask: []string{"PreviousEndTime"},
|
|
wantErrMatch: errors.T(errors.NotSpecificIntegrity),
|
|
wantErrContains: "immutable column",
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
tt := tt
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
assert, require := assert.New(t), require.New(t)
|
|
orig := new.Clone()
|
|
err := rw.LookupWhere(context.Background(), orig, "connection_id = ? and start_time = ?", []any{new.ConnectionId, new.StartTime})
|
|
require.NoError(err)
|
|
|
|
rowsUpdated, err := rw.Update(context.Background(), tt.update, tt.fieldMask, nil, db.WithSkipVetForWrite(true))
|
|
require.Error(err)
|
|
assert.Equal(0, rowsUpdated)
|
|
if tt.wantErrMatch != nil {
|
|
assert.Truef(errors.Match(tt.wantErrMatch, err), "wanted error %s and got: %s", tt.wantErrMatch.Code, err.Error())
|
|
}
|
|
if tt.wantErrContains != "" {
|
|
assert.Contains(err.Error(), tt.wantErrContains)
|
|
}
|
|
after := new.Clone()
|
|
err = rw.LookupWhere(context.Background(), after, "connection_id = ? and start_time = ?", []any{new.ConnectionId, new.StartTime})
|
|
require.NoError(err)
|
|
assert.Equal(orig.(*ConnectionState), after)
|
|
})
|
|
}
|
|
}
|