Newer
Older
"github.com/oidc-mytoken/server/internal/config"
"github.com/oidc-mytoken/server/internal/jws"
"github.com/oidc-mytoken/server/internal/model/version"
"github.com/oidc-mytoken/server/internal/utils/dbcl"
loggerUtils "github.com/oidc-mytoken/server/internal/utils/logger"
"github.com/oidc-mytoken/server/internal/utils/zipdownload"
"github.com/oidc-mytoken/server/shared/utils/fileutil"
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
type _rootDBCredentials struct {
User string
Password string
PasswordFile string
}
var rootDBCredentials _rootDBCredentials
func (cred _rootDBCredentials) ToDBConf() config.DBConf {
return config.DBConf{
Hosts: config.Get().DB.Hosts,
User: cred.User,
Password: cred.Password,
PasswordFile: cred.PasswordFile,
ReconnectInterval: config.Get().DB.ReconnectInterval,
}
}
var dbFlags = []cli.Flag{
&cli.StringFlag{
Name: "user",
Aliases: []string{"u", "root-user", "db-user"},
Usage: "The username for the (root) user used for setting up the db",
EnvVars: []string{"DB_USER"},
Value: "root",
Destination: &rootDBCredentials.User,
Placeholder: "ROOT",
},
&cli.StringFlag{
Name: "password",
Aliases: []string{"p", "pw", "db-password", "db-pw"},
Usage: "The password for the (root) user used for setting up the db",
EnvVars: []string{"DB_PW", "DB_PASSWORD"},
Destination: &rootDBCredentials.Password,
Placeholder: "PASSWORD",
},
&cli.StringFlag{
Name: "password-file",
Aliases: []string{"pw-file"},
Usage: "Read the password for connecting to the database from this file",
EnvVars: []string{"DB_PASSWORD_FILE", "DB_PW_FILE"},
Destination: &rootDBCredentials.PasswordFile,
TakesFile: true,
Placeholder: "FILE",
},
}
var app = &cli.App{
Name: "mytoken-setup",
Usage: "Command line client for easily setting up a mytoken server",
Version: version.VERSION(),
Compiled: time.Time{},
Authors: []*cli.Author{{
Name: "Gabriel Zachmann",
Email: "gabriel.zachmann@kit.edu",
}},
Copyright: "Karlsruhe Institute of Technology 2020-2021",
UseShortOptionHandling: true,
Commands: cli.Commands{
&cli.Command{
Name: "signing-key",
Aliases: []string{"key"},
Usage: "Generates a new signing key",
Description: "Generates a new signing key according to the properties specified in the config file and stores it.",
Action: createSigningKey,
},
&cli.Command{
Name: "install",
Usage: "Installs needed dependencies",
Subcommands: cli.Commands{
&cli.Command{
Name: "geoip-db",
Aliases: []string{"geo-ip-db"},
Usage: "Installs the ip geolocation database.",
Action: installGEOIPDB,
},
},
},
&cli.Command{
Name: "db",
Usage: "Setups for the database",
Flags: append([]cli.Flag{}, dbFlags...),
Subcommands: cli.Commands{
&cli.Command{
Name: "db",
Aliases: []string{"database"},
Usage: "Creates the database in the database server",
Action: createDB,
Flags: append([]cli.Flag{}, dbFlags...),
},
&cli.Command{
Name: "user",
Usage: "Creates the normal database user",
Action: createUser,
Flags: append([]cli.Flag{}, dbFlags...),
},
},
},
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
func installGEOIPDB(_ *cli.Context) error {
archive, err := zipdownload.DownloadZipped("https://download.ip2location.com/lite/IP2LOCATION-LITE-DB1.IPV6.BIN.ZIP")
if err != nil {
return err
}
log.Debug("Downloaded zip file")
err = ioutil.WriteFile(config.Get().GeoIPDBFile, archive["IP2LOCATION-LITE-DB1.IPV6.BIN"], 0644)
if err == nil {
log.WithField("file", config.Get().GeoIPDBFile).Debug("Installed geo ip database")
fmt.Printf("Installed geo ip database file to '%s'.\n", config.Get().GeoIPDBFile)
}
return err
func createSigningKey(_ *cli.Context) error {
sk, _, err := jws.GenerateKeyPair()
if err != nil {
return err
}
str := jws.ExportPrivateKeyAsPemStr(sk)
filepath := config.Get().Signing.KeyFile
if fileutil.FileExists(filepath) {
log.WithField("filepath", filepath).Debug("File already exists")
if !prompter.YesNo(fmt.Sprintf("File '%s' already exists. Do you want to overwrite it?", filepath), false) {
os.Exit(1)
}
}
if err = ioutil.WriteFile(filepath, []byte(str), 0600); err != nil {
log.WithField("filepath", filepath).Debug("Wrote key to file")
fmt.Printf("Wrote key to file '%s'.\n", filepath)
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
216
217
218
219
220
221
222
223
224
225
226
227
228
//go:embed scripts
var sqlScripts embed.FS
func readSQLFile(path string) (string, error) {
data, err := sqlScripts.ReadFile(path)
if err != nil {
return "", err
}
return string(data), nil
}
func _getSetVars() (string, error) {
return readSQLFile("scripts/vars.sql")
}
func getSetVarsCommands(db, user, password string) (string, error) {
cmds, err := _getSetVars()
if err != nil {
return "", err
}
if db != "" {
cmds += fmt.Sprintf(`EXECUTE setDB USING '%s';\n`, db)
}
if user != "" {
cmds += fmt.Sprintf(`EXECUTE setUser USING '%s';\n`, user)
}
if password != "" {
cmds += fmt.Sprintf(`EXECUTE setPassword USING '%s';\n`, password)
}
return cmds, nil
}
func getDBCmds() (string, error) {
return readSQLFile("scripts/db.sql")
}
func getUserCmds() (string, error) {
return readSQLFile("scripts/user.sql")
}
func createDB(_ *cli.Context) error {
cmds, err := getSetVarsCommands(config.Get().DB.DB, config.Get().DB.User, config.Get().DB.GetPassword())
if err != nil {
return err
}
dbCmds, err := getDBCmds()
if err != nil {
return err
}
cmds += dbCmds
return dbcl.RunDBCommands(cmds, rootDBCredentials.ToDBConf(), true)
}
func createUser(_ *cli.Context) error {
cmds, err := getSetVarsCommands(config.Get().DB.DB, config.Get().DB.User, config.Get().DB.GetPassword())
if err != nil {
return err
}
userCmds, err := getUserCmds()
if err != nil {
return err
}
cmds += userCmds
return dbcl.RunDBCommands(cmds, rootDBCredentials.ToDBConf(), true)
}