2022-06-04 15:13:37 -05:00
package coderd
import (
2023-04-20 16:59:45 -07:00
"context"
2023-04-17 14:57:21 -05:00
"database/sql"
2022-06-04 15:13:37 -05:00
"net/http"
"net/url"
2023-04-20 16:59:45 -07:00
"strings"
2022-09-23 08:30:32 +10:00
"time"
2022-06-04 15:13:37 -05:00
2023-04-17 14:57:21 -05:00
"golang.org/x/xerrors"
2023-08-18 13:55:43 -05:00
"github.com/coder/coder/v2/coderd/apikey"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/dbauthz"
2023-09-01 19:50:12 +03:00
"github.com/coder/coder/v2/coderd/database/dbtime"
2023-08-18 13:55:43 -05:00
"github.com/coder/coder/v2/coderd/httpapi"
"github.com/coder/coder/v2/coderd/httpmw"
2024-10-25 17:14:35 +01:00
"github.com/coder/coder/v2/coderd/jwtutils"
2024-05-15 09:46:35 -05:00
"github.com/coder/coder/v2/coderd/rbac/policy"
2023-08-18 13:55:43 -05:00
"github.com/coder/coder/v2/coderd/workspaceapps"
2024-01-17 10:41:42 -06:00
"github.com/coder/coder/v2/coderd/workspaceapps/appurl"
2023-08-18 13:55:43 -05:00
"github.com/coder/coder/v2/codersdk"
2022-06-04 15:13:37 -05:00
)
2022-12-21 15:37:30 +01:00
// @Summary Get applications host
2023-01-13 12:27:21 +01:00
// @ID get-applications-host
2022-12-21 15:37:30 +01:00
// @Security CoderSessionToken
// @Produce json
// @Tags Applications
2023-01-29 15:47:24 -06:00
// @Success 200 {object} codersdk.AppHostResponse
2022-12-21 15:37:30 +01:00
// @Router /applications/host [get]
2023-05-09 09:28:25 -05:00
// @Deprecated use api/v2/regions and see the primary proxy.
2022-09-23 08:30:32 +10:00
func ( api * API ) appHost ( rw http . ResponseWriter , r * http . Request ) {
2023-01-29 15:47:24 -06:00
httpapi . Write ( r . Context (), rw , http . StatusOK , codersdk . AppHostResponse {
2024-01-18 09:44:05 -06:00
Host : appurl . SubdomainAppHost ( api . AppHostname , api . AccessURL ),
2022-09-23 08:30:32 +10:00
})
}
2023-01-13 12:27:21 +01:00
// workspaceApplicationAuth is an endpoint on the main router that handles
// redirects from the subdomain handler.
//
// This endpoint is under /api so we don't return the friendly error page here.
// Any errors on this endpoint should be errors that are unlikely to happen
// in production unless the user messes with the URL.
//
2022-12-21 15:37:30 +01:00
// @Summary Redirect to URI with encrypted API key
// @ID redirect-to-uri-with-encrypted-api-key
// @Security CoderSessionToken
// @Tags Applications
// @Param redirect_uri query string false "Redirect destination"
// @Success 307
// @Router /applications/auth-redirect [get]
2022-09-23 08:30:32 +10:00
func ( api * API ) workspaceApplicationAuth ( rw http . ResponseWriter , r * http . Request ) {
ctx := r . Context ()
apiKey := httpmw . APIKey ( r )
2024-05-15 09:46:35 -05:00
if ! api . Authorize ( r , policy . ActionCreate , apiKey ) {
2022-09-23 08:30:32 +10:00
httpapi . ResourceNotFound ( rw )
return
}
// Get the redirect URI from the query parameters and parse it.
2023-03-08 06:38:11 +11:00
redirectURI := r . URL . Query (). Get ( workspaceapps . RedirectURIQueryParam )
2022-09-23 08:30:32 +10:00
if redirectURI == "" {
httpapi . Write ( ctx , rw , http . StatusBadRequest , codersdk . Response {
Message : "Missing redirect_uri query parameter." ,
})
return
}
u , err := url . Parse ( redirectURI )
if err != nil {
httpapi . Write ( ctx , rw , http . StatusBadRequest , codersdk . Response {
Message : "Invalid redirect_uri query parameter." ,
Detail : err . Error (),
})
return
}
2023-04-20 16:59:45 -07:00
u . Scheme , err = api . ValidWorkspaceAppHostname ( ctx , u . Host , ValidWorkspaceAppHostnameOpts {
// Allow all hosts except primary access URL since we don't need app
// tokens on the primary dashboard URL.
AllowPrimaryAccessURL : false ,
AllowPrimaryWildcard : true ,
AllowProxyAccessURL : true ,
AllowProxyWildcard : true ,
})
if err != nil {
httpapi . Write ( ctx , rw , http . StatusInternalServerError , codersdk . Response {
Message : "Failed to verify redirect_uri query parameter." ,
Detail : err . Error (),
})
return
2023-04-17 14:57:21 -05:00
}
2023-04-20 16:59:45 -07:00
if u . Scheme == "" {
httpapi . Write ( ctx , rw , http . StatusBadRequest , codersdk . Response {
Message : "Invalid redirect_uri." ,
Detail : "The redirect_uri query parameter must be the primary wildcard app hostname, a workspace proxy access URL or a workspace proxy wildcard app hostname." ,
})
return
2022-09-23 08:30:32 +10:00
}
// Create the application_connect-scoped API key with the same lifetime as
2023-02-04 04:38:36 +11:00
// the current session.
2022-09-23 08:30:32 +10:00
exp := apiKey . ExpiresAt
2023-02-04 04:38:36 +11:00
lifetimeSeconds := apiKey . LifetimeSeconds
2024-04-10 10:34:49 -05:00
if exp . IsZero () || time . Until ( exp ) > api . DeploymentValues . Sessions . DefaultDuration . Value () {
exp = dbtime . Now (). Add ( api . DeploymentValues . Sessions . DefaultDuration . Value ())
lifetimeSeconds = int64 ( api . DeploymentValues . Sessions . DefaultDuration . Value (). Seconds ())
2022-09-23 08:30:32 +10:00
}
2023-05-17 23:29:22 -05:00
cookie , _ , err := api . createAPIKey ( ctx , apikey . CreateParams {
2024-01-22 11:42:55 -09:00
UserID : apiKey . UserID ,
LoginType : database . LoginTypePassword ,
2024-04-10 10:34:49 -05:00
DefaultLifetime : api . DeploymentValues . Sessions . DefaultDuration . Value (),
2024-01-22 11:42:55 -09:00
ExpiresAt : exp ,
LifetimeSeconds : lifetimeSeconds ,
Scope : database . APIKeyScopeApplicationConnect ,
2022-09-23 08:30:32 +10:00
})
if err != nil {
httpapi . Write ( ctx , rw , http . StatusInternalServerError , codersdk . Response {
Message : "Failed to create API key." ,
Detail : err . Error (),
})
return
}
2024-10-25 17:14:35 +01:00
payload := workspaceapps . EncryptedAPIKeyPayload {
2022-09-23 08:30:32 +10:00
APIKey : cookie . Value ,
2024-10-25 17:14:35 +01:00
}
payload . Fill ( api . Clock . Now ())
encryptedAPIKey , err := jwtutils . Encrypt ( ctx , api . AppEncryptionKeyCache , payload )
2022-09-23 08:30:32 +10:00
if err != nil {
httpapi . Write ( ctx , rw , http . StatusInternalServerError , codersdk . Response {
Message : "Failed to encrypt API key." ,
Detail : err . Error (),
})
return
}
// Redirect to the redirect URI with the encrypted API key in the query
// parameters.
q := u . Query ()
2023-04-06 04:41:55 +10:00
q . Set ( workspaceapps . SubdomainProxyAPIKeyParam , encryptedAPIKey )
2022-09-23 08:30:32 +10:00
u . RawQuery = q . Encode ()
2023-04-17 14:57:21 -05:00
http . Redirect ( rw , r , u . String (), http . StatusSeeOther )
2022-09-23 08:30:32 +10:00
}
2023-04-20 16:59:45 -07:00
type ValidWorkspaceAppHostnameOpts struct {
AllowPrimaryAccessURL bool
AllowPrimaryWildcard bool
AllowProxyAccessURL bool
AllowProxyWildcard bool
}
// ValidWorkspaceAppHostname checks if the given host is a valid workspace app
// hostname based on the provided options. It returns a scheme to force on
// success. If the hostname is not valid or doesn't match, an empty string is
// returned. Any error returned is a 500 error.
//
// For hosts that match a wildcard app hostname, the scheme is forced to be the
// corresponding access URL scheme.
func ( api * API ) ValidWorkspaceAppHostname ( ctx context . Context , host string , opts ValidWorkspaceAppHostnameOpts ) ( string , error ) {
if opts . AllowPrimaryAccessURL && ( host == api . AccessURL . Hostname () || host == api . AccessURL . Host ) {
// Force the redirect URI to have the same scheme as the access URL for
// security purposes.
return api . AccessURL . Scheme , nil
}
if opts . AllowPrimaryWildcard && api . AppHostnameRegex != nil {
2024-01-17 10:41:42 -06:00
_ , ok := appurl . ExecuteHostnamePattern ( api . AppHostnameRegex , host )
2023-04-20 16:59:45 -07:00
if ok {
// Force the redirect URI to have the same scheme as the access URL
// for security purposes.
return api . AccessURL . Scheme , nil
}
}
// Ensure that the redirect URI is a subdomain of api.Hostname and is a
// valid app subdomain.
if opts . AllowProxyAccessURL || opts . AllowProxyWildcard {
// Strip the port for the database query.
host = strings . Split ( host , ":" )[ 0 ]
// nolint:gocritic // system query
systemCtx := dbauthz . AsSystemRestricted ( ctx )
proxy , err := api . Database . GetWorkspaceProxyByHostname ( systemCtx , database . GetWorkspaceProxyByHostnameParams {
Hostname : host ,
AllowAccessUrl : opts . AllowProxyAccessURL ,
AllowWildcardHostname : opts . AllowProxyWildcard ,
})
if xerrors . Is ( err , sql . ErrNoRows ) {
return "" , nil
}
if err != nil {
return "" , xerrors . Errorf ( "get workspace proxy by hostname %q: %w" , host , err )
}
proxyURL , err := url . Parse ( proxy . Url )
if err != nil {
return "" , xerrors . Errorf ( "parse proxy URL %q: %w" , proxy . Url , err )
}
// Force the redirect URI to use the same scheme as the proxy access URL
// for security purposes.
return proxyURL . Scheme , nil
}
return "" , nil
}