This repository has been archived by the owner on Oct 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfactory.go
89 lines (79 loc) · 2.46 KB
/
factory.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package auditlog
import (
"fmt"
"sync"
"github.com/containerssh/auditlog/codec"
"github.com/containerssh/auditlog/codec/asciinema"
"github.com/containerssh/auditlog/codec/binary"
noneCodec "github.com/containerssh/auditlog/codec/none"
"github.com/containerssh/auditlog/storage"
"github.com/containerssh/auditlog/storage/file"
noneStorage "github.com/containerssh/auditlog/storage/none"
"github.com/containerssh/auditlog/storage/s3"
"github.com/containerssh/geoip/geoipprovider"
"github.com/containerssh/log"
)
// New Creates a new audit logging pipeline based on the provided configuration.
func New(config Config, geoIPLookupProvider geoipprovider.LookupProvider, logger log.Logger) (Logger, error) {
if !config.Enable {
return &empty{}, nil
}
encoder, err := NewEncoder(config.Format, logger, geoIPLookupProvider)
if err != nil {
return nil, err
}
st, err := NewStorage(config, logger)
if err != nil {
return nil, err
}
return NewLogger(
config.Intercept,
encoder,
st,
logger,
geoIPLookupProvider,
)
}
// NewLogger creates a new audit logging pipeline with the provided elements.
func NewLogger(
intercept InterceptConfig,
encoder codec.Encoder,
storage storage.WritableStorage,
logger log.Logger,
geoIPLookup geoipprovider.LookupProvider,
) (Logger, error) {
return &loggerImplementation{
intercept: intercept,
encoder: encoder,
storage: storage,
logger: logger,
wg: &sync.WaitGroup{},
geoIPLookup: geoIPLookup,
}, nil
}
// NewEncoder creates a new audit log encoder of the specified format.
func NewEncoder(encoder Format, logger log.Logger, geoIPLookupProvider geoipprovider.LookupProvider) (codec.Encoder, error) {
switch encoder {
case FormatNone:
return noneCodec.NewEncoder(), nil
case FormatAsciinema:
return asciinema.NewEncoder(logger, geoIPLookupProvider), nil
case FormatBinary:
return binary.NewEncoder(geoIPLookupProvider), nil
default:
return nil, fmt.Errorf("invalid audit log encoder: %s", encoder)
}
}
// NewStorage creates a new audit log storage of the specified type and with the specified configuration.
func NewStorage(config Config, logger log.Logger) (storage.WritableStorage, error) {
switch config.Storage {
case StorageNone:
return noneStorage.NewStorage(), nil
case StorageFile:
return file.NewStorage(config.File, logger)
case StorageS3:
return s3.NewStorage(config.S3, logger)
default:
return nil, fmt.Errorf("invalid audit log storage: %s", config.Storage)
}
}