From bd763673c84561b7656f1099ecbc3ecd91645f37 Mon Sep 17 00:00:00 2001 From: Krystian Panek Date: Wed, 8 Feb 2023 15:22:38 +0100 Subject: [PATCH] Instance password validation #44 --- pkg/local_instance.go | 21 +++++++++++++++++++++ pkg/local_instance_manager.go | 5 ----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/pkg/local_instance.go b/pkg/local_instance.go index b4f9b5f2..8a4d6168 100644 --- a/pkg/local_instance.go +++ b/pkg/local_instance.go @@ -14,6 +14,7 @@ import ( "github.com/wttech/aemc/pkg/instance" "os" "os/exec" + "regexp" "sort" "strconv" "strings" @@ -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) @@ -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 } @@ -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 { diff --git a/pkg/local_instance_manager.go b/pkg/local_instance_manager.go index 201afb37..dbd80ae3 100644 --- a/pkg/local_instance_manager.go +++ b/pkg/local_instance_manager.go @@ -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() { @@ -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() {