Skip to content

Commit

Permalink
gscloud-server-create: auto generate password
Browse files Browse the repository at this point in the history
Auto-generate a sufficiently secure password (using
https://pkg.go.dev/github.com/sethvargo/go-password) when --password is
not given on the command line.

--password flag will be removed in future release.

Fixes #90
  • Loading branch information
bkircher committed Jan 10, 2021
1 parent c487594 commit e0a602f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
20 changes: 19 additions & 1 deletion cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/gridscale/gsclient-go/v3"
"github.com/gridscale/gscloud/render"
"github.com/sethvargo/go-password/password"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -185,17 +186,25 @@ To create a server without any storage just omit --with-template flag:
fmt.Println("Server created:", cServer.ObjectUUID)

if serverFlags.template != "" {
var password string

templateOp := rt.TemplateOperator()
template, _ := templateOp.GetTemplateByName(ctx, serverFlags.template)

if serverFlags.plainPassword == "" {
password = generatePassword()
} else {
password = serverFlags.plainPassword
}

storageOp := rt.StorageOperator()
cStorage, err := storageOp.CreateStorage(ctx, gsclient.StorageCreateRequest{
Name: string(serverFlags.serverName),
Capacity: serverFlags.storageSize,
StorageType: gsclient.DefaultStorageType,
Template: &gsclient.StorageTemplate{
TemplateUUID: template.Properties.ObjectUUID,
Password: serverFlags.plainPassword,
Password: password,
PasswordType: gsclient.PlainPasswordType,
Hostname: serverFlags.hostName,
},
Expand All @@ -213,6 +222,7 @@ To create a server without any storage just omit --with-template flag:
log.Fatalf("Create storage failed: %s", err)
}
fmt.Println("Storage created:", cStorage.ObjectUUID)
fmt.Println("Password:", password)
}
},
}
Expand Down Expand Up @@ -355,6 +365,14 @@ func init() {
rootCmd.AddCommand(serverCmd)
}

func generatePassword() string {
res, err := password.Generate(12, 10, 10, false, false)
if err != nil {
log.Fatalf("Failed generating password: %s\n", err)
}
return res
}

func toHardwareProfile(val string) gsclient.ServerHardwareProfile {
var prof gsclient.ServerHardwareProfile
switch val {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0
github.com/kirsle/configdir v0.0.0-20170128060238-e45d2f54772f
github.com/mattn/go-runewidth v0.0.9
github.com/sethvargo/go-password v0.2.0
github.com/sirupsen/logrus v1.7.0
github.com/spf13/cobra v1.1.1
github.com/spf13/pflag v1.0.5
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
github.com/sethvargo/go-password v0.2.0 h1:BTDl4CC/gjf/axHMaDQtw507ogrXLci6XRiLc7i/UHI=
github.com/sethvargo/go-password v0.2.0/go.mod h1:Ym4Mr9JXLBycr02MFuVQ/0JHidNetSgbzutTr3zsYXE=
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
Expand Down

0 comments on commit e0a602f

Please sign in to comment.