Skip to content

Commit

Permalink
fix: create oauth2 client credentials config with audience panics.
Browse files Browse the repository at this point in the history
Initiate EndpointParams map to prevent the panics.
  • Loading branch information
irving committed Nov 21, 2023
1 parent 4c4b821 commit f852a0e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
10 changes: 6 additions & 4 deletions plugins/common/oauth/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package oauth
import (
"context"
"net/http"
"net/url"

"golang.org/x/oauth2"
"golang.org/x/oauth2/clientcredentials"
Expand All @@ -23,10 +24,11 @@ func (o *OAuth2Config) CreateOauth2Client(ctx context.Context, client *http.Clie
}

oauthConfig := clientcredentials.Config{
ClientID: o.ClientID,
ClientSecret: o.ClientSecret,
TokenURL: o.TokenURL,
Scopes: o.Scopes,
ClientID: o.ClientID,
ClientSecret: o.ClientSecret,
TokenURL: o.TokenURL,
Scopes: o.Scopes,
EndpointParams: make(url.Values),
}

if o.Audience != "" {
Expand Down
28 changes: 28 additions & 0 deletions plugins/outputs/http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,34 @@ func TestOAuthClientCredentialsGrant(t *testing.T) {
w.WriteHeader(http.StatusOK)
},
},
{
name: "audience",
plugin: &HTTP{
URL: u.String() + "/write",
HTTPClientConfig: httpconfig.HTTPClientConfig{
OAuth2Config: oauth.OAuth2Config{
ClientID: "howdy",
ClientSecret: "secret",
TokenURL: u.String() + "/token",
Scopes: []string{"urn:opc:idm:__myscopes__"},
Audience: "audience",
},
},
},
tokenHandler: func(t *testing.T, w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
values := url.Values{}
values.Add("access_token", token)
values.Add("token_type", "bearer")
values.Add("expires_in", "3600")
_, err = w.Write([]byte(values.Encode()))
require.NoError(t, err)
},
handler: func(t *testing.T, w http.ResponseWriter, r *http.Request) {
require.Equal(t, []string{"Bearer " + token}, r.Header["Authorization"])
w.WriteHeader(http.StatusOK)
},
},
}

for _, tt := range tests {
Expand Down

0 comments on commit f852a0e

Please sign in to comment.