Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helper method to config.Secret to load a file #320

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all of these are here because of linting errors...

// +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