Add scope id and target id as searchable fields (#4012)

This commit is contained in:
Todd authored and Johan Brandhorst-Satzkorn committed 2024-01-05 07:43:20 -08:00
1 parent 9448fc931e
commit a077be691c
6 files changed
+73 -18

No files matched your search

+16 -12
View File
@@ -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 {
@@ -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",
},
}
@@ -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"`
}
@@ -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,
},
}
+14 -4
View File
@@ -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))
@@ -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,