mirror of
https://github.com/minio/minio.git
synced 2025-11-09 21:49:46 -05:00
Extend further validation of config values (#8469)
- This PR allows config KVS to be validated properly without being affected by ENV overrides, rejects invalid values during set operation - Expands unit tests and refactors the error handling for notification targets, returns error instead of ignoring targets for invalid KVS - Does all the prep-work for implementing safe-mode style operation for MinIO server, introduces a new global variable to toggle safe mode based operations NOTE: this PR itself doesn't provide safe mode operations
This commit is contained in:
committed by
kannappanr
parent
599aae5ba6
commit
9e7a3e6adc
35
pkg/env/env.go
vendored
35
pkg/env/env.go
vendored
@@ -3,26 +3,47 @@ package env
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var (
|
||||
privateMutex sync.RWMutex
|
||||
envOff bool
|
||||
)
|
||||
|
||||
// SetEnvOff - turns off env lookup
|
||||
func SetEnvOff() {
|
||||
privateMutex.Lock()
|
||||
defer privateMutex.Unlock()
|
||||
|
||||
envOff = true
|
||||
}
|
||||
|
||||
// SetEnvOn - turns on env lookup
|
||||
func SetEnvOn() {
|
||||
privateMutex.Lock()
|
||||
defer privateMutex.Unlock()
|
||||
|
||||
envOff = false
|
||||
}
|
||||
|
||||
// Get retrieves the value of the environment variable named
|
||||
// by the key. If the variable is present in the environment the
|
||||
// value (which may be empty) is returned. Otherwise it returns
|
||||
// the specified default value.
|
||||
func Get(key, defaultValue string) string {
|
||||
privateMutex.RLock()
|
||||
ok := envOff
|
||||
privateMutex.RUnlock()
|
||||
if ok {
|
||||
return defaultValue
|
||||
}
|
||||
if v, ok := os.LookupEnv(key); ok {
|
||||
return v
|
||||
}
|
||||
return defaultValue
|
||||
}
|
||||
|
||||
// Lookup retrieves the value of the environment variable named
|
||||
// by the key. If the variable is present in the environment the
|
||||
// value (which may be empty) is returned and the boolean is true.
|
||||
// Otherwise the returned value will be empty and the boolean will
|
||||
// be false.
|
||||
func Lookup(key string) (string, bool) { return os.LookupEnv(key) }
|
||||
|
||||
// List all envs with a given prefix.
|
||||
func List(prefix string) (envs []string) {
|
||||
for _, env := range os.Environ() {
|
||||
|
||||
@@ -98,7 +98,7 @@ func (target *WebhookTarget) Save(eventData event.Event) error {
|
||||
if target.store != nil {
|
||||
return target.store.Put(eventData)
|
||||
}
|
||||
u, pErr := xnet.ParseURL(target.args.Endpoint.String())
|
||||
u, pErr := xnet.ParseHTTPURL(target.args.Endpoint.String())
|
||||
if pErr != nil {
|
||||
return pErr
|
||||
}
|
||||
@@ -153,7 +153,7 @@ func (target *WebhookTarget) send(eventData event.Event) error {
|
||||
|
||||
// Send - reads an event from store and sends it to webhook.
|
||||
func (target *WebhookTarget) Send(eventKey string) error {
|
||||
u, pErr := xnet.ParseURL(target.args.Endpoint.String())
|
||||
u, pErr := xnet.ParseHTTPURL(target.args.Endpoint.String())
|
||||
if pErr != nil {
|
||||
return pErr
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ package net
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
@@ -103,6 +104,21 @@ func (u URL) DialHTTP() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// ParseHTTPURL - parses a string into HTTP URL, string is
|
||||
// expected to be of form http:// or https://
|
||||
func ParseHTTPURL(s string) (u *URL, err error) {
|
||||
u, err = ParseURL(s)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
switch u.Scheme {
|
||||
default:
|
||||
return nil, fmt.Errorf("unexpected scheme found %s", u.Scheme)
|
||||
case "http", "https":
|
||||
return u, nil
|
||||
}
|
||||
}
|
||||
|
||||
// ParseURL - parses string into URL.
|
||||
func ParseURL(s string) (u *URL, err error) {
|
||||
var uu *url.URL
|
||||
|
||||
@@ -133,6 +133,42 @@ func TestURLUnmarshalJSON(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseHTTPURL(t *testing.T) {
|
||||
testCases := []struct {
|
||||
s string
|
||||
expectedURL *URL
|
||||
expectErr bool
|
||||
}{
|
||||
{"http://play", &URL{Scheme: "http", Host: "play"}, false},
|
||||
{"https://play.min.io:0", &URL{Scheme: "https", Host: "play.min.io:0"}, false},
|
||||
{"https://147.75.201.93:9000/", &URL{Scheme: "https", Host: "147.75.201.93:9000", Path: "/"}, false},
|
||||
{"https://s3.amazonaws.com/?location", &URL{Scheme: "https", Host: "s3.amazonaws.com", Path: "/", RawQuery: "location"}, false},
|
||||
{"http://myminio:10000/mybucket//myobject/", &URL{Scheme: "http", Host: "myminio:10000", Path: "/mybucket/myobject/"}, false},
|
||||
{"ftp://myftp.server:10000/myuser", nil, true},
|
||||
{"https://my.server:10000000/myuser", nil, true},
|
||||
{"myserver:1000", nil, true},
|
||||
{"http://:1000/mybucket", nil, true},
|
||||
{"https://147.75.201.93:90000/", nil, true},
|
||||
{"http:/play", nil, true},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
testCase := testCase
|
||||
t.Run(testCase.s, func(t *testing.T) {
|
||||
url, err := ParseHTTPURL(testCase.s)
|
||||
expectErr := (err != nil)
|
||||
if expectErr != testCase.expectErr {
|
||||
t.Fatalf("error: expected: %v, got: %v", testCase.expectErr, expectErr)
|
||||
}
|
||||
if !testCase.expectErr {
|
||||
if !reflect.DeepEqual(url, testCase.expectedURL) {
|
||||
t.Fatalf("host: expected: %#v, got: %#v", testCase.expectedURL, url)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseURL(t *testing.T) {
|
||||
testCases := []struct {
|
||||
s string
|
||||
|
||||
Reference in New Issue
Block a user