mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-01-06 12:19:37 -06:00
refactor yaml labels, add overwrite protection
Signed-off-by: Christian Richter <crichter@owncloud.com>
This commit is contained in:
@@ -2,16 +2,32 @@ package command
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"github.com/owncloud/ocis/ocis-pkg/config"
|
||||
"github.com/owncloud/ocis/ocis/pkg/register"
|
||||
cli "github.com/urfave/cli/v2"
|
||||
"gopkg.in/yaml.v3"
|
||||
|
||||
accounts "github.com/owncloud/ocis/extensions/accounts/pkg/config"
|
||||
graph "github.com/owncloud/ocis/extensions/graph/pkg/config"
|
||||
idm "github.com/owncloud/ocis/extensions/idm/pkg/config"
|
||||
)
|
||||
|
||||
const configFilename string = "ocis.yml"
|
||||
|
||||
func InitCommand(cfg *config.Config) *cli.Command {
|
||||
// TODO: remove homedir get
|
||||
homeDir, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
log.Fatalf("could not get homedir")
|
||||
}
|
||||
return &cli.Command{
|
||||
Name: "init",
|
||||
Usage: "initialise an ocis config",
|
||||
@@ -21,12 +37,24 @@ func InitCommand(cfg *config.Config) *cli.Command {
|
||||
EnvVars: []string{"OCIS_INSECURE"},
|
||||
Value: "ask",
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
Name: "force-overwrite",
|
||||
Aliases: []string{"f"},
|
||||
EnvVars: []string{"OCIS_FORCE_CONFIG_OVERWRITE"},
|
||||
Value: false,
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "config-path",
|
||||
//Value: cfg.ConfigPath, // TODO: as soon as PR 3480 is merged, remove quotes
|
||||
Value: path.Join(homeDir, ".ocis"), // TODO: this is temporary for experimenting, line above is relevant
|
||||
Usage: "config path for the ocis runtime",
|
||||
// Destination: &cfg.ConfigFile, // TODO: same as above
|
||||
},
|
||||
},
|
||||
Action: func(c *cli.Context) error {
|
||||
// TODO: discuss if we want overwrite protection for existing configs
|
||||
insecureFlag := c.String("insecure")
|
||||
if insecureFlag == "ask" {
|
||||
answer := strings.ToLower(StringPrompt("Insecure Backends? [Yes|No]"))
|
||||
answer := strings.ToLower(stringPrompt("Insecure Backends? [Yes|No]"))
|
||||
if answer == "yes" || answer == "y" {
|
||||
cfg.Proxy.InsecureBackends = true
|
||||
} else {
|
||||
@@ -39,25 +67,99 @@ func InitCommand(cfg *config.Config) *cli.Command {
|
||||
cfg.Proxy.InsecureBackends = false
|
||||
}
|
||||
}
|
||||
fmt.Println(cfg.Proxy.InsecureBackends)
|
||||
err := createConfig(cfg.Proxy.InsecureBackends, c.Bool("force-overwrite"), c.String("config-path"))
|
||||
if err != nil {
|
||||
log.Fatalf("Could not create config: %s", err)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func StringPrompt(label string) string {
|
||||
var s string
|
||||
r := bufio.NewReader(os.Stdin)
|
||||
for {
|
||||
fmt.Fprint(os.Stderr, label+" ")
|
||||
s, _ = r.ReadString('\n')
|
||||
if s != "" {
|
||||
break
|
||||
}
|
||||
}
|
||||
return strings.TrimSpace(s)
|
||||
}
|
||||
|
||||
func init() {
|
||||
register.AddCommand(InitCommand)
|
||||
}
|
||||
|
||||
func checkConfigPath(configPath string) error {
|
||||
targetPath := path.Join(configPath, configFilename)
|
||||
_, err := os.Stat(targetPath)
|
||||
if err == nil {
|
||||
return errors.New(fmt.Sprintf("Config in %s already exists", targetPath))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func createConfig(insecure, forceOverwrite bool, configPath string) error {
|
||||
err := checkConfigPath(configPath)
|
||||
if err != nil && forceOverwrite == false {
|
||||
return err
|
||||
}
|
||||
err = os.MkdirAll(configPath, 0700)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
cfg := config.Config{
|
||||
Accounts: &accounts.Config{},
|
||||
//Audit: &audit.Config{},
|
||||
//GLAuth: &glauth.Config{},
|
||||
//GraphExplorer: &graphExplorer.Config{},
|
||||
Graph: &graph.Config{},
|
||||
IDM: &idm.Config{},
|
||||
//IDP: &idp.Config{},
|
||||
//Nats: &nats.Config{},
|
||||
//Notifications: ¬ifications.Config{},
|
||||
//OCS: &ocs.Config{},
|
||||
//Proxy: &proxy.Config{},
|
||||
//Settings: &settings.Config{},
|
||||
//Storage: &storage.Config{},
|
||||
//Thumbnails: &thumbnails.Config{},
|
||||
//Web: &web.Config{},
|
||||
//WebDAV: &webdav.Config{},
|
||||
}
|
||||
|
||||
idmServicePassword := "randomizeme"
|
||||
idpServicePassword := "randomizeme"
|
||||
ocisAdminServicePassword := "randomizeme"
|
||||
revaServicePassword := "randomizeme"
|
||||
tokenManagerJwtSecret := "randomizeme"
|
||||
|
||||
// TODO: generate outputs for all occurences above
|
||||
cfg.TokenManager.JWTSecret = tokenManagerJwtSecret
|
||||
cfg.Accounts.TokenManager.JWTSecret = tokenManagerJwtSecret
|
||||
cfg.Graph.TokenManager.JWTSecret = tokenManagerJwtSecret
|
||||
cfg.IDM.ServiceUserPasswords.Idm = idmServicePassword
|
||||
cfg.IDM.ServiceUserPasswords.Idp = idpServicePassword
|
||||
cfg.IDM.ServiceUserPasswords.OcisAdmin = ocisAdminServicePassword
|
||||
cfg.IDM.ServiceUserPasswords.Reva = revaServicePassword
|
||||
yamlOutput, err := yaml.Marshal(cfg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
targetPath := path.Join(configPath, configFilename)
|
||||
err = ioutil.WriteFile(targetPath, yamlOutput, 0600)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf(
|
||||
"======================================\n"+
|
||||
" generated OCIS Config\n"+
|
||||
"======================================\n"+
|
||||
" configpath : %s\n"+
|
||||
" user : admin\n"+
|
||||
" password : %s\n",
|
||||
targetPath, ocisAdminServicePassword)
|
||||
return nil
|
||||
}
|
||||
|
||||
func stringPrompt(label string) string {
|
||||
input := ""
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
for {
|
||||
fmt.Fprint(os.Stderr, label+" ")
|
||||
input, _ = reader.ReadString('\n')
|
||||
if input != "" {
|
||||
break
|
||||
}
|
||||
}
|
||||
return strings.TrimSpace(input)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user