-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Allow webauthn to be passed when issuing certs for web-based scp #22864
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
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 |
|---|---|---|
|
|
@@ -17,14 +17,19 @@ limitations under the License. | |
| package web | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "net/http" | ||
| "time" | ||
|
|
||
| "github.com/gravitational/trace" | ||
| "github.com/julienschmidt/httprouter" | ||
| "golang.org/x/crypto/ssh" | ||
|
|
||
| "github.com/gravitational/teleport/api/client/proto" | ||
| "github.com/gravitational/teleport/api/defaults" | ||
| "github.com/gravitational/teleport/api/types" | ||
| "github.com/gravitational/teleport/lib/auth" | ||
| wanlib "github.com/gravitational/teleport/lib/auth/webauthn" | ||
| "github.com/gravitational/teleport/lib/client" | ||
| "github.com/gravitational/teleport/lib/reversetunnel" | ||
| "github.com/gravitational/teleport/lib/sshutils/scp" | ||
|
|
@@ -44,6 +49,8 @@ type fileTransferRequest struct { | |
| remoteLocation string | ||
| // filename is a file name | ||
| filename string | ||
| // webauthn is an optional parameter that contains a webauthn response string used to issue single use certs | ||
| webauthn string | ||
| } | ||
|
|
||
| func (h *Handler) transferFile(w http.ResponseWriter, r *http.Request, p httprouter.Params, sctx *SessionContext, site reversetunnel.RemoteSite) (interface{}, error) { | ||
|
|
@@ -55,6 +62,7 @@ func (h *Handler) transferFile(w http.ResponseWriter, r *http.Request, p httprou | |
| remoteLocation: query.Get("location"), | ||
| filename: query.Get("filename"), | ||
| namespace: defaults.Namespace, | ||
| webauthn: query.Get("webauthn"), | ||
| } | ||
|
|
||
| clt, err := sctx.GetUserClient(r.Context(), site) | ||
|
|
@@ -68,6 +76,22 @@ func (h *Handler) transferFile(w http.ResponseWriter, r *http.Request, p httprou | |
| proxyHostPort: h.ProxyHostPort(), | ||
| } | ||
|
|
||
| mfaReq, err := clt.IsMFARequired(r.Context(), &proto.IsMFARequiredRequest{ | ||
| Target: &proto.IsMFARequiredRequest_Node{ | ||
| Node: &proto.NodeLogin{ | ||
| Node: p.ByName("server"), | ||
| Login: p.ByName("login"), | ||
| }, | ||
| }, | ||
| }) | ||
| if err != nil { | ||
| return nil, trace.Wrap(err) | ||
| } | ||
|
|
||
| if mfaReq.Required && query.Get("webauthn") == "" { | ||
| return nil, trace.AccessDenied("MFA required for file transfer") | ||
| } | ||
|
|
||
| isUpload := r.Method == http.MethodPost | ||
| if isUpload { | ||
| err = ft.upload(req, r) | ||
|
|
@@ -106,6 +130,13 @@ func (f *fileTransfer) download(req fileTransferRequest, httpReq *http.Request, | |
| return trace.Wrap(err) | ||
| } | ||
|
|
||
| if req.webauthn != "" { | ||
| err = f.issueSingleUseCert(req.webauthn, httpReq, tc) | ||
| if err != nil { | ||
| return trace.Wrap(err) | ||
| } | ||
| } | ||
|
|
||
| err = tc.ExecuteSCP(httpReq.Context(), req.serverID, cmd) | ||
| if err != nil { | ||
| return trace.Wrap(err) | ||
|
|
@@ -130,6 +161,13 @@ func (f *fileTransfer) upload(req fileTransferRequest, httpReq *http.Request) er | |
| return trace.Wrap(err) | ||
| } | ||
|
|
||
| if req.webauthn != "" { | ||
| err = f.issueSingleUseCert(req.webauthn, httpReq, tc) | ||
| if err != nil { | ||
| return trace.Wrap(err) | ||
| } | ||
| } | ||
|
|
||
| err = tc.ExecuteSCP(httpReq.Context(), req.serverID, cmd) | ||
| if err != nil { | ||
| return trace.Wrap(err) | ||
|
|
@@ -179,3 +217,49 @@ func (f *fileTransfer) createClient(req fileTransferRequest, httpReq *http.Reque | |
|
|
||
| return tc, nil | ||
| } | ||
|
|
||
| type mfaRequest struct { | ||
| // WebauthnResponse is the response from authenticators. | ||
| WebauthnAssertionResponse *wanlib.CredentialAssertionResponse `json:"webauthnAssertionResponse"` | ||
| } | ||
|
|
||
| // issueSingleUseCert will take an assertion response sent from a solved challenge in the web UI | ||
| // and use that to generate a cert. This cert is added to the Teleport Client as an authmethod that | ||
| // can be used to connect to a node. | ||
| func (f *fileTransfer) issueSingleUseCert(webauthn string, httpReq *http.Request, tc *client.TeleportClient) error { | ||
| var mfaReq mfaRequest | ||
| err := json.Unmarshal([]byte(webauthn), &mfaReq) | ||
| if err != nil { | ||
| return trace.Wrap(err) | ||
| } | ||
|
|
||
| key, err := client.GenerateRSAKey() | ||
| if err != nil { | ||
| return trace.Wrap(err) | ||
| } | ||
|
|
||
| cert, err := f.authClient.GenerateUserCerts(httpReq.Context(), proto.UserCertsRequest{ | ||
| PublicKey: key.MarshalSSHPublicKey(), | ||
| Username: f.ctx.GetUser(), | ||
| Expires: time.Now().Add(time.Minute).UTC(), | ||
|
Collaborator
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. Is 1 minute enough? Should we add a little buffer to account for clock drift, or is 1m what we use elsewhere too?
Contributor
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. 1m is used in the same way for testing connections in Discovery. I think 1m is fine as this cert is being used directly after this in the same request handler and no where else. I suppose we could make the expiry shorter if we needed, but doesn't need to be longer. |
||
| MFAResponse: &proto.MFAAuthenticateResponse{ | ||
| Response: &proto.MFAAuthenticateResponse_Webauthn{ | ||
| Webauthn: wanlib.CredentialAssertionResponseToProto(mfaReq.WebauthnAssertionResponse), | ||
| }, | ||
| }, | ||
| }) | ||
| if err != nil { | ||
| return trace.Wrap(err) | ||
| } | ||
|
|
||
| key.Cert = cert.SSH | ||
|
|
||
| am, err := key.AsAuthMethod() | ||
| if err != nil { | ||
| return trace.Wrap(err) | ||
| } | ||
|
|
||
| tc.AuthMethods = []ssh.AuthMethod{am} | ||
|
|
||
| return nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /** | ||
| * Copyright 2023 Gravitational, Inc | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import useAttempt from 'shared/hooks/useAttemptNext'; | ||
|
|
||
| import cfg, { UrlScpParams } from 'teleport/config'; | ||
| import auth from 'teleport/services/auth/auth'; | ||
|
|
||
| export default function useGetScpUrl(addMfaToScpUrls: boolean) { | ||
| const { setAttempt, attempt, handleError } = useAttempt(''); | ||
|
|
||
| async function getScpUrl(params: UrlScpParams) { | ||
| setAttempt({ | ||
| status: 'processing', | ||
| statusText: '', | ||
| }); | ||
| if (!addMfaToScpUrls) { | ||
| return cfg.getScpUrl(params); | ||
| } | ||
| try { | ||
| let webauthn = await auth.getWebauthnResponse(); | ||
| setAttempt({ | ||
| status: 'success', | ||
| statusText: '', | ||
| }); | ||
| return cfg.getScpUrl({ | ||
| webauthn, | ||
| ...params, | ||
| }); | ||
| } catch (error) { | ||
| handleError(error); | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| getScpUrl, | ||
| attempt, | ||
| }; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At first it wasn't clear to me what happens to the single use cert that gets issued. Maybe a godoc on
issueSingleUseCertthat explains that it configurestcto use the new cert would help.