-
Notifications
You must be signed in to change notification settings - Fork 2.1k
support proxy db tunnel mfa access #16958
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
Changes from 19 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
8c6efee
Add local proxy middleware for db cert checking
GavinFrazar d6608e1
Use tls conversion util instead of inline
GavinFrazar c664b47
Add middleware to local proxy config
GavinFrazar 072c8fd
Add middleware configuration in tsh
GavinFrazar 58d3bed
Use route to database check and set defaults func
GavinFrazar 6051701
Dont trigger normal db login flow if using local proxy tunnel
GavinFrazar 2dfeafc
Split out adding client creds into helper func for testing
GavinFrazar fcdf792
Add integration test for local proxy tunnel db cert middleware
GavinFrazar 188beb2
Add unit test for local proxy middleware
GavinFrazar 31108b4
Update comment
GavinFrazar a14627e
Make middleware on new conn block
GavinFrazar 8291cdd
godoc
GavinFrazar ff3309f
Make any cert check error trigger cert renewal in local proxy middleware
GavinFrazar e4c85c2
Move dbcertchecker into lib/client
GavinFrazar 094481d
Remove unneeded mutex in local proxy and unused func in lib/utils
GavinFrazar 284bd96
Make local proxy middleware integration test more robust
GavinFrazar c525d9a
Print message before mfa prompt in proxy tunnel
GavinFrazar b6b3643
Add before prompt option to test
GavinFrazar 4a4ffdd
Remove unneeded comment
GavinFrazar 093573c
Change local proxy messages to be more clear
GavinFrazar 6ddcc49
Pass local proxy opts by reference
GavinFrazar 93f979c
Pass certs in opts instead of cert/key file path
GavinFrazar d8de925
Move db route checking back to tsh
GavinFrazar 3f4c535
Fix lint err
GavinFrazar cae0be8
Merge branch 'master' into gavinfrazar/proxy_db_tunnel_mfa_access
GavinFrazar dd4b399
Fix typo and print the hint to same writer as the mfa prompt
GavinFrazar de66715
Merge branch 'master' into gavinfrazar/proxy_db_tunnel_mfa_access
GavinFrazar 6a8e38f
Merge branch 'master' into gavinfrazar/proxy_db_tunnel_mfa_access
GavinFrazar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| /* | ||
| Copyright 2022 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. | ||
| */ | ||
|
|
||
| package client | ||
|
|
||
| import ( | ||
| "context" | ||
| "crypto/tls" | ||
| "crypto/x509" | ||
| "errors" | ||
| "fmt" | ||
| "net" | ||
| "time" | ||
|
|
||
| "github.com/gravitational/trace" | ||
| "github.com/jonboulle/clockwork" | ||
|
|
||
| "github.com/gravitational/teleport/api/client/proto" | ||
| "github.com/gravitational/teleport/api/utils/keys" | ||
| "github.com/gravitational/teleport/lib/srv/alpnproxy" | ||
| "github.com/gravitational/teleport/lib/tlsca" | ||
| "github.com/gravitational/teleport/lib/utils" | ||
| ) | ||
|
|
||
| // DBCertChecker is a middleware that ensures that the local proxy has valid TLS database certs. | ||
| type DBCertChecker struct { | ||
| // tc is a TeleportClient used to reissue certificates when necessary. | ||
| tc *TeleportClient | ||
| // dbRoute contains database routing information. | ||
| dbRoute tlsca.RouteToDatabase | ||
| // Clock specifies the time provider. Will be used to override the time anchor | ||
| // for TLS certificate verification. | ||
| // Defaults to real clock if unspecified | ||
| clock clockwork.Clock | ||
| } | ||
|
|
||
| func NewDBCertChecker(tc *TeleportClient, dbRoute tlsca.RouteToDatabase, clock clockwork.Clock) (alpnproxy.LocalProxyMiddleware, error) { | ||
| if err := dbRoute.CheckAndSetDefaults(); err != nil { | ||
| return nil, trace.Wrap(err) | ||
| } | ||
| if clock == nil { | ||
| clock = clockwork.NewRealClock() | ||
| } | ||
| return &DBCertChecker{ | ||
| tc: tc, | ||
| dbRoute: dbRoute, | ||
| clock: clock, | ||
| }, nil | ||
| } | ||
|
|
||
| var _ alpnproxy.LocalProxyMiddleware = (*DBCertChecker)(nil) | ||
|
|
||
| // OnNewConnection is a callback triggered when a new downstream connection is | ||
| // accepted by the local proxy. | ||
| func (c *DBCertChecker) OnNewConnection(ctx context.Context, lp *alpnproxy.LocalProxy, conn net.Conn) error { | ||
| return trace.Wrap(c.ensureValidCerts(ctx, lp)) | ||
| } | ||
|
|
||
| // OnStart is a callback triggered when the local proxy starts. | ||
| func (c *DBCertChecker) OnStart(ctx context.Context, lp *alpnproxy.LocalProxy) error { | ||
| return trace.Wrap(c.ensureValidCerts(ctx, lp)) | ||
| } | ||
|
|
||
| // checkCerts checks if the local proxy TLS certs are configured, not expired, and match the db route. | ||
| func (c *DBCertChecker) checkCerts(lp *alpnproxy.LocalProxy) error { | ||
| log.Debug("checking local proxy database certs") | ||
| certs := lp.GetCerts() | ||
| if len(certs) == 0 { | ||
| return trace.Wrap(trace.NotFound("local proxy has no TLS certificates configured")) | ||
| } | ||
| cert, err := utils.TLSCertToX509(certs[0]) | ||
| if err != nil { | ||
| return trace.Wrap(err) | ||
| } | ||
| err = utils.VerifyCertificateExpiry(cert, c.clock) | ||
| if err != nil { | ||
| return trace.Wrap(err) | ||
| } | ||
| identity, err := tlsca.FromSubject(cert.Subject, cert.NotAfter) | ||
| if err != nil { | ||
| return trace.Wrap(err) | ||
| } | ||
| if c.dbRoute.Username != "" && c.dbRoute.Username != identity.RouteToDatabase.Username { | ||
| msg := fmt.Sprintf("certificate subject is for user %s, but need %s", identity.RouteToDatabase.Username, c.dbRoute.Username) | ||
| return trace.Wrap(errors.New(msg)) | ||
| } | ||
| if c.dbRoute.Database != "" && c.dbRoute.Database != identity.RouteToDatabase.Database { | ||
| msg := fmt.Sprintf("certificate subject is for database name %s, but need %s", identity.RouteToDatabase.Database, c.dbRoute.Database) | ||
| return trace.Wrap(errors.New(msg)) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // ensureValidCerts ensures that the local proxy is configured with valid certs. | ||
| func (c *DBCertChecker) ensureValidCerts(ctx context.Context, lp *alpnproxy.LocalProxy) error { | ||
| if err := c.checkCerts(lp); err != nil { | ||
| log.WithError(err).Debug("need cert renewal") | ||
| } else { | ||
| return nil | ||
| } | ||
| return trace.Wrap(c.renewCerts(ctx, lp)) | ||
| } | ||
|
|
||
| // renewCerts attempts to renew the database certs for the local proxy. | ||
| func (c *DBCertChecker) renewCerts(ctx context.Context, lp *alpnproxy.LocalProxy) error { | ||
| var accessRequests []string | ||
| if profile, err := StatusCurrent(c.tc.HomePath, c.tc.WebProxyAddr, ""); err != nil { | ||
| log.WithError(err).Warn("unable to load profile, requesting database certs without access requests") | ||
| } else { | ||
| accessRequests = profile.ActiveRequests.AccessRequests | ||
| } | ||
|
|
||
| msg := fmt.Sprintf("Local proxy tunnel requires credentials to access database %q", c.dbRoute.ServiceName) | ||
| var key *Key | ||
| if err := RetryWithRelogin(ctx, c.tc, func() error { | ||
| newKey, err := c.tc.IssueUserCertsWithMFA(ctx, ReissueParams{ | ||
| RouteToCluster: c.tc.SiteName, | ||
| RouteToDatabase: proto.RouteToDatabase{ | ||
| ServiceName: c.dbRoute.ServiceName, | ||
| Protocol: c.dbRoute.Protocol, | ||
| Username: c.dbRoute.Username, | ||
| Database: c.dbRoute.Database, | ||
| }, | ||
| AccessRequests: accessRequests, | ||
| }, func(opts *PromptMFAChallengeOpts) { | ||
| opts.BeforePrompt = msg | ||
| }) | ||
| key = newKey | ||
| return trace.Wrap(err) | ||
| }); err != nil { | ||
| return trace.Wrap(err) | ||
| } | ||
|
|
||
| dbCert, ok := key.DBTLSCerts[c.dbRoute.ServiceName] | ||
| if !ok { | ||
| return trace.NotFound("database '%v' TLS cert missing", c.dbRoute.ServiceName) | ||
| } | ||
| tlsCert, err := keys.X509KeyPair(dbCert, key.PrivateKeyPEM()) | ||
| if err != nil { | ||
| return trace.Wrap(err) | ||
| } | ||
| x509cert, err := x509.ParseCertificate(tlsCert.Certificate[0]) | ||
| if err != nil { | ||
| return trace.Wrap(err) | ||
| } | ||
| certTTL := x509cert.NotAfter.Sub(c.clock.Now()).Round(time.Minute) | ||
| fmt.Printf("Proxy credentials renewed. New cert valid until %s [valid for %v]\n", | ||
| x509cert.NotAfter.Format(time.RFC3339), certTTL) | ||
| lp.SetCerts([]tls.Certificate{tlsCert}) | ||
| return nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.