-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introducing secret Unmarshal function (#73)
* 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
Showing
5 changed files
with
699 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Oops, something went wrong.