2019-10-08 01:47:56 -04:00
|
|
|
/*
|
|
|
|
* MinIO Cloud Storage, (C) 2019 MinIO, 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 logger
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
2019-10-23 01:59:13 -04:00
|
|
|
"github.com/minio/minio/cmd/config"
|
2019-10-08 01:47:56 -04:00
|
|
|
"github.com/minio/minio/pkg/env"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Console logger target
|
|
|
|
type Console struct {
|
|
|
|
Enabled bool `json:"enabled"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// HTTP logger target
|
|
|
|
type HTTP struct {
|
2019-10-23 01:59:13 -04:00
|
|
|
Enabled bool `json:"enabled"`
|
|
|
|
Endpoint string `json:"endpoint"`
|
|
|
|
AuthToken string `json:"authToken"`
|
2019-10-08 01:47:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Config console and http logger targets
|
|
|
|
type Config struct {
|
|
|
|
Console Console `json:"console"`
|
|
|
|
HTTP map[string]HTTP `json:"http"`
|
|
|
|
Audit map[string]HTTP `json:"audit"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// HTTP endpoint logger
|
|
|
|
const (
|
2019-10-23 01:59:13 -04:00
|
|
|
Endpoint = "endpoint"
|
|
|
|
AuthToken = "auth_token"
|
|
|
|
|
|
|
|
EnvLoggerHTTPEndpoint = "MINIO_LOGGER_HTTP_ENDPOINT"
|
|
|
|
EnvLoggerHTTPAuthToken = "MINIO_LOGGER_HTTP_AUTH_TOKEN"
|
|
|
|
|
|
|
|
EnvLoggerHTTPAuditEndpoint = "MINIO_LOGGER_HTTP_AUDIT_ENDPOINT"
|
|
|
|
EnvLoggerHTTPAuditAuthToken = "MINIO_LOGGER_HTTP_AUDIT_AUTH_TOKEN"
|
2019-10-08 01:47:56 -04:00
|
|
|
)
|
|
|
|
|
2019-10-23 01:59:13 -04:00
|
|
|
// Default KVS for loggerHTTP and loggerAuditHTTP
|
|
|
|
var (
|
|
|
|
DefaultKVS = config.KVS{
|
|
|
|
config.State: config.StateOff,
|
|
|
|
config.Comment: "This is a default HTTP logging configuration",
|
|
|
|
Endpoint: "",
|
|
|
|
AuthToken: "",
|
|
|
|
}
|
|
|
|
DefaultAuditKVS = config.KVS{
|
|
|
|
config.State: config.StateOff,
|
|
|
|
config.Comment: "This is a default HTTP Audit logging configuration",
|
|
|
|
Endpoint: "",
|
|
|
|
AuthToken: "",
|
|
|
|
}
|
2019-10-08 01:47:56 -04:00
|
|
|
)
|
|
|
|
|
2019-10-09 02:11:15 -04:00
|
|
|
// NewConfig - initialize new logger config.
|
|
|
|
func NewConfig() Config {
|
|
|
|
cfg := Config{
|
|
|
|
// Console logging is on by default
|
|
|
|
Console: Console{
|
|
|
|
Enabled: true,
|
|
|
|
},
|
|
|
|
HTTP: make(map[string]HTTP),
|
|
|
|
Audit: make(map[string]HTTP),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create an example HTTP logger
|
2019-10-23 01:59:13 -04:00
|
|
|
cfg.HTTP[config.Default] = HTTP{
|
2019-10-09 02:11:15 -04:00
|
|
|
Endpoint: "https://username:password@example.com/api",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create an example Audit logger
|
2019-10-23 01:59:13 -04:00
|
|
|
cfg.Audit[config.Default] = HTTP{
|
2019-10-09 02:11:15 -04:00
|
|
|
Endpoint: "https://username:password@example.com/api/audit",
|
|
|
|
}
|
|
|
|
|
|
|
|
return cfg
|
|
|
|
}
|
|
|
|
|
2019-10-08 01:47:56 -04:00
|
|
|
// LookupConfig - lookup logger config, override with ENVs if set.
|
2019-10-23 01:59:13 -04:00
|
|
|
func LookupConfig(scfg config.Config) (Config, error) {
|
|
|
|
cfg := NewConfig()
|
|
|
|
|
2019-10-08 01:47:56 -04:00
|
|
|
envs := env.List(EnvLoggerHTTPEndpoint)
|
2019-10-23 01:59:13 -04:00
|
|
|
var loggerTargets []string
|
2019-10-11 21:21:04 -04:00
|
|
|
for _, k := range envs {
|
2019-10-23 01:59:13 -04:00
|
|
|
target := strings.TrimPrefix(k, EnvLoggerHTTPEndpoint+config.Default)
|
2019-10-11 21:21:04 -04:00
|
|
|
if target == EnvLoggerHTTPEndpoint {
|
2019-10-23 01:59:13 -04:00
|
|
|
target = config.Default
|
2019-10-08 01:47:56 -04:00
|
|
|
}
|
2019-10-23 01:59:13 -04:00
|
|
|
loggerTargets = append(loggerTargets, target)
|
|
|
|
}
|
|
|
|
|
|
|
|
var loggerAuditTargets []string
|
|
|
|
envs = env.List(EnvLoggerHTTPAuditEndpoint)
|
|
|
|
for _, k := range envs {
|
|
|
|
target := strings.TrimPrefix(k, EnvLoggerHTTPAuditEndpoint+config.Default)
|
|
|
|
if target == EnvLoggerHTTPAuditEndpoint {
|
|
|
|
target = config.Default
|
2019-10-08 01:47:56 -04:00
|
|
|
}
|
2019-10-23 01:59:13 -04:00
|
|
|
loggerAuditTargets = append(loggerAuditTargets, target)
|
2019-10-08 01:47:56 -04:00
|
|
|
}
|
2019-10-23 01:59:13 -04:00
|
|
|
|
|
|
|
// List legacy ENVs if any.
|
|
|
|
envs = env.List(EnvAuditLoggerHTTPEndpoint)
|
|
|
|
for _, k := range envs {
|
|
|
|
target := strings.TrimPrefix(k, EnvAuditLoggerHTTPEndpoint+config.Default)
|
2019-10-11 21:21:04 -04:00
|
|
|
if target == EnvAuditLoggerHTTPEndpoint {
|
2019-10-23 01:59:13 -04:00
|
|
|
target = config.Default
|
|
|
|
}
|
|
|
|
loggerAuditTargets = append(loggerAuditTargets, target)
|
|
|
|
}
|
|
|
|
|
|
|
|
for starget, kv := range scfg[config.LoggerHTTPSubSys] {
|
|
|
|
subSysTarget := config.LoggerHTTPSubSys
|
|
|
|
if starget != config.Default {
|
|
|
|
subSysTarget = config.LoggerHTTPSubSys + ":" + starget
|
|
|
|
}
|
|
|
|
if err := config.CheckValidKeys(subSysTarget, kv, DefaultKVS); err != nil {
|
|
|
|
return cfg, err
|
|
|
|
}
|
|
|
|
|
|
|
|
enabled, err := config.ParseBool(kv.Get(config.State))
|
|
|
|
if err != nil {
|
|
|
|
return cfg, err
|
2019-10-08 01:47:56 -04:00
|
|
|
}
|
2019-10-23 01:59:13 -04:00
|
|
|
if !enabled {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
endpointEnv := EnvLoggerHTTPEndpoint
|
|
|
|
if starget != config.Default {
|
|
|
|
endpointEnv = EnvLoggerHTTPEndpoint + config.Default + starget
|
|
|
|
}
|
|
|
|
authTokenEnv := EnvLoggerHTTPAuthToken
|
|
|
|
if starget != config.Default {
|
|
|
|
authTokenEnv = EnvLoggerHTTPAuthToken + config.Default + starget
|
|
|
|
}
|
|
|
|
cfg.HTTP[starget] = HTTP{
|
|
|
|
Enabled: true,
|
|
|
|
Endpoint: env.Get(endpointEnv, kv.Get(Endpoint)),
|
|
|
|
AuthToken: env.Get(authTokenEnv, kv.Get(AuthToken)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for starget, kv := range scfg[config.LoggerHTTPAuditSubSys] {
|
|
|
|
subSysTarget := config.LoggerHTTPAuditSubSys
|
|
|
|
if starget != config.Default {
|
|
|
|
subSysTarget = config.LoggerHTTPAuditSubSys + config.SubSystemSeparator + starget
|
|
|
|
}
|
|
|
|
if err := config.CheckValidKeys(subSysTarget, kv, DefaultAuditKVS); err != nil {
|
|
|
|
return cfg, err
|
|
|
|
}
|
|
|
|
|
|
|
|
enabled, err := config.ParseBool(kv.Get(config.State))
|
|
|
|
if err != nil {
|
|
|
|
return cfg, err
|
|
|
|
}
|
|
|
|
if !enabled {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
endpointEnv := EnvLoggerHTTPAuditEndpoint
|
|
|
|
if starget != config.Default {
|
|
|
|
endpointEnv = EnvLoggerHTTPAuditEndpoint + config.Default + starget
|
|
|
|
}
|
|
|
|
legacyEndpointEnv := EnvAuditLoggerHTTPEndpoint
|
|
|
|
if starget != config.Default {
|
|
|
|
legacyEndpointEnv = EnvAuditLoggerHTTPEndpoint + config.Default + starget
|
|
|
|
}
|
|
|
|
endpoint := env.Get(legacyEndpointEnv, "")
|
|
|
|
if endpoint == "" {
|
|
|
|
endpoint = env.Get(endpointEnv, kv.Get(Endpoint))
|
|
|
|
}
|
|
|
|
authTokenEnv := EnvLoggerHTTPAuditAuthToken
|
|
|
|
if starget != config.Default {
|
|
|
|
authTokenEnv = EnvLoggerHTTPAuditAuthToken + config.Default + starget
|
|
|
|
}
|
|
|
|
cfg.HTTP[starget] = HTTP{
|
|
|
|
Enabled: true,
|
|
|
|
Endpoint: endpoint,
|
|
|
|
AuthToken: env.Get(authTokenEnv, kv.Get(AuthToken)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, target := range loggerTargets {
|
|
|
|
endpointEnv := EnvLoggerHTTPEndpoint
|
|
|
|
if target != config.Default {
|
|
|
|
endpointEnv = EnvLoggerHTTPEndpoint + config.Default + target
|
|
|
|
}
|
|
|
|
authTokenEnv := EnvLoggerHTTPAuthToken
|
|
|
|
if target != config.Default {
|
|
|
|
authTokenEnv = EnvLoggerHTTPAuthToken + config.Default + target
|
|
|
|
}
|
|
|
|
cfg.HTTP[target] = HTTP{
|
|
|
|
Enabled: true,
|
|
|
|
Endpoint: env.Get(endpointEnv, ""),
|
|
|
|
AuthToken: env.Get(authTokenEnv, ""),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, target := range loggerAuditTargets {
|
|
|
|
endpointEnv := EnvLoggerHTTPAuditEndpoint
|
|
|
|
if target != config.Default {
|
|
|
|
endpointEnv = EnvLoggerHTTPAuditEndpoint + config.Default + target
|
|
|
|
}
|
|
|
|
legacyEndpointEnv := EnvAuditLoggerHTTPEndpoint
|
|
|
|
if target != config.Default {
|
|
|
|
legacyEndpointEnv = EnvAuditLoggerHTTPEndpoint + config.Default + target
|
|
|
|
}
|
|
|
|
endpoint := env.Get(legacyEndpointEnv, "")
|
|
|
|
if endpoint == "" {
|
|
|
|
endpoint = env.Get(endpointEnv, "")
|
|
|
|
}
|
|
|
|
authTokenEnv := EnvLoggerHTTPAuditAuthToken
|
|
|
|
if target != config.Default {
|
|
|
|
authTokenEnv = EnvLoggerHTTPAuditAuthToken + config.Default + target
|
|
|
|
}
|
|
|
|
cfg.HTTP[target] = HTTP{
|
|
|
|
Enabled: true,
|
|
|
|
Endpoint: endpoint,
|
|
|
|
AuthToken: env.Get(authTokenEnv, ""),
|
2019-10-08 01:47:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cfg, nil
|
|
|
|
}
|