JSON Config object key should match JSON tags
License: MIT Signed-off-by: Kishan Mohanbhai Sagathiya <kishansagathiya@gmail.com>
This commit is contained in:
parent
e1e331600a
commit
586253a2ce
|
@ -104,9 +104,9 @@ type Config struct {
|
||||||
ID peer.ID
|
ID peer.ID
|
||||||
PrivateKey crypto.PrivKey
|
PrivateKey crypto.PrivKey
|
||||||
|
|
||||||
// BasicAuthCreds is a map of username-password pairs
|
// BasicAuthCredentials is a map of username-password pairs
|
||||||
// which are authorized to use Basic Authentication
|
// which are authorized to use Basic Authentication
|
||||||
BasicAuthCreds map[string]string
|
BasicAuthCredentials map[string]string
|
||||||
|
|
||||||
// Headers provides customization for the headers returned
|
// Headers provides customization for the headers returned
|
||||||
// by the API on existing routes.
|
// by the API on existing routes.
|
||||||
|
@ -138,7 +138,7 @@ type jsonConfig struct {
|
||||||
ID string `json:"id,omitempty"`
|
ID string `json:"id,omitempty"`
|
||||||
PrivateKey string `json:"private_key,omitempty"`
|
PrivateKey string `json:"private_key,omitempty"`
|
||||||
|
|
||||||
BasicAuthCreds map[string]string `json:"basic_auth_credentials"`
|
BasicAuthCredentials map[string]string `json:"basic_auth_credentials"`
|
||||||
Headers map[string][]string `json:"headers"`
|
Headers map[string][]string `json:"headers"`
|
||||||
|
|
||||||
CORSAllowedOrigins []string `json:"cors_allowed_origins"`
|
CORSAllowedOrigins []string `json:"cors_allowed_origins"`
|
||||||
|
@ -174,7 +174,7 @@ func (cfg *Config) Default() error {
|
||||||
cfg.Libp2pListenAddr = nil
|
cfg.Libp2pListenAddr = nil
|
||||||
|
|
||||||
// Auth
|
// Auth
|
||||||
cfg.BasicAuthCreds = nil
|
cfg.BasicAuthCredentials = nil
|
||||||
|
|
||||||
// Headers
|
// Headers
|
||||||
cfg.Headers = DefaultHeaders
|
cfg.Headers = DefaultHeaders
|
||||||
|
@ -219,7 +219,7 @@ func (cfg *Config) Validate() error {
|
||||||
return errors.New("restapi.idle_timeout invalid")
|
return errors.New("restapi.idle_timeout invalid")
|
||||||
case cfg.MaxHeaderBytes < minMaxHeaderBytes:
|
case cfg.MaxHeaderBytes < minMaxHeaderBytes:
|
||||||
return fmt.Errorf("restapi.max_header_bytes must be not less then %d", minMaxHeaderBytes)
|
return fmt.Errorf("restapi.max_header_bytes must be not less then %d", minMaxHeaderBytes)
|
||||||
case cfg.BasicAuthCreds != nil && len(cfg.BasicAuthCreds) == 0:
|
case cfg.BasicAuthCredentials != nil && len(cfg.BasicAuthCredentials) == 0:
|
||||||
return errors.New("restapi.basic_auth_creds should be null or have at least one entry")
|
return errors.New("restapi.basic_auth_creds should be null or have at least one entry")
|
||||||
case (cfg.pathSSLCertFile != "" || cfg.pathSSLKeyFile != "") && cfg.TLS == nil:
|
case (cfg.pathSSLCertFile != "" || cfg.pathSSLKeyFile != "") && cfg.TLS == nil:
|
||||||
return errors.New("restapi: missing TLS configuration")
|
return errors.New("restapi: missing TLS configuration")
|
||||||
|
@ -270,7 +270,7 @@ func (cfg *Config) applyJSONConfig(jcfg *jsonConfig) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Other options
|
// Other options
|
||||||
cfg.BasicAuthCreds = jcfg.BasicAuthCreds
|
cfg.BasicAuthCredentials = jcfg.BasicAuthCredentials
|
||||||
cfg.Headers = jcfg.Headers
|
cfg.Headers = jcfg.Headers
|
||||||
|
|
||||||
return cfg.Validate()
|
return cfg.Validate()
|
||||||
|
@ -408,7 +408,7 @@ func (cfg *Config) toJSONConfig() (jcfg *jsonConfig, err error) {
|
||||||
WriteTimeout: cfg.WriteTimeout.String(),
|
WriteTimeout: cfg.WriteTimeout.String(),
|
||||||
IdleTimeout: cfg.IdleTimeout.String(),
|
IdleTimeout: cfg.IdleTimeout.String(),
|
||||||
MaxHeaderBytes: cfg.MaxHeaderBytes,
|
MaxHeaderBytes: cfg.MaxHeaderBytes,
|
||||||
BasicAuthCreds: cfg.BasicAuthCreds,
|
BasicAuthCredentials: cfg.BasicAuthCredentials,
|
||||||
Headers: cfg.Headers,
|
Headers: cfg.Headers,
|
||||||
CORSAllowedOrigins: cfg.CORSAllowedOrigins,
|
CORSAllowedOrigins: cfg.CORSAllowedOrigins,
|
||||||
CORSAllowedMethods: cfg.CORSAllowedMethods,
|
CORSAllowedMethods: cfg.CORSAllowedMethods,
|
||||||
|
|
|
@ -75,7 +75,7 @@ func TestLoadJSON(t *testing.T) {
|
||||||
|
|
||||||
j = &jsonConfig{}
|
j = &jsonConfig{}
|
||||||
json.Unmarshal(cfgJSON, j)
|
json.Unmarshal(cfgJSON, j)
|
||||||
j.BasicAuthCreds = make(map[string]string)
|
j.BasicAuthCredentials = make(map[string]string)
|
||||||
tst, _ = json.Marshal(j)
|
tst, _ = json.Marshal(j)
|
||||||
err = cfg.LoadJSON(tst)
|
err = cfg.LoadJSON(tst)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
@ -133,7 +133,7 @@ func TestApplyEnvVars(t *testing.T) {
|
||||||
password := "thisaintmypassword"
|
password := "thisaintmypassword"
|
||||||
user1 := "user1"
|
user1 := "user1"
|
||||||
user1pass := "user1passwd"
|
user1pass := "user1passwd"
|
||||||
os.Setenv("CLUSTER_RESTAPI_BASICAUTHCREDS", username+":"+password+","+user1+":"+user1pass)
|
os.Setenv("CLUSTER_RESTAPI_BASICAUTHCREDENTIALS", username+":"+password+","+user1+":"+user1pass)
|
||||||
cfg := &Config{}
|
cfg := &Config{}
|
||||||
cfg.Default()
|
cfg.Default()
|
||||||
err := cfg.ApplyEnvVars()
|
err := cfg.ApplyEnvVars()
|
||||||
|
@ -141,19 +141,19 @@ func TestApplyEnvVars(t *testing.T) {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, ok := cfg.BasicAuthCreds[username]; !ok {
|
if _, ok := cfg.BasicAuthCredentials[username]; !ok {
|
||||||
t.Fatalf("username '%s' not set in BasicAuthCreds map: %v", username, cfg.BasicAuthCreds)
|
t.Fatalf("username '%s' not set in BasicAuthCreds map: %v", username, cfg.BasicAuthCredentials)
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, ok := cfg.BasicAuthCreds[user1]; !ok {
|
if _, ok := cfg.BasicAuthCredentials[user1]; !ok {
|
||||||
t.Fatalf("username '%s' not set in BasicAuthCreds map: %v", user1, cfg.BasicAuthCreds)
|
t.Fatalf("username '%s' not set in BasicAuthCreds map: %v", user1, cfg.BasicAuthCredentials)
|
||||||
}
|
}
|
||||||
|
|
||||||
if gotpasswd := cfg.BasicAuthCreds[username]; gotpasswd != password {
|
if gotpasswd := cfg.BasicAuthCredentials[username]; gotpasswd != password {
|
||||||
t.Errorf("password not what was set in env var, got: %s, want: %s", gotpasswd, password)
|
t.Errorf("password not what was set in env var, got: %s, want: %s", gotpasswd, password)
|
||||||
}
|
}
|
||||||
|
|
||||||
if gotpasswd := cfg.BasicAuthCreds[user1]; gotpasswd != user1pass {
|
if gotpasswd := cfg.BasicAuthCredentials[user1]; gotpasswd != user1pass {
|
||||||
t.Errorf("password not what was set in env var, got: %s, want: %s", gotpasswd, user1pass)
|
t.Errorf("password not what was set in env var, got: %s, want: %s", gotpasswd, user1pass)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -118,7 +118,7 @@ func NewAPIWithHost(ctx context.Context, cfg *Config, h host.Host) (*API, error)
|
||||||
// wrapped with the basic auth handler.
|
// wrapped with the basic auth handler.
|
||||||
router := mux.NewRouter().StrictSlash(true)
|
router := mux.NewRouter().StrictSlash(true)
|
||||||
handler := basicAuthHandler(
|
handler := basicAuthHandler(
|
||||||
cfg.BasicAuthCreds,
|
cfg.BasicAuthCredentials,
|
||||||
cors.New(*cfg.corsOptions()).Handler(router),
|
cors.New(*cfg.corsOptions()).Handler(router),
|
||||||
)
|
)
|
||||||
if cfg.Tracing {
|
if cfg.Tracing {
|
||||||
|
|
|
@ -89,7 +89,7 @@ func testHTTPSAPI(t *testing.T) *API {
|
||||||
func testAPIwithBasicAuth(t *testing.T) *API {
|
func testAPIwithBasicAuth(t *testing.T) *API {
|
||||||
cfg := &Config{}
|
cfg := &Config{}
|
||||||
cfg.Default()
|
cfg.Default()
|
||||||
cfg.BasicAuthCreds = map[string]string{
|
cfg.BasicAuthCredentials = map[string]string{
|
||||||
validUserName: validUserPassword,
|
validUserName: validUserPassword,
|
||||||
adminUserName: adminUserPassword,
|
adminUserName: adminUserPassword,
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,12 +35,12 @@ type Config struct {
|
||||||
config.Saver
|
config.Saver
|
||||||
|
|
||||||
MetricTTL time.Duration
|
MetricTTL time.Duration
|
||||||
Type MetricType
|
MetricType MetricType
|
||||||
}
|
}
|
||||||
|
|
||||||
type jsonConfig struct {
|
type jsonConfig struct {
|
||||||
MetricTTL string `json:"metric_ttl"`
|
MetricTTL string `json:"metric_ttl"`
|
||||||
Type string `json:"metric_type"`
|
MetricType string `json:"metric_type"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// ConfigKey returns a human-friendly identifier for this type of Metric.
|
// ConfigKey returns a human-friendly identifier for this type of Metric.
|
||||||
|
@ -51,7 +51,7 @@ func (cfg *Config) ConfigKey() string {
|
||||||
// Default initializes this Config with sensible values.
|
// Default initializes this Config with sensible values.
|
||||||
func (cfg *Config) Default() error {
|
func (cfg *Config) Default() error {
|
||||||
cfg.MetricTTL = DefaultMetricTTL
|
cfg.MetricTTL = DefaultMetricTTL
|
||||||
cfg.Type = DefaultMetricType
|
cfg.MetricType = DefaultMetricType
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -75,7 +75,7 @@ func (cfg *Config) Validate() error {
|
||||||
return errors.New("disk.metric_ttl is invalid")
|
return errors.New("disk.metric_ttl is invalid")
|
||||||
}
|
}
|
||||||
|
|
||||||
if cfg.Type.String() == "" {
|
if cfg.MetricType.String() == "" {
|
||||||
return errors.New("disk.metric_type is invalid")
|
return errors.New("disk.metric_type is invalid")
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
@ -100,11 +100,11 @@ func (cfg *Config) applyJSONConfig(jcfg *jsonConfig) error {
|
||||||
t, _ := time.ParseDuration(jcfg.MetricTTL)
|
t, _ := time.ParseDuration(jcfg.MetricTTL)
|
||||||
cfg.MetricTTL = t
|
cfg.MetricTTL = t
|
||||||
|
|
||||||
switch jcfg.Type {
|
switch jcfg.MetricType {
|
||||||
case "reposize":
|
case "reposize":
|
||||||
cfg.Type = MetricRepoSize
|
cfg.MetricType = MetricRepoSize
|
||||||
case "freespace":
|
case "freespace":
|
||||||
cfg.Type = MetricFreeSpace
|
cfg.MetricType = MetricFreeSpace
|
||||||
default:
|
default:
|
||||||
return errors.New("disk.metric_type is invalid")
|
return errors.New("disk.metric_type is invalid")
|
||||||
}
|
}
|
||||||
|
@ -124,6 +124,6 @@ func (cfg *Config) ToJSON() (raw []byte, err error) {
|
||||||
func (cfg *Config) toJSONConfig() *jsonConfig {
|
func (cfg *Config) toJSONConfig() *jsonConfig {
|
||||||
return &jsonConfig{
|
return &jsonConfig{
|
||||||
MetricTTL: cfg.MetricTTL.String(),
|
MetricTTL: cfg.MetricTTL.String(),
|
||||||
Type: cfg.Type.String(),
|
MetricType: cfg.MetricType.String(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,7 +33,7 @@ func TestLoadJSON(t *testing.T) {
|
||||||
|
|
||||||
j = &jsonConfig{}
|
j = &jsonConfig{}
|
||||||
json.Unmarshal(cfgJSON, j)
|
json.Unmarshal(cfgJSON, j)
|
||||||
j.Type = "abc"
|
j.MetricType = "abc"
|
||||||
tst, _ = json.Marshal(j)
|
tst, _ = json.Marshal(j)
|
||||||
err = cfg.LoadJSON(tst)
|
err = cfg.LoadJSON(tst)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
@ -42,7 +42,7 @@ func TestLoadJSON(t *testing.T) {
|
||||||
|
|
||||||
j = &jsonConfig{}
|
j = &jsonConfig{}
|
||||||
json.Unmarshal(cfgJSON, j)
|
json.Unmarshal(cfgJSON, j)
|
||||||
j.Type = "reposize"
|
j.MetricType = "reposize"
|
||||||
tst, _ = json.Marshal(j)
|
tst, _ = json.Marshal(j)
|
||||||
err = cfg.LoadJSON(tst)
|
err = cfg.LoadJSON(tst)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -78,7 +78,7 @@ func TestDefault(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg.Default()
|
cfg.Default()
|
||||||
cfg.Type = MetricRepoSize
|
cfg.MetricType = MetricRepoSize
|
||||||
if cfg.Validate() != nil {
|
if cfg.Validate() != nil {
|
||||||
t.Fatal("MetricRepoSize is a valid type")
|
t.Fatal("MetricRepoSize is a valid type")
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,7 +47,7 @@ func NewInformer(cfg *Config) (*Informer, error) {
|
||||||
|
|
||||||
// Name returns the user-facing name of this informer.
|
// Name returns the user-facing name of this informer.
|
||||||
func (disk *Informer) Name() string {
|
func (disk *Informer) Name() string {
|
||||||
return disk.config.Type.String()
|
return disk.config.MetricType.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetClient provides us with an rpc.Client which allows
|
// SetClient provides us with an rpc.Client which allows
|
||||||
|
@ -96,7 +96,7 @@ func (disk *Informer) GetMetric(ctx context.Context) *api.Metric {
|
||||||
logger.Error(err)
|
logger.Error(err)
|
||||||
valid = false
|
valid = false
|
||||||
} else {
|
} else {
|
||||||
switch disk.config.Type {
|
switch disk.config.MetricType {
|
||||||
case MetricFreeSpace:
|
case MetricFreeSpace:
|
||||||
metric = repoStat.StorageMax - repoStat.RepoSize
|
metric = repoStat.StorageMax - repoStat.RepoSize
|
||||||
case MetricRepoSize:
|
case MetricRepoSize:
|
||||||
|
|
|
@ -52,7 +52,7 @@ func TestFreeSpace(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
cfg := &Config{}
|
cfg := &Config{}
|
||||||
cfg.Default()
|
cfg.Default()
|
||||||
cfg.Type = MetricFreeSpace
|
cfg.MetricType = MetricFreeSpace
|
||||||
|
|
||||||
inf, err := NewInformer(cfg)
|
inf, err := NewInformer(cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -78,7 +78,7 @@ func TestRepoSize(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
cfg := &Config{}
|
cfg := &Config{}
|
||||||
cfg.Default()
|
cfg.Default()
|
||||||
cfg.Type = MetricRepoSize
|
cfg.MetricType = MetricRepoSize
|
||||||
|
|
||||||
inf, err := NewInformer(cfg)
|
inf, err := NewInformer(cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user