2023-02-02 10:55:24 +01:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"forge.cadoles.com/Cadoles/emissary/internal/agent"
|
2023-03-03 20:37:09 +01:00
|
|
|
"forge.cadoles.com/Cadoles/emissary/internal/agent/controller/app"
|
2023-02-02 10:55:24 +01:00
|
|
|
"forge.cadoles.com/Cadoles/emissary/internal/agent/controller/gateway"
|
|
|
|
"forge.cadoles.com/Cadoles/emissary/internal/agent/controller/openwrt"
|
|
|
|
"forge.cadoles.com/Cadoles/emissary/internal/agent/controller/persistence"
|
|
|
|
"forge.cadoles.com/Cadoles/emissary/internal/agent/controller/spec"
|
2023-03-02 13:05:24 +01:00
|
|
|
"forge.cadoles.com/Cadoles/emissary/internal/agent/metadata"
|
|
|
|
"forge.cadoles.com/Cadoles/emissary/internal/agent/metadata/collector/buildinfo"
|
|
|
|
"forge.cadoles.com/Cadoles/emissary/internal/agent/metadata/collector/shell"
|
2023-02-02 10:55:24 +01:00
|
|
|
"forge.cadoles.com/Cadoles/emissary/internal/command/common"
|
2023-03-02 13:05:24 +01:00
|
|
|
"forge.cadoles.com/Cadoles/emissary/internal/config"
|
|
|
|
"forge.cadoles.com/Cadoles/emissary/internal/jwk"
|
|
|
|
"forge.cadoles.com/Cadoles/emissary/internal/machineid"
|
2023-02-02 10:55:24 +01:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
_ "github.com/santhosh-tekuri/jsonschema/v5/httploader"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
"gitlab.com/wpetit/goweb/logger"
|
|
|
|
)
|
|
|
|
|
|
|
|
func RunCommand() *cli.Command {
|
|
|
|
flags := common.Flags()
|
|
|
|
|
|
|
|
return &cli.Command{
|
|
|
|
Name: "run",
|
|
|
|
Usage: "Run the emissary agent",
|
|
|
|
Flags: flags,
|
|
|
|
Action: func(ctx *cli.Context) error {
|
|
|
|
conf, err := common.LoadConfig(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Could not load configuration")
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.SetFormat(logger.Format(conf.Logger.Format))
|
|
|
|
logger.SetLevel(logger.Level(conf.Logger.Level))
|
|
|
|
|
|
|
|
controllers := make([]agent.Controller, 0)
|
|
|
|
|
|
|
|
ctrlConf := conf.Agent.Controllers
|
|
|
|
|
|
|
|
if ctrlConf.Persistence.Enabled {
|
|
|
|
controllers = append(controllers, persistence.NewController(string(ctrlConf.Persistence.StateFile)))
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctrlConf.Spec.Enabled {
|
2023-03-02 13:05:24 +01:00
|
|
|
controllers = append(controllers, spec.NewController())
|
2023-02-02 10:55:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ctrlConf.Gateway.Enabled {
|
|
|
|
controllers = append(controllers, gateway.NewController())
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctrlConf.UCI.Enabled {
|
|
|
|
controllers = append(controllers, openwrt.NewUCIController(
|
|
|
|
string(ctrlConf.UCI.BinPath),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2023-03-03 20:37:09 +01:00
|
|
|
if ctrlConf.App.Enabled {
|
|
|
|
controllers = append(controllers, app.NewController(
|
|
|
|
app.WithDataDir(string(ctrlConf.App.DataDir)),
|
|
|
|
app.WithDownloadDir(string(ctrlConf.App.DownloadDir)),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2023-03-02 13:05:24 +01:00
|
|
|
key, err := jwk.LoadOrGenerate(string(conf.Agent.PrivateKeyPath), jwk.DefaultKeySize)
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
thumbprint, err := machineid.Get()
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
collectors := createShellCollectors(&conf.Agent)
|
|
|
|
collectors = append(collectors, buildinfo.NewCollector())
|
|
|
|
|
2023-02-02 10:55:24 +01:00
|
|
|
agent := agent.New(
|
2023-03-02 13:05:24 +01:00
|
|
|
string(conf.Agent.ServerURL),
|
|
|
|
key,
|
|
|
|
thumbprint,
|
2023-02-02 10:55:24 +01:00
|
|
|
agent.WithInterval(time.Duration(conf.Agent.ReconciliationInterval)*time.Second),
|
|
|
|
agent.WithControllers(controllers...),
|
2023-03-02 13:05:24 +01:00
|
|
|
agent.WithCollectors(collectors...),
|
2023-02-02 10:55:24 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
if err := agent.Run(ctx.Context); err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2023-03-02 13:05:24 +01:00
|
|
|
|
|
|
|
func createShellCollectors(conf *config.AgentConfig) []metadata.Collector {
|
|
|
|
collectors := make([]metadata.Collector, 0)
|
|
|
|
|
|
|
|
for _, c := range conf.Collectors {
|
|
|
|
collector := shell.NewCollector(string(c.Name), string(c.Command), c.Args...)
|
|
|
|
|
|
|
|
collectors = append(collectors, collector)
|
|
|
|
}
|
|
|
|
|
|
|
|
return collectors
|
|
|
|
}
|