Skip to content

feat: authorize port requests with port sharing #12040

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

Merged
merged 1 commit into from
Feb 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions coderd/workspaceapps/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package workspaceapps
import (
"context"
"database/sql"
"errors"
"fmt"
"net/url"
"strconv"
Expand Down Expand Up @@ -317,6 +318,40 @@ func (r Request) getDatabase(ctx context.Context, db database.Store) (*databaseR
// This is only supported for subdomain-based applications.
appURL = fmt.Sprintf("http://127.0.0.1:%d", portUint)
appSharingLevel = database.AppSharingLevelOwner

// Port sharing authorization
// First check if there is a port share for the port
agentName := agentNameOrID
id, err := uuid.Parse(agentNameOrID)
if err == nil {
// If parsing works it's an ID, let's get the name
agent, err := db.GetWorkspaceAgentByID(ctx, id)
if err != nil {
return nil, xerrors.Errorf("get workspace agent %q: %w", agentNameOrID, err)
}
agentName = agent.Name
}

ps, err := db.GetWorkspaceAgentPortShare(ctx, database.GetWorkspaceAgentPortShareParams{
WorkspaceID: workspace.ID,
AgentName: agentName,
Port: int32(portUint),
})
if err != nil {
if !errors.Is(err, sql.ErrNoRows) {
return nil, xerrors.Errorf("get workspace agent port share: %w", err)
}
// No port share found, so we keep default to owner.
} else {
switch ps.ShareLevel {
case int32(codersdk.WorkspaceAgentPortShareLevelAuthenticated):
appSharingLevel = database.AppSharingLevelAuthenticated
case int32(codersdk.WorkspaceAgentPortShareLevelPublic):
appSharingLevel = database.AppSharingLevelPublic
default:
return nil, xerrors.Errorf("invalid port share level %d", ps.ShareLevel)
}
}
} else {
for _, app := range apps {
if app.Slug == r.AppSlugOrPort {
Expand Down