-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
159 lines (140 loc) · 4.76 KB
/
main.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
/*******************************************************************************
*
* Copyright 2022 SAP SE
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You should have received a copy of the License along with this
* program. If not, 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 (
"flag"
"fmt"
"os"
policy "github.com/databus23/goslo.policy"
"github.com/sapcc/go-bits/logg"
"github.com/sapcc/go-bits/must"
"github.com/sapcc/go-bits/osext"
"github.com/spf13/viper"
"github.com/sapcc/hermes/pkg/api"
"github.com/sapcc/hermes/pkg/identity"
"github.com/sapcc/hermes/pkg/storage"
"github.com/sapcc/hermes/pkg/util"
)
const version = "1.2.0"
var configPath *string
var showVersion *bool // Add a flag to check for the version.
func main() {
logg.ShowDebug = osext.GetenvBool("HERMES_DEBUG")
parseCmdlineFlags()
// Check if the version flag is set, and if so, print the version and exit.
if *showVersion {
fmt.Println("Hermes version:", version)
os.Exit(0)
}
setDefaultConfig()
readConfig(configPath)
keystoneDriver := configuredKeystoneDriver()
storageDriver := configuredStorageDriver()
readPolicy()
must.Succeed(api.Server(keystoneDriver, storageDriver))
}
func parseCmdlineFlags() {
// Get config file location
configPath = flag.String("f", "hermes.conf", "specifies the location of the TOML-format configuration file")
showVersion = flag.Bool("version", false, "prints the version of the application")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
}
flag.Parse()
}
func setDefaultConfig() {
var nullEnforcer, err = policy.NewEnforcer(make(map[string]string))
if err != nil {
panic(err)
}
viper.SetDefault("hermes.keystone_driver", "keystone")
viper.SetDefault("hermes.storage_driver", "elasticsearch")
viper.SetDefault("hermes.PolicyEnforcer", &nullEnforcer)
viper.SetDefault("API.ListenAddress", "0.0.0.0:8788")
viper.SetDefault("elasticsearch.url", "localhost:9200")
// index.max_result_window defaults to 10000, as per
// https://www.elastic.co/guide/en/elasticsearch/reference/current/index-modules.html
// Increasing max_result_window to 20000, with corresponding changes to Elasticsearch to handle the increase.
viper.SetDefault("elasticsearch.max_result_window", "20000")
}
func readConfig(configPath *string) {
// Enable viper to read Environment Variables
viper.AutomaticEnv()
// Bind the specific environment variable to a viper key
err := viper.BindEnv("elasticsearch.username", "HERMES_ES_USERNAME")
if err != nil {
logg.Fatal(err.Error())
}
err = viper.BindEnv("elasticsearch.password", "HERMES_ES_PASSWORD")
if err != nil {
logg.Fatal(err.Error())
}
// Don't read config file if the default config file isn't there,
// as we will just fall back to config defaults in that case
var shouldReadConfig = true
if _, err := os.Stat(*configPath); os.IsNotExist(err) {
shouldReadConfig = *configPath != flag.Lookup("f").DefValue
}
// Now we sorted that out, read the config
logg.Debug("Should read config: %v, config file is %s", shouldReadConfig, *configPath)
if shouldReadConfig {
viper.SetConfigFile(*configPath)
viper.SetConfigType("toml")
must.Succeed(viper.ReadInConfig())
}
}
var keystoneIdentity = identity.Keystone{}
var mockIdentity = identity.Mock{}
func configuredKeystoneDriver() identity.Identity {
driverName := viper.GetString("hermes.keystone_driver")
switch driverName {
case "keystone":
return keystoneIdentity
case "mock":
return mockIdentity
default:
logg.Error("Couldn't match a keystone driver for configured value \"%s\"", driverName)
return nil
}
}
var elasticSearchStorage = storage.ElasticSearch{}
var mockStorage = storage.Mock{}
func configuredStorageDriver() storage.Storage {
driverName := viper.GetString("hermes.storage_driver")
switch driverName {
case "elasticsearch":
return elasticSearchStorage
case "mock":
return mockStorage
default:
logg.Error("Couldn't match a storage driver for configured value \"%s\"", driverName)
return nil
}
}
func readPolicy() {
// load the policy file
policyEnforcer, err := util.LoadPolicyFile(viper.GetString("hermes.PolicyFilePath"))
if err != nil {
logg.Fatal(err.Error())
}
if policyEnforcer != nil {
viper.Set("hermes.PolicyEnforcer", policyEnforcer)
}
}