mirror of
https://github.com/oauth2-proxy/oauth2-proxy.git
synced 2026-10-09 07:55:35 +02:00
Introduce Duration so that marshalling works for duration strings
This commit is contained in:
@@ -1,5 +1,11 @@
|
||||
package options
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// SecretSource references an individual secret value.
|
||||
// Only one source within the struct should be defined at any time.
|
||||
type SecretSource struct {
|
||||
@@ -12,3 +18,29 @@ type SecretSource struct {
|
||||
// FromFile expects a path to a file containing the secret value.
|
||||
FromFile string
|
||||
}
|
||||
|
||||
type Duration time.Duration
|
||||
|
||||
func (d *Duration) UnmarshalJSON(data []byte) error {
|
||||
input := string(data)
|
||||
input = strings.TrimPrefix(input, "\"")
|
||||
input = strings.TrimSuffix(input, "\"")
|
||||
du, err := time.ParseDuration(input)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*d = Duration(du)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Duration) MarshalJSON() ([]byte, error) {
|
||||
dStr := fmt.Sprintf("%q", d.Duration().String())
|
||||
return []byte(dStr), nil
|
||||
}
|
||||
|
||||
func (d *Duration) Duration() time.Duration {
|
||||
if d == nil {
|
||||
return time.Duration(0)
|
||||
}
|
||||
return time.Duration(*d)
|
||||
}
|
||||
|
||||
@@ -84,6 +84,7 @@ func (l *LegacyUpstreams) convert() (Upstreams, error) {
|
||||
u.Path = "/"
|
||||
}
|
||||
|
||||
flushInterval := Duration(l.FlushInterval)
|
||||
upstream := Upstream{
|
||||
ID: u.Path,
|
||||
Path: u.Path,
|
||||
@@ -91,7 +92,7 @@ func (l *LegacyUpstreams) convert() (Upstreams, error) {
|
||||
InsecureSkipTLSVerify: l.SSLUpstreamInsecureSkipVerify,
|
||||
PassHostHeader: &l.PassHostHeader,
|
||||
ProxyWebSockets: &l.ProxyWebSockets,
|
||||
FlushInterval: &l.FlushInterval,
|
||||
FlushInterval: &flushInterval,
|
||||
}
|
||||
|
||||
switch u.Scheme {
|
||||
|
||||
@@ -17,8 +17,8 @@ var _ = Describe("Legacy Options", func() {
|
||||
legacyOpts := NewLegacyOptions()
|
||||
|
||||
// Set upstreams and related options to test their conversion
|
||||
flushInterval := 5 * time.Second
|
||||
legacyOpts.LegacyUpstreams.FlushInterval = flushInterval
|
||||
flushInterval := Duration(5 * time.Second)
|
||||
legacyOpts.LegacyUpstreams.FlushInterval = time.Duration(flushInterval)
|
||||
legacyOpts.LegacyUpstreams.PassHostHeader = true
|
||||
legacyOpts.LegacyUpstreams.ProxyWebSockets = true
|
||||
legacyOpts.LegacyUpstreams.SSLUpstreamInsecureSkipVerify = true
|
||||
@@ -124,7 +124,7 @@ var _ = Describe("Legacy Options", func() {
|
||||
skipVerify := true
|
||||
passHostHeader := false
|
||||
proxyWebSockets := true
|
||||
flushInterval := 5 * time.Second
|
||||
flushInterval := Duration(5 * time.Second)
|
||||
|
||||
// Test cases and expected outcomes
|
||||
validHTTP := "http://foo.bar/baz"
|
||||
@@ -199,7 +199,7 @@ var _ = Describe("Legacy Options", func() {
|
||||
SSLUpstreamInsecureSkipVerify: skipVerify,
|
||||
PassHostHeader: passHostHeader,
|
||||
ProxyWebSockets: proxyWebSockets,
|
||||
FlushInterval: flushInterval,
|
||||
FlushInterval: time.Duration(flushInterval),
|
||||
}
|
||||
|
||||
upstreams, err := legacyUpstreams.convert()
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
package options
|
||||
|
||||
import "time"
|
||||
|
||||
// Upstreams is a collection of definitions for upstream servers.
|
||||
type Upstreams []Upstream
|
||||
|
||||
@@ -47,7 +45,7 @@ type Upstream struct {
|
||||
// FlushInterval is the period between flushing the response buffer when
|
||||
// streaming response from the upstream.
|
||||
// Defaults to 1 second.
|
||||
FlushInterval *time.Duration `json:"flushInterval,omitempty"`
|
||||
FlushInterval *Duration `json:"flushInterval,omitempty"`
|
||||
|
||||
// PassHostHeader determines whether the request host header should be proxied
|
||||
// to the upstream server.
|
||||
|
||||
Reference in New Issue
Block a user