From 143e839e78118ca07cc1b19f6210e4aea5e70546 Mon Sep 17 00:00:00 2001 From: schulze Date: Tue, 18 Apr 2023 11:08:31 +0200 Subject: [PATCH] loop MTD, checks before doing mtd to service --- main.go | 15 +++++++++++---- mtdaws/mtd.go | 20 ++++++++++++++++++++ state/config.go | 2 ++ 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 774efbd..f4f9c15 100644 --- a/main.go +++ b/main.go @@ -28,11 +28,18 @@ func main() { config = indexAllInstances(config) state.SaveConf(ConfigPath, config) - //TODO: figure out migration (MTD) - config = movingTargetDefense(config) - state.SaveConf(ConfigPath, config) + // START DOING MTD + mtdLoop(config) +} - //TODO: proxy commands +func mtdLoop(config state.Config) { + for true { + //TODO: figure out migration (MTD) + config = movingTargetDefense(config) + state.SaveConf(ConfigPath, config) + + //TODO: proxy commands + } } func movingTargetDefense(config state.Config) state.Config{ diff --git a/mtdaws/mtd.go b/mtdaws/mtd.go index 8e428ae..7811a34 100644 --- a/mtdaws/mtd.go +++ b/mtdaws/mtd.go @@ -7,6 +7,7 @@ import ( "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/ec2" + "github.com/aws/aws-sdk-go-v2/service/ec2/types" "github.com/google/uuid" "github.com/thefeli73/polemos/state" ) @@ -37,6 +38,12 @@ func AWSUpdateService(config state.Config, region string, service state.CustomUU return config } + +// isInstanceRunning returns if an instance is running (true=running) +func isInstanceRunning(instance *types.Instance) bool { + return instance.State.Name == types.InstanceStateNameRunning +} + // AWSMoveInstance moves a specified instance to a new availability region func AWSMoveInstance(config state.Config) (state.Config) { @@ -61,6 +68,19 @@ func AWSMoveInstance(config state.Config) (state.Config) { return config } + if !isInstanceRunning(realInstance) { + fmt.Println("Error, Instance is not running!") + return config + } + if instance.AdminDisabled { + fmt.Println("Error, Service is Disabled!") + return config + } + if instance.Inactive { + fmt.Println("Error, Service is Inactive!") + return config + } + imageName, err := createImage(svc, instanceID) if err != nil { fmt.Println("Error creating image:\t", err) diff --git a/state/config.go b/state/config.go index 395f9a0..e96bfdb 100644 --- a/state/config.go +++ b/state/config.go @@ -24,6 +24,8 @@ type mtdconf struct { // Service contains all necessary information about a service to identify it in the cloud as well as configuring a proxy for it type Service struct { CloudID string `yaml:"cloud_id"` + AdminDisabled bool `yaml:"admin_disabled"` + Inactive bool `yaml:"inactive"` EntryIP netip.Addr `yaml:"entry_ip"` EntryPort uint16 `yaml:"entry_port"` ServiceIP netip.Addr `yaml:"service_ip"`