Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix: ensure agent token is from latest build in middleware#12443

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
f0ssel merged 21 commits intomainfromf0ssel/ensure-latest-mw
Mar 14, 2024

Conversation

f0ssel
Copy link
Contributor

This applies the limit we currently use for some actions to all actions that use the WorkspaceAgent middleware.

@f0ssel
Copy link
ContributorAuthor

Spoke with@deansheather and he agrees and thinks this is a good way to solve this over nulling out the auth_token fields, so I'm going to spend some time figuring out the root cause of all the test failures - I'm assuming it's just related to the way we do some common test scaffolding.

Copy link
Member

@deansheatherdeansheather left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

LGTM, but needs a test that ensures other builds can't connect

@f0ssel
Copy link
ContributorAuthor

@deansheather Added the test

@spikecurtis
Copy link
Contributor

I'm concerned that this is fragile becauseGetWorkspaceAgentAndOwnerByAuthToken will still happily return the agent for non-latest builds. I realize that the middleware won't authorize non-latest builds and we use middleware for authentication pretty much everywhere right now, but I think we could make stronger guarantees and leave fewer sharp edges in our code if droppedGetWorkspaceAgentAndOwnerByAuthToken in favor of a single SQL query that takes the token and returns the agent and build (via joins) if it's the latest, and nothing if it's invalid or not the latest. It would also reduce the number of DB roundtrips to boot.

@f0ssel
Copy link
ContributorAuthor

Since I'm not the strongest SQL person, I'd like to propose the query before i go down the rabbit whole of changing all the interfaces and usages. Are you thinking something like this?

-- name: GetLatestWorkspaceAgentAndBuildAndOwnerByAuthToken :oneWITH LatestBuildsAS (SELECTworkspaces.idAS workspace_id,MAX(workspace_builds.build_number)AS latest_build_numberFROM workspacesINNER JOIN workspace_buildsONworkspace_builds.workspace_id=workspaces.idWHEREworkspaces.deleted= FALSEGROUP BYworkspaces.id)SELECTsqlc.embed(workspace_agents),workspaces.idAS workspace_id,users.idAS owner_id,users.usernameAS owner_name,users.statusAS owner_status,workspaces.template_idAS template_id,sqlc.embed(workspace_builds),    array_cat(        array_append(users.rbac_roles,'member'),        array_append(ARRAY[]::text[],'organization-member:'||organization_members.organization_id::text)    )::text[]as owner_roles,    array_agg(COALESCE(group_members.group_id::text,''))::text[]AS owner_groupsFROM usersINNER JOIN workspacesONworkspaces.owner_id=users.idINNER JOIN workspace_buildsONworkspace_builds.workspace_id=workspaces.idINNER JOIN LatestBuildsONworkspaces.id=LatestBuilds.workspace_idANDworkspace_builds.build_number=LatestBuilds.latest_build_numberINNER JOIN workspace_resourcesONworkspace_resources.job_id=workspace_builds.job_idINNER JOIN workspace_agentsONworkspace_agents.resource_id=workspace_resources.idINNER JOIN organization_membersONorganization_members.user_id=users.idLEFT JOIN group_membersONgroup_members.user_id=users.idWHEREworkspace_agents.auth_token= @auth_tokenGROUP BYworkspace_agents.id,workspaces.id,users.id,organization_members.organization_id,workspace_builds.build_numberORDER BYworkspace_builds.build_numberDESCLIMIT1;

This generates the struct I think I would want, a row with both row.WorkspaceAgent and row.WorkspaceBuildTable. I believe this will return no rows when the auth token is not from the latest build. Does this seem like the direction you were thinking?

@spikecurtis
Copy link
Contributor

Yeah, that's the kind of thing I was thinking.

BTW, I think theLIMIT 1 is superfluous if the query is working correctly because we have a unique constraint on workspace_builds (workspace_id, build_number), which implies that exactly one build can be the "latest".

I also think it would read more clearly if you moved theworkspace_builds.build_number = LatestBuilds.latest_build_number clause toWHERE rather than the join condition. This is just style, but to me it's easier to understand that

we're taking the workspace and looking up the latest build number, then only selecting rows where it's the latest build

vs

we're taking the workspace and then joining to a set of rows that is empty when it's not the latest

@f0ssel
Copy link
ContributorAuthor

@spikecurtis I've spoken with@Emyrk on how to accomplish this query, and we've landed on the following changes:

  1. GetWorkspaceAgentAndOwnerByAuthToken changed toGetWorkspaceAgentAndLatestBuildByAuthToken - rbac stuff was removed from query to simplify, array_agg was making things a mess. Since this query is only used here it's safe to change.
  2. Get rbac info from a call toDB.GetAuthorizationUserRoles(dbauthz.AsSystemRestricted(ctx), row.Workspace.OwnerID) in the middleware now.

Tests are all passing now so I think this is good for review. I've added@Emyrk as a reviewer as well.

@f0sself0ssel requested a review fromEmyrkMarch 13, 2024 20:03
Copy link
Member

@EmyrkEmyrk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Let's get this test back, then it LGTM

@f0sself0ssel requested a review fromEmyrkMarch 14, 2024 16:24
@f0sself0ssel merged commit0723dd3 intomainMar 14, 2024
@f0sself0ssel deleted the f0ssel/ensure-latest-mw branchMarch 14, 2024 16:27
@github-actionsgithub-actionsbot locked and limited conversation to collaboratorsMar 14, 2024
Sign up for freeto subscribe to this conversation on GitHub. Already have an account?Sign in.
Reviewers

@spikecurtisspikecurtisspikecurtis approved these changes

@EmyrkEmyrkEmyrk approved these changes

@deansheatherdeansheatherAwaiting requested review from deansheather

Assignees

@f0sself0ssel

Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

4 participants
@f0ssel@spikecurtis@Emyrk@deansheather

[8]ページ先頭

©2009-2025 Movatter.jp