forked from fydrah/loginapp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
195 lines (183 loc) · 5.87 KB
/
config.go
File metadata and controls
195 lines (183 loc) · 5.87 KB
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
// Copyright 2018 fydrah
//
// 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"
"io/ioutil"
"os"
"strings"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/version"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
type AppConfig struct {
Name string `yaml:"name"`
Listen string `yaml:"listen"`
OIDC struct {
Client struct {
ID string `yaml:"id"`
Secret string `yaml:"secret"`
RedirectURL string `yaml:"redirect_url"`
} `yaml:"client"`
Issuer struct {
URL string `yaml:"url"`
RootCA string `yaml:"root_ca"`
} `yaml:"issuer"`
ExtraScopes []string `yaml:"extra_scopes"`
OfflineAsScope *bool `yaml:"offline_as_scope"`
CrossClients []string `yaml:"cross_clients"`
} `yaml:"oidc"`
TLS struct {
Enabled bool `yaml:"enabled"`
Cert string `yaml:"cert"`
Key string `yaml:"key"`
} `yaml:"tls"`
Log struct {
Level string `yaml:"level"`
Format string `yaml:"format"`
} `yaml:"log"`
WebOutput struct {
MainUsernameClaim string `yaml:"main_username_claim"`
MainClientID string `yaml:"main_client_id"`
AssetsDir string `yaml:"assets_dir"`
SkipMainPage bool `yaml:"skip_main_page"`
} `yaml:"web_output"`
Template struct {
ClusterServer string `yaml:"cluster_server"`
ClusterCA string `yaml:"cluster_ca"`
ClusterName string `yaml:"cluster_name"`
ContextName string `yaml:"context_name"`
} `yaml:"template"`
}
// appCheck struct
// used by check function
type appCheck struct {
Condition bool
Message string
DefaultAction func()
}
// check checks each appCheck, if one
// check fails, return true
func check(checks []appCheck) bool {
checkFailed := false
for _, c := range checks {
if c.Condition {
logger.Error(c.Message)
checkFailed = true
if c.DefaultAction != nil {
c.DefaultAction()
}
}
}
return checkFailed
}
// configLogger setup application logger
// Default loglevel is info
func configLogger(format string, logLevel string) {
switch f := format; f {
case "json":
logger.Formatter = &logrus.JSONFormatter{}
case "text":
logger.Formatter = &logrus.TextFormatter{}
default:
logger.Formatter = &logrus.JSONFormatter{}
logger.Warningf("Format %q not available, use json|text. Using json format", f)
format = "json"
}
logger.Debugf("Using %s log format", format)
switch l := logLevel; l {
case "debug":
logger.Level = logrus.DebugLevel
case "info":
logger.Level = logrus.InfoLevel
case "warning":
logger.Level = logrus.WarnLevel
case "error":
logger.Level = logrus.ErrorLevel
default:
logger.Level = logrus.InfoLevel
logger.Warningf("Log level %q not available, use debug|info|warning|error. Using Info log level", l)
logLevel = "info"
}
logger.Debugf("Using %s log level", logLevel)
}
// Init load configuration,
// setup logger and run
// error/warning checks
func (a *AppConfig) Init(config string) error {
prometheus.MustRegister(version.NewCollector("loginapp"))
/*
Extract data from yaml configuration file
*/
logger.Debugf("Loading configuration file: %v", config)
configData, err := ioutil.ReadFile(config)
if err != nil {
return fmt.Errorf("failed to read config file %s: %v", config, err)
}
logger.Debugf("Unmarshal data: %v", configData)
if err := yaml.Unmarshal(configData, &a); err != nil {
return fmt.Errorf("error parse config file %s: %v", config, err)
}
/*
Configure log level
*/
configLogger(strings.ToLower(a.Log.Format), strings.ToLower(a.Log.Level))
/*
Configuration checks
(inspired from https://github.com/coreos/dex/blob/master/cmd/dex/serve.go)
*/
currentDir, err := os.Getwd()
if err != nil {
return fmt.Errorf("error getting current directory: %v", err)
}
defaultAssetsDir := fmt.Sprintf("%v/assets", currentDir)
/*
Error checks: list of checks which make loginapp failed
*/
errorChecks := []appCheck{
{a.Name == "", "no name specified", nil},
{a.Listen == "", "no bind 'ip:port' specified", nil},
{a.OIDC.Client.ID == "", "no client id specified", nil},
{a.OIDC.Client.Secret == "", "no client secret specified", nil},
{a.OIDC.Client.RedirectURL == "", "no redirect url specified", nil},
{a.OIDC.Issuer.URL == "", "no issuer url specified", nil},
{a.OIDC.Issuer.RootCA == "", "no issuer root_ca specified", nil},
{a.TLS.Enabled && a.TLS.Cert == "", "no tls cert specified", nil},
{a.TLS.Enabled && a.TLS.Key == "", "no tls key specified", nil},
}
if check(errorChecks) {
return fmt.Errorf("Error while loading configuration")
}
/*
Default checks: list of check which make loginapp setup a default value
Even if logger report this as an error log, this is not handle as an error.
Hope the following issue will be merged to use loglevel as a parameter:
https://github.com/sirupsen/logrus/issues/646
*/
defaultChecks := []appCheck{
{a.WebOutput.MainClientID == "", fmt.Sprintf("no output main_client_id specified, using default: %v", a.OIDC.Client.ID), func() {
a.WebOutput.MainClientID = a.OIDC.Client.ID
}},
{a.WebOutput.AssetsDir == "", fmt.Sprintf("no assets_dir specified, using default: %v", defaultAssetsDir), func() {
a.WebOutput.AssetsDir = defaultAssetsDir
}},
{a.WebOutput.MainUsernameClaim == "", "no output main_username_claim specified, using default: 'name'", func() {
a.WebOutput.MainUsernameClaim = "name"
}},
}
_ = check(defaultChecks)
logger.Debugf("Configuration loaded: %+v", a)
return nil
}