forked from louketo/louketo-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.go
215 lines (194 loc) · 5.91 KB
/
cli.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
Copyright 2015 All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"fmt"
"os"
"os/signal"
"reflect"
"syscall"
"time"
"github.com/urfave/cli"
)
// newOauthProxyApp creates a new cli application and runs it
func newOauthProxyApp() *cli.App {
config := newDefaultConfig()
app := cli.NewApp()
app.Name = prog
app.Usage = description
app.Version = getVersion()
app.Author = author
app.Email = email
app.Flags = getCommandLineOptions()
app.UsageText = "keycloak-proxy [options]"
// step: the standard usage message isn't that helpful
app.OnUsageError = func(context *cli.Context, err error, isSubcommand bool) error {
fmt.Fprintf(os.Stderr, "[error] invalid options, %s\n", err)
return err
}
// step: set the default action
app.Action = func(cx *cli.Context) error {
configFile := cx.String("config")
// step: do we have a configuration file?
if configFile != "" {
if err := readConfigFile(configFile, config); err != nil {
return printError("unable to read the configuration file: %s, error: %s", configFile, err.Error())
}
}
// step: parse the command line options
if err := parseCLIOptions(cx, config); err != nil {
return printError(err.Error())
}
// step: validate the configuration
if err := config.isValid(); err != nil {
return printError(err.Error())
}
// step: create the proxy
proxy, err := newProxy(config)
if err != nil {
return printError(err.Error())
}
// step: start the service
if err := proxy.Run(); err != nil {
return printError(err.Error())
}
// step: setup the termination signals
signalChannel := make(chan os.Signal)
signal.Notify(signalChannel, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
<-signalChannel
return nil
}
return app
}
// getCommandLineOptions builds the command line options by reflecting the Config struct and extracting
// the tagged information
func getCommandLineOptions() []cli.Flag {
defaults := newDefaultConfig()
var flags []cli.Flag
count := reflect.TypeOf(Config{}).NumField()
for i := 0; i < count; i++ {
field := reflect.TypeOf(Config{}).Field(i)
usage, found := field.Tag.Lookup("usage")
if !found {
continue
}
envName := field.Tag.Get("env")
if envName != "" {
envName = envPrefix + envName
}
optName := field.Tag.Get("yaml")
switch t := field.Type; t.Kind() {
case reflect.Bool:
dv := reflect.ValueOf(defaults).Elem().FieldByName(field.Name).Bool()
msg := fmt.Sprintf("%s (default: %t)", usage, dv)
flags = append(flags, cli.BoolTFlag{
Name: optName,
Usage: msg,
EnvVar: envName,
})
case reflect.String:
defaultValue := reflect.ValueOf(defaults).Elem().FieldByName(field.Name).String()
flags = append(flags, cli.StringFlag{
Name: optName,
Usage: usage,
EnvVar: envName,
Value: defaultValue,
})
case reflect.Slice:
fallthrough
case reflect.Map:
flags = append(flags, cli.StringSliceFlag{
Name: optName,
Usage: usage,
})
case reflect.Int64:
switch t.String() {
case "time.Duration":
dv := reflect.ValueOf(defaults).Elem().FieldByName(field.Name).Int()
flags = append(flags, cli.DurationFlag{
Name: optName,
Usage: usage,
Value: time.Duration(dv),
})
default:
panic("unknown uint64 type in the Config struct")
}
default:
errMsg := fmt.Sprintf("field: %s, type: %s, kind: %s is not being handled", field.Name, t.String(), t.Kind())
panic(errMsg)
}
}
return flags
}
// parseCLIOptions parses the command line options and constructs a config object
func parseCLIOptions(cx *cli.Context, config *Config) (err error) {
// step: we can ignore these options in the Config struct
ignoredOptions := []string{"tag-data", "match-claims", "resources", "headers"}
// step: iterate the Config and grab command line options via reflection
count := reflect.TypeOf(config).Elem().NumField()
for i := 0; i < count; i++ {
field := reflect.TypeOf(config).Elem().Field(i)
name := field.Tag.Get("yaml")
if containedIn(name, ignoredOptions) {
continue
}
if cx.IsSet(name) {
switch field.Type.Kind() {
case reflect.Bool:
reflect.ValueOf(config).Elem().FieldByName(field.Name).SetBool(cx.Bool(name))
case reflect.String:
reflect.ValueOf(config).Elem().FieldByName(field.Name).SetString(cx.String(name))
case reflect.Slice:
reflect.ValueOf(config).Elem().FieldByName(field.Name).Set(reflect.ValueOf(cx.StringSlice(name)))
case reflect.Int64:
switch field.Type.String() {
case "time.Duration":
reflect.ValueOf(config).Elem().FieldByName(field.Name).SetInt(int64(cx.Duration(name)))
default:
reflect.ValueOf(config).Elem().FieldByName(field.Name).SetInt(cx.Int64(name))
}
}
}
}
if cx.IsSet("tag") {
tags, err := decodeKeyPairs(cx.StringSlice("tag"))
if err != nil {
return err
}
mergeMaps(config.Tags, tags)
}
if cx.IsSet("match-claims") {
claims, err := decodeKeyPairs(cx.StringSlice("match-claims"))
if err != nil {
return err
}
mergeMaps(config.MatchClaims, claims)
}
if cx.IsSet("headers") {
headers, err := decodeKeyPairs(cx.StringSlice("headers"))
if err != nil {
return err
}
mergeMaps(config.Headers, headers)
}
if cx.IsSet("resources") {
for _, x := range cx.StringSlice("resources") {
resource, err := newResource().parse(x)
if err != nil {
return fmt.Errorf("invalid resource %s, %s", x, err)
}
config.Resources = append(config.Resources, resource)
}
}
return nil
}