From a077be691c7c115af3d488790b271fdd5c29445f Mon Sep 17 00:00:00 2001 From: Todd Date: Tue, 7 Nov 2023 09:18:05 -0800 Subject: [PATCH] Add scope id and target id as searchable fields (#4012) --- .../internal/cache/repository_sessions.go | 28 +++++++++++-------- .../cache/repository_sessions_test.go | 26 ++++++++++++++++- .../internal/cache/repository_targets.go | 2 ++ .../internal/cache/repository_targets_test.go | 14 +++++++++- .../clientcache/internal/cache/store_test.go | 18 +++++++++--- internal/clientcache/internal/db/schema.sql | 3 ++ 6 files changed, 73 insertions(+), 18 deletions(-) diff --git a/internal/clientcache/internal/cache/repository_sessions.go b/internal/clientcache/internal/cache/repository_sessions.go index c60cb7d63c..ec7ce7db60 100644 --- a/internal/clientcache/internal/cache/repository_sessions.go +++ b/internal/clientcache/internal/cache/repository_sessions.go @@ -218,12 +218,14 @@ func upsertSessions(ctx context.Context, w db.Writer, u *user, in []*sessions.Se return errors.Wrap(ctx, err, op) } newSession := &Session{ - UserId: u.Id, - Id: s.Id, - Type: s.Type, - Status: s.Status, - Endpoint: s.Endpoint, - Item: string(item), + UserId: u.Id, + Id: s.Id, + Type: s.Type, + Status: s.Status, + Endpoint: s.Endpoint, + ScopeId: s.ScopeId, + TargetId: s.TargetId, + Item: string(item), } onConflict := db.OnConflict{ Target: db.Columns{"user_id", "id"}, @@ -310,12 +312,14 @@ func (r *Repository) searchSessions(ctx context.Context, condition string, searc } type Session struct { - UserId string `gorm:"primaryKey"` - Id string `gorm:"primaryKey"` - Type string `gorm:"default:null"` - Endpoint string `gorm:"default:null"` - Status string `gorm:"default:null"` - Item string `gorm:"default:null"` + UserId string `gorm:"primaryKey"` + Id string `gorm:"primaryKey"` + Type string `gorm:"default:null"` + Endpoint string `gorm:"default:null"` + Status string `gorm:"default:null"` + ScopeId string `gorm:"default:null"` + TargetId string `gorm:"default:null"` + Item string `gorm:"default:null"` } func (*Session) TableName() string { diff --git a/internal/clientcache/internal/cache/repository_sessions_test.go b/internal/clientcache/internal/cache/repository_sessions_test.go index e558c15156..75477f5cd6 100644 --- a/internal/clientcache/internal/cache/repository_sessions_test.go +++ b/internal/clientcache/internal/cache/repository_sessions_test.go @@ -54,18 +54,24 @@ func TestRepository_refreshSessions(t *testing.T) { Id: "ttcp_1", Status: "status1", Endpoint: "address1", + ScopeId: "p_123", + TargetId: "ttcp_123", Type: "tcp", }, { Id: "ttcp_2", Status: "status2", Endpoint: "address2", + ScopeId: "p_123", + TargetId: "ttcp_123", Type: "tcp", }, { Id: "ttcp_3", Status: "status3", Endpoint: "address3", + ScopeId: "p_123", + TargetId: "ttcp_123", Type: "tcp", }, } @@ -172,12 +178,16 @@ func TestRepository_RefreshSessions_withRefreshTokens(t *testing.T) { Id: "ttcp_1", Status: "status1", Endpoint: "address1", + ScopeId: "p_123", + TargetId: "ttcp_123", Type: "tcp", }, { Id: "ttcp_2", Status: "status2", Endpoint: "address2", + ScopeId: "p_123", + TargetId: "ttcp_123", Type: "tcp", }, }, @@ -186,6 +196,8 @@ func TestRepository_RefreshSessions_withRefreshTokens(t *testing.T) { Id: "ttcp_3", Status: "status3", Endpoint: "address3", + ScopeId: "p_123", + TargetId: "ttcp_123", Type: "tcp", }, }, @@ -283,18 +295,24 @@ func TestRepository_ListSessions(t *testing.T) { Id: "ttcp_1", Status: "status1", Endpoint: "address1", + ScopeId: "p_123", + TargetId: "ttcp_123", Type: "tcp", }, { Id: "ttcp_2", Status: "status2", Endpoint: "address2", + ScopeId: "p_123", + TargetId: "ttcp_123", Type: "tcp", }, { Id: "ttcp_3", Status: "status3", Endpoint: "address3", + ScopeId: "p_123", + TargetId: "ttcp_123", Type: "tcp", }, } @@ -357,7 +375,7 @@ func TestRepository_QuerySessions(t *testing.T) { require.NoError(t, r.AddKeyringToken(ctx, addr, kt1)) require.NoError(t, r.AddKeyringToken(ctx, addr, kt2)) - query := "status % status1 or status % status2" + query := `(status % "status1" or status % "status2") and target_id % "ttcp_"` errorCases := []struct { name string @@ -390,18 +408,24 @@ func TestRepository_QuerySessions(t *testing.T) { Id: "ttcp_1", Status: "status1", Endpoint: "address1", + ScopeId: "p_123", + TargetId: "ttcp_123", Type: "tcp", }, { Id: "ttcp_2", Status: "status2", Endpoint: "address2", + ScopeId: "p_123", + TargetId: "ttcp_123", Type: "tcp", }, { Id: "ttcp_3", Status: "status3", Endpoint: "address3", + ScopeId: "p_123", + TargetId: "ttcp_123", Type: "tcp", }, } diff --git a/internal/clientcache/internal/cache/repository_targets.go b/internal/clientcache/internal/cache/repository_targets.go index c517b352e2..f4fa8d25d3 100644 --- a/internal/clientcache/internal/cache/repository_targets.go +++ b/internal/clientcache/internal/cache/repository_targets.go @@ -221,6 +221,7 @@ func upsertTargets(ctx context.Context, w db.Writer, u *user, in []*targets.Targ Name: t.Name, Description: t.Description, Address: t.Address, + ScopeId: t.ScopeId, Item: string(item), } onConflict := db.OnConflict{ @@ -313,6 +314,7 @@ type Target struct { Name string `gorm:"default:null"` Description string `gorm:"default:null"` Address string `gorm:"default:null"` + ScopeId string `gorm:"default:null"` Item string `gorm:"default:null"` } diff --git a/internal/clientcache/internal/cache/repository_targets_test.go b/internal/clientcache/internal/cache/repository_targets_test.go index 4eaf854664..99cc9f87d9 100644 --- a/internal/clientcache/internal/cache/repository_targets_test.go +++ b/internal/clientcache/internal/cache/repository_targets_test.go @@ -46,6 +46,7 @@ func TestRepository_refreshTargets(t *testing.T) { Id: "ttcp_1", Name: "name1", Type: "tcp", + ScopeId: "p_123", SessionMaxSeconds: 111, }, { @@ -53,6 +54,7 @@ func TestRepository_refreshTargets(t *testing.T) { Name: "name2", Address: "address2", Type: "tcp", + ScopeId: "p_123", SessionMaxSeconds: 222, }, { @@ -60,6 +62,7 @@ func TestRepository_refreshTargets(t *testing.T) { Name: "name3", Address: "address3", Type: "tcp", + ScopeId: "p_123", SessionMaxSeconds: 333, }, } @@ -167,6 +170,7 @@ func TestRepository_RefreshTargets_withRefreshTokens(t *testing.T) { Name: "name1", Address: "address1", Type: "tcp", + ScopeId: "p_123", SessionMaxSeconds: 111, }, { @@ -174,6 +178,7 @@ func TestRepository_RefreshTargets_withRefreshTokens(t *testing.T) { Name: "name2", Address: "address2", Type: "tcp", + ScopeId: "p_123", SessionMaxSeconds: 222, }, }, { @@ -182,6 +187,7 @@ func TestRepository_RefreshTargets_withRefreshTokens(t *testing.T) { Name: "name3", Address: "address3", Type: "tcp", + ScopeId: "p_123", SessionMaxSeconds: 333, }, }, @@ -272,6 +278,7 @@ func TestRepository_ListTargets(t *testing.T) { Name: "name1", Address: "address1", Type: "tcp", + ScopeId: "p_123", SessionMaxSeconds: 111, }, { @@ -279,6 +286,7 @@ func TestRepository_ListTargets(t *testing.T) { Name: "name2", Address: "address2", Type: "tcp", + ScopeId: "p_123", SessionMaxSeconds: 222, }, { @@ -286,6 +294,7 @@ func TestRepository_ListTargets(t *testing.T) { Name: "name3", Address: "address3", Type: "tcp", + ScopeId: "p_123", SessionMaxSeconds: 333, }, } @@ -341,7 +350,7 @@ func TestRepository_QueryTargets(t *testing.T) { require.NoError(t, r.AddKeyringToken(ctx, addr, kt1)) require.NoError(t, r.AddKeyringToken(ctx, addr, kt2)) - query := "name % name1 or name % name2" + query := `(name % name1 or name % name2) and scope_id = "p_123"` errorCases := []struct { name string @@ -376,6 +385,7 @@ func TestRepository_QueryTargets(t *testing.T) { Name: "name1", Address: "address1", Type: "tcp", + ScopeId: "p_123", SessionMaxSeconds: 111, }, { @@ -383,6 +393,7 @@ func TestRepository_QueryTargets(t *testing.T) { Name: "name2", Address: "address2", Type: "tcp", + ScopeId: "p_123", SessionMaxSeconds: 222, }, { @@ -390,6 +401,7 @@ func TestRepository_QueryTargets(t *testing.T) { Name: "name3", Address: "address3", Type: "tcp", + ScopeId: "p_123", SessionMaxSeconds: 333, }, } diff --git a/internal/clientcache/internal/cache/store_test.go b/internal/clientcache/internal/cache/store_test.go index 979fbd6cec..8457266b59 100644 --- a/internal/clientcache/internal/cache/store_test.go +++ b/internal/clientcache/internal/cache/store_test.go @@ -443,6 +443,7 @@ func TestTarget(t *testing.T) { Name: "target", Description: "target desc", Address: "some address", + ScopeId: "p_123", Item: "{id:'tssh_1234567890'}", } require.ErrorContains(t, rw.Create(ctx, unknownTarget), "constraint failed") @@ -455,6 +456,7 @@ func TestTarget(t *testing.T) { Name: "target", Description: "target desc", Address: "some address", + ScopeId: "p_123", Item: "{id:'tssh_1234567890'}", } @@ -482,6 +484,7 @@ func TestTarget(t *testing.T) { Name: "target", Description: "target desc", Address: "some address", + ScopeId: "p_123", Item: "{id:'tssh_1234567890'}", } require.NoError(t, rw.Create(ctx, target)) @@ -505,6 +508,7 @@ func TestTarget(t *testing.T) { Name: "target", Description: "target desc", Address: "some address", + ScopeId: "p_123", Item: "{id:'tssh_1234567890'}", } require.NoError(t, rw.Create(ctx, target)) @@ -548,10 +552,12 @@ func TestSession(t *testing.T) { }) t.Run("session actions", func(t *testing.T) { session := &Session{ - UserId: u.Id, - Id: "s_1234567890", - Endpoint: "endpoint", - Item: "{id:'s_1234567890'}", + UserId: u.Id, + Id: "s_1234567890", + Endpoint: "endpoint", + ScopeId: "p_123", + TargetId: "ttcp_123", + Item: "{id:'s_1234567890'}", } require.NoError(t, rw.Create(ctx, session)) @@ -576,6 +582,8 @@ func TestSession(t *testing.T) { UserId: u.Id, Id: "s_1234567890", Endpoint: "endpoint", + ScopeId: "p_123", + TargetId: "ttcp_123", Item: "{id:'s_1234567890'}", } require.NoError(t, rw.Create(ctx, session)) @@ -597,6 +605,8 @@ func TestSession(t *testing.T) { UserId: u.Id, Id: "s_1234567890", Endpoint: "endpoint", + ScopeId: "p_123", + TargetId: "ttcp_123", Item: "{id:'s_1234567890'}", } require.NoError(t, rw.Create(ctx, session)) diff --git a/internal/clientcache/internal/db/schema.sql b/internal/clientcache/internal/db/schema.sql index 650170d039..d296504f06 100644 --- a/internal/clientcache/internal/db/schema.sql +++ b/internal/clientcache/internal/db/schema.sql @@ -120,6 +120,7 @@ create table if not exists target ( name text, description text, address text, + scope_id text, -- item is the json representation of this resource from the perspective of -- the the requesting user. item text, @@ -141,6 +142,8 @@ create table if not exists session ( endpoint text, type text, status text, + scope_id text, + target_id text, -- item is the json representation of this resource from the perspective of -- of the user whose id is set in user_id item text,