2023-03-14 15:12:28 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-03-15 15:30:29 +01:00
|
|
|
"fmt"
|
2023-03-22 14:01:08 +01:00
|
|
|
"net/netip"
|
2023-03-15 15:30:29 +01:00
|
|
|
|
2023-03-22 14:01:08 +01:00
|
|
|
"github.com/google/uuid"
|
2023-03-21 13:34:52 +01:00
|
|
|
"github.com/thefeli73/polemos/mtdaws"
|
2023-03-15 15:30:29 +01:00
|
|
|
"github.com/thefeli73/polemos/state"
|
2023-03-14 15:12:28 +01:00
|
|
|
)
|
2023-03-21 13:34:52 +01:00
|
|
|
|
2023-03-22 14:01:08 +01:00
|
|
|
// ConfigPath is a string of the location for the configfile
|
|
|
|
var ConfigPath string
|
|
|
|
|
2023-03-14 15:12:28 +01:00
|
|
|
func main() {
|
2023-03-15 15:30:29 +01:00
|
|
|
fmt.Println("Starting Polemos")
|
|
|
|
|
2023-03-22 14:01:08 +01:00
|
|
|
ConfigPath = "config.yaml"
|
|
|
|
|
|
|
|
config := state.LoadConf(ConfigPath)
|
|
|
|
state.SaveConf(ConfigPath, config)
|
|
|
|
|
2023-03-28 16:09:30 +02:00
|
|
|
config = indexAllInstances(config)
|
2023-03-15 15:30:29 +01:00
|
|
|
|
2023-03-28 16:09:30 +02:00
|
|
|
//TODO: figure out migration (MTD)
|
|
|
|
config = movingTargetDefense(config)
|
|
|
|
|
|
|
|
//TODO: proxy commands
|
2023-03-21 13:34:52 +01:00
|
|
|
}
|
2023-03-22 14:01:08 +01:00
|
|
|
|
2023-03-28 16:09:30 +02:00
|
|
|
func movingTargetDefense(config state.Config) state.Config{
|
|
|
|
|
|
|
|
mtdaws.AWSMoveInstance(config)
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
|
|
|
func indexAllInstances(config state.Config) state.Config {
|
2023-03-22 14:01:08 +01:00
|
|
|
fmt.Println("Indexing instances")
|
|
|
|
|
|
|
|
//index AWS instances
|
2023-03-28 16:09:30 +02:00
|
|
|
awsNewInstanceCounter := 0
|
|
|
|
awsInstanceCounter := 0
|
2023-03-22 14:01:08 +01:00
|
|
|
awsInstances := mtdaws.GetInstances(config)
|
|
|
|
for _, instance := range awsInstances {
|
|
|
|
cloudID := mtdaws.GetCloudID(instance)
|
|
|
|
ip, err := netip.ParseAddr(instance.PublicIP)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error converting ip:", err)
|
|
|
|
continue
|
|
|
|
}
|
2023-03-22 14:12:12 +01:00
|
|
|
newService, found := indexInstance(config, cloudID, ip)
|
|
|
|
if !found {
|
2023-03-22 15:46:58 +01:00
|
|
|
fmt.Println("New instance found:", newService.CloudID)
|
2023-03-22 14:12:12 +01:00
|
|
|
config.MTD.Services = append(config.MTD.Services, newService)
|
|
|
|
state.SaveConf(ConfigPath, config)
|
2023-03-28 16:09:30 +02:00
|
|
|
awsNewInstanceCounter++
|
2023-03-22 14:12:12 +01:00
|
|
|
}
|
2023-03-28 16:09:30 +02:00
|
|
|
awsInstanceCounter++
|
2023-03-22 14:01:08 +01:00
|
|
|
}
|
2023-03-28 16:09:30 +02:00
|
|
|
fmt.Printf("Found %d AWS instances (%d newly added)\n", awsInstanceCounter, awsNewInstanceCounter)
|
|
|
|
|
|
|
|
|
2023-03-22 14:01:08 +01:00
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
2023-03-22 14:12:12 +01:00
|
|
|
func indexInstance(config state.Config, cloudID string, serviceIP netip.Addr) (state.Service, bool) {
|
|
|
|
found := false
|
2023-03-22 14:01:08 +01:00
|
|
|
for _, service := range config.MTD.Services {
|
|
|
|
if service.CloudID == cloudID {
|
2023-03-22 14:12:12 +01:00
|
|
|
found = true
|
2023-03-22 14:01:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
u := uuid.New()
|
|
|
|
newService := state.Service{
|
|
|
|
ID: state.CustomUUID(u),
|
|
|
|
CloudID: cloudID,
|
|
|
|
ServiceIP: serviceIP}
|
2023-03-22 14:12:12 +01:00
|
|
|
return newService, found
|
2023-03-22 14:24:56 +01:00
|
|
|
}
|