-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add rate limiting to DB proxy server.
Improve documentation. Minor refactoring.
- Loading branch information
Showing
10 changed files
with
169 additions
and
31 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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,71 @@ | ||
/* | ||
Copyright 2021 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 db | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/gravitational/teleport/api/types" | ||
"github.com/gravitational/teleport/lib/limiter" | ||
"github.com/jackc/pgconn" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestProxyConnectionLimiting(t *testing.T) { | ||
const ( | ||
user = "bob" | ||
role = "admin" | ||
dbName = "postgres" | ||
dbUser = user | ||
connLimitNumber = 3 // Arbitrary number | ||
) | ||
|
||
ctx := context.Background() | ||
testCtx := setupTestContext(ctx, t, withSelfHostedPostgres("postgres")) | ||
|
||
connLimit, err := limiter.NewConnectionsLimiter(limiter.Config{MaxConnections: connLimitNumber}) | ||
require.NoError(t, err) | ||
|
||
// Set proxy connection limiter | ||
testCtx.proxyServer.cfg.Limiter = connLimit | ||
|
||
go testCtx.startHandlingConnections() | ||
|
||
// Create user/role with the requested permissions. | ||
testCtx.createUserAndRole(ctx, t, user, role, []string{types.Wildcard}, []string{types.Wildcard}) | ||
|
||
conns := make([]*pgconn.PgConn, 0) | ||
defer func() { | ||
for _, conn := range conns { | ||
err := conn.Close(ctx) | ||
require.NoError(t, err) | ||
} | ||
}() | ||
|
||
for i := 0; i < connLimitNumber; i++ { | ||
// Try to connect to the database as this user. | ||
pgConn, err := testCtx.postgresClient(ctx, user, "postgres", dbUser, dbName) | ||
require.NoError(t, err) | ||
|
||
conns = append(conns, pgConn) | ||
} | ||
|
||
_, err = testCtx.postgresClient(ctx, user, "postgres", dbUser, dbName) | ||
require.Error(t, err) | ||
require.Contains(t, err.Error(), "exceeded connection limit") | ||
} |
This file contains 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 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,54 @@ | ||
/* | ||
Copyright 2021 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 utils | ||
|
||
import ( | ||
"net" | ||
|
||
"github.com/gravitational/trace" | ||
) | ||
|
||
// ClientIPFromConn extracts host from provided remote address. | ||
func ClientIPFromConn(conn net.Conn) (string, error) { | ||
clientRemoteAddr := conn.RemoteAddr() | ||
|
||
clientIP, _, err := net.SplitHostPort(clientRemoteAddr.String()) | ||
if err != nil { | ||
return "", trace.Wrap(err) | ||
} | ||
|
||
return clientIP, nil | ||
} | ||
|
||
// ConnCloseWrapper is a helper struct that allows to call additional function | ||
// before net.Conn.Close() is called. | ||
type ConnCloseWrapper struct { | ||
// Underlying connection. | ||
net.Conn | ||
// BeforeClose will be called on Close(), before net.Conn.Close(). | ||
BeforeClose func() | ||
} | ||
|
||
// Close calls user provided beforeClose function and then Close() from | ||
// underlying connection. | ||
func (c *ConnCloseWrapper) Close() error { | ||
if c.BeforeClose != nil { | ||
c.BeforeClose() | ||
} | ||
|
||
return c.Conn.Close() | ||
} |