Skip to content

Commit

Permalink
Merge pull request #59 from wttech/instance-pwd-validation
Browse files Browse the repository at this point in the history
Instance password validation
  • Loading branch information
krystian-panek-vmltech authored Feb 8, 2023
2 parents 54745ec + bd76367 commit 14001d9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
21 changes: 21 additions & 0 deletions pkg/local_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/wttech/aemc/pkg/instance"
"os"
"os/exec"
"regexp"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -143,7 +144,21 @@ func (li LocalInstance) LicenseFile() string {
return pathx.Canonical(li.Dir() + "/" + LicenseFilename)
}

var (
LocalInstancePasswordRegex = regexp.MustCompile("^[a-zA-Z0-9_]{5,}$")
)

func (li LocalInstance) Validate() error {
if !LocalInstancePasswordRegex.MatchString(li.instance.password) {
return fmt.Errorf("password for instance '%s' need to match regex '%s'", li.instance.ID(), LocalInstancePasswordRegex)
}
return nil
}

func (li LocalInstance) Create() error {
if err := li.Validate(); err != nil {
return err
}
log.Infof("creating instance '%s'", li.instance.ID())
if err := pathx.DeleteIfExists(li.Dir()); err != nil {
return fmt.Errorf("cannot clean up dir for instance '%s': %w", li.instance.ID(), err)
Expand Down Expand Up @@ -262,6 +277,9 @@ func (li LocalInstance) Start() error {
if !li.IsCreated() {
return fmt.Errorf("cannot start instance '%s' as it is not created", li.instance.ID())
}
if err := li.Validate(); err != nil {
return err
}
if err := li.update(); err != nil {
return err
}
Expand Down Expand Up @@ -441,6 +459,9 @@ func (li LocalInstance) Stop() error {
if !li.IsCreated() {
return fmt.Errorf("cannot stop instance as it is not created")
}
if err := li.Validate(); err != nil {
return err
}
log.Infof("stopping instance '%s'", li.instance.ID())
cmd, err := li.binScriptCommand(LocalInstanceScriptStop, true)
if err != nil {
Expand Down
5 changes: 0 additions & 5 deletions pkg/local_instance_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,10 +324,7 @@ func (im *InstanceManager) Delete(instances []Instance) ([]Instance, error) {
log.Debugf("no instances to delete")
return []Instance{}, nil
}
// im.LocalValidate()

log.Infof("deleting instance(s) '%s'", InstanceIds(instances))

deleted := []Instance{}
for _, i := range instances {
if i.local.IsCreated() {
Expand All @@ -347,9 +344,7 @@ func (im *InstanceManager) Clean(instances []Instance) ([]Instance, error) {
log.Debugf("no instances to clean")
return []Instance{}, nil
}

log.Infof("cleaning instance(s) '%s'", InstanceIds(instances))

cleaned := []Instance{}
for _, i := range instances {
if !i.local.IsRunning() {
Expand Down

0 comments on commit 14001d9

Please sign in to comment.