From dd43707bf8938ef081737b53c83c4abda4c48027 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Ruiz?= Date: Wed, 10 Apr 2024 11:31:53 +0200 Subject: [PATCH] feat: introduce OMNIVORE_AUTH_TOKEN_FILEPATH to be able to read the secret from a file. Refactor config setup --- cmd/omnivore-as-rss/omnivore-as-rss.go | 1 + internal/config.go | 54 ++++++++++++++++++++++++++ internal/graphql.go | 3 +- 3 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 internal/config.go diff --git a/cmd/omnivore-as-rss/omnivore-as-rss.go b/cmd/omnivore-as-rss/omnivore-as-rss.go index c03959c..9bd3fff 100644 --- a/cmd/omnivore-as-rss/omnivore-as-rss.go +++ b/cmd/omnivore-as-rss/omnivore-as-rss.go @@ -3,5 +3,6 @@ package main import "omnivore-as-rss/internal" func main() { + internal.InitConfig() internal.Serve() } diff --git a/internal/config.go b/internal/config.go new file mode 100644 index 0000000..d75918f --- /dev/null +++ b/internal/config.go @@ -0,0 +1,54 @@ +package internal + +import ( + "errors" + "log" + "os" + "strings" +) + +var Cfg Config + +type Config struct { + OmnivoreAuthToken string +} + +func InitConfig() { + omnivoreToken , err := getOmnivoreToken() + if err != nil { + log.Fatal(err) + } + + c := Config { + OmnivoreAuthToken: omnivoreToken, + } + + Cfg = c +} + +func getOmnivoreToken() (string, error) { + secretEnv := os.Getenv("OMNIVORE_AUTH_TOKEN") + secretFilePath := os.Getenv("OMNIVORE_AUTH_TOKEN_FILEPATH") + + if secretFilePath != "" { + log.Println("Reading secret from file") + dat, err := os.ReadFile(secretFilePath) + if err != nil { + log.Println(err) + return "", errors.New("unable to read Omnivore secret file") + } + + token := string(dat) + // Clean up secret string to avoid errors + token = strings.Replace(token, " ", "", -1) + token = strings.Replace(token, "\t", "", -1) + token = strings.Replace(token, "\n", "", -1) + + return token, nil + } else if secretEnv != "" { + log.Println("Reading secret from env var") + return secretEnv, nil + } + + return "", errors.New("omnivore authentication is needed for this service to work, please set environment variable or secret file") +} \ No newline at end of file diff --git a/internal/graphql.go b/internal/graphql.go index 1fd33b6..c9f353a 100644 --- a/internal/graphql.go +++ b/internal/graphql.go @@ -3,7 +3,6 @@ package internal import ( "context" "net/http" - "os" "time" "github.com/hasura/go-graphql-client" ) @@ -13,7 +12,7 @@ var client *graphql.Client func queryOmnivore() error { if client == nil { client = graphql.NewClient("https://api-prod.omnivore.app/api/graphql", nil).WithRequestModifier(func(r *http.Request) { - r.Header.Set("Authorization", os.Getenv("OMNIVORE_AUTH_TOKEN")) + r.Header.Set("Authorization", Cfg.OmnivoreAuthToken) }) }