Compare commits

..

No commits in common. "51f9e9a776f9a67e2d569c2788148911761bf34f" and "9c202cf3e9c799d8cae57f1950d3009babadf1a4" have entirely different histories.

15 changed files with 233 additions and 412 deletions

View File

@ -8,15 +8,10 @@ builds:
- env:
- CGO_ENABLED=0
goos:
- freebsd
- linux
goarch:
- "386"
- amd64
- arm64
archives:
- format: binary
- format: tar.gz
# this name template makes the OS and Arch compatible with the results of uname.
name_template: >-
{{ .ProjectName }}

View File

@ -14,17 +14,11 @@ You can also download binaries [here](https://git.andrewnw.xyz/CyberShell/backy/
## Features
- Allows easy configuration of executable commands
- Define lists of commands and run them
- Allows for commands to be run on many hosts over SSH
- Execute commands over SSH
- Commands can be grouped in list to run in specific order
- Notifications on completion and failure
- Run in cron mode
- For any command, especially backup commands
- More to come.
To run a config:
@ -35,11 +29,84 @@ Or to use a specific file:
If you leave the config path blank, the following paths will be searched in order:
- `./backy.yml`
- `./backy.yaml`
- `~/.config/backy.yml`
- `~/.config/backy.yaml`
Create a file at `~/.config/backy.yml`.
Create a file at `~/.config/backy.yaml`:
See the config file in the examples directory to configure it.
```yaml
commands:
stop-docker-container:
cmd: docker
Args:
- compose
- -f /some/path/to/docker-compose.yaml
- down
# if host is not defined, cmd will be run locally
host: some-host
backup-docker-container-script:
cmd: /path/to/script
# The host has to be defined in the config file
host: some-host
shell-cmd:
cmd: rsync
shell: bash
Args:
- -av some-host:/path/to/data ~/Docker/Backups/docker-data
hostname:
cmd: hostname
cmd-configs:
cmds-to-run: # this can be any name you want
# all commands have to be defined
order:
- stop-docker-container
- backup-docker-container-script
- shell-cmd
- hostname
notifications:
- matrix
name: backup-some-server
hostname:
name: hostname
order:
- hostname
notifications:
- prod-email
hosts:
some-host:
hostname: some-hostname
config: ~/.ssh/config
user: user
privatekeypath: /path/to/private/key
port: 22
password:
logging:
verbose: true
file: /path/to/logs/commands.log
console: false
cmd-std-out: false
notifications:
prod-email:
id: prod-email
type: mail
host: yourhost.tld:port
senderAddress: email@domain.tld
to:
- admin@domain.tld
username: smtp-username@domain.tld
password: your-password-here
matrix:
id: matrix
type: matrix
home-server: your-home-server.tld
room-id: room-id
access-token: your-access-token
user-id: your-user-id
```

View File

@ -25,7 +25,7 @@ var cmdLists []string
func init() {
backupCmd.Flags().StringSliceVarP(&cmdLists, "lists", "l", nil, "Accepts comma-separated names of command lists to execute.")
backupCmd.Flags().StringSliceVarP(&cmdLists, "lists", "l", nil, "Accepts a comma-separated names of command lists to execute.")
}

View File

@ -1,31 +0,0 @@
package cmd
// import (
// "git.andrewnw.xyz/CyberShell/backy/pkg/backy"
// "github.com/spf13/cobra"
// )
// var (
// configCmd = &cobra.Command{
// Use: "config list ...",
// Short: "Runs commands defined in config file.",
// Long: `Cron executes commands at the time defined in config file.`,
// Run: config,
// }
// cmds []string
// lists []string
// )
// func config(cmd *cobra.Command, args []string) {
// opts := backy.NewOpts(cfgFile, backy.UseCron())
// opts.InitConfig()
// }
// func init() {
// configCmd.PersistentFlags().StringArrayVarP(&cmds, "cmds", "c", nil, "Accepts comma-seperated list of commands to list")
// }

View File

@ -11,9 +11,6 @@ commands:
cmd: /path/to/script
# The host has to be defined in the config file
host: some-host
environment:
- FOO=BAR
- APP=$VAR
shell-cmd:
cmd: rsync
shell: bash
@ -41,19 +38,15 @@ cmd-configs:
- prod-email
hosts:
# any ssh_config(5) keys/values not listed here will be looked up in the config file or the default config file
some-host:
hostname: some-hostname
config: ~/.ssh/config
user: user
privatekeypath: /path/to/private/key
port: 22
# can also be env:VAR
password: file:/path/to/file
# only one is supported for now
proxyjump: some-proxy-host
password:
# optional
logging:
verbose: true
file: /path/to/logs/commands.log
@ -65,8 +58,7 @@ notifications:
prod-email:
id: prod-email
type: mail
host: yourhost.tld
port: 587
host: yourhost.tld:port
senderAddress: email@domain.tld
to:
- admin@domain.tld

View File

@ -11,17 +11,11 @@ import (
"io"
"os"
"os/exec"
"text/template"
"embed"
"github.com/rs/zerolog"
)
//go:embed templates/*.txt
var templates embed.FS
var requiredKeys = []string{"commands", "cmd-configs"}
var requiredKeys = []string{"commands", "cmd-configs", "logging"}
var Sprintf = fmt.Sprintf
@ -29,7 +23,7 @@ var Sprintf = fmt.Sprintf
// The environment of local commands will be the machine's environment plus any extra
// variables specified in the Env file or Environment.
// Dir can also be specified for local commands.
func (command *Command) RunCmd(log *zerolog.Logger, hosts map[string]*Host) error {
func (command *Command) RunCmd(log *zerolog.Logger) error {
var (
ArgsStr string
@ -50,12 +44,12 @@ func (command *Command) RunCmd(log *zerolog.Logger, hosts map[string]*Host) erro
if command.Host != nil {
log.Info().Str("Command", fmt.Sprintf("Running command: %s %s on host %s", command.Cmd, ArgsStr, *command.Host)).Send()
err := command.RemoteHost.ConnectToSSHHost(log, hosts)
sshc, err := command.RemoteHost.ConnectToSSHHost(log)
if err != nil {
return err
}
defer command.RemoteHost.SshClient.Close()
commandSession, err := command.RemoteHost.SshClient.NewSession()
defer sshc.Close()
commandSession, err := sshc.NewSession()
if err != nil {
log.Err(fmt.Errorf("new ssh session: %w", err)).Send()
return err
@ -164,7 +158,7 @@ func cmdListWorker(id int, jobs <-chan *CmdList, config *BackyConfigFile, result
fieldsMap["list"] = list.Name
cmdLog := config.Logger.Info()
var count int
var cmdsRan []string
var Msg string
for _, cmd := range list.Order {
currentCmd = config.Cmds[cmd].Cmd
fieldsMap["cmd"] = config.Cmds[cmd].Cmd
@ -173,22 +167,12 @@ func cmdListWorker(id int, jobs <-chan *CmdList, config *BackyConfigFile, result
cmdLogger := config.Logger.With().
Str("backy-cmd", cmd).
Logger()
runOutErr := cmdToRun.RunCmd(&cmdLogger, config.Hosts)
runOutErr := cmdToRun.RunCmd(&cmdLogger)
count++
if runOutErr != nil {
var errMsg bytes.Buffer
if list.NotifyConfig != nil {
errStruct := make(map[string]interface{})
errStruct["listName"] = list.Name
errStruct["Command"] = currentCmd
errStruct["Err"] = runOutErr
errStruct["CmdsRan"] = cmdsRan
t := template.Must(template.New("error.txt").ParseFS(templates, "templates/error.txt"))
tmpErr := t.Execute(&errMsg, errStruct)
if tmpErr != nil {
config.Logger.Err(tmpErr).Send()
}
notifySendErr := list.NotifyConfig.Send(context.Background(), fmt.Sprintf("List %s failed on command %s ", list.Name, cmd), errMsg.String())
notifySendErr := list.NotifyConfig.Send(context.Background(), fmt.Sprintf("List %s failed on command %s ", list.Name, cmd),
fmt.Sprintf("List %s failed on command %s running command %s. \n Error: %v", list.Name, cmd, currentCmd, runOutErr))
if notifySendErr != nil {
config.Logger.Err(notifySendErr).Send()
}
@ -198,32 +182,22 @@ func cmdListWorker(id int, jobs <-chan *CmdList, config *BackyConfigFile, result
} else {
if count == len(list.Order) {
cmdsRan = append(cmdsRan, cmd)
var successMsg bytes.Buffer
Msg += fmt.Sprintf("%s ", cmd)
if list.NotifyConfig != nil {
successStruct := make(map[string]interface{})
successStruct["listName"] = list.Name
successStruct["CmdsRan"] = cmdsRan
t := template.Must(template.New("success.txt").ParseFS(templates, "templates/success.txt"))
tmpErr := t.Execute(&successMsg, successStruct)
if tmpErr != nil {
config.Logger.Err(tmpErr).Send()
break
}
err := list.NotifyConfig.Send(context.Background(), fmt.Sprintf("List %s succeded", list.Name), successMsg.String())
err := list.NotifyConfig.Send(context.Background(), fmt.Sprintf("List %s succeded", list.Name),
fmt.Sprintf("Command list %s was completed successfully. The following commands ran:\n %s", list.Name, Msg))
if err != nil {
config.Logger.Err(err).Send()
}
}
} else {
cmdsRan = append(cmdsRan, cmd)
Msg += fmt.Sprintf("%s, ", cmd)
}
}
}
results <- "done"
}
}
// RunBackyConfig runs a command list from the BackyConfigFile.
@ -234,7 +208,7 @@ func (config *BackyConfigFile) RunBackyConfig(cron string) {
// This starts up 3 workers, initially blocked
// because there are no jobs yet.
for w := 1; w <= configListsLen; w++ {
for w := 1; w <= 3; w++ {
go cmdListWorker(w, listChan, config, results)
}
@ -242,10 +216,7 @@ func (config *BackyConfigFile) RunBackyConfig(cron string) {
// Here we send 5 `jobs` and then `close` that
// channel to indicate that's all the work we have.
// configChan <- config.Cmds
for listName, cmdConfig := range config.CmdConfigLists {
if cmdConfig.Name == "" {
cmdConfig.Name = listName
}
for _, cmdConfig := range config.CmdConfigLists {
if cron != "" {
if cron == cmdConfig.Cron {
listChan <- cmdConfig
@ -264,9 +235,6 @@ func (config *BackyConfigFile) RunBackyConfig(cron string) {
func (config *BackyConfigFile) ExecuteCmds() {
for _, cmd := range config.Cmds {
runErr := cmd.RunCmd(&config.Logger, config.Hosts)
if runErr != nil {
config.Logger.Err(runErr).Send()
}
cmd.RunCmd(&config.Logger)
}
}

View File

@ -12,6 +12,7 @@ import (
"github.com/mattn/go-isatty"
"github.com/rs/zerolog"
"github.com/spf13/viper"
"mvdan.cc/sh/v3/shell"
)
// ReadConfig validates and reads the config file.
@ -27,12 +28,13 @@ func ReadConfig(opts *BackyConfigOpts) *BackyConfigFile {
backyConfigFile := NewConfig()
backyViper := opts.viper
opts.loadEnv()
// loadEnv(backyViper)
envFileInConfigDir := fmt.Sprintf("%s/.env", path.Dir(backyViper.ConfigFileUsed()))
// load the .env file in config file directory
_ = godotenv.Load(envFileInConfigDir)
envFileErr := godotenv.Load()
if envFileErr != nil {
_ = godotenv.Load(envFileInConfigDir)
}
if backyViper.GetBool(getNestedConfig("logging", "cmd-std-out")) {
os.Setenv("BACKY_STDOUT", "enabled")
}
@ -50,28 +52,24 @@ func ReadConfig(opts *BackyConfigOpts) *BackyConfigFile {
}
}
var (
// backyLoggingOpts *viper.Viper
verbose bool
logFile string
)
verbose = backyViper.GetBool(getLoggingKeyFromConfig("verbose"))
logFile = fmt.Sprintf("%s/backy.log", path.Dir(backyViper.ConfigFileUsed()))
if backyViper.IsSet(getLoggingKeyFromConfig("file")) {
logFile = backyViper.GetString(getLoggingKeyFromConfig("file"))
var backyLoggingOpts *viper.Viper
isBackyLoggingOptsSet := backyViper.IsSet("logging")
if isBackyLoggingOptsSet {
backyLoggingOpts = backyViper.Sub("logging")
}
verbose := backyLoggingOpts.GetBool("verbose")
logFile := backyLoggingOpts.GetString("file")
zerolog.SetGlobalLevel(zerolog.InfoLevel)
if verbose {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
globalLvl := zerolog.GlobalLevel()
os.Setenv("BACKY_LOGLEVEL", Sprintf("%v", globalLvl))
os.Setenv("BACKY_LOGLEVEL", Sprintf("%x", globalLvl))
}
consoleLoggingEnabled := backyViper.GetBool(getLoggingKeyFromConfig("console"))
consoleLoggingEnabled := backyLoggingOpts.GetBool("console")
// Other qualifiers can go here as well
if consoleLoggingEnabled {
@ -80,13 +78,12 @@ func ReadConfig(opts *BackyConfigOpts) *BackyConfigFile {
os.Setenv("BACKY_CONSOLE_LOGGING", "")
}
writers := logging.SetLoggingWriters(logFile)
writers := logging.SetLoggingWriters(backyLoggingOpts, logFile)
log := zerolog.New(writers).With().Timestamp().Logger()
backyConfigFile.Logger = log
log.Info().Str("config file", backyViper.ConfigFileUsed()).Send()
commandsMap := backyViper.GetStringMapString("commands")
commandsMapViper := backyViper.Sub("commands")
unmarshalErr := commandsMapViper.Unmarshal(&backyConfigFile.Cmds)
@ -103,8 +100,6 @@ func ReadConfig(opts *BackyConfigOpts) *BackyConfigFile {
os.Exit(1)
}
expandEnvVars(opts.backyEnv, cmdConf.Environment)
host := cmdConf.Host
if host != nil {
if backyViper.IsSet(getNestedConfig("hosts", *host)) {
@ -119,37 +114,12 @@ func ReadConfig(opts *BackyConfigOpts) *BackyConfigFile {
if unmarshalErr != nil {
panic(fmt.Errorf("error unmarshalling hosts struct: %w", unmarshalErr))
}
for _, host := range backyConfigFile.Hosts {
if host.ProxyJump != "" {
proxyHosts := strings.Split(host.ProxyJump, ",")
if len(proxyHosts) > 1 {
for hostNum, h := range proxyHosts {
if hostNum > 1 {
proxyHost, defined := backyConfigFile.Hosts[h]
if defined {
host.ProxyHost = append(host.ProxyHost, proxyHost)
} else {
newProxy := &Host{Host: h}
host.ProxyHost = append(host.ProxyHost, newProxy)
}
} else {
proxyHost, defined := backyConfigFile.Hosts[h]
if defined {
host.ProxyHost = append(host.ProxyHost, proxyHost)
} else {
newHost := &Host{Host: h}
host.ProxyHost = append(host.ProxyHost, newHost)
}
}
}
} else {
proxyHost, defined := backyConfigFile.Hosts[proxyHosts[0]]
if defined {
host.ProxyHost = append(host.ProxyHost, proxyHost)
} else {
newProxy := &Host{Host: proxyHosts[0]}
host.ProxyHost = append(host.ProxyHost, newProxy)
}
for _, v := range backyConfigFile.Hosts {
if v.JumpHost != "" {
proxyHost, defined := backyConfigFile.Hosts[v.JumpHost]
if defined {
v.ProxyHost = proxyHost
}
}
}
@ -187,7 +157,10 @@ func ReadConfig(opts *BackyConfigOpts) *BackyConfigFile {
cmdNotFoundErrorLog.Errs("commands not found", cmdNotFoundSliceErr).Send()
}
if opts.useCron && (len(backyConfigFile.CmdConfigLists) == 0) {
if opts.useCron && len(backyConfigFile.CmdConfigLists) > 0 {
log.Info().Msg("Starting cron mode...")
} else if opts.useCron && (len(backyConfigFile.CmdConfigLists) == 0) {
logging.ExitWithMSG("No cron fields detected in any command lists", 1, nil)
}
@ -244,14 +217,6 @@ func getNestedConfig(nestedConfig, key string) string {
func getCmdFromConfig(key string) string {
return fmt.Sprintf("commands.%s", key)
}
func getLoggingKeyFromConfig(key string) string {
if key == "" {
return "logging"
}
return fmt.Sprintf("logging.%s", key)
}
func getCmdListFromConfig(list string) string {
return fmt.Sprintf("cmd-configs.%s", list)
}
@ -263,13 +228,8 @@ func (opts *BackyConfigOpts) InitConfig() {
backyViper := viper.New()
if strings.TrimSpace(opts.ConfigFilePath) != "" {
err := testFile(opts.ConfigFilePath)
if err != nil {
logging.ExitWithMSG(fmt.Sprintf("Could not open config file %s: %v", opts.ConfigFilePath, err), 1, nil)
}
backyViper.SetConfigFile(opts.ConfigFilePath)
} else {
backyViper.SetConfigName("backy.yml") // name of config file (with extension)
backyViper.SetConfigName("backy.yaml") // name of config file (with extension)
backyViper.SetConfigType("yaml") // REQUIRED if the config file does not have the extension in the name
backyViper.AddConfigPath(".") // optionally look for config in the working directory
@ -277,8 +237,40 @@ func (opts *BackyConfigOpts) InitConfig() {
}
err := backyViper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
msg := fmt.Sprintf("fatal error reading config file %s: %v", backyViper.ConfigFileUsed(), err)
logging.ExitWithMSG(msg, 1, nil)
panic(fmt.Errorf("fatal error reading config file %s: %w", backyViper.ConfigFileUsed(), err))
}
opts.viper = backyViper
}
func loadEnv(backyViper *viper.Viper) {
envFileInConfigDir := fmt.Sprintf("%s/.env", path.Dir(backyViper.ConfigFileUsed()))
var backyEnv map[string]string
backyEnv, envFileErr := godotenv.Read()
// envFile, envFileErr := os.Open(".env")
if envFileErr != nil {
backyEnv, _ = godotenv.Read(envFileInConfigDir)
}
envFileErr = godotenv.Load()
if envFileErr != nil {
_ = godotenv.Load(envFileInConfigDir)
}
env := func(name string) string {
name = strings.ToUpper(name)
envVar, found := backyEnv[name]
if found {
return envVar
}
return ""
}
envVars := []string{"APP=${BACKY_APP}"}
for indx, v := range envVars {
if strings.Contains(v, "$") || (strings.Contains(v, "${") && strings.Contains(v, "}")) {
out, _ := shell.Expand(v, env)
envVars[indx] = out
// println(out)
}
}
}

View File

@ -14,21 +14,15 @@ import (
func (conf *BackyConfigFile) Cron() {
s := gocron.NewScheduler(time.Local)
s.TagsUnique()
for listName, config := range conf.CmdConfigLists {
if config.Name == "" {
config.Name = listName
}
cron := strings.TrimSpace(config.Cron)
if cron != "" {
conf.Logger.Info().Str("Scheduling cron list", config.Name).Str("Time", cron).Send()
_, err := s.CronWithSeconds(cron).Tag(config.Name).Do(func(cron string) {
for _, config := range conf.CmdConfigLists {
if strings.TrimSpace(config.Cron) != "" {
_, err := s.CronWithSeconds(config.Cron).Tag(config.Name).Do(func(cron string) {
conf.RunBackyConfig(cron)
}, cron)
}, config.Cron)
if err != nil {
panic(err)
}
}
}
conf.Logger.Info().Msg("Starting cron mode...")
s.StartBlocking()
}

View File

@ -87,6 +87,5 @@ func setupMail(config mailConfig) *mail.Mail {
mailClient := mail.New(config.senderaddress, config.host+":"+config.port)
mailClient.AuthenticateSMTP("", config.username, config.password, config.host)
mailClient.AddReceivers(config.to...)
mailClient.BodyFormat(mail.PlainText)
return mailClient
}

View File

@ -7,9 +7,9 @@ package backy
import (
"bufio"
"fmt"
"log"
"os"
"os/user"
"strconv"
"strings"
"time"
@ -20,98 +20,83 @@ import (
"golang.org/x/crypto/ssh/knownhosts"
)
var ErrPrivateKeyFileFailedToOpen = errors.New("Failed to open private key file. If encrypted, make sure the password is specified.")
var ErrPrivateKeyFileFailedToOpen = errors.New("Private key file failed to open.")
var TS = strings.TrimSpace
// ConnectToSSHHost connects to a host by looking up the config values in the directory ~/.ssh/config
// It uses any set values and looks up an unset values in the config files
// It returns an ssh.Client used to run commands against.
// If configFile is empty, any required configuration is looked up in the default config files
// If any value is not found, defaults are used
func (remoteConfig *Host) ConnectToSSHHost(log *zerolog.Logger, hosts map[string]*Host) error {
func (remoteConfig *Host) ConnectToSSHHost(log *zerolog.Logger) (*ssh.Client, error) {
// var sshClient *ssh.Client
var sshClient *ssh.Client
var connectErr error
// TODO: add JumpHost config check
// if !remoteConfig.UseConfigFiles {
// log.Info().Msg("Not using config files")
// }
if TS(remoteConfig.ConfigFilePath) == "" {
remoteConfig.useDefaultConfig = true
}
if remoteConfig.ProxyHost != nil {
for _, proxyHost := range remoteConfig.ProxyHost {
log.Info().Msgf("Proxy Host %s", proxyHost.Host)
err := proxyHost.GetProxyJumpConfig(hosts)
if err != nil {
return err
}
}
}
khPath, khPathErr := GetKnownHosts(remoteConfig.KnownHostsFile)
if khPathErr != nil {
return khPathErr
return nil, khPathErr
}
if remoteConfig.ClientConfig == nil {
remoteConfig.ClientConfig = &ssh.ClientConfig{}
}
var configFile *os.File
var sshConfigFile *os.File
var sshConfigFileOpenErr error
if !remoteConfig.useDefaultConfig {
configFile, sshConfigFileOpenErr = os.Open(remoteConfig.ConfigFilePath)
sshConfigFile, sshConfigFileOpenErr = os.Open(remoteConfig.ConfigFilePath)
if sshConfigFileOpenErr != nil {
return sshConfigFileOpenErr
return nil, sshConfigFileOpenErr
}
} else {
defaultConfig, _ := resolveDir("~/.ssh/config")
configFile, sshConfigFileOpenErr = os.Open(defaultConfig)
sshConfigFile, sshConfigFileOpenErr = os.Open(defaultConfig)
if sshConfigFileOpenErr != nil {
return sshConfigFileOpenErr
return nil, sshConfigFileOpenErr
}
}
remoteConfig.SSHConfigFile = &sshConfigFile{}
remoteConfig.SSHConfigFile.DefaultUserSettings = ssh_config.DefaultUserSettings
var decodeErr error
remoteConfig.SSHConfigFile.SshConfigFile, decodeErr = ssh_config.Decode(configFile)
cfg, decodeErr := ssh_config.Decode(sshConfigFile)
if decodeErr != nil {
return decodeErr
return nil, decodeErr
}
remoteConfig.ClientConfig.Timeout = time.Second * 30
remoteConfig.GetPrivateKeyFileFromConfig()
remoteConfig.GetPort()
remoteConfig.GetHostName()
remoteConfig.CombineHostNameWithPort()
remoteConfig.SSHConfigFile.SshConfigFile = cfg
remoteConfig.GetPrivateKeyFromConfig()
remoteConfig.GetHostNameWithPort()
remoteConfig.GetSshUserFromConfig()
log.Info().Msgf("Port: %v", remoteConfig.Port)
if remoteConfig.HostName == "" {
return errors.New("No hostname found or specified")
return nil, errors.New("No hostname found or specified")
}
err := remoteConfig.GetAuthMethods()
if err != nil {
return err
return nil, err
}
// TODO: Add value/option to config for host key and add bool to check for host key
hostKeyCallback, err := knownhosts.New(khPath)
if err != nil {
return errors.Wrap(err, "could not create hostkeycallback function")
return nil, errors.Wrap(err, "could not create hostkeycallback function")
}
remoteConfig.ClientConfig.HostKeyCallback = hostKeyCallback
log.Info().Str("user", remoteConfig.ClientConfig.User).Send()
remoteConfig.SshClient, connectErr = remoteConfig.ConnectThroughBastion(log)
if connectErr != nil {
return connectErr
}
if remoteConfig.SshClient != nil {
return nil
}
log.Info().Msgf("Connecting to host %s", remoteConfig.HostName)
remoteConfig.SshClient, connectErr = ssh.Dial("tcp", remoteConfig.HostName, remoteConfig.ClientConfig)
remoteConfig.ClientConfig.Timeout = time.Second * 30
sshClient, connectErr = ssh.Dial("tcp", remoteConfig.HostName, remoteConfig.ClientConfig)
if connectErr != nil {
return connectErr
return nil, connectErr
}
return nil
return sshClient, nil
}
func (remoteHost *Host) GetSshUserFromConfig() {
@ -170,9 +155,9 @@ func (remoteHost *Host) GetAuthMethods() error {
// GetPrivateKeyFromConfig checks to see if the privateKeyPath is empty.
// If not, it keeps the value.
// If empty, the key is looked for in the specified config file.
// If that path is empty, the default config file is searched.
// If that path is empty, the default config file is searched
// If not found in the default file, the privateKeyPath is set to ~/.ssh/id_rsa
func (remoteHost *Host) GetPrivateKeyFileFromConfig() {
func (remoteHost *Host) GetPrivateKeyFromConfig() {
var identityFile string
if remoteHost.PrivateKeyPath == "" {
identityFile, _ = remoteHost.SSHConfigFile.SshConfigFile.Get(remoteHost.Host, "IdentityFile")
@ -190,11 +175,18 @@ func (remoteHost *Host) GetPrivateKeyFileFromConfig() {
remoteHost.PrivateKeyPath, _ = resolveDir(identityFile)
}
// GetPort checks if the port from the config file is 0
// GetHostNameWithPort checks if the port from the config file is 0
// If it is the port is searched in the SSH config file(s)
func (remoteHost *Host) GetPort() {
func (remoteHost *Host) GetHostNameWithPort() {
port := fmt.Sprintf("%v", remoteHost.Port)
// port specifed?
if remoteHost.HostName == "" {
remoteHost.HostName, _ = remoteHost.SSHConfigFile.SshConfigFile.Get(remoteHost.Host, "HostName")
if remoteHost.HostName == "" {
remoteHost.HostName = remoteHost.SSHConfigFile.DefaultUserSettings.Get(remoteHost.Host, "HostName")
}
}
// no port specifed
if port == "0" {
port, _ = remoteHost.SSHConfigFile.SshConfigFile.Get(remoteHost.Host, "Port")
if port == "" {
@ -203,34 +195,16 @@ func (remoteHost *Host) GetPort() {
port = "22"
}
}
println(port)
}
portNum, _ := strconv.ParseUint(port, 10, 32)
remoteHost.Port = uint16(portNum)
}
func (remoteHost *Host) CombineHostNameWithPort() {
remoteHost.HostName = fmt.Sprintf("%s:%v", remoteHost.HostName, remoteHost.Port)
}
func (remoteHost *Host) GetHostName() {
if remoteHost.HostName == "" {
remoteHost.HostName, _ = remoteHost.SSHConfigFile.SshConfigFile.Get(remoteHost.Host, "HostName")
if remoteHost.HostName == "" {
remoteHost.HostName = remoteHost.SSHConfigFile.DefaultUserSettings.Get(remoteHost.Host, "HostName")
}
if !strings.HasSuffix(remoteHost.HostName, ":"+port) {
remoteHost.HostName = remoteHost.HostName + ":" + port
}
}
func (remoteHost *Host) ConnectThroughBastion(log *zerolog.Logger) (*ssh.Client, error) {
if remoteHost.ProxyHost == nil {
return nil, nil
}
log.Info().Msgf("Connecting to proxy host %s", remoteHost.ProxyHost[0].HostName)
func (remoteHost *Host) ConnectThroughBastion() (*ssh.Client, error) {
// connect to the bastion host
bClient, err := ssh.Dial("tcp", remoteHost.ProxyHost[0].HostName, remoteHost.ProxyHost[0].ClientConfig)
bClient, err := ssh.Dial("tcp", remoteHost.ProxyHost.HostName, remoteHost.ProxyHost.ClientConfig)
if err != nil {
return nil, err
}
@ -240,10 +214,10 @@ func (remoteHost *Host) ConnectThroughBastion(log *zerolog.Logger) (*ssh.Client,
if err != nil {
return nil, err
}
log.Info().Msgf("Connecting to host %s", remoteHost.HostName)
ncc, chans, reqs, err := ssh.NewClientConn(conn, remoteHost.HostName, remoteHost.ClientConfig)
if err != nil {
return nil, err
log.Fatal(err)
}
sClient := ssh.NewClient(ncc, chans, reqs)
@ -284,14 +258,14 @@ func GetPrivateKeyPassword(key string) (string, error) {
return prKeyPassword, nil
}
func GetPassword(pass string) (string, error) {
pass = strings.TrimSpace(pass)
if pass == "" {
func GetPassword(key string) (string, error) {
key = strings.TrimSpace(key)
if key == "" {
return "", nil
}
var password string
if strings.HasPrefix(pass, "file:") {
passFilePath := strings.TrimPrefix(pass, "file:")
if strings.HasPrefix(key, "file:") {
passFilePath := strings.TrimPrefix(key, "file:")
passFilePath, _ = resolveDir(passFilePath)
keyFile, keyFileErr := os.Open(passFilePath)
if keyFileErr != nil {
@ -301,94 +275,14 @@ func GetPassword(pass string) (string, error) {
for passwordScanner.Scan() {
password = passwordScanner.Text()
}
} else if strings.HasPrefix(pass, "env:") {
passEnv := strings.TrimPrefix(pass, "env:")
} else if strings.HasPrefix(key, "env:") {
passEnv := strings.TrimPrefix(key, "env:")
passEnv = strings.TrimPrefix(passEnv, "${")
passEnv = strings.TrimSuffix(passEnv, "}")
passEnv = strings.TrimPrefix(passEnv, "$")
password = os.Getenv(passEnv)
} else {
password = pass
password = key
}
return password, nil
}
func (remoteConfig *Host) GetProxyJumpFromConfig(hosts map[string]*Host) error {
proxyJump, _ := remoteConfig.SSHConfigFile.SshConfigFile.Get(remoteConfig.Host, "ProxyJump")
if proxyJump == "" {
proxyJump = remoteConfig.SSHConfigFile.DefaultUserSettings.Get(remoteConfig.Host, "ProxyJump")
}
if remoteConfig.ProxyJump == "" && proxyJump != "" {
remoteConfig.ProxyJump = proxyJump
}
proxyJumpHosts := strings.Split(remoteConfig.ProxyJump, ",")
if remoteConfig.ProxyHost == nil && len(proxyJumpHosts) == 1 {
remoteConfig.ProxyJump = proxyJump
proxyHost, proxyHostFound := hosts[proxyJump]
if proxyHostFound {
remoteConfig.ProxyHost = append(remoteConfig.ProxyHost, proxyHost)
} else {
newProxy := &Host{Host: proxyJump}
remoteConfig.ProxyHost = append(remoteConfig.ProxyHost, newProxy)
}
}
return nil
}
func (remoteConfig *Host) GetProxyJumpConfig(hosts map[string]*Host) error {
if TS(remoteConfig.ConfigFilePath) == "" {
remoteConfig.useDefaultConfig = true
}
// log.Info().Msgf("Proxy Host %s", remoteConfig.ProxyHost[0].Host)
khPath, khPathErr := GetKnownHosts(remoteConfig.KnownHostsFile)
if khPathErr != nil {
return khPathErr
}
if remoteConfig.ClientConfig == nil {
remoteConfig.ClientConfig = &ssh.ClientConfig{}
}
var configFile *os.File
var sshConfigFileOpenErr error
if !remoteConfig.useDefaultConfig {
configFile, sshConfigFileOpenErr = os.Open(remoteConfig.ConfigFilePath)
if sshConfigFileOpenErr != nil {
return sshConfigFileOpenErr
}
} else {
defaultConfig, _ := resolveDir("~/.ssh/config")
configFile, sshConfigFileOpenErr = os.Open(defaultConfig)
if sshConfigFileOpenErr != nil {
return sshConfigFileOpenErr
}
}
remoteConfig.SSHConfigFile = &sshConfigFile{}
remoteConfig.SSHConfigFile.DefaultUserSettings = ssh_config.DefaultUserSettings
var decodeErr error
remoteConfig.SSHConfigFile.SshConfigFile, decodeErr = ssh_config.Decode(configFile)
if decodeErr != nil {
return decodeErr
}
remoteConfig.GetPrivateKeyFileFromConfig()
remoteConfig.GetPort()
remoteConfig.GetHostName()
remoteConfig.CombineHostNameWithPort()
remoteConfig.GetSshUserFromConfig()
if remoteConfig.HostName == "" {
return errors.New("No hostname found or specified")
}
err := remoteConfig.GetAuthMethods()
if err != nil {
return err
}
// TODO: Add value/option to config for host key and add bool to check for host key
hostKeyCallback, err := knownhosts.New(khPath)
if err != nil {
return errors.Wrap(err, "could not create hostkeycallback function")
}
remoteConfig.ClientConfig.HostKeyCallback = hostKeyCallback
return nil
}

View File

@ -1,8 +0,0 @@
Command list {{.listName }} failed on running {{.Command}}.
The error was {{ .Err }}
The following commands ran:
{{- range .CmdsRan}}
- {{. -}}
{{end}}

View File

@ -1,7 +0,0 @@
Command list {{ .listName }} was completed successfully.
The following commands ran:
{{- range .CmdsRan}}
- {{. -}}
{{end}}

View File

@ -41,18 +41,17 @@ type Host struct {
HostName string `yaml:"hostname,omitempty"`
KnownHostsFile string `yaml:"knownhostsfile,omitempty"`
ClientConfig *ssh.ClientConfig
SSHConfigFile *sshConfigFile
SshClient *ssh.Client
SSHConfigFile sshConfigFile
Port uint16 `yaml:"port,omitempty"`
ProxyJump string `yaml:"proxyjump,omitempty"`
JumpHost string `yaml:"jumphost,omitempty"`
Password string `yaml:"password,omitempty"`
PrivateKeyPath string `yaml:"privatekeypath,omitempty"`
PrivateKeyPassword string `yaml:"privatekeypassword,omitempty"`
UseConfigFiles bool `yaml:"use_config_files,omitempty"`
useDefaultConfig bool
User string `yaml:"user,omitempty"`
// ProxyHost holds the configuration for a ProxyJump host
ProxyHost []*Host
// ProxyHost holds the configuration for a JumpHost host
ProxyHost *Host
}
type sshConfigFile struct {
@ -145,9 +144,6 @@ type BackyConfigOpts struct {
// Holds commands to execute for the exec command
executeLists []string
// Holds env vars from .env file
backyEnv map[string]string
viper *viper.Viper
}

View File

@ -9,7 +9,6 @@ import (
"fmt"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
@ -18,7 +17,6 @@ import (
"github.com/rs/zerolog"
"github.com/spf13/viper"
"golang.org/x/crypto/ssh"
"mvdan.cc/sh/v3/shell"
)
func injectEnvIntoSSH(envVarsToInject environmentVars, process *ssh.Session, log *zerolog.Logger) {
@ -209,33 +207,3 @@ func resolveDir(path string) (string, error) {
}
return path, nil
}
func (opts *BackyConfigOpts) loadEnv() {
envFileInConfigDir := fmt.Sprintf("%s/.env", path.Dir(opts.viper.ConfigFileUsed()))
var backyEnv map[string]string
backyEnv, envFileErr := godotenv.Read(envFileInConfigDir)
if envFileErr != nil {
return
}
opts.backyEnv = backyEnv
}
func expandEnvVars(backyEnv map[string]string, envVars []string) {
env := func(name string) string {
name = strings.ToUpper(name)
envVar, found := backyEnv[name]
if found {
return envVar
}
return ""
}
for indx, v := range envVars {
if strings.Contains(v, "$") || (strings.Contains(v, "${") && strings.Contains(v, "}")) {
out, _ := shell.Expand(v, env)
envVars[indx] = out
}
}
}

View File

@ -7,6 +7,7 @@ import (
"time"
"github.com/rs/zerolog"
"github.com/spf13/viper"
"gopkg.in/natefinch/lumberjack.v2"
)
@ -24,7 +25,7 @@ func ExitWithMSG(msg string, code int, log *zerolog.Logger) {
os.Exit(code)
}
func SetLoggingWriters(logFile string) (writers zerolog.LevelWriter) {
func SetLoggingWriters(v *viper.Viper, logFile string) (writers zerolog.LevelWriter) {
console := zerolog.ConsoleWriter{}
if IsConsoleLoggingEnabled() {
@ -54,7 +55,12 @@ func SetLoggingWriters(logFile string) (writers zerolog.LevelWriter) {
MaxAge: 28, //days
Compress: true, // disabled by default
}
fileLogger.Filename = logFile
if strings.TrimSpace(logFile) != "" {
fileLogger.Filename = logFile
} else {
fileLogger.Filename = "./backy.log"
}
// UNIX Time is faster and smaller than most timestamps
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
// zerolog.TimeFieldFormat = time.RFC1123
@ -69,7 +75,3 @@ func SetLoggingWriters(logFile string) (writers zerolog.LevelWriter) {
func IsConsoleLoggingEnabled() bool {
return os.Getenv("BACKY_CONSOLE_LOGGING") == "enabled"
}
// func IsTerminal() bool {
// return os.Getenv("BACKY_TERM") == "enabled"
// }