From 9789762a2ddbdb71a060abf484c53e6512911fb8 Mon Sep 17 00:00:00 2001 From: "Marcelo E. Magallon" Date: Mon, 2 Aug 2021 10:37:13 -0600 Subject: [PATCH] Add helper method to config.Secret to load a file LoadFromFile is a very small helper to avoid repeating this code over and over again. This is related to prometheus/prometheus#8551 Signed-off-by: Marcelo E. Magallon --- config/config.go | 11 ++++++++ config/config_test.go | 55 ++++++++++++++++++++++++++++++++++++++ config/http_config.go | 1 + config/http_config_test.go | 1 + config/tls_config_test.go | 1 + expfmt/fuzz.go | 1 + 6 files changed, 70 insertions(+) diff --git a/config/config.go b/config/config.go index fffda4a7..f773b7c6 100644 --- a/config/config.go +++ b/config/config.go @@ -18,6 +18,8 @@ package config import ( "encoding/json" + "fmt" + "io/ioutil" "path/filepath" ) @@ -48,6 +50,15 @@ func (s Secret) MarshalJSON() ([]byte, error) { return json.Marshal(secretToken) } +func (s *Secret) LoadFromFile(filename string) error { + buf, err := ioutil.ReadFile(filename) + if err != nil { + return fmt.Errorf("cannot read %s: %w", filename, err) + } + *s = Secret(buf) + return nil +} + // DirectorySetter is a config type that contains file paths that may // be relative to the file containing the config. type DirectorySetter interface { diff --git a/config/config_test.go b/config/config_test.go index 81c78555..89794d4e 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -11,12 +11,15 @@ // See the License for the specific language governing permissions and // limitations under the License. +//go:build go1.8 // +build go1.8 package config import ( "encoding/json" + "io/ioutil" + "os" "testing" ) @@ -53,3 +56,55 @@ func TestJSONMarshalSecret(t *testing.T) { }) } } + +func TestSecretLoadFromFile(t *testing.T) { + dn, err := ioutil.TempDir("", "test-secret-loadfromfile.") + if err != nil { + t.Fatalf("cannot create temporary directory: %s", err) + } + defer os.RemoveAll(dn) + + fh, err := ioutil.TempFile(dn, "") + if err != nil { + t.Fatalf("cannot create temporary file: %s", err) + } + + fn := fh.Name() + + secretData := "test" + + n, err := fh.WriteString(secretData) + if err != nil { + t.Fatalf("cannot write to temporary file %s: %s", fn, err) + } + + if n != len(secretData) { + t.Fatalf("short write writing to temporary file %s, expecting %d, got %d", fn, len(secretData), n) + } + + err = fh.Close() + if err != nil { + t.Fatalf("error closing temporary file %s after write: %s", fn, err) + } + + var s Secret + err = s.LoadFromFile(fn) + if err != nil { + t.Fatalf("cannot read secret from temporary file %s: %s", fn, err) + } + + if string(s) != secretData { + t.Fatalf("unexpected secret data, expected %q, actual %q", secretData, string(s)) + } + + err = os.Remove(fn) + if err != nil { + t.Fatalf("cannot remove temporary file %s: %s", fn, err) + } + + // this should report an error now + err = s.LoadFromFile(fn) + if err == nil { + t.Fatalf("expecting error reading non-existent temporary file %s, got nil", fn) + } +} diff --git a/config/http_config.go b/config/http_config.go index 8c07c41f..11bb9bfe 100644 --- a/config/http_config.go +++ b/config/http_config.go @@ -11,6 +11,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +//go:build go1.8 // +build go1.8 package config diff --git a/config/http_config_test.go b/config/http_config_test.go index 689bbde2..b02f8ede 100644 --- a/config/http_config_test.go +++ b/config/http_config_test.go @@ -11,6 +11,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +//go:build go1.8 // +build go1.8 package config diff --git a/config/tls_config_test.go b/config/tls_config_test.go index 57ad4bb4..bd8add67 100644 --- a/config/tls_config_test.go +++ b/config/tls_config_test.go @@ -11,6 +11,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +//go:build go1.8 // +build go1.8 package config diff --git a/expfmt/fuzz.go b/expfmt/fuzz.go index dc2eedee..f819e4f8 100644 --- a/expfmt/fuzz.go +++ b/expfmt/fuzz.go @@ -12,6 +12,7 @@ // limitations under the License. // Build only when actually fuzzing +//go:build gofuzz // +build gofuzz package expfmt