Skip to content
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

Fix Per-session MFA for leaf apps #51394

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
18 changes: 9 additions & 9 deletions lib/web/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -4562,15 +4562,6 @@ func (h *Handler) authenticateRequestWithCluster(w http.ResponseWriter, r *http.
// remote trusted cluster) as specified by the ":site" url parameter.
func (h *Handler) getSiteByParams(ctx context.Context, sctx *SessionContext, p httprouter.Params) (reversetunnelclient.RemoteSite, error) {
clusterName := p.ByName("site")
if clusterName == currentSiteShortcut {
res, err := h.cfg.ProxyClient.GetClusterName()
if err != nil {
h.logger.WarnContext(ctx, "Failed to query cluster name", "error", err)
return nil, trace.Wrap(err)
}
clusterName = res.GetClusterName()
}

site, err := h.getSiteByClusterName(ctx, sctx, clusterName)
if err != nil {
return nil, trace.Wrap(err)
Expand All @@ -4580,6 +4571,15 @@ func (h *Handler) getSiteByParams(ctx context.Context, sctx *SessionContext, p h
}

func (h *Handler) getSiteByClusterName(ctx context.Context, sctx *SessionContext, clusterName string) (reversetunnelclient.RemoteSite, error) {
if clusterName == currentSiteShortcut {
res, err := h.cfg.ProxyClient.GetClusterName()
if err != nil {
h.logger.WarnContext(ctx, "Failed to query cluster name", "error", err)
return nil, trace.Wrap(err)
}
clusterName = res.GetClusterName()
}

proxy, err := h.ProxyWithRoles(ctx, sctx)
if err != nil {
h.logger.WarnContext(ctx, "Failed to get proxy with roles", "error", err)
Expand Down
39 changes: 36 additions & 3 deletions lib/web/mfa.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ type createAuthenticateChallengeRequest struct {
// createAuthenticateChallengeHandle creates and returns MFA authentication challenges for the user in context (logged in user).
// Used when users need to re-authenticate their second factors.
func (h *Handler) createAuthenticateChallengeHandle(w http.ResponseWriter, r *http.Request, p httprouter.Params, c *SessionContext) (interface{}, error) {
ctx := r.Context()

var req createAuthenticateChallengeRequest
if err := httplib.ReadResourceJSON(r, &req); err != nil {
return nil, trace.Wrap(err)
Expand All @@ -192,10 +194,36 @@ func (h *Handler) createAuthenticateChallengeHandle(w http.ResponseWriter, r *ht

var mfaRequiredCheckProto *proto.IsMFARequiredRequest
if req.IsMFARequiredRequest != nil {
mfaRequiredCheckProto, err = h.checkAndGetProtoRequest(r.Context(), c, req.IsMFARequiredRequest)
mfaRequiredCheckProto, err = h.checkAndGetProtoRequest(ctx, c, req.IsMFARequiredRequest)
if err != nil {
return nil, trace.Wrap(err)
}

// If the MFA requirement check is being performed for a leaf host, we must check directly
// with the leaf cluster before the authentication challenge request through root.
if req.IsMFARequiredRequest.ClusterID != "" && req.IsMFARequiredRequest.ClusterID != c.cfg.RootClusterName {
site, err := h.getSiteByClusterName(ctx, c, req.IsMFARequiredRequest.ClusterID)
if err != nil {
return nil, trace.Wrap(err)
}

clusterClient, err := c.GetUserClient(ctx, site)
if err != nil {
return false, trace.Wrap(err)
}

res, err := clusterClient.IsMFARequired(ctx, mfaRequiredCheckProto)
if err != nil {
return false, trace.Wrap(err)
}

if !res.Required {
return &client.MFAAuthenticateChallenge{}, nil
}

// We don't want to check again through the root cluster below.
mfaRequiredCheckProto = nil
}
}

allowReuse := mfav1.ChallengeAllowReuse_CHALLENGE_ALLOW_REUSE_NO
Expand All @@ -219,7 +247,7 @@ func (h *Handler) createAuthenticateChallengeHandle(w http.ResponseWriter, r *ht
query.Set("channel_id", channelID)
ssoClientRedirectURL.RawQuery = query.Encode()

chal, err := clt.CreateAuthenticateChallenge(r.Context(), &proto.CreateAuthenticateChallengeRequest{
chal, err := clt.CreateAuthenticateChallenge(ctx, &proto.CreateAuthenticateChallengeRequest{
Request: &proto.CreateAuthenticateChallengeRequest_ContextUser{
ContextUser: &proto.ContextUser{},
},
Expand Down Expand Up @@ -428,6 +456,9 @@ type isMFARequiredApp struct {
type isMFARequiredAdminAction struct{}

type isMFARequiredRequest struct {
// ClusterID is the ID of the cluster to check against for MFA requirements.
// If not set, MFA requirements will be checked against the root cluster.
ClusterID string `json:"clusterId,omitempty"`
// Database contains fields required to check if target database
// requires MFA check.
Database *isMFARequiredDatabase `json:"database,omitempty"`
Expand Down Expand Up @@ -533,7 +564,9 @@ func (h *Handler) checkAndGetProtoRequest(ctx context.Context, scx *SessionConte
protoReq = &proto.IsMFARequiredRequest{
Target: &proto.IsMFARequiredRequest_App{
App: &proto.RouteToApp{
Name: resolvedApp.App.GetName(),
Name: resolvedApp.App.GetName(),
PublicAddr: resolvedApp.App.GetPublicAddr(),
ClusterName: resolvedApp.ClusterName,
},
},
}
Expand Down
1 change: 1 addition & 0 deletions web/packages/teleport/src/AppLauncher/AppLauncher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export function AppLauncher() {
req: {
scope: MfaChallengeScope.USER_SESSION,
isMfaRequiredRequest: {
clusterId: pathParams.clusterId,
app: {
fqdn: pathParams.fqdn,
cluster_name: pathParams.clusterId,
Expand Down
6 changes: 4 additions & 2 deletions web/packages/teleport/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -749,8 +749,10 @@ const cfg = {
return generatePath(cfg.api.connectionDiagnostic, { clusterId });
},

getMfaRequiredUrl() {
const clusterId = cfg.proxyCluster;
getMfaRequiredUrl(clusterId?: string) {
if (!clusterId) {
clusterId = cfg.proxyCluster;
}
return generatePath(cfg.api.mfaRequired, { clusterId });
},

Expand Down
11 changes: 8 additions & 3 deletions web/packages/teleport/src/services/auth/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ function checkMfaRequired(
params: IsMfaRequiredRequest,
abortSignal?
): Promise<IsMfaRequiredResponse> {
return api.post(cfg.getMfaRequiredUrl(), params, abortSignal);
return api.post(cfg.getMfaRequiredUrl(params.clusterId), params, abortSignal);
}

function base64EncodeUnicode(str: string) {
Expand Down Expand Up @@ -508,13 +508,18 @@ function waitForMessage(

export default auth;

export type IsMfaRequiredRequest =
export type IsMfaRequiredRequest = {
// clusterId is the cluster to check mfa requirement against. When connecting to
// leaf hosts, this should be set to the leaf clusterId.
clusterId?: string;
} & (
| IsMfaRequiredDatabase
| IsMfaRequiredNode
| IsMfaRequiredKube
| IsMfaRequiredWindowsDesktop
| IsMfaRequiredApp
| IsMfaRequiredAdminAction;
| IsMfaRequiredAdminAction
);

export type IsMfaRequiredResponse = {
required: boolean;
Expand Down
Loading