Skip to content

Commit

Permalink
Add helper method to config.Secret to load a file
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
mem committed Oct 11, 2021
1 parent 5a26535 commit 9789762
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 0 deletions.
11 changes: 11 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package config

import (
"encoding/json"
"fmt"
"io/ioutil"
"path/filepath"
)

Expand Down Expand Up @@ -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 {
Expand Down
55 changes: 55 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}
}
1 change: 1 addition & 0 deletions config/http_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions config/http_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions config/tls_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions expfmt/fuzz.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// limitations under the License.

// Build only when actually fuzzing
//go:build gofuzz
// +build gofuzz

package expfmt
Expand Down

0 comments on commit 9789762

Please sign in to comment.