Skip to content

Commit 8a2f884

Browse files
committed
implement flag.Getter interface for StringArray for the bumped go-options
1 parent 5b0629c commit 8a2f884

File tree

3 files changed

+38
-22
lines changed

3 files changed

+38
-22
lines changed

main.go

+15-15
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ func main() {
2121
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
2222
flagSet := flag.NewFlagSet("oauth2_proxy", flag.ExitOnError)
2323

24-
emailDomains := StringArray{}
25-
upstreams := StringArray{}
26-
skipAuthRegex := StringArray{}
27-
bypassAuthRegex := StringArray{}
28-
bypassAuthExceptRegex := StringArray{}
29-
openshiftCAs := StringArray{}
24+
emailDomains := NewStringArray()
25+
upstreams := NewStringArray()
26+
skipAuthRegex := NewStringArray()
27+
bypassAuthRegex := NewStringArray()
28+
bypassAuthExceptRegex := NewStringArray()
29+
openshiftCAs := NewStringArray()
3030
clientCA := ""
31-
upstreamCAs := StringArray{}
31+
upstreamCAs := NewStringArray()
3232

3333
config := flagSet.String("config", "", "path to config file")
3434
showVersion := flagSet.Bool("version", false, "print version string")
@@ -41,22 +41,22 @@ func main() {
4141
flagSet.StringVar(&clientCA, "tls-client-ca", clientCA, "path to a CA file for admitting client certificates.")
4242
flagSet.String("redirect-url", "", "the OAuth Redirect URL. ie: \"https://internalapp.yourcompany.com/oauth/callback\"")
4343
flagSet.Bool("set-xauthrequest", false, "set X-Auth-Request-User and X-Auth-Request-Email response headers (useful in Nginx auth_request mode)")
44-
flagSet.Var(&upstreams, "upstream", "the http url(s) of the upstream endpoint or file:// paths for static files. Routing is based on the path")
44+
flagSet.Var(upstreams, "upstream", "the http url(s) of the upstream endpoint or file:// paths for static files. Routing is based on the path")
4545
flagSet.Bool("pass-basic-auth", true, "pass HTTP Basic Auth, X-Forwarded-User and X-Forwarded-Email information to upstream")
4646
flagSet.Bool("pass-user-headers", true, "pass X-Forwarded-User and X-Forwarded-Email information to upstream")
4747
flagSet.String("basic-auth-password", "", "the password to set when passing the HTTP Basic Auth header")
4848
flagSet.Bool("pass-access-token", false, "pass OAuth access_token to upstream via X-Forwarded-Access-Token header")
4949
flagSet.Bool("pass-user-bearer-token", false, "pass OAuth access token received from the client to upstream via X-Forwarded-Access-Token header")
5050
flagSet.Bool("pass-host-header", true, "pass the request Host Header to upstream")
51-
flagSet.Var(&bypassAuthExceptRegex, "bypass-auth-except-for", "provide authentication ONLY for request paths under proxy-prefix and those that match the given regex (may be given multiple times). Cannot be set with -skip-auth-regex/-bypass-auth-for")
52-
flagSet.Var(&bypassAuthRegex, "bypass-auth-for", "alias for skip-auth-regex")
53-
flagSet.Var(&skipAuthRegex, "skip-auth-regex", "bypass authentication for request paths that match (may be given multiple times). Cannot be set with -bypass-auth-except-for. Alias for -bypass-auth-for")
51+
flagSet.Var(bypassAuthExceptRegex, "bypass-auth-except-for", "provide authentication ONLY for request paths under proxy-prefix and those that match the given regex (may be given multiple times). Cannot be set with -skip-auth-regex/-bypass-auth-for")
52+
flagSet.Var(bypassAuthRegex, "bypass-auth-for", "alias for skip-auth-regex")
53+
flagSet.Var(skipAuthRegex, "skip-auth-regex", "bypass authentication for request paths that match (may be given multiple times). Cannot be set with -bypass-auth-except-for. Alias for -bypass-auth-for")
5454
flagSet.Bool("skip-provider-button", false, "will skip sign-in-page to directly reach the next step: oauth/start")
5555
flagSet.Bool("skip-auth-preflight", false, "will skip authentication for OPTIONS requests")
5656
flagSet.Bool("ssl-insecure-skip-verify", false, "skip validation of certificates presented when using HTTPS")
5757
flagSet.String("debug-address", "", "[http://]<addr>:<port> or unix://<path> to listen on for debug and requests")
5858

59-
flagSet.Var(&emailDomains, "email-domain", "authenticate emails with the specified domain (may be given multiple times). Use * to authenticate any email")
59+
flagSet.Var(emailDomains, "email-domain", "authenticate emails with the specified domain (may be given multiple times). Use * to authenticate any email")
6060
flagSet.String("client-id", "", "the OAuth Client ID: ie: \"123456.apps.googleusercontent.com\"")
6161
flagSet.String("client-secret", "", "the OAuth Client Secret")
6262
flagSet.String("client-secret-file", "", "a file containing the client-secret")
@@ -71,7 +71,7 @@ func main() {
7171
flagSet.String("openshift-group", "", "restrict logins to members of this group (or groups, if encoded as a JSON array).")
7272
flagSet.String("openshift-sar", "", "require this encoded subject access review to authorize (may be a JSON list).")
7373
flagSet.String("openshift-sar-by-host", "", "require this encoded subject access review to authorize (must be a JSON array).")
74-
flagSet.Var(&openshiftCAs, "openshift-ca", "paths to CA roots for the OpenShift API (may be given multiple times, defaults to /var/run/secrets/kubernetes.io/serviceaccount/ca.crt).")
74+
flagSet.Var(openshiftCAs, "openshift-ca", "paths to CA roots for the OpenShift API (may be given multiple times, defaults to /var/run/secrets/kubernetes.io/serviceaccount/ca.crt).")
7575
flagSet.String("openshift-review-url", "", "Permission check endpoint (defaults to the subject access review endpoint)")
7676
flagSet.String("openshift-delegate-urls", "", "If set, perform delegated authorization against the OpenShift API server. Value is a JSON map of path prefixes to v1beta1.ResourceAttribute records that must be granted to the user to continue. E.g. {\"/\":{\"resource\":\"pods\",\"namespace\":\"default\",\"name\":\"test\"}} only allows users who can see the pod test in namespace default.")
7777
flagSet.String("openshift-service-account", "", "An optional name of an OpenShift service account to act as. If set, the injected service account info will be used to determine the client ID and client secret.")
@@ -98,15 +98,15 @@ func main() {
9898
flagSet.String("approval-prompt", "force", "OAuth approval_prompt")
9999

100100
flagSet.String("signature-key", "", "GAP-Signature request signature key (algorithm:secretkey)")
101-
flagSet.Var(&upstreamCAs, "upstream-ca", "paths to CA roots for the Upstream (target) Server (may be given multiple times, defaults to system trust store).")
101+
flagSet.Var(upstreamCAs, "upstream-ca", "paths to CA roots for the Upstream (target) Server (may be given multiple times, defaults to system trust store).")
102102

103103
providerOpenShift := openshift.New()
104104
providerOpenShift.Bind(flagSet)
105105

106106
flagSet.Parse(os.Args[1:])
107107

108108
providerOpenShift.SetClientCAFile(clientCA)
109-
providerOpenShift.SetReviewCAs(openshiftCAs)
109+
providerOpenShift.SetReviewCAs(openshiftCAs.Get().([]string))
110110

111111
if *showVersion {
112112
fmt.Printf("oauth2_proxy v%s (built with %s)\n", VERSION, runtime.Version())

string_array.go

+20-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,31 @@
11
package main
22

33
import (
4+
"flag"
45
"strings"
56
)
67

7-
type StringArray []string
8+
var _ flag.Getter = StringArray{}
89

9-
func (a *StringArray) Set(s string) error {
10-
*a = append(*a, s)
10+
type StringArray struct {
11+
stringArray *[]string
12+
}
13+
14+
func NewStringArray() *StringArray {
15+
return &StringArray{
16+
stringArray: &[]string{},
17+
}
18+
}
19+
20+
func (a StringArray) Get() interface{} {
21+
return *a.stringArray
22+
}
23+
24+
func (a StringArray) Set(s string) error {
25+
*a.stringArray = append(*a.stringArray, s)
1126
return nil
1227
}
1328

14-
func (a *StringArray) String() string {
15-
return strings.Join(*a, ",")
29+
func (a StringArray) String() string {
30+
return strings.Join(*a.stringArray, ",")
1631
}

string_array_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package main
22

33
import (
44
"testing"
5+
56
"github.com/bmizerany/assert"
67
)
78

89
func TestStringArray(t *testing.T) {
9-
sa := StringArray{}
10+
sa := NewStringArray()
1011
assert.Equal(t, "", sa.String())
1112
err := sa.Set("foo")
1213
if err != nil {
@@ -18,4 +19,4 @@ func TestStringArray(t *testing.T) {
1819
t.Errorf("unexpected error %v", err)
1920
}
2021
assert.Equal(t, "foo,bar", sa.String())
21-
}
22+
}

0 commit comments

Comments
 (0)