Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
cd5f7611a9 | |||
b542711078 | |||
52dbc353e5 | |||
6bef0c3e5b | |||
4d705d78fb |
3
.changes/unreleased/Added-20250313-232320.yaml
Normal file
3
.changes/unreleased/Added-20250313-232320.yaml
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
kind: Added
|
||||||
|
body: 'Notifications: http service added'
|
||||||
|
time: 2025-03-13T23:23:20.130625927-05:00
|
3
.changes/unreleased/Changed-20250313-224840.yaml
Normal file
3
.changes/unreleased/Changed-20250313-224840.yaml
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
kind: Changed
|
||||||
|
body: 'vault: initialize vault before validating config'
|
||||||
|
time: 2025-03-13T22:48:40.584581357-05:00
|
@ -5,7 +5,7 @@ steps:
|
|||||||
- go build
|
- go build
|
||||||
- go test
|
- go test
|
||||||
release:
|
release:
|
||||||
image: golangci/golangci-lint:v1.53.3
|
image: golangci/golangci-lint:v1.64.7
|
||||||
commands:
|
commands:
|
||||||
- golangci-lint run -v --timeout 5m
|
- golangci-lint run -v --timeout 5m
|
||||||
|
|
||||||
|
4
go.mod
4
go.mod
@ -2,9 +2,7 @@ module git.andrewnw.xyz/CyberShell/backy
|
|||||||
|
|
||||||
go 1.23
|
go 1.23
|
||||||
|
|
||||||
toolchain go1.23.6
|
toolchain go1.23.7
|
||||||
|
|
||||||
replace git.andrewnw.xyz/CyberShell/backy => /home/andrew/Projects/backy
|
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/aws/aws-sdk-go-v2/service/s3 v1.76.0
|
github.com/aws/aws-sdk-go-v2/service/s3 v1.76.0
|
||||||
|
@ -217,7 +217,13 @@ func (command *Command) RunCmd(cmdCtxLogger zerolog.Logger, opts *ConfigOpts) ([
|
|||||||
command.UserHome = strings.TrimSpace(string(userHome))
|
command.UserHome = strings.TrimSpace(string(userHome))
|
||||||
userSshDir := fmt.Sprintf("%s/.ssh", command.UserHome)
|
userSshDir := fmt.Sprintf("%s/.ssh", command.UserHome)
|
||||||
|
|
||||||
os.MkdirAll(userSshDir, 0700)
|
if _, err := os.Stat(userSshDir); os.IsNotExist(err) {
|
||||||
|
err := os.MkdirAll(userSshDir, 0700)
|
||||||
|
if err != nil {
|
||||||
|
return collectOutput(&cmdOutBuf, command.Name, cmdCtxLogger, command.OutputToLog), fmt.Errorf("error creating directory %s %v", userSshDir, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if _, err := os.Stat(fmt.Sprintf("%s/authorized_keys", userSshDir)); os.IsNotExist(err) {
|
if _, err := os.Stat(fmt.Sprintf("%s/authorized_keys", userSshDir)); os.IsNotExist(err) {
|
||||||
_, err := os.Create(fmt.Sprintf("%s/authorized_keys", userSshDir))
|
_, err := os.Create(fmt.Sprintf("%s/authorized_keys", userSshDir))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -457,7 +463,7 @@ func (cmd *Command) ExecuteHooks(hookType string, opts *ConfigOpts) {
|
|||||||
cmdLogger := opts.Logger.With().
|
cmdLogger := opts.Logger.With().
|
||||||
Str("backy-cmd", v).Str("hookType", "error").
|
Str("backy-cmd", v).Str("hookType", "error").
|
||||||
Logger()
|
Logger()
|
||||||
errCmd.RunCmd(cmdLogger, opts)
|
_, _ = errCmd.RunCmd(cmdLogger, opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
case "success":
|
case "success":
|
||||||
@ -467,7 +473,7 @@ func (cmd *Command) ExecuteHooks(hookType string, opts *ConfigOpts) {
|
|||||||
cmdLogger := opts.Logger.With().
|
cmdLogger := opts.Logger.With().
|
||||||
Str("backy-cmd", v).Str("hookType", "success").
|
Str("backy-cmd", v).Str("hookType", "success").
|
||||||
Logger()
|
Logger()
|
||||||
successCmd.RunCmd(cmdLogger, opts)
|
_, _ = successCmd.RunCmd(cmdLogger, opts)
|
||||||
}
|
}
|
||||||
case "final":
|
case "final":
|
||||||
for _, v := range cmd.Hooks.Final {
|
for _, v := range cmd.Hooks.Final {
|
||||||
@ -476,7 +482,7 @@ func (cmd *Command) ExecuteHooks(hookType string, opts *ConfigOpts) {
|
|||||||
cmdLogger := opts.Logger.With().
|
cmdLogger := opts.Logger.With().
|
||||||
Str("backy-cmd", v).Str("hookType", "final").
|
Str("backy-cmd", v).Str("hookType", "final").
|
||||||
Logger()
|
Logger()
|
||||||
finalCmd.RunCmd(cmdLogger, opts)
|
_, _ = finalCmd.RunCmd(cmdLogger, opts)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -125,6 +125,10 @@ func (opts *ConfigOpts) ReadConfig() *ConfigOpts {
|
|||||||
|
|
||||||
log.Info().Str("config file", opts.ConfigFilePath).Send()
|
log.Info().Str("config file", opts.ConfigFilePath).Send()
|
||||||
|
|
||||||
|
if err := opts.setupVault(); err != nil {
|
||||||
|
log.Err(err).Send()
|
||||||
|
}
|
||||||
|
|
||||||
unmarshalConfig(backyKoanf, "commands", &opts.Cmds, opts.Logger)
|
unmarshalConfig(backyKoanf, "commands", &opts.Cmds, opts.Logger)
|
||||||
|
|
||||||
getCommandEnvironments(opts)
|
getCommandEnvironments(opts)
|
||||||
@ -153,10 +157,6 @@ func (opts *ConfigOpts) ReadConfig() *ConfigOpts {
|
|||||||
|
|
||||||
opts.SetupNotify()
|
opts.SetupNotify()
|
||||||
|
|
||||||
if err := opts.setupVault(); err != nil {
|
|
||||||
log.Err(err).Send()
|
|
||||||
}
|
|
||||||
|
|
||||||
return opts
|
return opts
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -220,6 +220,7 @@ func setLoggingOptions(k *koanf.Koanf, opts *ConfigOpts) {
|
|||||||
logFile = k.String(getLoggingKeyFromConfig("file"))
|
logFile = k.String(getLoggingKeyFromConfig("file"))
|
||||||
opts.LogFilePath = logFile
|
opts.LogFilePath = logFile
|
||||||
}
|
}
|
||||||
|
opts.LogFilePath = logFile
|
||||||
|
|
||||||
zerolog.SetGlobalLevel(zerolog.InfoLevel)
|
zerolog.SetGlobalLevel(zerolog.InfoLevel)
|
||||||
if isLoggingVerbose {
|
if isLoggingVerbose {
|
||||||
|
@ -9,6 +9,7 @@ import (
|
|||||||
|
|
||||||
"git.andrewnw.xyz/CyberShell/backy/pkg/logging"
|
"git.andrewnw.xyz/CyberShell/backy/pkg/logging"
|
||||||
"github.com/nikoksr/notify"
|
"github.com/nikoksr/notify"
|
||||||
|
"github.com/nikoksr/notify/service/http"
|
||||||
"github.com/nikoksr/notify/service/mail"
|
"github.com/nikoksr/notify/service/mail"
|
||||||
"github.com/nikoksr/notify/service/matrix"
|
"github.com/nikoksr/notify/service/matrix"
|
||||||
"maunium.net/go/mautrix/id"
|
"maunium.net/go/mautrix/id"
|
||||||
@ -30,6 +31,12 @@ type MailConfig struct {
|
|||||||
Password string `yaml:"password"`
|
Password string `yaml:"password"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type HttpConfig struct {
|
||||||
|
URL string `yaml:"url"`
|
||||||
|
Method string `yaml:"method"`
|
||||||
|
Headers map[string][]string `yaml:"headers"`
|
||||||
|
}
|
||||||
|
|
||||||
// SetupNotify sets up notify instances for each command list.
|
// SetupNotify sets up notify instances for each command list.
|
||||||
func (opts *ConfigOpts) SetupNotify() {
|
func (opts *ConfigOpts) SetupNotify() {
|
||||||
|
|
||||||
@ -59,6 +66,7 @@ func (opts *ConfigOpts) SetupNotify() {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
conf.Password = getExternalConfigDirectiveValue(conf.Password, opts)
|
conf.Password = getExternalConfigDirectiveValue(conf.Password, opts)
|
||||||
|
opts.Logger.Debug().Str("list", confName).Str("id", confId).Msg("adding mail notification service")
|
||||||
mailConf := setupMail(conf)
|
mailConf := setupMail(conf)
|
||||||
services = append(services, mailConf)
|
services = append(services, mailConf)
|
||||||
case "matrix":
|
case "matrix":
|
||||||
@ -68,13 +76,22 @@ func (opts *ConfigOpts) SetupNotify() {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
conf.AccessToken = getExternalConfigDirectiveValue(conf.AccessToken, opts)
|
conf.AccessToken = getExternalConfigDirectiveValue(conf.AccessToken, opts)
|
||||||
|
opts.Logger.Debug().Str("list", confName).Str("id", confId).Msg("adding matrix notification service")
|
||||||
mtrxConf, mtrxErr := setupMatrix(conf)
|
mtrxConf, mtrxErr := setupMatrix(conf)
|
||||||
if mtrxErr != nil {
|
if mtrxErr != nil {
|
||||||
opts.Logger.Info().Str("list", confName).Err(fmt.Errorf("error: configuring matrix id %s failed during setup: %w", id, mtrxErr))
|
opts.Logger.Info().Str("list", confName).Err(fmt.Errorf("error: configuring matrix id %s failed during setup: %w", id, mtrxErr))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
services = append(services, mtrxConf)
|
services = append(services, mtrxConf)
|
||||||
|
case "http":
|
||||||
|
conf, ok := opts.NotificationConf.HttpConfig[confId]
|
||||||
|
if !ok {
|
||||||
|
opts.Logger.Info().Err(fmt.Errorf("error: ID %s not found in http object", confId)).Str("list", confName).Send()
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
opts.Logger.Debug().Str("list", confName).Str("id", confId).Msg("adding http notification service")
|
||||||
|
httpConf := setupHttp(conf)
|
||||||
|
services = append(services, httpConf)
|
||||||
default:
|
default:
|
||||||
opts.Logger.Info().Err(fmt.Errorf("id %s not found", id)).Str("list", confName).Send()
|
opts.Logger.Info().Err(fmt.Errorf("id %s not found", id)).Str("list", confName).Send()
|
||||||
}
|
}
|
||||||
@ -100,3 +117,19 @@ func setupMail(config MailConfig) *mail.Mail {
|
|||||||
mailClient.BodyFormat(mail.PlainText)
|
mailClient.BodyFormat(mail.PlainText)
|
||||||
return mailClient
|
return mailClient
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func setupHttp(httpConf HttpConfig) *http.Service {
|
||||||
|
|
||||||
|
httpService := http.New()
|
||||||
|
httpService.AddReceivers(&http.Webhook{
|
||||||
|
URL: httpConf.URL,
|
||||||
|
Header: httpConf.Headers,
|
||||||
|
ContentType: "text/plain",
|
||||||
|
Method: httpConf.Method,
|
||||||
|
BuildPayload: func(subject, message string) (payload any) {
|
||||||
|
return subject + "\n\n" + message
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
return httpService
|
||||||
|
}
|
||||||
|
@ -523,12 +523,19 @@ func (command *Command) RunCmdSSH(cmdCtxLogger zerolog.Logger, opts *ConfigOpts)
|
|||||||
return collectOutput(&cmdOutBuf, command.Name, cmdCtxLogger, command.OutputToLog), fmt.Errorf("error creating file /tmp/%s: %v", uuidFile.String(), passFileErr)
|
return collectOutput(&cmdOutBuf, command.Name, cmdCtxLogger, command.OutputToLog), fmt.Errorf("error creating file /tmp/%s: %v", uuidFile.String(), passFileErr)
|
||||||
}
|
}
|
||||||
|
|
||||||
passFile.Write([]byte(userNamePass))
|
_, err = passFile.Write([]byte(userNamePass))
|
||||||
|
if err != nil {
|
||||||
|
return collectOutput(&cmdOutBuf, command.Name, cmdCtxLogger, command.OutputToLog), fmt.Errorf("error writing to file /tmp/%s: %v", uuidFile.String(), err)
|
||||||
|
}
|
||||||
|
|
||||||
ArgsStr = fmt.Sprintf("cat %s | chpasswd", passFilePath)
|
ArgsStr = fmt.Sprintf("cat %s | chpasswd", passFilePath)
|
||||||
defer passFile.Close()
|
defer passFile.Close()
|
||||||
|
|
||||||
defer client.Remove(passFilePath)
|
rmFileFunc := func() {
|
||||||
|
_ = client.Remove(passFilePath)
|
||||||
|
}
|
||||||
|
|
||||||
|
defer rmFileFunc()
|
||||||
// commandSession.Stdin = command.stdin
|
// commandSession.Stdin = command.stdin
|
||||||
}
|
}
|
||||||
if err := commandSession.Run(ArgsStr); err != nil {
|
if err := commandSession.Run(ArgsStr); err != nil {
|
||||||
@ -561,7 +568,10 @@ func (command *Command) RunCmdSSH(cmdCtxLogger zerolog.Logger, opts *ConfigOpts)
|
|||||||
return collectOutput(&cmdOutBuf, command.Name, cmdCtxLogger, command.OutputToLog), fmt.Errorf("error creating sftp client: %v", err)
|
return collectOutput(&cmdOutBuf, command.Name, cmdCtxLogger, command.OutputToLog), fmt.Errorf("error creating sftp client: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
client.MkdirAll(userSshDir)
|
err = client.MkdirAll(userSshDir)
|
||||||
|
if err != nil {
|
||||||
|
return collectOutput(&cmdOutBuf, command.Name, cmdCtxLogger, command.OutputToLog), fmt.Errorf("error creating directory %s: %v", userSshDir, err)
|
||||||
|
}
|
||||||
_, err = client.Create(fmt.Sprintf("%s/authorized_keys", userSshDir))
|
_, err = client.Create(fmt.Sprintf("%s/authorized_keys", userSshDir))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return collectOutput(&cmdOutBuf, command.Name, cmdCtxLogger, command.OutputToLog), fmt.Errorf("error opening file %s/authorized_keys: %v", userSshDir, err)
|
return collectOutput(&cmdOutBuf, command.Name, cmdCtxLogger, command.OutputToLog), fmt.Errorf("error opening file %s/authorized_keys: %v", userSshDir, err)
|
||||||
|
@ -239,6 +239,7 @@ type (
|
|||||||
Notifications struct {
|
Notifications struct {
|
||||||
MailConfig map[string]MailConfig `yaml:"mail,omitempty"`
|
MailConfig map[string]MailConfig `yaml:"mail,omitempty"`
|
||||||
MatrixConfig map[string]MatrixStruct `yaml:"matrix,omitempty"`
|
MatrixConfig map[string]MatrixStruct `yaml:"matrix,omitempty"`
|
||||||
|
HttpConfig map[string]HttpConfig `yaml:"http,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
CmdOutput struct {
|
CmdOutput struct {
|
||||||
|
@ -110,7 +110,11 @@ func injectEnvIntoSSH(envVarsToInject environmentVars, process *ssh.Session, opt
|
|||||||
goto errEnvFile
|
goto errEnvFile
|
||||||
}
|
}
|
||||||
for key, val := range envMap {
|
for key, val := range envMap {
|
||||||
process.Setenv(key, GetVaultKey(val, opts, log))
|
err = process.Setenv(key, GetVaultKey(val, opts, log))
|
||||||
|
if err != nil {
|
||||||
|
log.Error().Err(err).Send()
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -121,7 +125,11 @@ errEnvFile:
|
|||||||
if strings.Contains(envVal, "=") {
|
if strings.Contains(envVal, "=") {
|
||||||
envVarArr := strings.Split(envVal, "=")
|
envVarArr := strings.Split(envVal, "=")
|
||||||
|
|
||||||
process.Setenv(envVarArr[0], getExternalConfigDirectiveValue(envVarArr[1], opts))
|
err := process.Setenv(envVarArr[0], getExternalConfigDirectiveValue(envVarArr[1], opts))
|
||||||
|
if err != nil {
|
||||||
|
log.Error().Err(err).Send()
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -385,7 +393,6 @@ func getExternalConfigDirectiveValue(key string, opts *ConfigOpts) string {
|
|||||||
key = strings.TrimSuffix(key, externDirectiveEnd)
|
key = strings.TrimSuffix(key, externDirectiveEnd)
|
||||||
key = GetVaultKey(key, opts, opts.Logger)
|
key = GetVaultKey(key, opts, opts.Logger)
|
||||||
}
|
}
|
||||||
println(key)
|
|
||||||
|
|
||||||
return key
|
return key
|
||||||
}
|
}
|
||||||
@ -410,7 +417,6 @@ func getVaultSecret(vaultClient *vault.Client, key *VaultKey) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
value, ok := secret.Data[key.Key].(string)
|
value, ok := secret.Data[key.Key].(string)
|
||||||
println(value)
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return "", fmt.Errorf("value type assertion failed for vault key %s: %T %#v", key.Name, secret.Data[key.Name], secret.Data[key.Name])
|
return "", fmt.Errorf("value type assertion failed for vault key %s: %T %#v", key.Name, secret.Data[key.Name], secret.Data[key.Name])
|
||||||
}
|
}
|
||||||
|
@ -116,7 +116,7 @@ func (c *Cache) Set(source, hash string, data []byte, dataType string) (CacheDat
|
|||||||
path := filepath.Join(c.dir, fmt.Sprintf("%s-%s", fileName, sourceHash))
|
path := filepath.Join(c.dir, fmt.Sprintf("%s-%s", fileName, sourceHash))
|
||||||
|
|
||||||
if _, err := os.Stat(path); os.IsNotExist(err) {
|
if _, err := os.Stat(path); os.IsNotExist(err) {
|
||||||
os.MkdirAll(c.dir, 0700)
|
_ = os.MkdirAll(c.dir, 0700)
|
||||||
}
|
}
|
||||||
|
|
||||||
err := os.WriteFile(path, data, 0644)
|
err := os.WriteFile(path, data, 0644)
|
||||||
@ -171,7 +171,7 @@ func (cf *CachedFetcher) Hash(data []byte) string {
|
|||||||
func LoadMetadataFromFile(filePath string) ([]*CacheData, error) {
|
func LoadMetadataFromFile(filePath string) ([]*CacheData, error) {
|
||||||
if _, err := os.Stat(filePath); os.IsNotExist(err) {
|
if _, err := os.Stat(filePath); os.IsNotExist(err) {
|
||||||
// Create the file if it does not exist
|
// Create the file if it does not exist
|
||||||
os.MkdirAll(path.Dir(filePath), 0700)
|
_ = os.MkdirAll(path.Dir(filePath), 0700)
|
||||||
emptyData := []byte("[]")
|
emptyData := []byte("[]")
|
||||||
err := os.WriteFile(filePath, emptyData, 0644)
|
err := os.WriteFile(filePath, emptyData, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user