mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
feat: allow setting port share protocol (#12383)
Co-authored-by: Garrett Delfosse <garrett@coder.com>
This commit is contained in:
co-authored by
Garrett Delfosse
parent
23ff807a27
commit
46a2ff1061
@@ -1608,6 +1608,7 @@ func (s *MethodTestSuite) TestWorkspacePortSharing() {
|
||||
AgentName: ps.AgentName,
|
||||
Port: ps.Port,
|
||||
ShareLevel: ps.ShareLevel,
|
||||
Protocol: ps.Protocol,
|
||||
}).Asserts(ws, rbac.ActionUpdate).Returns(ps)
|
||||
}))
|
||||
s.Run("GetWorkspaceAgentPortShare", s.Subtest(func(db database.Store, check *expects) {
|
||||
|
||||
@@ -140,6 +140,7 @@ func WorkspaceAgentPortShare(t testing.TB, db database.Store, orig database.Work
|
||||
AgentName: takeFirst(orig.AgentName, namesgenerator.GetRandomName(1)),
|
||||
Port: takeFirst(orig.Port, 8080),
|
||||
ShareLevel: takeFirst(orig.ShareLevel, database.AppSharingLevelPublic),
|
||||
Protocol: takeFirst(orig.Protocol, database.PortShareProtocolHttp),
|
||||
})
|
||||
require.NoError(t, err, "insert workspace agent")
|
||||
return ps
|
||||
|
||||
@@ -86,7 +86,7 @@ func New() database.Store {
|
||||
UpdatedAt: dbtime.Now(),
|
||||
})
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("failed to create default organization: %w", err))
|
||||
panic(xerrors.Errorf("failed to create default organization: %w", err))
|
||||
}
|
||||
q.defaultProxyDisplayName = "Default"
|
||||
q.defaultProxyIconURL = "/emojis/1f3e1.png"
|
||||
@@ -7933,6 +7933,7 @@ func (q *FakeQuerier) UpsertWorkspaceAgentPortShare(_ context.Context, arg datab
|
||||
for i, share := range q.workspaceAgentPortShares {
|
||||
if share.WorkspaceID == arg.WorkspaceID && share.Port == arg.Port && share.AgentName == arg.AgentName {
|
||||
share.ShareLevel = arg.ShareLevel
|
||||
share.Protocol = arg.Protocol
|
||||
q.workspaceAgentPortShares[i] = share
|
||||
return share, nil
|
||||
}
|
||||
@@ -7944,6 +7945,7 @@ func (q *FakeQuerier) UpsertWorkspaceAgentPortShare(_ context.Context, arg datab
|
||||
AgentName: arg.AgentName,
|
||||
Port: arg.Port,
|
||||
ShareLevel: arg.ShareLevel,
|
||||
Protocol: arg.Protocol,
|
||||
}
|
||||
q.workspaceAgentPortShares = append(q.workspaceAgentPortShares, psl)
|
||||
|
||||
|
||||
Generated
+7
-1
@@ -95,6 +95,11 @@ CREATE TYPE parameter_type_system AS ENUM (
|
||||
'hcl'
|
||||
);
|
||||
|
||||
CREATE TYPE port_share_protocol AS ENUM (
|
||||
'http',
|
||||
'https'
|
||||
);
|
||||
|
||||
CREATE TYPE provisioner_job_status AS ENUM (
|
||||
'pending',
|
||||
'running',
|
||||
@@ -1027,7 +1032,8 @@ CREATE TABLE workspace_agent_port_share (
|
||||
workspace_id uuid NOT NULL,
|
||||
agent_name text NOT NULL,
|
||||
port integer NOT NULL,
|
||||
share_level app_sharing_level NOT NULL
|
||||
share_level app_sharing_level NOT NULL,
|
||||
protocol port_share_protocol DEFAULT 'http'::port_share_protocol NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE workspace_agent_scripts (
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
ALTER TABLE workspace_agent_port_share DROP COLUMN protocol;
|
||||
|
||||
DROP TYPE port_share_protocol;
|
||||
@@ -0,0 +1,4 @@
|
||||
CREATE TYPE port_share_protocol AS ENUM ('http', 'https');
|
||||
|
||||
ALTER TABLE workspace_agent_port_share
|
||||
ADD COLUMN protocol port_share_protocol NOT NULL DEFAULT 'http'::port_share_protocol;
|
||||
@@ -898,6 +898,64 @@ func AllParameterTypeSystemValues() []ParameterTypeSystem {
|
||||
}
|
||||
}
|
||||
|
||||
type PortShareProtocol string
|
||||
|
||||
const (
|
||||
PortShareProtocolHttp PortShareProtocol = "http"
|
||||
PortShareProtocolHttps PortShareProtocol = "https"
|
||||
)
|
||||
|
||||
func (e *PortShareProtocol) Scan(src interface{}) error {
|
||||
switch s := src.(type) {
|
||||
case []byte:
|
||||
*e = PortShareProtocol(s)
|
||||
case string:
|
||||
*e = PortShareProtocol(s)
|
||||
default:
|
||||
return fmt.Errorf("unsupported scan type for PortShareProtocol: %T", src)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type NullPortShareProtocol struct {
|
||||
PortShareProtocol PortShareProtocol `json:"port_share_protocol"`
|
||||
Valid bool `json:"valid"` // Valid is true if PortShareProtocol is not NULL
|
||||
}
|
||||
|
||||
// Scan implements the Scanner interface.
|
||||
func (ns *NullPortShareProtocol) Scan(value interface{}) error {
|
||||
if value == nil {
|
||||
ns.PortShareProtocol, ns.Valid = "", false
|
||||
return nil
|
||||
}
|
||||
ns.Valid = true
|
||||
return ns.PortShareProtocol.Scan(value)
|
||||
}
|
||||
|
||||
// Value implements the driver Valuer interface.
|
||||
func (ns NullPortShareProtocol) Value() (driver.Value, error) {
|
||||
if !ns.Valid {
|
||||
return nil, nil
|
||||
}
|
||||
return string(ns.PortShareProtocol), nil
|
||||
}
|
||||
|
||||
func (e PortShareProtocol) Valid() bool {
|
||||
switch e {
|
||||
case PortShareProtocolHttp,
|
||||
PortShareProtocolHttps:
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func AllPortShareProtocolValues() []PortShareProtocol {
|
||||
return []PortShareProtocol{
|
||||
PortShareProtocolHttp,
|
||||
PortShareProtocolHttps,
|
||||
}
|
||||
}
|
||||
|
||||
// Computed status of a provisioner job. Jobs could be stuck in a hung state, these states do not guarantee any transition to another state.
|
||||
type ProvisionerJobStatus string
|
||||
|
||||
@@ -2312,10 +2370,11 @@ type WorkspaceAgentMetadatum struct {
|
||||
}
|
||||
|
||||
type WorkspaceAgentPortShare struct {
|
||||
WorkspaceID uuid.UUID `db:"workspace_id" json:"workspace_id"`
|
||||
AgentName string `db:"agent_name" json:"agent_name"`
|
||||
Port int32 `db:"port" json:"port"`
|
||||
ShareLevel AppSharingLevel `db:"share_level" json:"share_level"`
|
||||
WorkspaceID uuid.UUID `db:"workspace_id" json:"workspace_id"`
|
||||
AgentName string `db:"agent_name" json:"agent_name"`
|
||||
Port int32 `db:"port" json:"port"`
|
||||
ShareLevel AppSharingLevel `db:"share_level" json:"share_level"`
|
||||
Protocol PortShareProtocol `db:"protocol" json:"protocol"`
|
||||
}
|
||||
|
||||
type WorkspaceAgentScript struct {
|
||||
|
||||
@@ -8469,7 +8469,12 @@ func (q *sqlQuerier) UpdateUserStatus(ctx context.Context, arg UpdateUserStatusP
|
||||
}
|
||||
|
||||
const deleteWorkspaceAgentPortShare = `-- name: DeleteWorkspaceAgentPortShare :exec
|
||||
DELETE FROM workspace_agent_port_share WHERE workspace_id = $1 AND agent_name = $2 AND port = $3
|
||||
DELETE FROM
|
||||
workspace_agent_port_share
|
||||
WHERE
|
||||
workspace_id = $1
|
||||
AND agent_name = $2
|
||||
AND port = $3
|
||||
`
|
||||
|
||||
type DeleteWorkspaceAgentPortShareParams struct {
|
||||
@@ -8484,7 +8489,17 @@ func (q *sqlQuerier) DeleteWorkspaceAgentPortShare(ctx context.Context, arg Dele
|
||||
}
|
||||
|
||||
const deleteWorkspaceAgentPortSharesByTemplate = `-- name: DeleteWorkspaceAgentPortSharesByTemplate :exec
|
||||
DELETE FROM workspace_agent_port_share WHERE workspace_id IN (SELECT id FROM workspaces WHERE template_id = $1)
|
||||
DELETE FROM
|
||||
workspace_agent_port_share
|
||||
WHERE
|
||||
workspace_id IN (
|
||||
SELECT
|
||||
id
|
||||
FROM
|
||||
workspaces
|
||||
WHERE
|
||||
template_id = $1
|
||||
)
|
||||
`
|
||||
|
||||
func (q *sqlQuerier) DeleteWorkspaceAgentPortSharesByTemplate(ctx context.Context, templateID uuid.UUID) error {
|
||||
@@ -8493,7 +8508,14 @@ func (q *sqlQuerier) DeleteWorkspaceAgentPortSharesByTemplate(ctx context.Contex
|
||||
}
|
||||
|
||||
const getWorkspaceAgentPortShare = `-- name: GetWorkspaceAgentPortShare :one
|
||||
SELECT workspace_id, agent_name, port, share_level FROM workspace_agent_port_share WHERE workspace_id = $1 AND agent_name = $2 AND port = $3
|
||||
SELECT
|
||||
workspace_id, agent_name, port, share_level, protocol
|
||||
FROM
|
||||
workspace_agent_port_share
|
||||
WHERE
|
||||
workspace_id = $1
|
||||
AND agent_name = $2
|
||||
AND port = $3
|
||||
`
|
||||
|
||||
type GetWorkspaceAgentPortShareParams struct {
|
||||
@@ -8510,12 +8532,18 @@ func (q *sqlQuerier) GetWorkspaceAgentPortShare(ctx context.Context, arg GetWork
|
||||
&i.AgentName,
|
||||
&i.Port,
|
||||
&i.ShareLevel,
|
||||
&i.Protocol,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const listWorkspaceAgentPortShares = `-- name: ListWorkspaceAgentPortShares :many
|
||||
SELECT workspace_id, agent_name, port, share_level FROM workspace_agent_port_share WHERE workspace_id = $1
|
||||
SELECT
|
||||
workspace_id, agent_name, port, share_level, protocol
|
||||
FROM
|
||||
workspace_agent_port_share
|
||||
WHERE
|
||||
workspace_id = $1
|
||||
`
|
||||
|
||||
func (q *sqlQuerier) ListWorkspaceAgentPortShares(ctx context.Context, workspaceID uuid.UUID) ([]WorkspaceAgentPortShare, error) {
|
||||
@@ -8532,6 +8560,7 @@ func (q *sqlQuerier) ListWorkspaceAgentPortShares(ctx context.Context, workspace
|
||||
&i.AgentName,
|
||||
&i.Port,
|
||||
&i.ShareLevel,
|
||||
&i.Protocol,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -8547,7 +8576,20 @@ func (q *sqlQuerier) ListWorkspaceAgentPortShares(ctx context.Context, workspace
|
||||
}
|
||||
|
||||
const reduceWorkspaceAgentShareLevelToAuthenticatedByTemplate = `-- name: ReduceWorkspaceAgentShareLevelToAuthenticatedByTemplate :exec
|
||||
UPDATE workspace_agent_port_share SET share_level = 'authenticated' WHERE share_level = 'public' AND workspace_id IN (SELECT id FROM workspaces WHERE template_id = $1)
|
||||
UPDATE
|
||||
workspace_agent_port_share
|
||||
SET
|
||||
share_level = 'authenticated'
|
||||
WHERE
|
||||
share_level = 'public'
|
||||
AND workspace_id IN (
|
||||
SELECT
|
||||
id
|
||||
FROM
|
||||
workspaces
|
||||
WHERE
|
||||
template_id = $1
|
||||
)
|
||||
`
|
||||
|
||||
func (q *sqlQuerier) ReduceWorkspaceAgentShareLevelToAuthenticatedByTemplate(ctx context.Context, templateID uuid.UUID) error {
|
||||
@@ -8556,16 +8598,38 @@ func (q *sqlQuerier) ReduceWorkspaceAgentShareLevelToAuthenticatedByTemplate(ctx
|
||||
}
|
||||
|
||||
const upsertWorkspaceAgentPortShare = `-- name: UpsertWorkspaceAgentPortShare :one
|
||||
INSERT INTO workspace_agent_port_share (workspace_id, agent_name, port, share_level)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
ON CONFLICT (workspace_id, agent_name, port) DO UPDATE SET share_level = $4 RETURNING workspace_id, agent_name, port, share_level
|
||||
INSERT INTO
|
||||
workspace_agent_port_share (
|
||||
workspace_id,
|
||||
agent_name,
|
||||
port,
|
||||
share_level,
|
||||
protocol
|
||||
)
|
||||
VALUES (
|
||||
$1,
|
||||
$2,
|
||||
$3,
|
||||
$4,
|
||||
$5
|
||||
)
|
||||
ON CONFLICT (
|
||||
workspace_id,
|
||||
agent_name,
|
||||
port
|
||||
)
|
||||
DO UPDATE SET
|
||||
share_level = $4,
|
||||
protocol = $5
|
||||
RETURNING workspace_id, agent_name, port, share_level, protocol
|
||||
`
|
||||
|
||||
type UpsertWorkspaceAgentPortShareParams struct {
|
||||
WorkspaceID uuid.UUID `db:"workspace_id" json:"workspace_id"`
|
||||
AgentName string `db:"agent_name" json:"agent_name"`
|
||||
Port int32 `db:"port" json:"port"`
|
||||
ShareLevel AppSharingLevel `db:"share_level" json:"share_level"`
|
||||
WorkspaceID uuid.UUID `db:"workspace_id" json:"workspace_id"`
|
||||
AgentName string `db:"agent_name" json:"agent_name"`
|
||||
Port int32 `db:"port" json:"port"`
|
||||
ShareLevel AppSharingLevel `db:"share_level" json:"share_level"`
|
||||
Protocol PortShareProtocol `db:"protocol" json:"protocol"`
|
||||
}
|
||||
|
||||
func (q *sqlQuerier) UpsertWorkspaceAgentPortShare(ctx context.Context, arg UpsertWorkspaceAgentPortShareParams) (WorkspaceAgentPortShare, error) {
|
||||
@@ -8574,6 +8638,7 @@ func (q *sqlQuerier) UpsertWorkspaceAgentPortShare(ctx context.Context, arg Upse
|
||||
arg.AgentName,
|
||||
arg.Port,
|
||||
arg.ShareLevel,
|
||||
arg.Protocol,
|
||||
)
|
||||
var i WorkspaceAgentPortShare
|
||||
err := row.Scan(
|
||||
@@ -8581,6 +8646,7 @@ func (q *sqlQuerier) UpsertWorkspaceAgentPortShare(ctx context.Context, arg Upse
|
||||
&i.AgentName,
|
||||
&i.Port,
|
||||
&i.ShareLevel,
|
||||
&i.Protocol,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
@@ -1,19 +1,80 @@
|
||||
-- name: GetWorkspaceAgentPortShare :one
|
||||
SELECT * FROM workspace_agent_port_share WHERE workspace_id = $1 AND agent_name = $2 AND port = $3;
|
||||
SELECT
|
||||
*
|
||||
FROM
|
||||
workspace_agent_port_share
|
||||
WHERE
|
||||
workspace_id = $1
|
||||
AND agent_name = $2
|
||||
AND port = $3;
|
||||
|
||||
-- name: ListWorkspaceAgentPortShares :many
|
||||
SELECT * FROM workspace_agent_port_share WHERE workspace_id = $1;
|
||||
SELECT
|
||||
*
|
||||
FROM
|
||||
workspace_agent_port_share
|
||||
WHERE
|
||||
workspace_id = $1;
|
||||
|
||||
-- name: DeleteWorkspaceAgentPortShare :exec
|
||||
DELETE FROM workspace_agent_port_share WHERE workspace_id = $1 AND agent_name = $2 AND port = $3;
|
||||
DELETE FROM
|
||||
workspace_agent_port_share
|
||||
WHERE
|
||||
workspace_id = $1
|
||||
AND agent_name = $2
|
||||
AND port = $3;
|
||||
|
||||
-- name: UpsertWorkspaceAgentPortShare :one
|
||||
INSERT INTO workspace_agent_port_share (workspace_id, agent_name, port, share_level)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
ON CONFLICT (workspace_id, agent_name, port) DO UPDATE SET share_level = $4 RETURNING *;
|
||||
INSERT INTO
|
||||
workspace_agent_port_share (
|
||||
workspace_id,
|
||||
agent_name,
|
||||
port,
|
||||
share_level,
|
||||
protocol
|
||||
)
|
||||
VALUES (
|
||||
$1,
|
||||
$2,
|
||||
$3,
|
||||
$4,
|
||||
$5
|
||||
)
|
||||
ON CONFLICT (
|
||||
workspace_id,
|
||||
agent_name,
|
||||
port
|
||||
)
|
||||
DO UPDATE SET
|
||||
share_level = $4,
|
||||
protocol = $5
|
||||
RETURNING *;
|
||||
|
||||
-- name: ReduceWorkspaceAgentShareLevelToAuthenticatedByTemplate :exec
|
||||
UPDATE workspace_agent_port_share SET share_level = 'authenticated' WHERE share_level = 'public' AND workspace_id IN (SELECT id FROM workspaces WHERE template_id = $1);
|
||||
UPDATE
|
||||
workspace_agent_port_share
|
||||
SET
|
||||
share_level = 'authenticated'
|
||||
WHERE
|
||||
share_level = 'public'
|
||||
AND workspace_id IN (
|
||||
SELECT
|
||||
id
|
||||
FROM
|
||||
workspaces
|
||||
WHERE
|
||||
template_id = $1
|
||||
);
|
||||
|
||||
-- name: DeleteWorkspaceAgentPortSharesByTemplate :exec
|
||||
DELETE FROM workspace_agent_port_share WHERE workspace_id IN (SELECT id FROM workspaces WHERE template_id = $1);
|
||||
DELETE FROM
|
||||
workspace_agent_port_share
|
||||
WHERE
|
||||
workspace_id IN (
|
||||
SELECT
|
||||
id
|
||||
FROM
|
||||
workspaces
|
||||
WHERE
|
||||
template_id = $1
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user