forked from reddit/baseplate.go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This allows you to create a new secrets.Store using a MockFilewatcher with the secrets you pass into it rather than having to create a temporary directory and writing the secrets JSON yourself in tests. This change also exports some additional parts of the secrets package: * Encoding is now exported. This is a part of the GenericSecret struct which is used by NewTestSecrets but was not previously exported. * Export the strings that define the three secret types. Both of these are a part of the Baseplate spec so exporting them should be reasonable and they are unlikely to change.
- Loading branch information
1 parent
7924513
commit c2704da
Showing
7 changed files
with
539 additions
and
52 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
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,74 @@ | ||
package secrets | ||
|
||
import ( | ||
"encoding/base64" | ||
"encoding/json" | ||
) | ||
|
||
// Encoding represents the Encoding used to encode a secret. | ||
type Encoding int | ||
|
||
const ( | ||
// IdentityEncoding indicates no encoding beyond JSON itself. | ||
IdentityEncoding Encoding = iota | ||
// Base64Encoding indicates that the secret is base64 encoded. | ||
Base64Encoding | ||
) | ||
|
||
const ( | ||
identityEncodingJSON = `"identity"` | ||
identityEncodingStr = "identity" | ||
|
||
base64EncodingJSON = `"base64"` | ||
base64EncodingStr = "base64" | ||
) | ||
|
||
// MarshalJSON returns a JSON string representation of the encoding. | ||
func (e Encoding) MarshalJSON() ([]byte, error) { | ||
switch e { | ||
case IdentityEncoding: | ||
return []byte(identityEncodingJSON), nil | ||
case Base64Encoding: | ||
return []byte(base64EncodingJSON), nil | ||
default: | ||
return nil, ErrInvalidEncoding | ||
} | ||
} | ||
|
||
// UnmarshalJSON unmarshals the given JSON data into an encoding. | ||
func (e *Encoding) UnmarshalJSON(data []byte) error { | ||
var s string | ||
if err := json.Unmarshal(data, &s); err != nil { | ||
return err | ||
} | ||
switch s { | ||
case identityEncodingStr, "": | ||
*e = IdentityEncoding | ||
case base64EncodingStr: | ||
*e = Base64Encoding | ||
default: | ||
return ErrInvalidEncoding | ||
} | ||
return nil | ||
} | ||
|
||
func (e Encoding) decodeValue(value string) (Secret, error) { | ||
if value == "" { | ||
return nil, nil | ||
} | ||
switch e { | ||
case IdentityEncoding: | ||
return Secret(value), nil | ||
default: | ||
data, err := base64.StdEncoding.DecodeString(value) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return Secret(data), nil | ||
} | ||
} | ||
|
||
var ( | ||
_ json.Marshaler = Encoding(0) | ||
_ json.Unmarshaler = (*Encoding)(nil) | ||
) |
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,98 @@ | ||
package secrets | ||
|
||
import ( | ||
"errors" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestEncoding(t *testing.T) { | ||
t.Parallel() | ||
|
||
cases := []struct { | ||
name string | ||
enc Encoding | ||
marshalled string | ||
err error | ||
}{ | ||
{ | ||
name: "invalid", | ||
enc: -1, | ||
marshalled: `"invalid"`, | ||
err: ErrInvalidEncoding, | ||
}, | ||
{ | ||
name: "identity", | ||
enc: IdentityEncoding, | ||
marshalled: identityEncodingJSON, | ||
}, | ||
{ | ||
name: "base64", | ||
enc: Base64Encoding, | ||
marshalled: base64EncodingJSON, | ||
}, | ||
} | ||
|
||
for _, _c := range cases { | ||
c := _c | ||
t.Run( | ||
"c.name", | ||
func(t *testing.T) { | ||
t.Run( | ||
"MarshalJSON", | ||
func(t *testing.T) { | ||
b, err := c.enc.MarshalJSON() | ||
if !errors.Is(err, c.err) { | ||
t.Fatalf("error mismatch, expected %#v, got %#v", c.err, err) | ||
} | ||
if err != nil { | ||
return | ||
} | ||
|
||
marshalled := string(b) | ||
if strings.Compare(marshalled, c.marshalled) != 0 { | ||
t.Fatalf("value mismatch, expected %q, got %q", c.marshalled, marshalled) | ||
} | ||
}, | ||
) | ||
|
||
t.Run( | ||
"UnmarshalJSON", | ||
func(t *testing.T) { | ||
var e Encoding | ||
err := (&e).UnmarshalJSON([]byte(c.marshalled)) | ||
if !errors.Is(err, c.err) { | ||
t.Fatalf("error mismatch, expected %#v, got %#v", c.err, err) | ||
} | ||
if err != nil { | ||
return | ||
} | ||
|
||
if e != c.enc { | ||
t.Fatalf("encoding does not match, expected %v, got %v", c.enc, e) | ||
} | ||
}, | ||
) | ||
}, | ||
) | ||
} | ||
|
||
t.Run( | ||
"UnmarshalJSON/fallback", | ||
func(t *testing.T) { | ||
var e Encoding | ||
err := (&e).UnmarshalJSON([]byte(`""`)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if e != IdentityEncoding { | ||
t.Fatalf( | ||
"encoding does not match, expected %v, got %v", | ||
IdentityEncoding, | ||
e, | ||
) | ||
} | ||
}, | ||
) | ||
} |
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
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
Oops, something went wrong.