MCPcopy Create free account
hub / github.com/prometheus/common / Validate

Method Validate

config/http_config.go:331–395  ·  view source on GitHub ↗

Validate validates the HTTPClientConfig to check only one of BearerToken, BasicAuth and BearerTokenFile is configured. It also validates that ProxyURL is set if ProxyConnectHeader is set.

()

Source from the content-addressed store, hash-verified

329// BasicAuth and BearerTokenFile is configured. It also validates that ProxyURL
330// is set if ProxyConnectHeader is set.
331func (c *HTTPClientConfig) Validate() error {
332 // Backwards compatibility with the bearer_token field.
333 if len(c.BearerToken) > 0 && len(c.BearerTokenFile) > 0 {
334 return fmt.Errorf("at most one of bearer_token & bearer_token_file must be configured")
335 }
336 if (c.BasicAuth != nil || c.OAuth2 != nil) && (len(c.BearerToken) > 0 || len(c.BearerTokenFile) > 0) {
337 return fmt.Errorf("at most one of basic_auth, oauth2, bearer_token & bearer_token_file must be configured")
338 }
339 if c.BasicAuth != nil && (string(c.BasicAuth.Username) != "" && c.BasicAuth.UsernameFile != "") {
340 return fmt.Errorf("at most one of basic_auth username & username_file must be configured")
341 }
342 if c.BasicAuth != nil && (string(c.BasicAuth.Password) != "" && c.BasicAuth.PasswordFile != "") {
343 return fmt.Errorf("at most one of basic_auth password & password_file must be configured")
344 }
345 if c.Authorization != nil {
346 if len(c.BearerToken) > 0 || len(c.BearerTokenFile) > 0 {
347 return fmt.Errorf("authorization is not compatible with bearer_token & bearer_token_file")
348 }
349 if string(c.Authorization.Credentials) != "" && c.Authorization.CredentialsFile != "" {
350 return fmt.Errorf("at most one of authorization credentials & credentials_file must be configured")
351 }
352 c.Authorization.Type = strings.TrimSpace(c.Authorization.Type)
353 if len(c.Authorization.Type) == 0 {
354 c.Authorization.Type = "Bearer"
355 }
356 if strings.ToLower(c.Authorization.Type) == "basic" {
357 return fmt.Errorf(`authorization type cannot be set to "basic", use "basic_auth" instead`)
358 }
359 if c.BasicAuth != nil || c.OAuth2 != nil {
360 return fmt.Errorf("at most one of basic_auth, oauth2 & authorization must be configured")
361 }
362 } else {
363 if len(c.BearerToken) > 0 {
364 c.Authorization = &Authorization{Credentials: c.BearerToken}
365 c.Authorization.Type = "Bearer"
366 c.BearerToken = ""
367 }
368 if len(c.BearerTokenFile) > 0 {
369 c.Authorization = &Authorization{CredentialsFile: c.BearerTokenFile}
370 c.Authorization.Type = "Bearer"
371 c.BearerTokenFile = ""
372 }
373 }
374 if c.OAuth2 != nil {
375 if c.BasicAuth != nil {
376 return fmt.Errorf("at most one of basic_auth, oauth2 & authorization must be configured")
377 }
378 if len(c.OAuth2.ClientID) == 0 {
379 return fmt.Errorf("oauth2 client_id must be configured")
380 }
381 if len(c.OAuth2.ClientSecret) == 0 && len(c.OAuth2.ClientSecretFile) == 0 {
382 return fmt.Errorf("either oauth2 client_secret or client_secret_file must be configured")
383 }
384 if len(c.OAuth2.TokenURL) == 0 {
385 return fmt.Errorf("oauth2 token_url must be configured")
386 }
387 if len(c.OAuth2.ClientSecret) > 0 && len(c.OAuth2.ClientSecretFile) > 0 {
388 return fmt.Errorf("at most one of oauth2 client_secret & client_secret_file must be configured")

Callers 2

UnmarshalYAMLMethod · 0.95
UnmarshalJSONMethod · 0.95

Calls 1

ValidateMethod · 0.45

Tested by

no test coverage detected