Skip to content

Commit 2c494f4

Browse files
Merge pull request #1598 from JoaoBraveCoding/mon-2207
MON-2207: Expose Authorization settings for remote write in the CMO configuration
2 parents 11c1877 + a6b416a commit 2c494f4

File tree

5 files changed

+119
-31
lines changed

5 files changed

+119
-31
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- [#1350](https://github.com/openshift/cluster-monitoring-operator/pull/1350) Support label scrape limits in user-workload monitoring
99
- [#1601](https://github.com/openshift/cluster-monitoring-operator/pull/1601) Expose the /federate endpoint of UWM Prometheus as a service
1010
- [#1617](https://github.com/openshift/cluster-monitoring-operator/pull/1617) Add Oauth2 setting to PrometheusK8s remoteWrite config
11+
- [#1598](https://github.com/openshift/cluster-monitoring-operator/pull/1598) Expose Authorization settings for remote write in the CMO configuration
1112

1213
## 4.10
1314

Diff for: pkg/manifests/config.go

+2
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ type RemoteWriteSpec struct {
156156
BasicAuth *monv1.BasicAuth `json:"basicAuth,omitempty"`
157157
// Bearer token for remote write.
158158
BearerTokenFile string `json:"bearerTokenFile,omitempty"`
159+
// Authorization section for remote write
160+
Authorization *monv1.SafeAuthorization `json:"authorization,omitempty"`
159161
// TLS Config to use for remote write.
160162
TLSConfig *monv1.SafeTLSConfig `json:"tlsConfig,omitempty"`
161163
// Optional ProxyURL

Diff for: pkg/manifests/manifests.go

+5
Original file line numberDiff line numberDiff line change
@@ -4235,6 +4235,11 @@ func addRemoteWriteConfigs(clusterID string, rw []monv1.RemoteWriteSpec, rwTarge
42354235
SafeTLSConfig: *target.TLSConfig,
42364236
}
42374237
}
4238+
if target.Authorization != nil {
4239+
rwConf.Authorization = &monv1.Authorization{
4240+
SafeAuthorization: *target.Authorization,
4241+
}
4242+
}
42384243
rw = append(rw, rwConf)
42394244
}
42404245
return rw

Diff for: pkg/manifests/manifests_test.go

+110-30
Original file line numberDiff line numberDiff line change
@@ -1088,11 +1088,7 @@ func TestPrometheusK8sRemoteWriteURLs(t *testing.T) {
10881088
name: "default config",
10891089

10901090
config: func() *Config {
1091-
c, err := NewConfigFromString("")
1092-
if err != nil {
1093-
t.Fatal(err)
1094-
}
1095-
1091+
c := NewDefaultConfig()
10961092
return c
10971093
},
10981094

@@ -1102,11 +1098,7 @@ func TestPrometheusK8sRemoteWriteURLs(t *testing.T) {
11021098
name: "legacy telemetry",
11031099

11041100
config: func() *Config {
1105-
c, err := NewConfigFromString("")
1106-
if err != nil {
1107-
t.Fatal(err)
1108-
}
1109-
1101+
c := NewDefaultConfig()
11101102
c.ClusterMonitoringConfiguration.TelemeterClientConfig.ClusterID = "123"
11111103
c.ClusterMonitoringConfiguration.TelemeterClientConfig.Token = "secret"
11121104

@@ -1119,11 +1111,7 @@ func TestPrometheusK8sRemoteWriteURLs(t *testing.T) {
11191111
name: "legacy telemetry and custom remote write",
11201112

11211113
config: func() *Config {
1122-
c, err := NewConfigFromString("")
1123-
if err != nil {
1124-
t.Fatal(err)
1125-
}
1126-
1114+
c := NewDefaultConfig()
11271115
c.ClusterMonitoringConfiguration.TelemeterClientConfig.ClusterID = "123"
11281116
c.ClusterMonitoringConfiguration.TelemeterClientConfig.Token = "secret"
11291117
c.ClusterMonitoringConfiguration.PrometheusK8sConfig.RemoteWrite = []RemoteWriteSpec{{URL: "http://custom"}}
@@ -1139,11 +1127,7 @@ func TestPrometheusK8sRemoteWriteURLs(t *testing.T) {
11391127
name: "remote write telemetry",
11401128

11411129
config: func() *Config {
1142-
c, err := NewConfigFromString("")
1143-
if err != nil {
1144-
t.Fatal(err)
1145-
}
1146-
1130+
c := NewDefaultConfig()
11471131
c.SetRemoteWrite(true)
11481132
c.ClusterMonitoringConfiguration.TelemeterClientConfig.ClusterID = "123"
11491133
c.ClusterMonitoringConfiguration.TelemeterClientConfig.Token = "secret"
@@ -1159,11 +1143,7 @@ func TestPrometheusK8sRemoteWriteURLs(t *testing.T) {
11591143
name: "remote write telemetry and custom remote write",
11601144

11611145
config: func() *Config {
1162-
c, err := NewConfigFromString("")
1163-
if err != nil {
1164-
t.Fatal(err)
1165-
}
1166-
1146+
c := NewDefaultConfig()
11671147
c.SetRemoteWrite(true)
11681148
c.ClusterMonitoringConfiguration.TelemeterClientConfig.ClusterID = "123"
11691149
c.ClusterMonitoringConfiguration.TelemeterClientConfig.Token = "secret"
@@ -1181,11 +1161,7 @@ func TestPrometheusK8sRemoteWriteURLs(t *testing.T) {
11811161
name: "remote write telemetry with custom url and custom remote write",
11821162

11831163
config: func() *Config {
1184-
c, err := NewConfigFromString("")
1185-
if err != nil {
1186-
t.Fatal(err)
1187-
}
1188-
1164+
c := NewDefaultConfig()
11891165
c.SetRemoteWrite(true)
11901166
c.ClusterMonitoringConfiguration.TelemeterClientConfig.TelemeterServerURL = "http://custom-telemeter"
11911167
c.ClusterMonitoringConfiguration.TelemeterClientConfig.ClusterID = "123"
@@ -1292,6 +1268,107 @@ func TestPrometheusK8sRemoteWriteOauth2(t *testing.T) {
12921268
}
12931269

12941270
}
1271+
func TestRemoteWriteAuthorizationConfig(t *testing.T) {
1272+
for _, tc := range []struct {
1273+
name string
1274+
config string
1275+
checkFn []func(*testing.T, monv1.RemoteWriteSpec)
1276+
}{
1277+
{
1278+
name: "basic authentication configuration",
1279+
config: `prometheusK8s:
1280+
remoteWrite:
1281+
- url: "https://basicAuth.remotewrite.com/api/write"
1282+
basicAuth:
1283+
username:
1284+
name: remoteWriteAuth
1285+
key: user
1286+
password:
1287+
name: remoteWriteAuth
1288+
key: password
1289+
`,
1290+
checkFn: []func(*testing.T, monv1.RemoteWriteSpec){
1291+
func(t *testing.T, target monv1.RemoteWriteSpec) {
1292+
if target.BasicAuth.Username.Name != "remoteWriteAuth" {
1293+
t.Fatalf("Name field not correct in section RemoteWriteSpec.BasicAuth.Username expected 'remoteWriteAuth', got %s", target.BasicAuth.Username.Name)
1294+
}
1295+
if target.BasicAuth.Username.Key != "user" {
1296+
t.Fatalf("Key field not correct in section RemoteWriteSpec.BasicAuth.Username expected 'user', got %s", target.BasicAuth.Username.Key)
1297+
}
1298+
if target.BasicAuth.Password.Name != "remoteWriteAuth" {
1299+
t.Fatalf("Name field not correct in section RemoteWriteSpec.BasicAuth.Password expected 'remoteWriteAuth', got %s", target.BasicAuth.Password.Name)
1300+
}
1301+
if target.BasicAuth.Password.Key != "password" {
1302+
t.Fatalf("Key field not correct in section RemoteWriteSpec.BasicAuth.Password expected 'password', got %s", target.BasicAuth.Password.Key)
1303+
}
1304+
},
1305+
},
1306+
},
1307+
{
1308+
name: "bearerTokenFile authentication configuration",
1309+
config: `prometheusK8s:
1310+
remoteWrite:
1311+
- url: "https://bearerTokenFile.remotewrite.com/api/write"
1312+
bearerTokenFile: "/secret/remoteWriteAuth"
1313+
`,
1314+
checkFn: []func(*testing.T, monv1.RemoteWriteSpec){
1315+
func(t *testing.T, target monv1.RemoteWriteSpec) {
1316+
if target.BearerTokenFile != "/secret/remoteWriteAuth" {
1317+
t.Fatalf("BearerTokenFile field not correct in section RemoteWriteSpec expected '/secret/remoteWriteAuth', got %s", target.BearerTokenFile)
1318+
}
1319+
},
1320+
},
1321+
},
1322+
{
1323+
name: "authorization authentication configuration",
1324+
config: `prometheusK8s:
1325+
remoteWrite:
1326+
- url: "https://authorization.remotewrite.com/api/write"
1327+
authorization:
1328+
type: Bearer
1329+
credentials:
1330+
name: remoteWriteAuth
1331+
key: token
1332+
`,
1333+
checkFn: []func(*testing.T, monv1.RemoteWriteSpec){
1334+
func(t *testing.T, target monv1.RemoteWriteSpec) {
1335+
if target.Authorization.Type != "Bearer" {
1336+
t.Fatalf("Bearer field not correct in section RemoteWriteSpec expected 'Bearer', got %s", target.Authorization.Type)
1337+
}
1338+
if target.Authorization.Credentials.Name != "remoteWriteAuth" {
1339+
t.Fatalf("Name field not correct in section RemoteWriteSpec.Authorization.Credentials expected 'remoteWriteAuth', got %s", target.Authorization.Credentials.Name)
1340+
}
1341+
if target.Authorization.Credentials.Key != "token" {
1342+
t.Fatalf("Key field not correct in section RemoteWriteSpec.Authorization.Credentials expected 'token', got %s", target.Authorization.Credentials.Key)
1343+
}
1344+
},
1345+
},
1346+
},
1347+
} {
1348+
t.Run(tc.name, func(t *testing.T) {
1349+
c, err := NewConfigFromString(tc.config)
1350+
if err != nil {
1351+
t.Fatal(err)
1352+
}
1353+
f := NewFactory("openshift-monitoring", "openshift-user-workload-monitoring", c, defaultInfrastructureReader(), &fakeProxyReader{}, NewAssets(assetsPath), &APIServerConfig{}, &configv1.Console{})
1354+
p, err := f.PrometheusK8s(
1355+
&v1.Secret{ObjectMeta: metav1.ObjectMeta{Name: "foo"}},
1356+
&v1.ConfigMap{ObjectMeta: metav1.ObjectMeta{Name: "foo"}},
1357+
)
1358+
if err != nil {
1359+
t.Fatal(err)
1360+
}
1361+
if len(p.Spec.RemoteWrite) != len(tc.checkFn) {
1362+
t.Fatalf("got %d check functions but only %d RemoteWrite targets", len(tc.checkFn), len(p.Spec.RemoteWrite))
1363+
}
1364+
1365+
for i, target := range p.Spec.RemoteWrite {
1366+
tc.checkFn[i](t, target)
1367+
}
1368+
})
1369+
1370+
}
1371+
}
12951372

12961373
func TestPrometheusK8sConfiguration(t *testing.T) {
12971374
c, err := NewConfigFromString(`prometheusK8s:
@@ -1753,6 +1830,9 @@ func TestPrometheusK8sAdditionalAlertManagerConfigsSecret(t *testing.T) {
17531830
&v1.Secret{ObjectMeta: metav1.ObjectMeta{Name: "foo"}},
17541831
&v1.ConfigMap{ObjectMeta: metav1.ObjectMeta{Name: "foo"}},
17551832
)
1833+
if err != nil {
1834+
t.Fatal(err)
1835+
}
17561836

17571837
secrets := make(map[string]struct{})
17581838
for _, s := range p.Spec.Secrets {

Diff for: test/e2e/prometheus_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ func TestPrometheusRemoteWrite(t *testing.T) {
254254

255255
cmoConfigMap := fmt.Sprintf(`prometheusK8s:
256256
logLevel: debug
257-
remoteWrite: %s
257+
remoteWrite:%s
258258
`, rw)
259259

260260
t.Run(tc.name, func(t *testing.T) {

0 commit comments

Comments
 (0)