Skip to content

Commit

Permalink
Introducing secret Unmarshal function (#73)
Browse files Browse the repository at this point in the history
* First draft of unmarshal function

* Add unit test

* Stricter type checking

* Always return type check errors while unmarshaling

* More comprehensive tests and pointer support

* Use options pattern for setting apex

* Trim apex during config time instead of runtime

* Pass options when recursing structs

* Latest iteration of Secret Unmarshaler
  • Loading branch information
broamski authored Oct 24, 2022
1 parent 5e644de commit 38754ad
Show file tree
Hide file tree
Showing 5 changed files with 699 additions and 3 deletions.
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
cmd/daytona/daytona
daytona
coverage.out
/cmd/daytona/daytona
/daytona
/coverage.out
45 changes: 45 additions & 0 deletions pkg/daytona/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package daytona

import (
"github.com/hashicorp/vault/api"
)

type Option interface {
Apply(s *SecretUnmarshler)
}

// WithClient allows callers to provice a custom
// vault client
func WithClient(client *api.Client) Option {
return withClient{client}
}

type withClient struct{ c *api.Client }

func (w withClient) Apply(s *SecretUnmarshler) {
s.client = w.c
}

// WithTokenString allows callers to provide a token
// in the form of a string
func WithTokenString(token string) Option {
return withTokenString{token}
}

type withTokenString struct{ token string }

func (w withTokenString) Apply(s *SecretUnmarshler) {
s.tokenString = w.token
}

// WithTokenFile allows callers to provide a path
// to a file where a vault token is stored
func WithTokenFile(path string) Option {
return withTokenFile{path}
}

type withTokenFile struct{ path string }

func (w withTokenFile) Apply(s *SecretUnmarshler) {
s.tokenFile = w.path
}
70 changes: 70 additions & 0 deletions pkg/daytona/options_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package daytona

import (
"io/ioutil"
"log"
"os"
"testing"

"github.com/hashicorp/vault/api"
)

var testToken = "THIS IS MY TOKEN"

func TestOptionsWithClient(t *testing.T) {
client, err := api.NewClient(api.DefaultConfig())
if err != nil {
t.Fatal(err)
}

client.SetToken(testToken)

u, err := NewSecretUnmarshler(WithClient(client))
if err != nil {
t.Fatal(err)
}

if u.client.Token() != testToken {
// we purposely don't log api.Client.Token() in the
// unlikely event we pickup a production token
t.Fatalf("WithClient options is not working. exptected token %s, got something else...", testToken)
}
}

func TestOptionsWithTokenString(t *testing.T) {
u, err := NewSecretUnmarshler(WithTokenString(testToken))
if err != nil {
t.Fatal(err)
}

if u.client.Token() != testToken {
// we purposely don't log api.Client.Token() in the
// unlikely event we pickup a production token
t.Fatalf("WithTokenString options is not working. exptected token %s, got something else...", testToken)
}
}

func TestOptionsWithTokenFile(t *testing.T) {
fileTokenContents := "THIS IS MY FILE TOKEN"
file, err := ioutil.TempFile("", "test-vault-token")
if err != nil {
log.Fatal(err)
}
defer os.Remove(file.Name())

_, err = file.Write([]byte(fileTokenContents))
if err != nil {
t.Fatal(err)
}

u, err := NewSecretUnmarshler(WithTokenFile(file.Name()))
if err != nil {
t.Fatal(err)
}

if u.client.Token() != fileTokenContents {
// we purposely don't log api.Client.Token() in the
// unlikely event we pickup a production token
t.Fatalf("WithTokenFile options is not working. exptected token %s, got something else...", testToken)
}
}
Loading

0 comments on commit 38754ad

Please sign in to comment.