From 9d5c28b8ca67a4e9b4922f3a877c8e56167d5010 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Tegn=C3=A9r?= Date: Fri, 30 Aug 2019 09:35:39 +0200 Subject: [PATCH 1/3] Created a ReadFileOrDefault function in shared/utils.go to enable file reading that returns a default value in case of error or no data in file. --- src/shared/utils.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/shared/utils.go b/src/shared/utils.go index e99351f..5955309 100644 --- a/src/shared/utils.go +++ b/src/shared/utils.go @@ -1,6 +1,8 @@ package shared import ( + "io/ioutil" + "os" "os/user" "path/filepath" "strings" @@ -29,3 +31,20 @@ func ExpandPathWithTilde(path string) string { } return path } + +// Tries to read data from a file from given path. +// If no file is found, the file read creates an error or is empty, +// the function will return the value of the `or` parameter instead. +func ReadFileOrDefault(filePath string, or string) string { + if len(filePath) <= 0 { + return or + } + if _, err := os.Stat(filePath); os.IsNotExist(err) { + return or + } + data, err := ioutil.ReadFile(filePath) + if err != nil || len(data) <= 0 { + return or + } + return string(data) +} \ No newline at end of file From 8b44c9ae7c9c1262439255b2bce22d8cb18999dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Tegn=C3=A9r?= Date: Fri, 30 Aug 2019 10:43:55 +0200 Subject: [PATCH 2/3] Updated config.go file to use the `ReadFileOrDefault` function instead of using the `KEYBASE_...` variables right away for paperkey and username. New env variables presedes the old. --- src/keybaseca/config/config.go | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/keybaseca/config/config.go b/src/keybaseca/config/config.go index 4d5371a..3b50bbf 100644 --- a/src/keybaseca/config/config.go +++ b/src/keybaseca/config/config.go @@ -5,11 +5,8 @@ import ( "io/ioutil" "os" "strings" - "github.com/keybase/bot-sshca/src/keybaseca/constants" - "github.com/keybase/bot-sshca/src/keybaseca/botwrapper" - "github.com/keybase/bot-sshca/src/shared" ) @@ -143,13 +140,13 @@ func (ef *EnvConfig) GetKeybaseHomeDir() string { // Get the keybase paper key for the bot account. Used if you are running a separate instance of keybase for the chatbot. // May be empty. func (ef *EnvConfig) GetKeybasePaperKey() string { - return os.Getenv("KEYBASE_PAPERKEY") + return shared.ReadFileOrDefault(os.Getenv("KEYBASE_PAPERKEY_PATH"), os.Getenv("KEYBASE_PAPERKEY")) } // Get the keybase username for the bot account. Used if you are running a separate instance of keybase for the chatbot. // May be empty. func (ef *EnvConfig) GetKeybaseUsername() string { - return os.Getenv("KEYBASE_USERNAME") + return shared.ReadFileOrDefault(os.Getenv("KEYBASE_USERNAME"), os.Getenv("KEYBASE_USERNAME_PATH")) } // Get the expiration period for signatures generated by the bot. From a2c2f041e338e2b2057d0873d357ab7818c7165d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Tegn=C3=A9r?= Date: Sun, 26 Apr 2020 11:18:35 +0200 Subject: [PATCH 3/3] Corrected order of params to fetch username from file. --- src/keybaseca/config/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/keybaseca/config/config.go b/src/keybaseca/config/config.go index ef4cdf4..244c393 100644 --- a/src/keybaseca/config/config.go +++ b/src/keybaseca/config/config.go @@ -192,7 +192,7 @@ func (ef *EnvConfig) GetKeybasePaperKey() string { // Get the keybase username for the bot account. Used if you are running a separate instance of keybase for the chatbot. // May be empty. func (ef *EnvConfig) GetKeybaseUsername() string { - return shared.ReadFileOrDefault(os.Getenv("KEYBASE_USERNAME"), os.Getenv("KEYBASE_USERNAME_PATH")) + return shared.ReadFileOrDefault(os.Getenv("KEYBASE_USERNAME_PATH"), os.Getenv("KEYBASE_USERNAME")) } // Get the expiration period for signatures generated by the bot.