Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Downloader: Add structured logging, fails storing and statistics #443

Merged
merged 23 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
7823364
started with forwarding support in downloader
s-l-teichmann Aug 22, 2023
3f0c0ad
Add missing files.
s-l-teichmann Aug 22, 2023
48e6c8a
Add missing files.
s-l-teichmann Aug 22, 2023
bf9b511
Raise needed Go version
s-l-teichmann Aug 22, 2023
3caef23
More Go version bumping.
s-l-teichmann Aug 22, 2023
12a740b
Fix forwarding
s-l-teichmann Aug 22, 2023
53cc97e
Go 1.21+ needed
s-l-teichmann Aug 23, 2023
5ab1272
Make terminating forwarder more robust.
s-l-teichmann Aug 23, 2023
d32d274
Better var naming
s-l-teichmann Aug 23, 2023
1558a11
Remove dead code. Improve commentary.
s-l-teichmann Aug 23, 2023
7cf91b2
Prepare validation status adjustment.
s-l-teichmann Aug 23, 2023
7212f16
Move validations to functions to make them executable in a loop.
s-l-teichmann Aug 23, 2023
e77ee2f
Introduce validation mode flag (strict, unsafe)
s-l-teichmann Aug 23, 2023
5e57e38
Add structured logging
s-l-teichmann Aug 24, 2023
241c4af
Merge branch 'main' into downloader-logging
s-l-teichmann Aug 25, 2023
8c3ad34
Place log into download folder
s-l-teichmann Aug 25, 2023
c701faf
Name default log file downloader.log
s-l-teichmann Aug 25, 2023
286cd1a
Improve code comment (bernhardreiter)
s-l-teichmann Aug 25, 2023
d4da714
Store invalid, unsafe advisories in special folder.
s-l-teichmann Aug 25, 2023
9e329bd
Store advisories that failed forwarding in a special folder.
s-l-teichmann Aug 25, 2023
c86656c
Add counting stats to downloader.
s-l-teichmann Aug 25, 2023
0148d18
Add statistics to forwarder
s-l-teichmann Aug 25, 2023
8ce9488
Count as success in no-store mode if validation does not fail.
s-l-teichmann Aug 25, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 115 additions & 4 deletions cmd/csaf_downloader/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ package main
import (
"crypto/tls"
"fmt"
"io"
"log/slog"
"net/http"
"os"
"path/filepath"

"github.com/csaf-poc/csaf_distribution/v2/internal/certs"
"github.com/csaf-poc/csaf_distribution/v2/internal/filter"
Expand All @@ -24,6 +28,8 @@ const (
defaultPreset = "mandatory"
defaultForwardQueue = 5
defaultValidationMode = validationStrict
defaultLogFile = "downloader.log"
defaultLogLevel = logLevelInfo
)

type validationMode string
Expand All @@ -33,8 +39,17 @@ const (
validationUnsafe = validationMode("unsafe")
)

type logLevel string

const (
logLevelDebug = logLevel("debug")
logLevelInfo = logLevel("info")
logLevelWarn = logLevel("warn")
logLevelError = logLevel("error")
)

type config struct {
Directory *string `short:"d" long:"directory" description:"DIRectory to store the downloaded files in" value-name:"DIR" toml:"directory"`
Directory string `short:"d" long:"directory" description:"DIRectory to store the downloaded files in" value-name:"DIR" toml:"directory"`
Insecure bool `long:"insecure" description:"Do not check TLS certificates from provider" toml:"insecure"`
IgnoreSignatureCheck bool `long:"ignoresigcheck" description:"Ignore signature check results, just warn on mismatch" toml:"ignoresigcheck"`
ClientCert *string `long:"client-cert" description:"TLS client certificate file (PEM encoded data)" value-name:"CERT-FILE" toml:"client_cert"`
Expand Down Expand Up @@ -62,6 +77,10 @@ type config struct {
ForwardQueue int `long:"forwardqueue" description:"Maximal queue LENGTH before forwarder" value-name:"LENGTH" toml:"forward_queue"`
ForwardInsecure bool `long:"forwardinsecure" description:"Do not check TLS certificates from forward endpoint" toml:"forward_insecure"`

LogFile string `long:"logfile" description:"FILE to log download to" value-name:"FILE" toml:"log_file"`
//lint:ignore SA5008 We are using choice or than once: debug, info, warn, error
LogLevel logLevel `long:"loglevel" description:"LEVEL of logging details" value-name:"LEVEL" choice:"debug" choice:"info" choice:"warn" choice:"error" toml:"log_level"`

Config string `short:"c" long:"config" description:"Path to config TOML file" value-name:"TOML-FILE" toml:"-"`

clientCerts []tls.Certificate
Expand All @@ -87,6 +106,8 @@ func parseArgsConfig() ([]string, *config, error) {
cfg.RemoteValidatorPresets = []string{defaultPreset}
cfg.ValidationMode = defaultValidationMode
cfg.ForwardQueue = defaultForwardQueue
cfg.LogFile = defaultLogFile
cfg.LogLevel = defaultLogLevel
},
// Re-establish default values if not set.
EnsureDefaults: func(cfg *config) {
Expand Down Expand Up @@ -117,11 +138,94 @@ func (vm *validationMode) UnmarshalText(text []byte) error {
return nil
}

// UnmarshalText implements [encoding/text.TextUnmarshaler].
func (ll *logLevel) UnmarshalText(text []byte) error {
switch l := logLevel(text); l {
case logLevelDebug, logLevelInfo, logLevelWarn, logLevelError:
*ll = l
default:
return fmt.Errorf(`invalid value %q (expected "debug", "info", "warn", "error")`, l)
}
return nil
}

// ignoreFile returns true if the given URL should not be downloaded.
func (cfg *config) ignoreURL(u string) bool {
return cfg.ignorePattern.Matches(u)
}

// slogLevel converts logLevel to [slog.Level].
func (ll logLevel) slogLevel() slog.Level {
switch ll {
case logLevelDebug:
return slog.LevelDebug
case logLevelInfo:
return slog.LevelInfo
case logLevelWarn:
return slog.LevelWarn
case logLevelError:
return slog.LevelError
default:
return slog.LevelInfo
}
}

// prepareDirectory ensures that the working directory
// exists and is setup properly.
func (cfg *config) prepareDirectory() error {
// If no special given use current working directory.
if cfg.Directory == "" {
dir, err := os.Getwd()
if err != nil {
return err
}
cfg.Directory = dir
return nil
}
// Use given directory
if _, err := os.Stat(cfg.Directory); err != nil {
// If it does not exist create it.
if os.IsNotExist(err) {
if err = os.MkdirAll(cfg.Directory, 0755); err != nil {
return err
}
} else {
return err
}
}
return nil
}

// prepareLogging sets up the structured logging.
func (cfg *config) prepareLogging() error {
var w io.Writer
if cfg.LogFile == "" {
w = os.Stderr
} else {
var fname string
// We put the log inside the download folder
// if it is not absolute.
if filepath.IsAbs(cfg.LogFile) {
fname = cfg.LogFile
} else {
fname = filepath.Join(cfg.Directory, cfg.LogFile)
}
f, err := os.OpenFile(fname, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
if err != nil {
return err
}
w = f
}
ho := slog.HandlerOptions{
//AddSource: true,
Level: cfg.LogLevel.slogLevel(),
}
handler := slog.NewJSONHandler(w, &ho)
logger := slog.New(handler)
slog.SetDefault(logger)
return nil
}

// compileIgnorePatterns compiles the configure patterns to be ignored.
func (cfg *config) compileIgnorePatterns() error {
pm, err := filter.NewPatternMatcher(cfg.IgnorePattern)
Expand All @@ -145,8 +249,15 @@ func (cfg *config) prepareCertificates() error {

// prepare prepares internal state of a loaded configuration.
func (cfg *config) prepare() error {
if err := cfg.prepareCertificates(); err != nil {
return err
for _, prepare := range []func(*config) error{
(*config).prepareDirectory,
(*config).prepareLogging,
(*config).prepareCertificates,
(*config).compileIgnorePatterns,
} {
if err := prepare(cfg); err != nil {
return err
}
}
return cfg.compileIgnorePatterns()
return nil
}
Loading
Loading