Skip to content

Commit

Permalink
Add oauth2 support to HTTP checks (#377)
Browse files Browse the repository at this point in the history
This adds support for checks that include OAuth2 settings. It requires:
 - client ID and secret, for authentication.
 - URL for the token endpoint, to obtain a token.
 - A list of scopes, service-specific.
 - An additional list of endpoint parameters (key-value pairs) that
   are passed to the token endpoint as additional POST fields.
  • Loading branch information
adriansr authored Nov 30, 2022
1 parent 1507e74 commit 257e2f6
Show file tree
Hide file tree
Showing 3 changed files with 779 additions and 216 deletions.
35 changes: 35 additions & 0 deletions internal/prober/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,44 @@ func settingsToModule(ctx context.Context, settings *sm.HttpSettings, logger zer
}
}

if settings.Oauth2Config != nil && settings.Oauth2Config.ClientId != "" {
var err error
m.HTTP.HTTPClientConfig.OAuth2, err = convertOAuth2Config(ctx, settings.Oauth2Config, logger.With().Str("prober", m.Prober).Logger())
if err != nil {
return m, fmt.Errorf("parsing OAuth2 settings: %w", err)
}
}

return m, nil
}

func convertOAuth2Config(ctx context.Context, cfg *sm.OAuth2Config, logger zerolog.Logger) (*promconfig.OAuth2, error) {
r := &promconfig.OAuth2{}
r.ClientID = cfg.ClientId
r.ClientSecret = promconfig.Secret(cfg.ClientSecret)
r.TokenURL = cfg.TokenURL
r.Scopes = make([]string, len(cfg.Scopes))
copy(r.Scopes, cfg.Scopes)
r.EndpointParams = make(map[string]string, len(cfg.EndpointParams))
for _, pair := range cfg.EndpointParams {
r.EndpointParams[pair.Name] = pair.Value
}
var err error
if cfg.ProxyURL != "" {
r.ProxyURL.URL, err = url.Parse(cfg.ProxyURL)
if err != nil {
return nil, fmt.Errorf("parsing proxy URL: %w", err)
}
}
if cfg.TlsConfig != nil {
r.TLSConfig, err = tls.SMtoProm(ctx, logger, cfg.TlsConfig)
if err != nil {
return nil, fmt.Errorf("parsing TLS config: %w", err)
}
}
return r, nil
}

func buildHttpHeaders(headers []string) map[string]string {
userAgentHeader := "user-agent"

Expand Down
Loading

0 comments on commit 257e2f6

Please sign in to comment.