forked from coder/coder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkspaceapps.sql
More file actions
126 lines (116 loc) · 4.32 KB
/
Copy pathworkspaceapps.sql
File metadata and controls
126 lines (116 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
-- name: GetWorkspaceAppsByAgentID :many
SELECT * FROM workspace_apps WHERE agent_id = $1 ORDER BY slug ASC;
-- name: GetWorkspaceAppsByAgentIDs :many
SELECT * FROM workspace_apps WHERE agent_id = ANY(@ids :: uuid [ ]) ORDER BY slug ASC;
-- name: GetWorkspaceAppByAgentIDAndSlug :one
SELECT * FROM workspace_apps WHERE agent_id = $1 AND slug = $2;
-- name: GetWorkspaceAppsCreatedAfter :many
SELECT * FROM workspace_apps WHERE created_at > $1 ORDER BY slug ASC;
-- name: UpsertWorkspaceApp :one
INSERT INTO
workspace_apps (
id,
created_at,
agent_id,
slug,
display_name,
icon,
command,
url,
external,
subdomain,
sharing_level,
healthcheck_url,
healthcheck_interval,
healthcheck_threshold,
health,
display_order,
hidden,
open_in,
display_group,
tooltip
)
VALUES
($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20)
ON CONFLICT (id) DO UPDATE SET
display_name = EXCLUDED.display_name,
icon = EXCLUDED.icon,
command = EXCLUDED.command,
url = EXCLUDED.url,
external = EXCLUDED.external,
subdomain = EXCLUDED.subdomain,
sharing_level = EXCLUDED.sharing_level,
healthcheck_url = EXCLUDED.healthcheck_url,
healthcheck_interval = EXCLUDED.healthcheck_interval,
healthcheck_threshold = EXCLUDED.healthcheck_threshold,
health = EXCLUDED.health,
display_order = EXCLUDED.display_order,
hidden = EXCLUDED.hidden,
open_in = EXCLUDED.open_in,
display_group = EXCLUDED.display_group,
agent_id = EXCLUDED.agent_id,
slug = EXCLUDED.slug,
tooltip = EXCLUDED.tooltip
WHERE
-- Prevent cross-tenant/cross-workspace agent rebinding (SEC-91).
-- App IDs persist across builds of the same workspace, but agent IDs are
-- regenerated every build, so compare by the workspace that owns the agent
-- rather than by agent_id. Permit unowned apps to be claimed and permit
-- same-workspace rebuilds. If an existing app belongs to a workspace, block
-- moves to both different workspaces and template import or dry-run agents
-- that resolve to no workspace. The conflicting row is then left untouched,
-- and the :one query returns no row, which the caller treats as a
-- rejection.
NOT EXISTS (
SELECT 1
FROM workspace_agents AS existing_agent
INNER JOIN workspace_resources AS existing_resource
ON existing_agent.resource_id = existing_resource.id
INNER JOIN workspace_builds AS existing_build
ON existing_resource.job_id = existing_build.job_id
WHERE existing_agent.id = workspace_apps.agent_id
)
OR EXISTS (
SELECT 1
FROM workspace_agents AS existing_agent
INNER JOIN workspace_resources AS existing_resource
ON existing_agent.resource_id = existing_resource.id
INNER JOIN workspace_builds AS existing_build
ON existing_resource.job_id = existing_build.job_id
INNER JOIN workspace_agents AS incoming_agent
ON incoming_agent.id = EXCLUDED.agent_id
INNER JOIN workspace_resources AS incoming_resource
ON incoming_agent.resource_id = incoming_resource.id
INNER JOIN workspace_builds AS incoming_build
ON incoming_resource.job_id = incoming_build.job_id
WHERE
existing_agent.id = workspace_apps.agent_id
AND existing_build.workspace_id = incoming_build.workspace_id
)
RETURNING *;
-- name: UpdateWorkspaceAppHealthByID :exec
UPDATE
workspace_apps
SET
health = $2
WHERE
id = $1;
-- name: InsertWorkspaceAppStatus :one
INSERT INTO workspace_app_statuses (id, created_at, workspace_id, agent_id, app_id, state, message, uri)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
RETURNING *;
-- name: GetWorkspaceAppStatusesByAppIDs :many
SELECT * FROM workspace_app_statuses WHERE app_id = ANY(@ids :: uuid [ ])
ORDER BY created_at DESC, id DESC;
-- name: GetLatestWorkspaceAppStatusByAppID :one
SELECT *
FROM workspace_app_statuses
WHERE app_id = @app_id::uuid
ORDER BY created_at DESC, id DESC
LIMIT 1;
-- name: GetLatestWorkspaceAppStatusesByWorkspaceIDs :many
SELECT DISTINCT ON (workspace_id)
*
FROM workspace_app_statuses
WHERE workspace_id = ANY(@ids :: uuid[])
ORDER BY workspace_id, created_at DESC;