feat: Add support for correlationId

This commit is contained in:
Louis Ruch committed 2024-04-11 09:08:45 -07:00
1 parent f2c2a774c6
commit c376d06355
31 files changed
+726 -57

No files matched your search

@@ -32,6 +32,7 @@ import (
"github.com/hashicorp/boundary/internal/types/action"
"github.com/hashicorp/boundary/internal/types/resource"
wrapping "github.com/hashicorp/go-kms-wrapping/v2"
"github.com/hashicorp/go-uuid"
"github.com/jackc/pgconn"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@@ -501,6 +502,31 @@ func TestRepository_CreateSession(t *testing.T) {
workerAddresses: workerAddresses,
},
},
{
name: "valid-with-correlation-id",
args: args{
composedOf: func() ComposedOf {
c := TestSessionParams(t, conn, wrapper, iamRepo)
c.CorrelationId, err = uuid.GenerateUUID()
require.NoError(t, err)
return c
}(),
workerAddresses: workerAddresses,
},
},
{
name: "invalid-correlation-id",
args: args{
composedOf: func() ComposedOf {
c := TestSessionParams(t, conn, wrapper, iamRepo)
c.CorrelationId = "invalid-format"
return c
}(),
workerAddresses: workerAddresses,
},
wantErr: true,
wantIsError: errors.InvalidTextRepresentation,
},
{
name: "invalid-protocol-worker",
args: args{
@@ -642,6 +668,7 @@ func TestRepository_CreateSession(t *testing.T) {
DynamicCredentials: tt.args.composedOf.DynamicCredentials,
StaticCredentials: tt.args.composedOf.StaticCredentials,
ProtocolWorkerId: tt.args.composedOf.ProtocolWorkerId,
CorrelationId: tt.args.composedOf.CorrelationId,
}
ses, err := repo.CreateSession(context.Background(), wrapper, s, tt.args.workerAddresses)
+2
View File
@@ -158,5 +158,7 @@ func TestRepository_convertToSessions(t *testing.T) {
sess.CtTofuToken = nil
sess.TofuToken = nil
sess.KeyId = ""
// CorrelationId is an internal attribute and not returned
sess.CorrelationId = ""
assert.Equal(t, sessions[0], sess)
}
+1 -1
View File
@@ -69,7 +69,7 @@ func TestService_List(t *testing.T) {
require.NoError(t, err)
cmpIgnoreUnexportedOpts := cmpopts.IgnoreUnexported(session.Session{}, session.State{}, timestamp.Timestamp{}, timestamppb.Timestamp{})
cmpIgnoreFieldsOpts := cmpopts.IgnoreFields(session.Session{}, "CtCertificatePrivateKey", "CertificatePrivateKey", "KeyId")
cmpIgnoreFieldsOpts := cmpopts.IgnoreFields(session.Session{}, "CtCertificatePrivateKey", "CertificatePrivateKey", "KeyId", "CorrelationId")
t.Run("List validation", func(t *testing.T) {
t.Parallel()
+10
View File
@@ -66,6 +66,8 @@ type ComposedOf struct {
StaticCredentials []*StaticCredential
// Which worker is performing protocol-related tasks
ProtocolWorkerId string
// CorrelationId is the CorrelationId of the authorize-session request
CorrelationId string `json:"-" gorm:"default:null"`
}
// Session contains information about a user's session with a target
@@ -138,6 +140,9 @@ type Session struct {
// Connections for the session are for read only and are ignored during write operations
Connections []*Connection `gorm:"-"`
// CorrelationId is the CorrelationId of the authorize-session request
CorrelationId string `json:"-" gorm:"default:null"`
tableName string `gorm:"-"`
}
@@ -205,6 +210,7 @@ func New(ctx context.Context, c ComposedOf, _ ...Option) (*Session, error) {
DynamicCredentials: c.DynamicCredentials,
StaticCredentials: c.StaticCredentials,
ProtocolWorkerId: c.ProtocolWorkerId,
CorrelationId: c.CorrelationId,
}
if err := s.validateNewSession(ctx); err != nil {
return nil, errors.Wrap(ctx, err, op)
@@ -236,6 +242,7 @@ func (s *Session) Clone() any {
IngressWorkerFilter: s.IngressWorkerFilter,
KeyId: s.KeyId,
ProtocolWorkerId: s.ProtocolWorkerId,
CorrelationId: s.CorrelationId,
}
if len(s.States) > 0 {
clone.States = make([]*State, 0, len(s.States))
@@ -413,6 +420,9 @@ func (s *Session) validateNewSession(ctx context.Context) error {
if s.CtTofuToken != nil {
return errors.New(ctx, errors.InvalidParameter, op, "ct must be empty")
}
if s.CorrelationId == "" {
return errors.New(ctx, errors.InvalidParameter, op, "missing correlation id")
}
// It is okay for the worker filter and protocol worker ID to be empty, so
// they are not checked here.
return nil
+27
View File
@@ -18,6 +18,7 @@ import (
"github.com/hashicorp/boundary/internal/db/timestamp"
"github.com/hashicorp/boundary/internal/errors"
"github.com/hashicorp/boundary/internal/iam"
"github.com/hashicorp/go-uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/types/known/timestamppb"
@@ -67,6 +68,7 @@ func TestSession_Create(t *testing.T) {
ConnectionLimit: composedOf.ConnectionLimit,
DynamicCredentials: composedOf.DynamicCredentials,
StaticCredentials: composedOf.StaticCredentials,
CorrelationId: composedOf.CorrelationId,
},
create: true,
},
@@ -94,6 +96,7 @@ func TestSession_Create(t *testing.T) {
ConnectionLimit: composedOf.ConnectionLimit,
DynamicCredentials: composedOf.DynamicCredentials,
StaticCredentials: composedOf.StaticCredentials,
CorrelationId: composedOf.CorrelationId,
},
create: true,
},
@@ -164,6 +167,19 @@ func TestSession_Create(t *testing.T) {
wantErr: true,
wantIsErr: errors.InvalidParameter,
},
{
name: "empty-correlationId",
args: args{
composedOf: func() ComposedOf {
c := composedOf
c.CorrelationId = ""
return c
}(),
addresses: defaultAddresses,
},
wantErr: true,
wantIsErr: errors.InvalidParameter,
},
{
name: "empty-addresses",
args: args{
@@ -184,6 +200,7 @@ func TestSession_Create(t *testing.T) {
ConnectionLimit: composedOf.ConnectionLimit,
DynamicCredentials: composedOf.DynamicCredentials,
StaticCredentials: composedOf.StaticCredentials,
CorrelationId: composedOf.CorrelationId,
},
wantAddrErr: true,
wantIsErr: errors.InvalidParameter,
@@ -303,6 +320,16 @@ func TestSession_Clone(t *testing.T) {
cp := s.Clone()
assert.Equal(cp.(*Session), s)
})
t.Run("with-correlation-id", func(t *testing.T) {
assert := assert.New(t)
composedOf := TestSessionParams(t, conn, wrapper, iamRepo)
corId, err := uuid.GenerateUUID()
require.NoError(t, err)
composedOf.CorrelationId = corId
s := TestSession(t, conn, wrapper, composedOf)
cp := s.Clone()
assert.Equal(cp.(*Session), s)
})
t.Run("not-equal", func(t *testing.T) {
assert := assert.New(t)
s := TestDefaultSession(t, conn, wrapper, iamRepo)
+12
View File
@@ -22,6 +22,7 @@ import (
"github.com/hashicorp/boundary/internal/target/tcp"
wrapping "github.com/hashicorp/go-kms-wrapping/v2"
"github.com/hashicorp/go-secure-stdlib/base62"
"github.com/hashicorp/go-uuid"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/types/known/timestamppb"
)
@@ -105,6 +106,11 @@ func TestSession(t testing.TB, conn *db.DB, rootWrapper wrapping.Wrapper, c Comp
future := timestamppb.New(time.Now().Add(time.Hour))
c.ExpirationTime = &timestamp.Timestamp{Timestamp: future}
}
if c.CorrelationId == "" {
correlationId, err := uuid.GenerateUUID()
require.NoError(err)
c.CorrelationId = correlationId
}
rw := db.New(conn)
s, err := New(ctx, c, opt...)
require.NoError(err)
@@ -191,6 +197,8 @@ func TestSessionTargetAddressParams(t testing.TB, conn *db.DB, wrapper wrapping.
expTime := timestamppb.Now()
expTime.Seconds += int64(tcpTarget.GetSessionMaxSeconds())
correlationId, err := uuid.GenerateUUID()
require.NoError(err)
return ComposedOf{
UserId: user.PublicId,
TargetId: tcpTarget.GetPublicId(),
@@ -199,6 +207,7 @@ func TestSessionTargetAddressParams(t testing.TB, conn *db.DB, wrapper wrapping.
Endpoint: "tcp://127.0.0.1:22",
ExpirationTime: &timestamp.Timestamp{Timestamp: expTime},
ConnectionLimit: tcpTarget.GetSessionConnectionLimit(),
CorrelationId: correlationId,
}
}
@@ -245,6 +254,8 @@ func TestSessionParams(t testing.TB, conn *db.DB, wrapper wrapping.Wrapper, iamR
expTime := timestamppb.Now()
expTime.Seconds += int64(tcpTarget.GetSessionMaxSeconds())
correlationId, err := uuid.GenerateUUID()
require.NoError(err)
return ComposedOf{
UserId: user.PublicId,
HostId: hosts[0].PublicId,
@@ -255,6 +266,7 @@ func TestSessionParams(t testing.TB, conn *db.DB, wrapper wrapping.Wrapper, iamR
Endpoint: "tcp://127.0.0.1:22",
ExpirationTime: &timestamp.Timestamp{Timestamp: expTime},
ConnectionLimit: tcpTarget.GetSessionConnectionLimit(),
CorrelationId: correlationId,
}
}