File tree 2 files changed +39
-4
lines changed
2 files changed +39
-4
lines changed Original file line number Diff line number Diff line change @@ -27,8 +27,16 @@ const secretToken = "<secret>"
27
27
// Secret special type for storing secrets.
28
28
type Secret string
29
29
30
+ // MarshalSecretValue if set to true will expose Secret type
31
+ // through the marshal interfaces. Useful for outside projects
32
+ // that load and marshal the Prometheus config.
33
+ var MarshalSecretValue bool = false
34
+
30
35
// MarshalYAML implements the yaml.Marshaler interface for Secrets.
31
36
func (s Secret ) MarshalYAML () (interface {}, error ) {
37
+ if MarshalSecretValue {
38
+ return string (s ), nil
39
+ }
32
40
if s != "" {
33
41
return secretToken , nil
34
42
}
@@ -43,6 +51,9 @@ func (s *Secret) UnmarshalYAML(unmarshal func(interface{}) error) error {
43
51
44
52
// MarshalJSON implements the json.Marshaler interface for Secret.
45
53
func (s Secret ) MarshalJSON () ([]byte , error ) {
54
+ if MarshalSecretValue {
55
+ return json .Marshal (string (s ))
56
+ }
46
57
if len (s ) == 0 {
47
58
return json .Marshal ("" )
48
59
}
Original file line number Diff line number Diff line change @@ -28,9 +28,11 @@ func TestJSONMarshalSecret(t *testing.T) {
28
28
S Secret
29
29
}
30
30
for _ , tc := range []struct {
31
- desc string
32
- data tmp
33
- expected string
31
+ desc string
32
+ data tmp
33
+ expected string
34
+ trueValue bool
35
+ testYAML bool
34
36
}{
35
37
{
36
38
desc : "inhabited" ,
@@ -39,14 +41,36 @@ func TestJSONMarshalSecret(t *testing.T) {
39
41
data : tmp {"test" },
40
42
expected : "{\" S\" :\" \\ u003csecret\\ u003e\" }" ,
41
43
},
44
+ {
45
+ desc : "true value in JSON" ,
46
+ data : tmp {"test" },
47
+ expected : `{"S":"test"}` ,
48
+ trueValue : true ,
49
+ },
50
+ {
51
+ desc : "true value in YAML" ,
52
+ data : tmp {"test" },
53
+ expected : `s: test
54
+ ` ,
55
+ trueValue : true ,
56
+ testYAML : true ,
57
+ },
42
58
{
43
59
desc : "empty" ,
44
60
data : tmp {},
45
61
expected : "{\" S\" :\" \" }" ,
46
62
},
47
63
} {
48
64
t .Run (tc .desc , func (t * testing.T ) {
49
- c , err := json .Marshal (tc .data )
65
+ MarshalSecretValue = tc .trueValue
66
+
67
+ var marshalFN func (any ) ([]byte , error )
68
+ if tc .testYAML {
69
+ marshalFN = yaml .Marshal
70
+ } else {
71
+ marshalFN = json .Marshal
72
+ }
73
+ c , err := marshalFN (tc .data )
50
74
if err != nil {
51
75
t .Fatal (err )
52
76
}
You can’t perform that action at this time.
0 commit comments