mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
embed: update "CorsInfo" field to "map[string]struct{}"
Signed-off-by: Gyuho Lee <gyuhox@gmail.com>
This commit is contained in:
parent
35b01b982c
commit
df6cd22d59
@ -28,7 +28,7 @@ import (
|
|||||||
|
|
||||||
"github.com/coreos/etcd/compactor"
|
"github.com/coreos/etcd/compactor"
|
||||||
"github.com/coreos/etcd/etcdserver"
|
"github.com/coreos/etcd/etcdserver"
|
||||||
"github.com/coreos/etcd/pkg/cors"
|
"github.com/coreos/etcd/pkg/flags"
|
||||||
"github.com/coreos/etcd/pkg/netutil"
|
"github.com/coreos/etcd/pkg/netutil"
|
||||||
"github.com/coreos/etcd/pkg/srv"
|
"github.com/coreos/etcd/pkg/srv"
|
||||||
"github.com/coreos/etcd/pkg/transport"
|
"github.com/coreos/etcd/pkg/transport"
|
||||||
@ -79,9 +79,8 @@ var (
|
|||||||
DefaultInitialAdvertisePeerURLs = "http://localhost:2380"
|
DefaultInitialAdvertisePeerURLs = "http://localhost:2380"
|
||||||
DefaultAdvertiseClientURLs = "http://localhost:2379"
|
DefaultAdvertiseClientURLs = "http://localhost:2379"
|
||||||
|
|
||||||
defaultHostname string
|
defaultHostname string
|
||||||
defaultHostStatus error
|
defaultHostStatus error
|
||||||
defaultHostWhitelist = []string{} // if empty, allow all
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -107,7 +106,6 @@ func init() {
|
|||||||
|
|
||||||
// Config holds the arguments for configuring an etcd server.
|
// Config holds the arguments for configuring an etcd server.
|
||||||
type Config struct {
|
type Config struct {
|
||||||
CorsInfo *cors.CORSInfo
|
|
||||||
LPUrls, LCUrls []url.URL
|
LPUrls, LCUrls []url.URL
|
||||||
Dir string `json:"data-dir"`
|
Dir string `json:"data-dir"`
|
||||||
WalDir string `json:"wal-dir"`
|
WalDir string `json:"wal-dir"`
|
||||||
@ -171,6 +169,8 @@ type Config struct {
|
|||||||
PeerTLSInfo transport.TLSInfo
|
PeerTLSInfo transport.TLSInfo
|
||||||
PeerAutoTLS bool
|
PeerAutoTLS bool
|
||||||
|
|
||||||
|
CorsInfo map[string]struct{}
|
||||||
|
|
||||||
// HostWhitelist lists acceptable hostnames from HTTP client requests.
|
// HostWhitelist lists acceptable hostnames from HTTP client requests.
|
||||||
// Client origin policy protects against "DNS Rebinding" attacks
|
// Client origin policy protects against "DNS Rebinding" attacks
|
||||||
// to insecure etcd servers. That is, any website can simply create
|
// to insecure etcd servers. That is, any website can simply create
|
||||||
@ -186,7 +186,7 @@ type Config struct {
|
|||||||
// Note that the client origin policy is enforced whether authentication
|
// Note that the client origin policy is enforced whether authentication
|
||||||
// is enabled or not, for tighter controls.
|
// is enabled or not, for tighter controls.
|
||||||
//
|
//
|
||||||
// By default, "HostWhitelist" is empty, which allows any hostnames.
|
// By default, "HostWhitelist" is empty or "*", which allows any hostnames.
|
||||||
// Note that when specifying hostnames, loopback addresses are not added
|
// Note that when specifying hostnames, loopback addresses are not added
|
||||||
// automatically. To allow loopback interfaces, leave it empty or add them
|
// automatically. To allow loopback interfaces, leave it empty or add them
|
||||||
// to whitelist manually (e.g. "localhost", "127.0.0.1", etc.).
|
// to whitelist manually (e.g. "localhost", "127.0.0.1", etc.).
|
||||||
@ -195,7 +195,7 @@ type Config struct {
|
|||||||
// - https://bugs.chromium.org/p/project-zero/issues/detail?id=1447#c2
|
// - https://bugs.chromium.org/p/project-zero/issues/detail?id=1447#c2
|
||||||
// - https://github.com/transmission/transmission/pull/468
|
// - https://github.com/transmission/transmission/pull/468
|
||||||
// - https://github.com/coreos/etcd/issues/9353
|
// - https://github.com/coreos/etcd/issues/9353
|
||||||
HostWhitelist []string `json:"host-whitelist"`
|
HostWhitelist map[string]struct{}
|
||||||
|
|
||||||
Debug bool `json:"debug"`
|
Debug bool `json:"debug"`
|
||||||
LogPkgLevels string `json:"log-package-levels"`
|
LogPkgLevels string `json:"log-package-levels"`
|
||||||
@ -237,11 +237,14 @@ type configYAML struct {
|
|||||||
|
|
||||||
// configJSON has file options that are translated into Config options
|
// configJSON has file options that are translated into Config options
|
||||||
type configJSON struct {
|
type configJSON struct {
|
||||||
LPUrlsJSON string `json:"listen-peer-urls"`
|
LPUrlsJSON string `json:"listen-peer-urls"`
|
||||||
LCUrlsJSON string `json:"listen-client-urls"`
|
LCUrlsJSON string `json:"listen-client-urls"`
|
||||||
CorsJSON string `json:"cors"`
|
APUrlsJSON string `json:"initial-advertise-peer-urls"`
|
||||||
APUrlsJSON string `json:"initial-advertise-peer-urls"`
|
ACUrlsJSON string `json:"advertise-client-urls"`
|
||||||
ACUrlsJSON string `json:"advertise-client-urls"`
|
|
||||||
|
CorsJSON string `json:"cors"`
|
||||||
|
HostWhitelistJSON string `json:"host-whitelist"`
|
||||||
|
|
||||||
ClientSecurityJSON securityConfig `json:"client-transport-security"`
|
ClientSecurityJSON securityConfig `json:"client-transport-security"`
|
||||||
PeerSecurityJSON securityConfig `json:"peer-transport-security"`
|
PeerSecurityJSON securityConfig `json:"peer-transport-security"`
|
||||||
}
|
}
|
||||||
@ -261,7 +264,6 @@ func NewConfig() *Config {
|
|||||||
lcurl, _ := url.Parse(DefaultListenClientURLs)
|
lcurl, _ := url.Parse(DefaultListenClientURLs)
|
||||||
acurl, _ := url.Parse(DefaultAdvertiseClientURLs)
|
acurl, _ := url.Parse(DefaultAdvertiseClientURLs)
|
||||||
cfg := &Config{
|
cfg := &Config{
|
||||||
CorsInfo: &cors.CORSInfo{},
|
|
||||||
MaxSnapFiles: DefaultMaxSnapshots,
|
MaxSnapFiles: DefaultMaxSnapshots,
|
||||||
MaxWalFiles: DefaultMaxWALs,
|
MaxWalFiles: DefaultMaxWALs,
|
||||||
Name: DefaultName,
|
Name: DefaultName,
|
||||||
@ -283,7 +285,8 @@ func NewConfig() *Config {
|
|||||||
LogOutput: DefaultLogOutput,
|
LogOutput: DefaultLogOutput,
|
||||||
Metrics: "basic",
|
Metrics: "basic",
|
||||||
EnableV2: DefaultEnableV2,
|
EnableV2: DefaultEnableV2,
|
||||||
HostWhitelist: defaultHostWhitelist,
|
CorsInfo: make(map[string]struct{}),
|
||||||
|
HostWhitelist: make(map[string]struct{}),
|
||||||
AuthToken: "simple",
|
AuthToken: "simple",
|
||||||
PreVote: false, // TODO: enable by default in v3.5
|
PreVote: false, // TODO: enable by default in v3.5
|
||||||
}
|
}
|
||||||
@ -381,12 +384,6 @@ func (cfg *configYAML) configFromFile(path string) error {
|
|||||||
cfg.LCUrls = []url.URL(u)
|
cfg.LCUrls = []url.URL(u)
|
||||||
}
|
}
|
||||||
|
|
||||||
if cfg.CorsJSON != "" {
|
|
||||||
if err := cfg.CorsInfo.Set(cfg.CorsJSON); err != nil {
|
|
||||||
plog.Panicf("unexpected error setting up cors: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if cfg.APUrlsJSON != "" {
|
if cfg.APUrlsJSON != "" {
|
||||||
u, err := types.NewURLs(strings.Split(cfg.APUrlsJSON, ","))
|
u, err := types.NewURLs(strings.Split(cfg.APUrlsJSON, ","))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -411,6 +408,16 @@ func (cfg *configYAML) configFromFile(path string) error {
|
|||||||
cfg.ListenMetricsUrls = []url.URL(u)
|
cfg.ListenMetricsUrls = []url.URL(u)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if cfg.CorsJSON != "" {
|
||||||
|
uv := flags.NewUniqueURLsWithExceptions(cfg.HostWhitelistJSON, "*")
|
||||||
|
cfg.CorsInfo = uv.Values
|
||||||
|
}
|
||||||
|
|
||||||
|
if cfg.HostWhitelistJSON != "" {
|
||||||
|
uv := flags.NewUniqueStringsValue(cfg.HostWhitelistJSON)
|
||||||
|
cfg.HostWhitelist = uv.Values
|
||||||
|
}
|
||||||
|
|
||||||
// If a discovery flag is set, clear default initial cluster set by InitialClusterFromName
|
// If a discovery flag is set, clear default initial cluster set by InitialClusterFromName
|
||||||
if (cfg.Durl != "" || cfg.DNSCluster != "") && cfg.InitialCluster == defaultInitialCluster {
|
if (cfg.Durl != "" || cfg.DNSCluster != "") && cfg.InitialCluster == defaultInitialCluster {
|
||||||
cfg.InitialCluster = ""
|
cfg.InitialCluster = ""
|
||||||
|
@ -168,6 +168,7 @@ func StartEtcd(inCfg *Config) (e *Etcd, err error) {
|
|||||||
StrictReconfigCheck: cfg.StrictReconfigCheck,
|
StrictReconfigCheck: cfg.StrictReconfigCheck,
|
||||||
ClientCertAuthEnabled: cfg.ClientTLSInfo.ClientCertAuth,
|
ClientCertAuthEnabled: cfg.ClientTLSInfo.ClientCertAuth,
|
||||||
AuthToken: cfg.AuthToken,
|
AuthToken: cfg.AuthToken,
|
||||||
|
HostWhitelist: cfg.HostWhitelist,
|
||||||
InitialCorruptCheck: cfg.ExperimentalInitialCorruptCheck,
|
InitialCorruptCheck: cfg.ExperimentalInitialCorruptCheck,
|
||||||
CorruptCheckTime: cfg.ExperimentalCorruptCheckTime,
|
CorruptCheckTime: cfg.ExperimentalCorruptCheckTime,
|
||||||
PreVote: cfg.PreVote,
|
PreVote: cfg.PreVote,
|
||||||
@ -175,13 +176,6 @@ func StartEtcd(inCfg *Config) (e *Etcd, err error) {
|
|||||||
ForceNewCluster: cfg.ForceNewCluster,
|
ForceNewCluster: cfg.ForceNewCluster,
|
||||||
}
|
}
|
||||||
|
|
||||||
srvcfg.HostWhitelist = make(map[string]struct{}, len(cfg.HostWhitelist))
|
|
||||||
for _, h := range cfg.HostWhitelist {
|
|
||||||
if h != "" {
|
|
||||||
srvcfg.HostWhitelist[h] = struct{}{}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if e.Server, err = etcdserver.NewServer(srvcfg); err != nil {
|
if e.Server, err = etcdserver.NewServer(srvcfg); err != nil {
|
||||||
return e, err
|
return e, err
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user