Skip to content

Commit

Permalink
feat: generate random admin password for m1 and m2
Browse files Browse the repository at this point in the history
  • Loading branch information
Janos Miko committed Feb 2, 2021
1 parent 0685d7a commit 51d8670
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 14 deletions.
2 changes: 1 addition & 1 deletion VERSION.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.5-beta
0.1.6-beta
5 changes: 5 additions & 0 deletions cmd/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,9 @@ func init() {
"magento-version", GetMagentoVersion().String(), "magento version")

_ = viper.BindPFlag(AppName+"_magento_version", bootstrapCmd.Flags().Lookup("magento-version"))

bootstrapCmd.Flags().Bool(
"disable-tfa", false, "disable magento 2 two-factor authentication")

_ = viper.BindPFlag(AppName+"_magento_disable_tfa", bootstrapCmd.Flags().Lookup("disable-tfa"))
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ require (
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.1 // indirect
github.com/sethvargo/go-password v0.2.0
github.com/sirupsen/logrus v1.6.0
github.com/spf13/afero v1.1.2
github.com/spf13/cobra v1.1.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR
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/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sirupsen/logrus v1.2.0 h1:juTguoYk5qI21pwyTXY3B3Y5cOTH3ZUyZCg1v/mihuo=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
Expand Down
80 changes: 67 additions & 13 deletions internal/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"text/template"

"github.com/hashicorp/go-version"
"github.com/sethvargo/go-password/password"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -70,13 +71,19 @@ func bootstrapMagento2() error {
baseCommand = []string{"exec", "-T", "php-fpm", "bash", "-c"}
freshInstall := false

composerCommand := "composer"
minimumMagentoVersionForComposer2, _ := version.NewVersion("2.4.2")
if GetMagentoVersion().GreaterThan(minimumMagentoVersionForComposer2) {
composerCommand = "composer2"
}

// Composer Install
if !IsSkipComposerInstall() {
if !IsNoParallel() {
if IsDebug() {
composeCommand = append(baseCommand, `composer global require -vvv --profile hirak/prestissimo`)
composeCommand = append(baseCommand, composerCommand+` global require -vvv --profile hirak/prestissimo`)
} else {
composeCommand = append(baseCommand, `composer global require --verbose --profile hirak/prestissimo`)
composeCommand = append(baseCommand, composerCommand+` global require --verbose --profile hirak/prestissimo`)
}

if err := EnvCmd(composeCommand); err != nil {
Expand All @@ -90,7 +97,7 @@ func bootstrapMagento2() error {
if IsDebug() {
composeCommand = append(baseCommand,
fmt.Sprintf(
`composer create-project `+
composerCommand+` create-project `+
`-vvv --profile --no-install `+
`--repository-url=https://repo.magento.com/ `+
`magento/project-%v-edition=%v /tmp/magento-tmp/`,
Expand All @@ -100,7 +107,7 @@ func bootstrapMagento2() error {
} else {
composeCommand = append(baseCommand,
fmt.Sprintf(
`composer create-project `+
composerCommand+` create-project `+
`--verbose --profile --no-install `+
`--repository-url=https://repo.magento.com/ `+
`magento/project-%v-edition=%v /tmp/magento-tmp/`,
Expand Down Expand Up @@ -128,9 +135,9 @@ func bootstrapMagento2() error {
}

if IsDebug() {
composeCommand = append(baseCommand, `composer install -vvv --profile`)
composeCommand = append(baseCommand, composerCommand+` install -vvv --profile`)
} else {
composeCommand = append(baseCommand, `composer install -v --profile`)
composeCommand = append(baseCommand, composerCommand+` install -v --profile`)
}

if err := EnvCmd(composeCommand); err != nil {
Expand All @@ -139,9 +146,9 @@ func bootstrapMagento2() error {

if !IsNoParallel() {
if IsDebug() {
composeCommand = append(baseCommand, `composer global remove hirak/prestissimo -vvv --profile`)
composeCommand = append(baseCommand, composerCommand+` global remove hirak/prestissimo -vvv --profile`)
} else {
composeCommand = append(baseCommand, `composer global remove hirak/prestissimo --verbose --profile`)
composeCommand = append(baseCommand, composerCommand+` global remove hirak/prestissimo --verbose --profile`)
}

if err := EnvCmd(composeCommand); err != nil {
Expand Down Expand Up @@ -222,7 +229,7 @@ func bootstrapMagento2() error {
}

magentoCmdParams = []string{
fmt.Sprintf("-q --lock-env web/unsecure/base_url http://%v/", GetTraefikFullDomain()),
fmt.Sprintf("-q web/unsecure/base_url http://%v/", GetTraefikFullDomain()),
}
composeCommand = append(baseCommand, `bin/magento config:set `+strings.Join(magentoCmdParams, " "))

Expand All @@ -231,7 +238,7 @@ func bootstrapMagento2() error {
}

magentoCmdParams = []string{
fmt.Sprintf("-q --lock-env web/secure/base_url https://%v/", GetTraefikFullDomain()),
fmt.Sprintf("-q web/secure/base_url https://%v/", GetTraefikFullDomain()),
}
composeCommand = append(baseCommand, `bin/magento config:set `+strings.Join(magentoCmdParams, " "))

Expand Down Expand Up @@ -356,17 +363,30 @@ func bootstrapMagento2() error {
return err
}

// TODO: Generate admin password
// Disable MFA for local development.
minimumMagentoVersionForMFA, _ := version.NewVersion("2.4.0")
if GetMagentoVersion().GreaterThan(minimumMagentoVersionForMFA) && IsMagentoDisableTFA() {
magentoCommand = append(baseCommand, `bin/magento module:disable Magento_TwoFactorAuth`)
if err := EnvCmd(magentoCommand); err != nil {
return err
}
}

adminPassword, err := password.Generate(16, 2, 0, false, false)
if err != nil {
return err
}

magentoCmdParams = []string{
"--admin-password=admin123",
"--admin-password=" + adminPassword,
"--admin-user=localadmin",
"--admin-firstname=Local",
"--admin-lastname=Admin",
`--admin-email="[email protected]"`,
}
magentoCommand = append(baseCommand, `bin/magento admin:user:create `+strings.Join(magentoCmdParams, " "))

if err := EnvCmd(magentoCommand); err != nil {
if err = EnvCmd(magentoCommand); err != nil {
return err
}

Expand Down Expand Up @@ -416,6 +436,9 @@ func bootstrapMagento2() error {
}

log.Println("Base Url: https://" + GetTraefikFullDomain())
log.Println("Backend Url: https://" + GetTraefikFullDomain() + "/" + GetMagentoBackendFrontname())
log.Println("Admin user: localadmin")
log.Println("Admin password: " + adminPassword)
log.Println("Installation finished successfully.")

return nil
Expand Down Expand Up @@ -564,13 +587,36 @@ func bootstrapMagento1() error {
return err
}

adminPassword, err := password.Generate(16, 2, 0, false, false)
if err != nil {
return err
}

magentoCmdParams := []string{
"localadmin", // username
`[email protected]`, // email
adminPassword, // password
"Local", // firstname
"Admin", // lastname
}
magerunCommand = append(baseCommand, `/usr/bin/n98-magerun admin:user:create `+strings.Join(magentoCmdParams, " "))

if err = EnvCmd(magerunCommand); err != nil {
return err
}

magerunCommand = append(baseCommand, `/usr/bin/n98-magerun cache:flush`)

if err := EnvCmd(magerunCommand); err != nil {
return err
}

log.Println("Base Url: https://" + GetTraefikFullDomain())
log.Println("Backend Url: https://" + GetTraefikFullDomain() + "/" + GetMagentoBackendFrontname())
log.Println("Admin user: localadmin")
log.Println("Admin password: " + adminPassword)
log.Println("Installation finished successfully.")

log.Println("Installation finished successfully.")

return nil
Expand Down Expand Up @@ -714,6 +760,14 @@ func IsWithSampleData() bool {
return false
}

func IsMagentoDisableTFA() bool {
if viper.IsSet(AppName + "_magento_disable_tfa") {
return viper.GetBool(AppName + "_magento_disable_tfa")
}

return false
}

func GetMagentoType() string {
if viper.IsSet(AppName + "_magento_type") {
if viper.GetString(AppName+"_magento_type") == "enterprise" ||
Expand Down

0 comments on commit 51d8670

Please sign in to comment.