-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sqlproxyccl: change denylist into an access control list
To support an IP Allowlist in the sqlproxy, this change extends the denylist code to make the Watcher support multiple AccessControllers. Each AccessController is consulted before allowing a connection through, and rechecked on any changes to the underlying files. The sqlproxy will also fail to start if it begins with an invalid allow or deny list, but if invalid files are written later then it increments a new error metric so we can be alerted and take action to fix it. Part of: https://cockroachlabs.atlassian.net/browse/CC-8136 Release note: None
- Loading branch information
PJ Tatlow
committed
Mar 16, 2023
1 parent
ea8e307
commit a20ff9b
Showing
13 changed files
with
791 additions
and
307 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright 2023 The Cockroach Authors. | ||
// | ||
// Licensed as a CockroachDB Enterprise file under the Cockroach Community | ||
// License (the "License"); you may not use this file except in compliance with | ||
// the License. You may obtain a copy of the License at | ||
// | ||
// https://github.com/cockroachdb/cockroach/blob/master/licenses/CCL.txt | ||
|
||
package acl | ||
|
||
import "github.com/cockroachdb/cockroach/pkg/util/timeutil" | ||
|
||
// ConnectionTags contains connection properties to match against the denylist. | ||
type ConnectionTags struct { | ||
IP string | ||
Cluster string | ||
} | ||
|
||
type AccessController interface { | ||
CheckConnection(ConnectionTags, timeutil.TimeSource) error | ||
} |
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,85 @@ | ||
// Copyright 2023 The Cockroach Authors. | ||
// | ||
// Licensed as a CockroachDB Enterprise file under the Cockroach Community | ||
// License (the "License"); you may not use this file except in compliance with | ||
// the License. You may obtain a copy of the License at | ||
// | ||
// https://github.com/cockroachdb/cockroach/blob/master/licenses/CCL.txt | ||
|
||
package acl | ||
|
||
import ( | ||
"net" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/util/timeutil" | ||
"github.com/cockroachdb/errors" | ||
) | ||
|
||
type AllowlistFile struct { | ||
Seq int64 `yaml:"SequenceNumber"` | ||
Allowlist map[string]AllowEntry `yaml:"allowlist"` | ||
} | ||
|
||
// Allowlist represents the current IP Allowlist, | ||
// which maps cluster IDs to a list of allowed IP ranges. | ||
type Allowlist struct { | ||
entries map[string]AllowEntry | ||
} | ||
|
||
func (al *Allowlist) UnmarshalYAML(unmarshal func(interface{}) error) error { | ||
var f AllowlistFile | ||
if err := unmarshal(&f); err != nil { | ||
return err | ||
} | ||
al.entries = f.Allowlist | ||
return nil | ||
} | ||
|
||
func (al *Allowlist) CheckConnection( | ||
connection ConnectionTags, timeSource timeutil.TimeSource, | ||
) error { | ||
entry, ok := al.entries[connection.Cluster] | ||
if !ok { | ||
// No allowlist entry, allow all traffic | ||
return nil | ||
} | ||
ip := net.ParseIP(connection.IP) | ||
if ip == nil { | ||
return errors.Newf("could not parse ip address: '%s'", ip) | ||
} | ||
// Check all ips for this cluster. | ||
// If one of them contains the current IP then it's allowed. | ||
for _, allowedIP := range entry.ips { | ||
if allowedIP.Contains(ip) { | ||
return nil | ||
} | ||
} | ||
|
||
return errors.Newf("connection ip '%s' denied: ip address not allowed", connection.IP) | ||
} | ||
|
||
type AllowEntry struct { | ||
ips []*net.IPNet | ||
} | ||
|
||
// This custom unmarshal code converts each string IP address into a *net.IPNet. | ||
// If it cannot be parsed, it is currently ignored and not added to the AllowEntry. | ||
func (e *AllowEntry) UnmarshalYAML(unmarshal func(interface{}) error) error { | ||
var raw struct { | ||
IPs []string `yaml:"ips"` | ||
} | ||
|
||
if err := unmarshal(&raw); err != nil { | ||
return err | ||
} | ||
e.ips = make([]*net.IPNet, 0) | ||
|
||
for _, ip := range raw.IPs { | ||
_, ipNet, _ := net.ParseCIDR(ip) | ||
if ipNet != nil { | ||
e.ips = append(e.ips, ipNet) | ||
} | ||
} | ||
|
||
return nil | ||
} |
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
Oops, something went wrong.