summaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorPaul Buetow <pbuetow@mimecast.com>2021-10-15 13:20:48 +0300
committerPaul Buetow <pbuetow@mimecast.com>2021-10-19 19:02:57 +0300
commit10314cef906fd9b73e003be69c2f6b7b3d66570c (patch)
tree03e8b96ad97d3382d439725166f83c774dc999a4 /internal
parentb27fc108ecd6eead5c97cf6e894bf8d639fff75c (diff)
Can configure DTail client not to mess with ~/.ssh/known_hosts via env var - this is useful for running unit and integration tests in jenkins
Diffstat (limited to 'internal')
-rw-r--r--internal/config/client.go3
-rw-r--r--internal/config/initializer.go10
-rw-r--r--internal/ssh/client/authmethods.go9
-rw-r--r--internal/ssh/client/knownhostscallback.go7
4 files changed, 26 insertions, 3 deletions
diff --git a/internal/config/client.go b/internal/config/client.go
index 9f4df97..86f97f0 100644
--- a/internal/config/client.go
+++ b/internal/config/client.go
@@ -104,6 +104,9 @@ type termColors struct {
type ClientConfig struct {
TermColorsEnable bool `json:",omitempty"`
TermColors termColors `json:",omitempty"`
+ // When unit testing in Jenkins you don't want to touch files in ~jenkins
+ // during integration tests really.
+ SSHDontAddHostsToKnownHostsFile bool `json:",omitempty"`
}
// Create a new default client configuration.
diff --git a/internal/config/initializer.go b/internal/config/initializer.go
index 8215891..35105bf 100644
--- a/internal/config/initializer.go
+++ b/internal/config/initializer.go
@@ -65,6 +65,8 @@ func (in *initializer) parseSpecificConfig(configFile string) error {
func (in *initializer) transformConfig(sourceProcess source.Source, args *Args,
additionalArgs []string) error {
+ in.readEnvironmentVars()
+
switch sourceProcess {
case source.Server:
return in.optimusPrime(transformServer, args, additionalArgs)
@@ -78,6 +80,14 @@ func (in *initializer) transformConfig(sourceProcess source.Source, args *Args,
}
}
+// There are some special options which can be set by environment variable.
+func (in *initializer) readEnvironmentVars() {
+ if len(os.Getenv("DTAIL_SSH_DONT_ADD_HOSTS_TO_KNOWNHOSTS_FILE")) != 0 ||
+ len(os.Getenv("DTAIL_JENKINS")) != 0 {
+ in.Client.SSHDontAddHostsToKnownHostsFile = true
+ }
+}
+
func (in *initializer) optimusPrime(sourceCb transformCb, args *Args,
additionalArgs []string) error {
diff --git a/internal/ssh/client/authmethods.go b/internal/ssh/client/authmethods.go
index ced1fb9..089a66a 100644
--- a/internal/ssh/client/authmethods.go
+++ b/internal/ssh/client/authmethods.go
@@ -35,8 +35,7 @@ func initKnownHostsAuthMethods(trustAllHosts bool, throttleCh chan struct{},
if err != nil {
dlog.Common.FatalPanic(knownHostsPath, err)
}
- dlog.Common.Debug("initKnownHostsAuthMethods", "Added known hosts file path",
- knownHostsPath)
+ dlog.Common.Debug("initKnownHostsAuthMethods", "Added known hosts file path", knownHostsPath)
if config.Common.ExperimentalFeaturesEnable {
sshAuthMethods = append(sshAuthMethods, gossh.Password("experimental feature test"))
dlog.Common.Debug("initKnownHostsAuthMethods", "Added experimental method to list of auth methods")
@@ -88,7 +87,11 @@ func initKnownHostsAuthMethods(trustAllHosts bool, throttleCh chan struct{},
}
dlog.Common.Debug("initKnownHostsAuthMethods", "Unable to use private key",
privateKeyPath, err)
- dlog.Common.FatalPanic("Unable to find private SSH key information")
+
+ // This is only a panic when we expect to do something about it.
+ if !config.Client.SSHDontAddHostsToKnownHostsFile {
+ dlog.Common.FatalPanic("Unable to find private SSH key information")
+ }
// Never reach this point.
return sshAuthMethods, knownHostsCallback
diff --git a/internal/ssh/client/knownhostscallback.go b/internal/ssh/client/knownhostscallback.go
index 65a590a..2aa0168 100644
--- a/internal/ssh/client/knownhostscallback.go
+++ b/internal/ssh/client/knownhostscallback.go
@@ -10,6 +10,7 @@ import (
"sync"
"time"
+ "github.com/mimecast/dtail/internal/config"
"github.com/mimecast/dtail/internal/io/dlog"
"github.com/mimecast/dtail/internal/io/prompt"
@@ -214,6 +215,12 @@ func (c KnownHostsCallback) promptAddHosts(hosts []unknownHost) {
func (c KnownHostsCallback) trustHosts(hosts []unknownHost) {
tmpKnownHostsPath := fmt.Sprintf("%s.tmp", c.knownHostsPath)
+
+ if config.Client.SSHDontAddHostsToKnownHostsFile {
+ dlog.Common.Verbose("Not adding hosts to known hosts file, as disabled by config")
+ return
+ }
+
newFd, err := os.OpenFile(tmpKnownHostsPath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0600)
if err != nil {
panic(fmt.Sprintf("%s: %s", tmpKnownHostsPath, err.Error()))