Skip to content

Commit 753079f

Browse files
committed
add support for logging
1 parent 845f071 commit 753079f

File tree

7 files changed

+201
-70
lines changed

7 files changed

+201
-70
lines changed

docs/docs/configuration/alpha_config.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ They may change between releases without notice.
173173
| `Cookie` | _[Cookie](#cookie)_ | Cookie is used to configure the cookie used to store the session state.<br/>This includes options such as the cookie name, its expiry and its domain. |
174174
| `Session` | _[SessionOptions](#sessionoptions)_ | Session is used to configure the session storage.<br/>To either use a cookie or a redis store. |
175175
| `PageTemplates` | _[PageTemplates](#pagetemplates)_ | PageTemplates is used to configure custom page templates.<br/>This includes the sign in and error pages. |
176+
| `Logging` | _[Logging](#logging)_ | Logging is used to configure the logging output.<br/>Which formats are enabled and where to write the logs. |
176177

177178
### AzureOptions
178179

@@ -317,6 +318,41 @@ make up the header value
317318
| `Groups` | _[]string_ | Group enables to restrict login to members of indicated group |
318319
| `Roles` | _[]string_ | Role enables to restrict login to users with role (only available when using the keycloak-oidc provider) |
319320

321+
### LogFileOptions
322+
323+
(**Appears on:** [Logging](#logging))
324+
325+
LogFileOptions contains options for configuring logging to a file
326+
327+
| Field | Type | Description |
328+
| ----- | ---- | ----------- |
329+
| `Filename` | _string_ | |
330+
| `MaxSize` | _int_ | |
331+
| `MaxAge` | _int_ | |
332+
| `MaxBackups` | _int_ | |
333+
| `Compress` | _bool_ | |
334+
335+
### Logging
336+
337+
(**Appears on:** [AlphaOptions](#alphaoptions))
338+
339+
Logging contains all options required for configuring the logging
340+
341+
| Field | Type | Description |
342+
| ----- | ---- | ----------- |
343+
| `AuthEnabled` | _bool_ | |
344+
| `AuthFormat` | _string_ | |
345+
| `RequestEnabled` | _bool_ | |
346+
| `RequestFormat` | _string_ | |
347+
| `StandardEnabled` | _bool_ | |
348+
| `StandardFormat` | _string_ | |
349+
| `ErrToInfo` | _bool_ | |
350+
| `ExcludePaths` | _[]string_ | |
351+
| `LocalTime` | _bool_ | |
352+
| `SilencePing` | _bool_ | |
353+
| `RequestIDHeader` | _string_ | |
354+
| `File` | _[LogFileOptions](#logfileoptions)_ | |
355+
320356
### LoginGovOptions
321357

322358
(**Appears on:** [Provider](#provider))

pkg/apis/options/alpha_options.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ type AlphaOptions struct {
6464
// PageTemplates is used to configure custom page templates.
6565
// This includes the sign in and error pages.
6666
PageTemplates PageTemplates `yaml:"pageTemplates,omitempty"`
67+
68+
// Logging is used to configure the logging output.
69+
// Which formats are enabled and where to write the logs.
70+
Logging Logging `yaml:"logging,omitempty"`
6771
}
6872

6973
// Initialize alpha options with default values and settings of the core options
@@ -90,6 +94,7 @@ func (a *AlphaOptions) ExtractFrom(opts *Options) {
9094
a.Cookie = opts.Cookie
9195
a.Session = opts.Session
9296
a.PageTemplates = opts.PageTemplates
97+
a.Logging = opts.Logging
9398
}
9499

95100
// MergeInto replaces alpha options in the Options struct with the values
@@ -106,4 +111,5 @@ func (a *AlphaOptions) MergeInto(opts *Options) {
106111
opts.Cookie = a.Cookie
107112
opts.Session = a.Session
108113
opts.PageTemplates = a.PageTemplates
114+
opts.Logging = a.Logging
109115
}

pkg/apis/options/legacy_logging.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package options
2+
3+
import (
4+
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/logger"
5+
"github.com/spf13/pflag"
6+
)
7+
8+
// Logging contains all options required for configuring the logging
9+
type LegacyLogging struct {
10+
AuthEnabled bool `flag:"auth-logging" cfg:"auth_logging"`
11+
AuthFormat string `flag:"auth-logging-format" cfg:"auth_logging_format"`
12+
RequestEnabled bool `flag:"request-logging" cfg:"request_logging"`
13+
RequestFormat string `flag:"request-logging-format" cfg:"request_logging_format"`
14+
StandardEnabled bool `flag:"standard-logging" cfg:"standard_logging"`
15+
StandardFormat string `flag:"standard-logging-format" cfg:"standard_logging_format"`
16+
ErrToInfo bool `flag:"errors-to-info-log" cfg:"errors_to_info_log"`
17+
ExcludePaths []string `flag:"exclude-logging-path" cfg:"exclude_logging_paths"`
18+
LocalTime bool `flag:"logging-local-time" cfg:"logging_local_time"`
19+
SilencePing bool `flag:"silence-ping-logging" cfg:"silence_ping_logging"`
20+
RequestIDHeader string `flag:"request-id-header" cfg:"request_id_header"`
21+
File LegacyLogFileOptions `cfg:",squash"`
22+
}
23+
24+
// LogFileOptions contains options for configuring logging to a file
25+
type LegacyLogFileOptions struct {
26+
Filename string `flag:"logging-filename" cfg:"logging_filename"`
27+
MaxSize int `flag:"logging-max-size" cfg:"logging_max_size"`
28+
MaxAge int `flag:"logging-max-age" cfg:"logging_max_age"`
29+
MaxBackups int `flag:"logging-max-backups" cfg:"logging_max_backups"`
30+
Compress bool `flag:"logging-compress" cfg:"logging_compress"`
31+
}
32+
33+
func legacyLoggingFlagSet() *pflag.FlagSet {
34+
flagSet := pflag.NewFlagSet("logging", pflag.ExitOnError)
35+
36+
flagSet.Bool("auth-logging", true, "Log authentication attempts")
37+
flagSet.String("auth-logging-format", logger.DefaultAuthLoggingFormat, "Template for authentication log lines")
38+
flagSet.Bool("standard-logging", true, "Log standard runtime information")
39+
flagSet.String("standard-logging-format", logger.DefaultStandardLoggingFormat, "Template for standard log lines")
40+
flagSet.Bool("request-logging", true, "Log HTTP requests")
41+
flagSet.String("request-logging-format", logger.DefaultRequestLoggingFormat, "Template for HTTP request log lines")
42+
flagSet.Bool("errors-to-info-log", false, "Log errors to the standard logging channel instead of stderr")
43+
44+
flagSet.StringSlice("exclude-logging-path", []string{}, "Exclude logging requests to paths (eg: '/path1,/path2,/path3')")
45+
flagSet.Bool("logging-local-time", true, "If the time in log files and backup filenames are local or UTC time")
46+
flagSet.Bool("silence-ping-logging", false, "Disable logging of requests to ping & ready endpoints")
47+
flagSet.String("request-id-header", "X-Request-Id", "Request header to use as the request ID")
48+
49+
flagSet.String("logging-filename", "", "File to log requests to, empty for stdout")
50+
flagSet.Int("logging-max-size", 100, "Maximum size in megabytes of the log file before rotation")
51+
flagSet.Int("logging-max-age", 7, "Maximum number of days to retain old log files")
52+
flagSet.Int("logging-max-backups", 0, "Maximum number of old log files to retain; 0 to disable")
53+
flagSet.Bool("logging-compress", false, "Should rotated log files be compressed using gzip")
54+
55+
return flagSet
56+
}
57+
58+
func (l *LegacyLogging) convert() Logging {
59+
return Logging{
60+
AuthEnabled: l.AuthEnabled,
61+
AuthFormat: l.AuthFormat,
62+
RequestEnabled: l.RequestEnabled,
63+
RequestFormat: l.RequestFormat,
64+
StandardEnabled: l.StandardEnabled,
65+
StandardFormat: l.StandardFormat,
66+
ErrToInfo: l.ErrToInfo,
67+
ExcludePaths: l.ExcludePaths,
68+
LocalTime: l.LocalTime,
69+
SilencePing: l.SilencePing,
70+
RequestIDHeader: l.RequestIDHeader,
71+
File: LogFileOptions{
72+
Filename: l.File.Filename,
73+
MaxSize: l.File.MaxSize,
74+
MaxAge: l.File.MaxAge,
75+
MaxBackups: l.File.MaxBackups,
76+
Compress: l.File.Compress,
77+
},
78+
}
79+
}

pkg/apis/options/legacy_options.go

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"time"
66

7+
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/logger"
78
"github.com/spf13/pflag"
89
)
910

@@ -35,7 +36,8 @@ type LegacyOptions struct {
3536
// Legacy options for configuring the cookie session storage
3637
LegacySessionOptions LegacySessionOptions `cfg:",squash"`
3738

38-
Options Options `cfg:",squash"`
39+
// Legacy options for logging
40+
LegacyLogging LegacyLogging `cfg:",squash"`
3941
}
4042

4143
func NewLegacyOptions() *LegacyOptions {
@@ -108,12 +110,31 @@ func NewLegacyOptions() *LegacyOptions {
108110
},
109111
},
110112

111-
Options: *NewOptions(),
113+
LegacyLogging: LegacyLogging{
114+
ExcludePaths: nil,
115+
LocalTime: true,
116+
SilencePing: false,
117+
RequestIDHeader: "X-Request-Id",
118+
AuthEnabled: true,
119+
AuthFormat: logger.DefaultAuthLoggingFormat,
120+
RequestEnabled: true,
121+
RequestFormat: logger.DefaultRequestLoggingFormat,
122+
StandardEnabled: true,
123+
StandardFormat: logger.DefaultStandardLoggingFormat,
124+
ErrToInfo: false,
125+
File: LegacyLogFileOptions{
126+
Filename: "",
127+
MaxSize: 100,
128+
MaxAge: 7,
129+
MaxBackups: 0,
130+
Compress: false,
131+
},
132+
},
112133
}
113134
}
114135

115136
func NewLegacyFlagSet() *pflag.FlagSet {
116-
flagSet := NewFlagSet()
137+
flagSet := pflag.NewFlagSet("oauth2-proxy", pflag.ExitOnError)
117138

118139
flagSet.AddFlagSet(legacyProxyOptionsFlagSet())
119140
flagSet.AddFlagSet(legacyUpstreamsFlagSet())
@@ -125,38 +146,36 @@ func NewLegacyFlagSet() *pflag.FlagSet {
125146
flagSet.AddFlagSet(legacyProbeOptionsFlagSet())
126147
flagSet.AddFlagSet(legacyPageTemplatesFlagSet())
127148
flagSet.AddFlagSet(legacySessionFlagSet())
149+
flagSet.AddFlagSet(legacyLoggingFlagSet())
128150

129151
return flagSet
130152
}
131153

132154
func (l *LegacyOptions) ToOptions() (*Options, error) {
133-
l.Options.ProxyOptions = l.LegacyProxyOptions.convert()
155+
opts := NewOptions()
156+
opts.ProxyOptions = l.LegacyProxyOptions.convert()
134157

135158
upstreams, err := l.LegacyUpstreams.convert()
136159
if err != nil {
137160
return nil, fmt.Errorf("error converting upstreams: %v", err)
138161
}
139-
l.Options.UpstreamServers = upstreams
140-
141-
l.Options.InjectRequestHeaders, l.Options.InjectResponseHeaders = l.LegacyHeaders.convert()
142-
143-
l.Options.Server, l.Options.MetricsServer = l.LegacyServer.convert()
144162

145-
l.Options.LegacyPreferEmailToUser = l.LegacyHeaders.PreferEmailToUser
163+
opts.UpstreamServers = upstreams
164+
opts.InjectRequestHeaders, opts.InjectResponseHeaders = l.LegacyHeaders.convert()
165+
opts.Server, opts.MetricsServer = l.LegacyServer.convert()
166+
opts.LegacyPreferEmailToUser = l.LegacyHeaders.PreferEmailToUser
146167

147168
providers, err := l.LegacyProvider.convert()
148169
if err != nil {
149170
return nil, fmt.Errorf("error converting provider: %v", err)
150171
}
151-
l.Options.Providers = providers
152-
153-
l.Options.Cookie = l.LegacyCookie.convert()
154-
155-
l.Options.ProbeOptions = l.LegacyProbeOptions.convert()
156-
157-
l.Options.PageTemplates = l.LegacyPageTemplates.convert()
158172

159-
l.Options.Session = l.LegacySessionOptions.convert()
173+
opts.Providers = providers
174+
opts.Cookie = l.LegacyCookie.convert()
175+
opts.ProbeOptions = l.LegacyProbeOptions.convert()
176+
opts.PageTemplates = l.LegacyPageTemplates.convert()
177+
opts.Session = l.LegacySessionOptions.convert()
178+
opts.Logging = l.LegacyLogging.convert()
160179

161-
return &l.Options, nil
180+
return opts, nil
162181
}

pkg/apis/options/load_test.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"time"
88

99
. "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/options/testutil"
10+
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/logger"
1011
. "github.com/onsi/ginkgo"
1112
. "github.com/onsi/ginkgo/extensions/table"
1213
. "github.com/onsi/gomega"
@@ -83,8 +84,25 @@ var _ = Describe("Load", func() {
8384
},
8485
},
8586

86-
Options: Options{
87-
Logging: loggingDefaults(),
87+
LegacyLogging: LegacyLogging{
88+
ExcludePaths: nil,
89+
LocalTime: true,
90+
SilencePing: false,
91+
RequestIDHeader: "X-Request-Id",
92+
AuthEnabled: true,
93+
AuthFormat: logger.DefaultAuthLoggingFormat,
94+
RequestEnabled: true,
95+
RequestFormat: logger.DefaultRequestLoggingFormat,
96+
StandardEnabled: true,
97+
StandardFormat: logger.DefaultStandardLoggingFormat,
98+
ErrToInfo: false,
99+
File: LegacyLogFileOptions{
100+
Filename: "",
101+
MaxSize: 100,
102+
MaxAge: 7,
103+
MaxBackups: 0,
104+
Compress: false,
105+
},
88106
},
89107
}
90108

pkg/apis/options/logging.go

Lines changed: 17 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,57 +2,31 @@ package options
22

33
import (
44
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/logger"
5-
"github.com/spf13/pflag"
65
)
76

87
// Logging contains all options required for configuring the logging
98
type Logging struct {
10-
AuthEnabled bool `flag:"auth-logging" cfg:"auth_logging"`
11-
AuthFormat string `flag:"auth-logging-format" cfg:"auth_logging_format"`
12-
RequestEnabled bool `flag:"request-logging" cfg:"request_logging"`
13-
RequestFormat string `flag:"request-logging-format" cfg:"request_logging_format"`
14-
StandardEnabled bool `flag:"standard-logging" cfg:"standard_logging"`
15-
StandardFormat string `flag:"standard-logging-format" cfg:"standard_logging_format"`
16-
ErrToInfo bool `flag:"errors-to-info-log" cfg:"errors_to_info_log"`
17-
ExcludePaths []string `flag:"exclude-logging-path" cfg:"exclude_logging_paths"`
18-
LocalTime bool `flag:"logging-local-time" cfg:"logging_local_time"`
19-
SilencePing bool `flag:"silence-ping-logging" cfg:"silence_ping_logging"`
20-
RequestIDHeader string `flag:"request-id-header" cfg:"request_id_header"`
21-
File LogFileOptions `cfg:",squash"`
9+
AuthEnabled bool `yaml:"authEnabled"`
10+
AuthFormat string `yaml:"authFormat,omitempty"`
11+
RequestEnabled bool `yaml:"requestEnabled"`
12+
RequestFormat string `yaml:"requestFormat,omitempty"`
13+
StandardEnabled bool `yaml:"standardEnabled"`
14+
StandardFormat string `yaml:"standardFormat,omitempty"`
15+
ErrToInfo bool `yaml:"errToInfo,omitempty"`
16+
ExcludePaths []string `yaml:"excludePaths,omitempty"`
17+
LocalTime bool `yaml:"localTime"`
18+
SilencePing bool `yaml:"silencePing,omitempty"`
19+
RequestIDHeader string `yaml:"requestIdHeader,omitempty"`
20+
File LogFileOptions `yaml:"fileOptions,omitempty"`
2221
}
2322

2423
// LogFileOptions contains options for configuring logging to a file
2524
type LogFileOptions struct {
26-
Filename string `flag:"logging-filename" cfg:"logging_filename"`
27-
MaxSize int `flag:"logging-max-size" cfg:"logging_max_size"`
28-
MaxAge int `flag:"logging-max-age" cfg:"logging_max_age"`
29-
MaxBackups int `flag:"logging-max-backups" cfg:"logging_max_backups"`
30-
Compress bool `flag:"logging-compress" cfg:"logging_compress"`
31-
}
32-
33-
func loggingFlagSet() *pflag.FlagSet {
34-
flagSet := pflag.NewFlagSet("logging", pflag.ExitOnError)
35-
36-
flagSet.Bool("auth-logging", true, "Log authentication attempts")
37-
flagSet.String("auth-logging-format", logger.DefaultAuthLoggingFormat, "Template for authentication log lines")
38-
flagSet.Bool("standard-logging", true, "Log standard runtime information")
39-
flagSet.String("standard-logging-format", logger.DefaultStandardLoggingFormat, "Template for standard log lines")
40-
flagSet.Bool("request-logging", true, "Log HTTP requests")
41-
flagSet.String("request-logging-format", logger.DefaultRequestLoggingFormat, "Template for HTTP request log lines")
42-
flagSet.Bool("errors-to-info-log", false, "Log errors to the standard logging channel instead of stderr")
43-
44-
flagSet.StringSlice("exclude-logging-path", []string{}, "Exclude logging requests to paths (eg: '/path1,/path2,/path3')")
45-
flagSet.Bool("logging-local-time", true, "If the time in log files and backup filenames are local or UTC time")
46-
flagSet.Bool("silence-ping-logging", false, "Disable logging of requests to ping & ready endpoints")
47-
flagSet.String("request-id-header", "X-Request-Id", "Request header to use as the request ID")
48-
49-
flagSet.String("logging-filename", "", "File to log requests to, empty for stdout")
50-
flagSet.Int("logging-max-size", 100, "Maximum size in megabytes of the log file before rotation")
51-
flagSet.Int("logging-max-age", 7, "Maximum number of days to retain old log files")
52-
flagSet.Int("logging-max-backups", 0, "Maximum number of old log files to retain; 0 to disable")
53-
flagSet.Bool("logging-compress", false, "Should rotated log files be compressed using gzip")
54-
55-
return flagSet
25+
Filename string `yaml:"filename,omitempty"`
26+
MaxSize int `yaml:"maxSize,omitempty"`
27+
MaxAge int `yaml:"maxAge,omitempty"`
28+
MaxBackups int `yaml:"maxBackups,omitempty"`
29+
Compress bool `yaml:"compress,omitempty"`
5630
}
5731

5832
// loggingDefaults creates a Logging structure, populating each field with its default value

pkg/apis/options/options.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ type Options struct {
3434
// To either use a cookie or a redis store.
3535
Session SessionOptions `cfg:",internal"`
3636

37-
Logging Logging `cfg:",squash"`
37+
// Logging is used to configure the logging output.
38+
// Which formats are enabled and where to write the logs.
39+
Logging Logging `cfg:",internal"`
3840

3941
// PageTemplates is used to configure custom page templates.
4042
// This includes the sign in and error pages.
@@ -93,10 +95,7 @@ func NewOptions() *Options {
9395
}
9496

9597
// NewFlagSet creates a new FlagSet with all of the flags required by Options
98+
// Deprecated: NewFlagSet is deprecated. Flags will be removed in v8.
9699
func NewFlagSet() *pflag.FlagSet {
97-
flagSet := pflag.NewFlagSet("oauth2-proxy", pflag.ExitOnError)
98-
99-
flagSet.AddFlagSet(loggingFlagSet())
100-
101-
return flagSet
100+
return pflag.NewFlagSet("oauth2-proxy", pflag.ExitOnError)
102101
}

0 commit comments

Comments
 (0)