Skip to content

Commit a237946

Browse files
committed
secret & url {un,}marshaling
Signed-off-by: Owen Diehl <[email protected]>
1 parent a415517 commit a237946

File tree

6 files changed

+129
-3
lines changed

6 files changed

+129
-3
lines changed

config/config.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,20 @@
1616

1717
package config
1818

19-
import "path/filepath"
19+
import (
20+
"encoding/json"
21+
"path/filepath"
22+
)
23+
24+
const secretToken = "<secret>"
2025

2126
// Secret special type for storing secrets.
2227
type Secret string
2328

2429
// MarshalYAML implements the yaml.Marshaler interface for Secrets.
2530
func (s Secret) MarshalYAML() (interface{}, error) {
2631
if s != "" {
27-
return "<secret>", nil
32+
return secretToken, nil
2833
}
2934
return nil, nil
3035
}
@@ -35,6 +40,11 @@ func (s *Secret) UnmarshalYAML(unmarshal func(interface{}) error) error {
3540
return unmarshal((*plain)(s))
3641
}
3742

43+
// MarshalJSON implements the json.Marshaler interface for Secret.
44+
func (s Secret) MarshalJSON() ([]byte, error) {
45+
return json.Marshal(secretToken)
46+
}
47+
3848
// DirectorySetter is a config type that contains file paths that may
3949
// be relative to the file containing the config.
4050
type DirectorySetter interface {

config/config_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2021 The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
// +build go1.8
15+
16+
package config
17+
18+
import (
19+
"encoding/json"
20+
"testing"
21+
22+
"github.com/stretchr/testify/require"
23+
)
24+
25+
func TestJSONMarshalSecret(t *testing.T) {
26+
test := struct {
27+
S Secret
28+
}{
29+
S: Secret("test"),
30+
}
31+
32+
c, err := json.Marshal(test)
33+
if err != nil {
34+
t.Fatal(err)
35+
}
36+
37+
// u003c -> "<"
38+
// u003e -> ">"
39+
require.Equal(t, "{\"S\":\"\\u003csecret\\u003e\"}", string(c), "Secret not properly elided.")
40+
}

config/http_config.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"crypto/sha256"
2222
"crypto/tls"
2323
"crypto/x509"
24+
"encoding/json"
2425
"fmt"
2526
"io/ioutil"
2627
"net"
@@ -110,6 +111,28 @@ func (u URL) MarshalYAML() (interface{}, error) {
110111
return nil, nil
111112
}
112113

114+
// UnmarshalJSON implements the json.Marshaler interface for URL.
115+
func (u *URL) UnmarshalJSON(data []byte) error {
116+
var s string
117+
if err := json.Unmarshal(data, &s); err != nil {
118+
return err
119+
}
120+
urlp, err := url.Parse(s)
121+
if err != nil {
122+
return err
123+
}
124+
u.URL = urlp
125+
return nil
126+
}
127+
128+
// MarshalJSON implements the json.Marshaler interface for URL.
129+
func (u URL) MarshalJSON() ([]byte, error) {
130+
if u.URL != nil {
131+
return json.Marshal(u.URL.String())
132+
}
133+
return nil, nil
134+
}
135+
113136
// OAuth2 is the oauth2 client configuration.
114137
type OAuth2 struct {
115138
ClientID string `yaml:"client_id" json:"client_id"`
@@ -236,6 +259,16 @@ func (c *HTTPClientConfig) UnmarshalYAML(unmarshal func(interface{}) error) erro
236259
return c.Validate()
237260
}
238261

262+
// UnmarshalJSON implements the json.Marshaler interface for URL.
263+
func (c *HTTPClientConfig) UnmarshalJSON(data []byte) error {
264+
type plain HTTPClientConfig
265+
*c = DefaultHTTPClientConfig
266+
if err := json.Unmarshal(data, (*plain)(c)); err != nil {
267+
return err
268+
}
269+
return c.Validate()
270+
}
271+
239272
// UnmarshalYAML implements the yaml.Unmarshaler interface.
240273
func (a *BasicAuth) UnmarshalYAML(unmarshal func(interface{}) error) error {
241274
type plain BasicAuth

config/http_config_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"net"
2727
"net/http"
2828
"net/http/httptest"
29+
"net/url"
2930
"os"
3031
"path/filepath"
3132
"reflect"
@@ -36,6 +37,7 @@ import (
3637
"testing"
3738
"time"
3839

40+
"github.com/stretchr/testify/require"
3941
yaml "gopkg.in/yaml.v2"
4042
)
4143

@@ -1276,3 +1278,40 @@ endpoint_params:
12761278
t.Fatalf("Expected authorization header to be 'Bearer 12345', got '%s'", authorization)
12771279
}
12781280
}
1281+
1282+
func TestMarshalURL(t *testing.T) {
1283+
urlp, err := url.Parse("http://example.com/")
1284+
if err != nil {
1285+
t.Fatal(err)
1286+
}
1287+
u := &URL{urlp}
1288+
1289+
c, err := json.Marshal(u)
1290+
if err != nil {
1291+
t.Fatal(err)
1292+
}
1293+
require.Equal(t, "\"http://example.com/\"", string(c), "URL not properly marshaled in JSON.")
1294+
1295+
c, err = yaml.Marshal(u)
1296+
if err != nil {
1297+
t.Fatal(err)
1298+
}
1299+
require.Equal(t, "http://example.com/\n", string(c), "URL not properly marshaled in YAML.")
1300+
}
1301+
1302+
func TestUnmarshalURL(t *testing.T) {
1303+
b := []byte(`"http://example.com/a b"`)
1304+
var u URL
1305+
1306+
err := json.Unmarshal(b, &u)
1307+
if err != nil {
1308+
t.Fatal(err)
1309+
}
1310+
require.Equal(t, "http://example.com/a%20b", u.String(), "URL not properly unmarshaled in JSON.")
1311+
1312+
err = yaml.Unmarshal(b, &u)
1313+
if err != nil {
1314+
t.Fatal(err)
1315+
}
1316+
require.Equal(t, "http://example.com/a%20b", u.String(), "URL not properly unmarshaled in YAML.")
1317+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ require (
1212
github.com/prometheus/client_golang v1.7.1
1313
github.com/prometheus/client_model v0.2.0
1414
github.com/sirupsen/logrus v1.6.0
15+
github.com/stretchr/testify v1.7.0
1516
golang.org/x/net v0.0.0-20200625001655-4c5254603344
1617
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421
1718
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae

go.sum

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,9 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
261261
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
262262
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
263263
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
264-
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
265264
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
265+
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
266+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
266267
github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
267268
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
268269
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
@@ -409,6 +410,8 @@ gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
409410
gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
410411
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
411412
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
413+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
414+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
412415
honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
413416
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
414417
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

0 commit comments

Comments
 (0)