2022-02-24 12:05:33 -05:00
|
|
|
// Copyright (c) 2015-2022 MinIO, Inc.
|
2021-04-18 15:41:13 -04:00
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2018-07-19 18:55:06 -04:00
|
|
|
|
|
|
|
package logger
|
|
|
|
|
2021-10-28 10:35:28 -04:00
|
|
|
import (
|
2023-05-09 00:20:31 -04:00
|
|
|
"context"
|
2022-11-10 13:20:21 -05:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2021-10-28 10:35:28 -04:00
|
|
|
"sync"
|
2022-02-17 14:11:15 -05:00
|
|
|
|
2023-06-19 20:53:08 -04:00
|
|
|
"github.com/minio/madmin-go/v3"
|
2022-02-17 14:11:15 -05:00
|
|
|
"github.com/minio/minio/internal/logger/target/http"
|
2022-02-24 12:05:33 -05:00
|
|
|
"github.com/minio/minio/internal/logger/target/kafka"
|
|
|
|
"github.com/minio/minio/internal/logger/target/types"
|
2021-10-28 10:35:28 -04:00
|
|
|
)
|
|
|
|
|
2018-11-19 17:47:03 -05:00
|
|
|
// Target is the entity that we will receive
|
|
|
|
// a single log entry and Send it to the log target
|
2022-08-26 15:52:29 -04:00
|
|
|
//
|
|
|
|
// e.g. Send the log to a http server
|
2018-11-19 17:47:03 -05:00
|
|
|
type Target interface {
|
2020-10-02 19:19:44 -04:00
|
|
|
String() string
|
|
|
|
Endpoint() string
|
2022-11-10 13:20:21 -05:00
|
|
|
Stats() types.TargetStats
|
2023-05-09 00:20:31 -04:00
|
|
|
Init(ctx context.Context) error
|
|
|
|
IsOnline(ctx context.Context) bool
|
2022-02-17 14:11:15 -05:00
|
|
|
Cancel()
|
2023-05-09 00:20:31 -04:00
|
|
|
Send(ctx context.Context, entry interface{}) error
|
2022-02-24 12:05:33 -05:00
|
|
|
Type() types.TargetType
|
2018-07-19 18:55:06 -04:00
|
|
|
}
|
|
|
|
|
2022-01-06 16:08:21 -05:00
|
|
|
var (
|
2022-05-12 10:20:58 -04:00
|
|
|
swapAuditMuRW sync.RWMutex
|
|
|
|
swapSystemMuRW sync.RWMutex
|
2018-07-19 18:55:06 -04:00
|
|
|
|
2022-02-24 12:05:33 -05:00
|
|
|
// systemTargets is the set of enabled loggers.
|
2022-01-06 16:08:21 -05:00
|
|
|
// Must be immutable at all times.
|
|
|
|
// Can be swapped to another while holding swapMu
|
2022-02-24 12:05:33 -05:00
|
|
|
systemTargets = []Target{}
|
2022-03-15 20:45:51 -04:00
|
|
|
|
|
|
|
// This is always set represent /dev/console target
|
|
|
|
consoleTgt Target
|
2022-01-06 16:08:21 -05:00
|
|
|
)
|
2021-10-28 10:35:28 -04:00
|
|
|
|
2022-11-28 11:03:26 -05:00
|
|
|
// TargetStatus returns status of the target (online|offline)
|
2023-05-09 00:20:31 -04:00
|
|
|
func TargetStatus(ctx context.Context, h Target) madmin.Status {
|
|
|
|
if h.IsOnline(ctx) {
|
2022-11-28 11:03:26 -05:00
|
|
|
return madmin.Status{Status: string(madmin.ItemOnline)}
|
|
|
|
}
|
|
|
|
// Previous initialization had failed. Try again.
|
2023-05-09 00:20:31 -04:00
|
|
|
if e := h.Init(ctx); e == nil {
|
2022-11-28 11:03:26 -05:00
|
|
|
return madmin.Status{Status: string(madmin.ItemOnline)}
|
|
|
|
}
|
|
|
|
return madmin.Status{Status: string(madmin.ItemOffline)}
|
|
|
|
}
|
|
|
|
|
2022-02-24 12:05:33 -05:00
|
|
|
// SystemTargets returns active targets.
|
2021-10-28 10:35:28 -04:00
|
|
|
// Returned slice may not be modified in any way.
|
2022-02-24 12:05:33 -05:00
|
|
|
func SystemTargets() []Target {
|
2022-05-12 10:20:58 -04:00
|
|
|
swapSystemMuRW.RLock()
|
|
|
|
defer swapSystemMuRW.RUnlock()
|
|
|
|
|
2022-02-24 12:05:33 -05:00
|
|
|
res := systemTargets
|
2021-10-28 10:35:28 -04:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// AuditTargets returns active audit targets.
|
|
|
|
// Returned slice may not be modified in any way.
|
|
|
|
func AuditTargets() []Target {
|
2022-05-12 10:20:58 -04:00
|
|
|
swapAuditMuRW.RLock()
|
|
|
|
defer swapAuditMuRW.RUnlock()
|
|
|
|
|
2021-10-28 10:35:28 -04:00
|
|
|
res := auditTargets
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2022-11-10 13:20:21 -05:00
|
|
|
// CurrentStats returns the current statistics.
|
|
|
|
func CurrentStats() map[string]types.TargetStats {
|
|
|
|
sys := SystemTargets()
|
|
|
|
audit := AuditTargets()
|
|
|
|
res := make(map[string]types.TargetStats, len(sys)+len(audit))
|
|
|
|
cnt := make(map[string]int, len(sys)+len(audit))
|
|
|
|
|
|
|
|
// Add system and audit.
|
|
|
|
for _, t := range sys {
|
|
|
|
key := strings.ToLower(t.Type().String())
|
|
|
|
n := cnt[key]
|
|
|
|
cnt[key]++
|
|
|
|
key = fmt.Sprintf("sys_%s_%d", key, n)
|
|
|
|
res[key] = t.Stats()
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, t := range audit {
|
|
|
|
key := strings.ToLower(t.Type().String())
|
|
|
|
n := cnt[key]
|
|
|
|
cnt[key]++
|
|
|
|
key = fmt.Sprintf("audit_%s_%d", key, n)
|
|
|
|
res[key] = t.Stats()
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2021-10-28 10:35:28 -04:00
|
|
|
// auditTargets is the list of enabled audit loggers
|
|
|
|
// Must be immutable at all times.
|
|
|
|
// Can be swapped to another while holding swapMu
|
2022-01-06 16:08:21 -05:00
|
|
|
var (
|
2022-05-12 10:20:58 -04:00
|
|
|
auditTargets = []Target{}
|
2022-01-06 16:08:21 -05:00
|
|
|
)
|
2020-08-16 13:25:00 -04:00
|
|
|
|
2022-02-24 12:05:33 -05:00
|
|
|
// AddSystemTarget adds a new logger target to the
|
2020-08-16 13:25:00 -04:00
|
|
|
// list of enabled loggers
|
2023-05-09 00:20:31 -04:00
|
|
|
func AddSystemTarget(ctx context.Context, t Target) error {
|
|
|
|
if err := t.Init(ctx); err != nil {
|
2020-08-16 13:25:00 -04:00
|
|
|
return err
|
|
|
|
}
|
2022-05-12 10:20:58 -04:00
|
|
|
|
|
|
|
swapSystemMuRW.Lock()
|
|
|
|
defer swapSystemMuRW.Unlock()
|
|
|
|
|
2022-03-15 20:45:51 -04:00
|
|
|
if consoleTgt == nil {
|
|
|
|
if t.Type() == types.TargetConsole {
|
|
|
|
consoleTgt = t
|
|
|
|
}
|
|
|
|
}
|
2022-02-24 12:05:33 -05:00
|
|
|
updated := append(make([]Target, 0, len(systemTargets)+1), systemTargets...)
|
2021-10-28 10:35:28 -04:00
|
|
|
updated = append(updated, t)
|
2022-02-24 12:05:33 -05:00
|
|
|
systemTargets = updated
|
2021-10-28 10:35:28 -04:00
|
|
|
|
2020-08-16 13:25:00 -04:00
|
|
|
return nil
|
2018-07-19 18:55:06 -04:00
|
|
|
}
|
2022-02-17 14:11:15 -05:00
|
|
|
|
2023-05-09 00:20:31 -04:00
|
|
|
func initSystemTargets(ctx context.Context, cfgMap map[string]http.Config) ([]Target, []error) {
|
2022-11-28 11:03:26 -05:00
|
|
|
tgts := []Target{}
|
|
|
|
errs := []error{}
|
2022-02-24 12:05:33 -05:00
|
|
|
for _, l := range cfgMap {
|
2022-02-17 14:11:15 -05:00
|
|
|
if l.Enabled {
|
|
|
|
t := http.New(l)
|
|
|
|
tgts = append(tgts, t)
|
2022-11-28 11:03:26 -05:00
|
|
|
|
2023-05-09 00:20:31 -04:00
|
|
|
e := t.Init(ctx)
|
2022-11-28 11:03:26 -05:00
|
|
|
if e != nil {
|
|
|
|
errs = append(errs, e)
|
|
|
|
}
|
2022-02-17 14:11:15 -05:00
|
|
|
}
|
|
|
|
}
|
2022-11-28 11:03:26 -05:00
|
|
|
return tgts, errs
|
2022-02-17 14:11:15 -05:00
|
|
|
}
|
|
|
|
|
2023-05-09 00:20:31 -04:00
|
|
|
func initKafkaTargets(ctx context.Context, cfgMap map[string]kafka.Config) ([]Target, []error) {
|
2022-11-28 11:03:26 -05:00
|
|
|
tgts := []Target{}
|
|
|
|
errs := []error{}
|
2022-02-24 12:05:33 -05:00
|
|
|
for _, l := range cfgMap {
|
|
|
|
if l.Enabled {
|
|
|
|
t := kafka.New(l)
|
|
|
|
tgts = append(tgts, t)
|
2022-11-28 11:03:26 -05:00
|
|
|
|
2023-05-09 00:20:31 -04:00
|
|
|
e := t.Init(ctx)
|
2022-11-28 11:03:26 -05:00
|
|
|
if e != nil {
|
|
|
|
errs = append(errs, e)
|
|
|
|
}
|
2022-02-24 12:05:33 -05:00
|
|
|
}
|
|
|
|
}
|
2022-11-28 11:03:26 -05:00
|
|
|
return tgts, errs
|
2022-02-24 12:05:33 -05:00
|
|
|
}
|
|
|
|
|
2022-05-16 16:32:36 -04:00
|
|
|
// Split targets into two groups:
|
2022-08-26 15:52:29 -04:00
|
|
|
//
|
|
|
|
// group1 contains all targets of type t
|
|
|
|
// group2 contains the remaining targets
|
2022-05-16 16:32:36 -04:00
|
|
|
func splitTargets(targets []Target, t types.TargetType) (group1 []Target, group2 []Target) {
|
|
|
|
for _, target := range targets {
|
|
|
|
if target.Type() == t {
|
|
|
|
group1 = append(group1, target)
|
|
|
|
} else {
|
|
|
|
group2 = append(group2, target)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func cancelTargets(targets []Target) {
|
|
|
|
for _, target := range targets {
|
|
|
|
target.Cancel()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-24 12:05:33 -05:00
|
|
|
// UpdateSystemTargets swaps targets with newly loaded ones from the cfg
|
2023-05-09 00:20:31 -04:00
|
|
|
func UpdateSystemTargets(ctx context.Context, cfg Config) []error {
|
|
|
|
newTgts, errs := initSystemTargets(ctx, cfg.HTTP)
|
2022-02-17 14:11:15 -05:00
|
|
|
|
2022-05-12 10:20:58 -04:00
|
|
|
swapSystemMuRW.Lock()
|
2022-05-16 16:32:36 -04:00
|
|
|
consoleTargets, otherTargets := splitTargets(systemTargets, types.TargetConsole)
|
|
|
|
newTgts = append(newTgts, consoleTargets...)
|
2022-05-12 10:20:58 -04:00
|
|
|
systemTargets = newTgts
|
2022-05-16 16:32:36 -04:00
|
|
|
swapSystemMuRW.Unlock()
|
2022-05-12 10:20:58 -04:00
|
|
|
|
2022-05-16 16:32:36 -04:00
|
|
|
cancelTargets(otherTargets) // cancel running targets
|
2022-11-28 11:03:26 -05:00
|
|
|
return errs
|
2022-02-24 12:05:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateAuditWebhookTargets swaps audit webhook targets with newly loaded ones from the cfg
|
2023-05-09 00:20:31 -04:00
|
|
|
func UpdateAuditWebhookTargets(ctx context.Context, cfg Config) []error {
|
|
|
|
newWebhookTgts, errs := initSystemTargets(ctx, cfg.AuditWebhook)
|
2022-05-12 10:20:58 -04:00
|
|
|
|
|
|
|
swapAuditMuRW.Lock()
|
2022-05-16 16:32:36 -04:00
|
|
|
// Retain kafka targets
|
|
|
|
oldWebhookTgts, otherTgts := splitTargets(auditTargets, types.TargetHTTP)
|
|
|
|
newWebhookTgts = append(newWebhookTgts, otherTgts...)
|
|
|
|
auditTargets = newWebhookTgts
|
|
|
|
swapAuditMuRW.Unlock()
|
2022-05-12 10:20:58 -04:00
|
|
|
|
2022-05-16 16:32:36 -04:00
|
|
|
cancelTargets(oldWebhookTgts) // cancel running targets
|
2022-11-28 11:03:26 -05:00
|
|
|
return errs
|
2022-02-24 12:05:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateAuditKafkaTargets swaps audit kafka targets with newly loaded ones from the cfg
|
2023-05-09 00:20:31 -04:00
|
|
|
func UpdateAuditKafkaTargets(ctx context.Context, cfg Config) []error {
|
|
|
|
newKafkaTgts, errs := initKafkaTargets(ctx, cfg.AuditKafka)
|
2022-05-12 10:20:58 -04:00
|
|
|
|
|
|
|
swapAuditMuRW.Lock()
|
2022-05-16 16:32:36 -04:00
|
|
|
// Retain webhook targets
|
|
|
|
oldKafkaTgts, otherTgts := splitTargets(auditTargets, types.TargetKafka)
|
|
|
|
newKafkaTgts = append(newKafkaTgts, otherTgts...)
|
|
|
|
auditTargets = newKafkaTgts
|
|
|
|
swapAuditMuRW.Unlock()
|
2022-05-12 10:20:58 -04:00
|
|
|
|
2022-05-16 16:32:36 -04:00
|
|
|
cancelTargets(oldKafkaTgts) // cancel running targets
|
2022-11-28 11:03:26 -05:00
|
|
|
return errs
|
2022-02-17 14:11:15 -05:00
|
|
|
}
|