Repository: prometheus/common
Pull request #294 (2021-04-27 16:59:31)

Make OAuth 2.0 client_id and client_secret mandatory
These should be mandatory as per the spec.

cc @roidelapluie 

LeviHarrison: Whoops, that too. Thanks!

Patch:
diff --git a/config/http_config.go b/config/http_config.go
index 7128074..b98ff7c 100644
--- a/config/http_config.go
+++ b/config/http_config.go
@@ -210,6 +210,15 @@ func (c *HTTPClientConfig) Validate() error {
 		if c.BasicAuth != nil {
 			return fmt.Errorf("at most one of basic_auth, oauth2 & authorization must be configured")
 		}
+		if len(c.OAuth2.ClientID) == 0 {
+			return fmt.Errorf("oauth2 client_id must be configured")
+		}
+		if len(c.OAuth2.ClientSecret) == 0 && len(c.OAuth2.ClientSecretFile) == 0 {
+			return fmt.Errorf("either oauth2 client_secret or client_secret_file must be configured")
+		}
+		if len(c.OAuth2.TokenURL) == 0 {
+			return fmt.Errorf("oauth2 token_url must be configured")
+		}
 		if len(c.OAuth2.ClientSecret) > 0 && len(c.OAuth2.ClientSecretFile) > 0 {
 			return fmt.Errorf("at most one of oauth2 client_secret & client_secret_file must be configured")
 		}


Test patch:
diff --git a/config/http_config_test.go b/config/http_config_test.go
index 173bf87..aff3b8a 100644
--- a/config/http_config_test.go
+++ b/config/http_config_test.go
@@ -107,6 +107,18 @@ var invalidHTTPClientConfigs = []struct {
 		httpClientConfigFile: "testdata/http.conf.oauth2-secret-and-file-set.bad.yml",
 		errMsg:               "at most one of oauth2 client_secret & client_secret_file must be configured",
 	},
+	{
+		httpClientConfigFile: "testdata/http.conf.oauth2-no-client-id.bad.yaml",
+		errMsg:               "oauth2 client_id must be configured",
+	},
+	{
+		httpClientConfigFile: "testdata/http.conf.oauth2-no-client-secret.bad.yaml",
+		errMsg:               "either oauth2 client_secret or client_secret_file must be configured",
+	},
+	{
+		httpClientConfigFile: "testdata/http.conf.oauth2-no-token-url.bad.yaml",
+		errMsg:               "oauth2 token_url must be configured",
+	},
 }
 
 func newTestServer(handler func(w http.ResponseWriter, r *http.Request)) (*httptest.Server, error) {
diff --git a/config/testdata/http.conf.oauth2-no-client-id.bad.yaml b/config/testdata/http.conf.oauth2-no-client-id.bad.yaml
new file mode 100644
index 0000000..73b20b8
--- /dev/null
+++ b/config/testdata/http.conf.oauth2-no-client-id.bad.yaml
@@ -0,0 +1,3 @@
+oauth2:
+  client_secret: "mysecret"
+  token_url: "http://auth"
diff --git a/config/testdata/http.conf.oauth2-no-client-secret.bad.yaml b/config/testdata/http.conf.oauth2-no-client-secret.bad.yaml
new file mode 100644
index 0000000..774a599
--- /dev/null
+++ b/config/testdata/http.conf.oauth2-no-client-secret.bad.yaml
@@ -0,0 +1,3 @@
+oauth2:
+  client_id: "myclientid"
+  token_url: "http://auth"
diff --git a/config/testdata/http.conf.oauth2-no-token-url.bad.yaml b/config/testdata/http.conf.oauth2-no-token-url.bad.yaml
new file mode 100644
index 0000000..a99a2c0
--- /dev/null
+++ b/config/testdata/http.conf.oauth2-no-token-url.bad.yaml
@@ -0,0 +1,3 @@
+oauth2:
+  client_id: "myclientid"
+  client_secret: "mysecret"
diff --git a/config/testdata/http.conf.oauth2-secret-and-file-set.bad.yml b/config/testdata/http.conf.oauth2-secret-and-file-set.bad.yml
index d9fc6f8..1c453cd 100644
--- a/config/testdata/http.conf.oauth2-secret-and-file-set.bad.yml
+++ b/config/testdata/http.conf.oauth2-secret-and-file-set.bad.yml
@@ -1,3 +1,5 @@
 oauth2:
+  client_id: "myclient"
   client_secret: "mysecret"
   client_secret_file: "mysecret"
+  token_url: "http://auth"