-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
config: Mad sure config is generatable: Added unsafe option for Secrets.
Signed-off-by: Bartek Plotka <[email protected]>
- Loading branch information
Showing
6 changed files
with
235 additions
and
76 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 |
---|---|---|
|
@@ -343,11 +343,11 @@ func TestJSONMarshal(t *testing.T) { | |
} | ||
} | ||
|
||
func TestJSONMarshalSecret(t *testing.T) { | ||
func TestMarshalSecret(t *testing.T) { | ||
test := struct { | ||
S Secret | ||
S *Secret | ||
}{ | ||
S: Secret("test"), | ||
S: NewSecret("test"), | ||
} | ||
|
||
c, err := json.Marshal(test) | ||
|
@@ -358,14 +358,76 @@ func TestJSONMarshalSecret(t *testing.T) { | |
// u003c -> "<" | ||
// u003e -> ">" | ||
require.Equal(t, "{\"S\":\"\\u003csecret\\u003e\"}", string(c), "Secret not properly elided.") | ||
|
||
c, err = yaml.Marshal(test) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// u003c -> "<" | ||
// u003e -> ">" | ||
require.Equal(t, "s: \u003csecret\u003e\n", string(c), "Secret not properly elided.") | ||
} | ||
|
||
func TestMarshalUnsafeSecret(t *testing.T) { | ||
test := struct { | ||
S *Secret | ||
}{ | ||
S: NewUnsafeSecret("test"), | ||
} | ||
|
||
c, err := json.Marshal(test) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
require.Equal(t, "{\"S\":\"test\"}", string(c), "Unsafe Secret not marshaled.") | ||
|
||
c, err = yaml.Marshal(test) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
require.Equal(t, "s: test\n", string(c), "Unsafe Secret not marshaled.") | ||
} | ||
|
||
func TestUnmarshalSecret(t *testing.T) { | ||
b := []byte(`"secret"`) | ||
var u Secret | ||
|
||
err := json.Unmarshal(b, &u) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
require.Equal(t, "secret", u.String(), "Secret not properly unmarshalled from JSON.") | ||
require.Equal(t, false, u.unsafe, "Secret unmarhalled as unsafe from JSON.") | ||
|
||
err = yaml.Unmarshal(b, &u) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
require.Equal(t, "secret", u.String(), "Secret not properly unmarshalled from YAML.") | ||
require.Equal(t, false, u.unsafe, "Secret unmarhalled as unsafe from YAML.") | ||
} | ||
|
||
func TestUnmarshalEmptySecret(t *testing.T) { | ||
b := []byte(`""`) | ||
var u Secret | ||
|
||
err := json.Unmarshal(b, &u) | ||
require.Error(t, err, "expected error while unmarshalling empty secret from JSON.") | ||
|
||
err = yaml.Unmarshal(b, &u) | ||
require.Error(t, err, "expected error while unmarshalling empty secret from YAML.") | ||
} | ||
|
||
func TestMarshalSecretURL(t *testing.T) { | ||
urlp, err := url.Parse("http://example.com/") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
u := &SecretURL{urlp} | ||
u := NewSecretURL(URL{urlp}) | ||
|
||
c, err := json.Marshal(u) | ||
if err != nil { | ||
|
@@ -394,6 +456,39 @@ func TestMarshalSecretURL(t *testing.T) { | |
} | ||
} | ||
|
||
func TestMarshalUnsafeSecretURL(t *testing.T) { | ||
urlp, err := url.Parse("http://example.com/") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
u := NewUnsafeSecretURL(URL{urlp}) | ||
|
||
c, err := json.Marshal(u) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
require.Equal(t, "\"http://example.com/\"", string(c), "SecretURL not properly marshaled in JSON.") | ||
// Check that the marshaled data can be unmarshaled again. | ||
out := &SecretURL{} | ||
err = json.Unmarshal(c, out) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
c, err = yaml.Marshal(u) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
require.Equal(t, "http://example.com/\n", string(c), "SecretURL not properly marshaled in YAML.") | ||
// Check that the marshaled data can be unmarshaled again. | ||
out = &SecretURL{} | ||
err = yaml.Unmarshal(c, &out) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
func TestUnmarshalSecretURL(t *testing.T) { | ||
b := []byte(`"http://example.com/se cret"`) | ||
var u SecretURL | ||
|
@@ -552,9 +647,9 @@ func TestEmptyFieldsAndRegex(t *testing.T) { | |
ResolveTimeout: model.Duration(5 * time.Minute), | ||
SMTPSmarthost: "localhost:25", | ||
SMTPFrom: "[email protected]", | ||
HipchatAuthToken: "mysecret", | ||
HipchatAuthToken: NewSecret("mysecret"), | ||
HipchatAPIURL: mustParseURL("https://hipchat.foobar.org/"), | ||
SlackAPIURL: (*SecretURL)(mustParseURL("http://slack.example.com/")), | ||
SlackAPIURL: NewSecretURL(*mustParseURL("http://slack.example.com/")), | ||
SMTPRequireTLS: true, | ||
PagerdutyURL: mustParseURL("https://events.pagerduty.com/v2/enqueue"), | ||
OpsGenieAPIURL: mustParseURL("https://api.opsgenie.com/"), | ||
|
Oops, something went wrong.