Compare commits

..

No commits in common. "develop" and "v0.10.1" have entirely different histories.

11 changed files with 22 additions and 83 deletions

View File

@ -1,3 +0,0 @@
kind: Added
body: 'Notifications: http service added'
time: 2025-03-13T23:23:20.130625927-05:00

View File

@ -1,3 +0,0 @@
kind: Changed
body: 'vault: initialize vault before validating config'
time: 2025-03-13T22:48:40.584581357-05:00

View File

@ -5,7 +5,7 @@ steps:
- go build
- go test
release:
image: golangci/golangci-lint:v1.64.7
image: golangci/golangci-lint:v1.53.3
commands:
- golangci-lint run -v --timeout 5m

4
go.mod
View File

@ -2,7 +2,9 @@ module git.andrewnw.xyz/CyberShell/backy
go 1.23
toolchain go1.23.7
toolchain go1.23.6
replace git.andrewnw.xyz/CyberShell/backy => /home/andrew/Projects/backy
require (
github.com/aws/aws-sdk-go-v2/service/s3 v1.76.0

View File

@ -217,13 +217,7 @@ func (command *Command) RunCmd(cmdCtxLogger zerolog.Logger, opts *ConfigOpts) ([
command.UserHome = strings.TrimSpace(string(userHome))
userSshDir := fmt.Sprintf("%s/.ssh", command.UserHome)
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)
}
}
os.MkdirAll(userSshDir, 0700)
if _, err := os.Stat(fmt.Sprintf("%s/authorized_keys", userSshDir)); os.IsNotExist(err) {
_, err := os.Create(fmt.Sprintf("%s/authorized_keys", userSshDir))
if err != nil {
@ -463,7 +457,7 @@ func (cmd *Command) ExecuteHooks(hookType string, opts *ConfigOpts) {
cmdLogger := opts.Logger.With().
Str("backy-cmd", v).Str("hookType", "error").
Logger()
_, _ = errCmd.RunCmd(cmdLogger, opts)
errCmd.RunCmd(cmdLogger, opts)
}
case "success":
@ -473,7 +467,7 @@ func (cmd *Command) ExecuteHooks(hookType string, opts *ConfigOpts) {
cmdLogger := opts.Logger.With().
Str("backy-cmd", v).Str("hookType", "success").
Logger()
_, _ = successCmd.RunCmd(cmdLogger, opts)
successCmd.RunCmd(cmdLogger, opts)
}
case "final":
for _, v := range cmd.Hooks.Final {
@ -482,7 +476,7 @@ func (cmd *Command) ExecuteHooks(hookType string, opts *ConfigOpts) {
cmdLogger := opts.Logger.With().
Str("backy-cmd", v).Str("hookType", "final").
Logger()
_, _ = finalCmd.RunCmd(cmdLogger, opts)
finalCmd.RunCmd(cmdLogger, opts)
}
}
}

View File

@ -125,10 +125,6 @@ func (opts *ConfigOpts) ReadConfig() *ConfigOpts {
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)
getCommandEnvironments(opts)
@ -157,6 +153,10 @@ func (opts *ConfigOpts) ReadConfig() *ConfigOpts {
opts.SetupNotify()
if err := opts.setupVault(); err != nil {
log.Err(err).Send()
}
return opts
}
@ -220,7 +220,6 @@ func setLoggingOptions(k *koanf.Koanf, opts *ConfigOpts) {
logFile = k.String(getLoggingKeyFromConfig("file"))
opts.LogFilePath = logFile
}
opts.LogFilePath = logFile
zerolog.SetGlobalLevel(zerolog.InfoLevel)
if isLoggingVerbose {

View File

@ -9,7 +9,6 @@ import (
"git.andrewnw.xyz/CyberShell/backy/pkg/logging"
"github.com/nikoksr/notify"
"github.com/nikoksr/notify/service/http"
"github.com/nikoksr/notify/service/mail"
"github.com/nikoksr/notify/service/matrix"
"maunium.net/go/mautrix/id"
@ -31,12 +30,6 @@ type MailConfig struct {
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.
func (opts *ConfigOpts) SetupNotify() {
@ -66,7 +59,6 @@ func (opts *ConfigOpts) SetupNotify() {
continue
}
conf.Password = getExternalConfigDirectiveValue(conf.Password, opts)
opts.Logger.Debug().Str("list", confName).Str("id", confId).Msg("adding mail notification service")
mailConf := setupMail(conf)
services = append(services, mailConf)
case "matrix":
@ -76,22 +68,13 @@ func (opts *ConfigOpts) SetupNotify() {
continue
}
conf.AccessToken = getExternalConfigDirectiveValue(conf.AccessToken, opts)
opts.Logger.Debug().Str("list", confName).Str("id", confId).Msg("adding matrix notification service")
mtrxConf, mtrxErr := setupMatrix(conf)
if mtrxErr != nil {
opts.Logger.Info().Str("list", confName).Err(fmt.Errorf("error: configuring matrix id %s failed during setup: %w", id, mtrxErr))
continue
}
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:
opts.Logger.Info().Err(fmt.Errorf("id %s not found", id)).Str("list", confName).Send()
}
@ -117,19 +100,3 @@ func setupMail(config MailConfig) *mail.Mail {
mailClient.BodyFormat(mail.PlainText)
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
}

View File

@ -523,19 +523,12 @@ 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)
}
_, 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)
}
passFile.Write([]byte(userNamePass))
ArgsStr = fmt.Sprintf("cat %s | chpasswd", passFilePath)
defer passFile.Close()
rmFileFunc := func() {
_ = client.Remove(passFilePath)
}
defer rmFileFunc()
defer client.Remove(passFilePath)
// commandSession.Stdin = command.stdin
}
if err := commandSession.Run(ArgsStr); err != nil {
@ -568,10 +561,7 @@ 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)
}
err = client.MkdirAll(userSshDir)
if err != nil {
return collectOutput(&cmdOutBuf, command.Name, cmdCtxLogger, command.OutputToLog), fmt.Errorf("error creating directory %s: %v", userSshDir, err)
}
client.MkdirAll(userSshDir)
_, err = client.Create(fmt.Sprintf("%s/authorized_keys", userSshDir))
if err != nil {
return collectOutput(&cmdOutBuf, command.Name, cmdCtxLogger, command.OutputToLog), fmt.Errorf("error opening file %s/authorized_keys: %v", userSshDir, err)

View File

@ -239,7 +239,6 @@ type (
Notifications struct {
MailConfig map[string]MailConfig `yaml:"mail,omitempty"`
MatrixConfig map[string]MatrixStruct `yaml:"matrix,omitempty"`
HttpConfig map[string]HttpConfig `yaml:"http,omitempty"`
}
CmdOutput struct {

View File

@ -110,11 +110,7 @@ func injectEnvIntoSSH(envVarsToInject environmentVars, process *ssh.Session, opt
goto errEnvFile
}
for key, val := range envMap {
err = process.Setenv(key, GetVaultKey(val, opts, log))
if err != nil {
log.Error().Err(err).Send()
}
process.Setenv(key, GetVaultKey(val, opts, log))
}
}
@ -125,11 +121,7 @@ errEnvFile:
if strings.Contains(envVal, "=") {
envVarArr := strings.Split(envVal, "=")
err := process.Setenv(envVarArr[0], getExternalConfigDirectiveValue(envVarArr[1], opts))
if err != nil {
log.Error().Err(err).Send()
}
process.Setenv(envVarArr[0], getExternalConfigDirectiveValue(envVarArr[1], opts))
}
}
}
@ -393,6 +385,7 @@ func getExternalConfigDirectiveValue(key string, opts *ConfigOpts) string {
key = strings.TrimSuffix(key, externDirectiveEnd)
key = GetVaultKey(key, opts, opts.Logger)
}
println(key)
return key
}
@ -417,6 +410,7 @@ func getVaultSecret(vaultClient *vault.Client, key *VaultKey) (string, error) {
}
value, ok := secret.Data[key.Key].(string)
println(value)
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])
}

View File

@ -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))
if _, err := os.Stat(path); os.IsNotExist(err) {
_ = os.MkdirAll(c.dir, 0700)
os.MkdirAll(c.dir, 0700)
}
err := os.WriteFile(path, data, 0644)
@ -171,7 +171,7 @@ func (cf *CachedFetcher) Hash(data []byte) string {
func LoadMetadataFromFile(filePath string) ([]*CacheData, error) {
if _, err := os.Stat(filePath); os.IsNotExist(err) {
// Create the file if it does not exist
_ = os.MkdirAll(path.Dir(filePath), 0700)
os.MkdirAll(path.Dir(filePath), 0700)
emptyData := []byte("[]")
err := os.WriteFile(filePath, emptyData, 0644)
if err != nil {