Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
52dbc353e5 | |||
6bef0c3e5b | |||
4d705d78fb |
@ -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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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 {
|
||||||
|
@ -5,10 +5,12 @@ package backy
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
stdHttp "net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"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 +32,11 @@ type MailConfig struct {
|
|||||||
Password string `yaml:"password"`
|
Password string `yaml:"password"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type HttpConfig struct {
|
||||||
|
Url string `yaml:"url"`
|
||||||
|
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() {
|
||||||
|
|
||||||
@ -74,6 +81,14 @@ func (opts *ConfigOpts) SetupNotify() {
|
|||||||
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
|
||||||
|
}
|
||||||
|
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 +115,20 @@ 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.AddReceiversURLs(httpConf.Url)
|
||||||
|
httpService.AddReceivers(&http.Webhook{
|
||||||
|
URL: httpConf.Url,
|
||||||
|
Header: httpConf.Headers,
|
||||||
|
ContentType: "text/plain",
|
||||||
|
Method: stdHttp.MethodPost,
|
||||||
|
BuildPayload: func(subject, message string) (payload any) {
|
||||||
|
return "[text/plain]: " + subject + " - " + 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()
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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