-
Notifications
You must be signed in to change notification settings - Fork 1.4k
fix: clamp template port sharing level in SubAgentAPI #26061
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
311f744
d72158d
e51db6d
1294e09
6274499
49e0119
4d5a974
14c2ffb
7113ddf
95df4d9
03da607
e473519
f47731f
7f27810
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ import ( | |
| "errors" | ||
| "fmt" | ||
| "strings" | ||
| "sync/atomic" | ||
|
|
||
| "github.com/google/uuid" | ||
| "github.com/sqlc-dev/pqtype" | ||
|
|
@@ -17,6 +18,7 @@ import ( | |
| agentproto "github.com/coder/coder/v2/agent/proto" | ||
| "github.com/coder/coder/v2/coderd/database" | ||
| "github.com/coder/coder/v2/coderd/database/dbauthz" | ||
| "github.com/coder/coder/v2/coderd/portsharing" | ||
| "github.com/coder/coder/v2/codersdk" | ||
| "github.com/coder/coder/v2/provisioner" | ||
| "github.com/coder/quartz" | ||
|
|
@@ -27,9 +29,10 @@ type SubAgentAPI struct { | |
| OrganizationID uuid.UUID | ||
| AgentFn func(context.Context) (database.WorkspaceAgent, error) | ||
|
|
||
| Log slog.Logger | ||
| Clock quartz.Clock | ||
| Database database.Store | ||
| Log slog.Logger | ||
| Clock quartz.Clock | ||
| Database database.Store | ||
| PortSharer *atomic.Pointer[portsharing.PortSharer] | ||
| } | ||
|
|
||
| func (a *SubAgentAPI) CreateSubAgent(ctx context.Context, req *agentproto.CreateSubAgentRequest) (*agentproto.CreateSubAgentResponse, error) { | ||
|
|
@@ -129,6 +132,21 @@ func (a *SubAgentAPI) CreateSubAgent(ctx context.Context, req *agentproto.Create | |
| Detail: fmt.Sprintf("agent name %q does not match regex %q", agentName, provisioner.AgentNameRegex), | ||
| } | ||
| } | ||
| var template database.Template | ||
|
johnstcn marked this conversation as resolved.
|
||
| if len(req.Apps) > 0 { | ||
| workspace, err := a.Database.GetWorkspaceByAgentID(ctx, parentAgent.ID) | ||
| if err != nil { | ||
| return nil, xerrors.Errorf("get workspace by agent id: %w", err) | ||
| } | ||
|
|
||
| // Intentional: SubAgentAPI auth context enforces template ACL. | ||
| // Normal workspace operations depend on this. | ||
| template, err = a.Database.GetTemplateByID(ctx, workspace.TemplateID) | ||
| if err != nil { | ||
| return nil, xerrors.Errorf("get template policy: %w. If template access was recently changed, restart the workspace to refresh agent permissions", err) | ||
| } | ||
| } | ||
|
johnstcn marked this conversation as resolved.
|
||
|
|
||
| subAgent, err := a.Database.InsertWorkspaceAgent(ctx, database.InsertWorkspaceAgentParams{ | ||
| ID: uuid.New(), | ||
| ParentID: uuid.NullUUID{Valid: true, UUID: parentAgent.ID}, | ||
|
|
@@ -155,6 +173,14 @@ func (a *SubAgentAPI) CreateSubAgent(ctx context.Context, req *agentproto.Create | |
| return nil, xerrors.Errorf("insert sub agent: %w", err) | ||
| } | ||
|
|
||
| // A nil PortSharer uses the AGPL default, which permits all share levels. | ||
| portSharer := portsharing.DefaultPortSharer | ||
| if a.PortSharer != nil { | ||
| if loaded := a.PortSharer.Load(); loaded != nil { | ||
| portSharer = *loaded | ||
| } | ||
| } | ||
|
|
||
| var appCreationErrors []*agentproto.CreateSubAgentResponse_AppCreationError | ||
| appSlugs := make(map[string]struct{}) | ||
|
|
||
|
|
@@ -198,6 +224,18 @@ func (a *SubAgentAPI) CreateSubAgent(ctx context.Context, req *agentproto.Create | |
| } | ||
| } | ||
| sharingLevel := database.AppSharingLevel(strings.ToLower(protoSharingLevel)) | ||
| // Clamp instead of rejecting so a too-permissive app share level does | ||
| // not block the sub-agent from starting. | ||
| if err := portSharer.AuthorizedLevel(template, codersdk.WorkspaceAgentPortShareLevel(sharingLevel)); err != nil { | ||
|
johnstcn marked this conversation as resolved.
|
||
| a.Log.Warn(ctx, "clamping sub-agent app sharing level to template max port sharing level", | ||
| slog.F("sub_agent_name", subAgent.Name), | ||
| slog.F("sub_agent_id", subAgent.ID), | ||
| slog.F("app_slug", slug), | ||
| slog.F("requested_share_level", sharingLevel), | ||
| slog.F("max_port_share_level", template.MaxPortSharingLevel), | ||
| slog.Error(err)) | ||
| sharingLevel = template.MaxPortSharingLevel | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nice to surface this as a warning in the response / on the workspace agent side. But that's definitely not in scope, maybe a future improvement/refactor.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 100% agreed, unfortunately we don't seem to have an easy way to surface this kind of things to the user. |
||
|
|
||
| var openIn database.WorkspaceAppOpenIn | ||
| switch app.GetOpenIn() { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.