Skip to content

Commit

Permalink
add password generator
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Richter <[email protected]>
  • Loading branch information
dragonchaser committed Apr 20, 2022
1 parent 8d81e39 commit 3956108
Showing 1 changed file with 38 additions and 6 deletions.
44 changes: 38 additions & 6 deletions ocis/pkg/command/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package command

import (
"bufio"
"crypto/rand"
"errors"
"fmt"
"io/ioutil"
"log"
"math/big"
"os"
"path"
"strings"
Expand All @@ -21,6 +23,7 @@ import (
)

const configFilename string = "ocis.yml"
const passwordLength int = 32

func InitCommand(cfg *config.Config) *cli.Command {
// TODO: remove homedir get
Expand Down Expand Up @@ -117,11 +120,26 @@ func createConfig(insecure, forceOverwrite bool, configPath string) error {
//WebDAV: &webdav.Config{},
}

idmServicePassword := "randomizeme"
idpServicePassword := "randomizeme"
ocisAdminServicePassword := "randomizeme"
revaServicePassword := "randomizeme"
tokenManagerJwtSecret := "randomizeme"
idmServicePassword, err := generateRandomPassword(passwordLength)
if err != nil {
return errors.New(fmt.Sprintf("Could not generate random password for idm: %s", err))
}
idpServicePassword, err := generateRandomPassword(passwordLength)
if err != nil {
return errors.New(fmt.Sprintf("Could not generate random password for idp: %s", err))
}
ocisAdminServicePassword, err := generateRandomPassword(passwordLength)
if err != nil {
return errors.New(fmt.Sprintf("Could not generate random password for ocis admin: %s", err))
}
revaServicePassword, err := generateRandomPassword(passwordLength)
if err != nil {
return errors.New(fmt.Sprintf("Could not generate random password for reva: %s", err))
}
tokenManagerJwtSecret, err := generateRandomPassword(passwordLength)
if err != nil {
return errors.New(fmt.Sprintf("Could not generate random password for tokenmanager: %s", err))
}

// TODO: generate outputs for all occurences above
cfg.TokenManager.JWTSecret = tokenManagerJwtSecret
Expand All @@ -133,7 +151,7 @@ func createConfig(insecure, forceOverwrite bool, configPath string) error {
cfg.IDM.ServiceUserPasswords.Reva = revaServicePassword
yamlOutput, err := yaml.Marshal(cfg)
if err != nil {
return err
return errors.New(fmt.Sprintf("Could not marshall config into yaml: %s", err))
}
targetPath := path.Join(configPath, configFilename)
err = ioutil.WriteFile(targetPath, yamlOutput, 0600)
Expand Down Expand Up @@ -163,3 +181,17 @@ func stringPrompt(label string) string {
}
return strings.TrimSpace(input)
}

func generateRandomPassword(length int) (string, error) {
const chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-=+!@#$%^&*."
ret := make([]byte, length)
for i := 0; i < length; i++ {
num, err := rand.Int(rand.Reader, big.NewInt(int64(len(chars))))
if err != nil {
return "", err
}
ret[i] = chars[num.Int64()]
}

return string(ret), nil
}

0 comments on commit 3956108

Please sign in to comment.