Repository navigation
Expand file tree
/
Copy pathsecrets_test.go
More file actions
66 lines (62 loc) · 2.44 KB
/
Copy pathsecrets_test.go
File metadata and controls
66 lines (62 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package sdk
import (
"encoding/json"
"fmt"
"strings"
"testing"
)
func TestPrintingAModelMasksItsSecrets(t *testing.T) {
created := ApiConfigCreated{ApiKey: "KEY", ApiKeySecret: "hunter2", Name: "ci"}
aws := AwsVmConfig{AwsSecretAccessKey: Value("wJalrXUtnFEMI")}
holder := struct {
Key *ApiConfigCreated
AWS AwsVmConfig
}{&created, aws}
for _, s := range []string{
fmt.Sprint(created), fmt.Sprintf("%v", &created), fmt.Sprintf("%+v", created), fmt.Sprintf("%+v", holder),
} {
if strings.Contains(s, "hunter2") || strings.Contains(s, "wJalrXUtnFEMI") {
t.Errorf("a secret is printed: %s", s)
}
}
if s := fmt.Sprint(created); !strings.Contains(s, "KEY") || !strings.Contains(s, "***") {
t.Errorf("printed: %s", s)
}
// The value itself, and its JSON, keep the secrets.
b, _ := json.Marshal(holder)
if created.ApiKeySecret != "hunter2" || !strings.Contains(string(b), "hunter2") || !strings.Contains(string(b), "wJalrXUtnFEMI") {
t.Errorf("the value lost its secret: %s", b)
}
if v, _ := aws.AwsSecretAccessKey.Get(); v != "wJalrXUtnFEMI" {
t.Errorf("printing changed the Nullable it copied: %q", v)
}
}
func TestPrintingAUnionMasksItsSecrets(t *testing.T) {
var u VmProviderConfigResponse
if err := u.UnmarshalJSON([]byte(`{"vm_provider_name":"AWS","aws_access_key_id":"AKIA","aws_secret_access_key":"wJalrXUtnFEMI","nested":[{"password":"pw-1"}]}`)); err != nil {
t.Fatal(err)
}
for _, s := range []string{fmt.Sprint(u), fmt.Sprintf("%+v", u), fmt.Sprintf("%+v", struct{ R *VmProviderConfigResponse }{&u})} {
if strings.Contains(s, "wJalrXUtnFEMI") || strings.Contains(s, "pw-1") || !strings.Contains(s, "AKIA") {
t.Errorf("printed: %s", s)
}
}
if b, _ := u.MarshalJSON(); !strings.Contains(string(b), "wJalrXUtnFEMI") {
t.Errorf("the union lost its secret: %s", b)
}
}
func TestPrintingGCPCredentialsMasksTheWholeObject(t *testing.T) {
var u VmProviderConfigResponse
if err := u.UnmarshalJSON([]byte(`{"vm_provider_name":"GCP","gcp_credentials":{"private_key":"REVIEW_FAKE_PRIVATE_KEY","client_email":"svc@example.com"}}`)); err != nil {
t.Fatal(err)
}
gcp, err := u.AsGcpVmConfig()
if err != nil {
t.Fatal(err)
}
for _, s := range []string{fmt.Sprintf("%+v", u), fmt.Sprintf("%+v", gcp), fmt.Sprint(&gcp), fmt.Sprintf("%+v", gcp.GcpCredentials), fmt.Sprint(gcp.GcpCredentials.MustGet())} {
if strings.Contains(s, "REVIEW_FAKE_PRIVATE_KEY") || strings.Contains(s, "svc@example.com") {
t.Errorf("printed: %s", s)
}
}
}