-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[extension/oauth2clientauth] Enable dynamically reading ClientID and …
…ClientSecret from files (#26310) **Description:** This PR implements the feature described in detail in the issue linked below. In a nutshell, it extends the `oauth2clientauth` extension to read ClientID and/or ClientSecret from files whenever a new token is needed for the OAuth flow. As a result, the extension can use updated credentials (when the old ones expire for example) without the need to restart the OTEL collector, as long as the file contents are in sync. **Link to tracking Issue:** #26117 **Testing:** Apart from the unit testing you can see in the PR, I've tested this feature in two real-life environments: 1. As a systemd service exporting `otlphttp` data 2. A Kubernetes microservice (deployed by an OpenTelemetryCollector CR) exporting `otlphttp` data In both cases, the collectors export the data to a service which sits behind an OIDC authentication proxy. Using the `oauth2clientauth` extension, the `otlphttp` exporter hits the authentication provider to issue tokens for the OIDC client and successfully authenticates to the service. In my cases, the ClientSecret gets rotated quite frequently and there is a stack making sure the ClientID and ClientSecret in the corresponding files are up-to-date. **Documentation:** I have extended the extension's README file. I'm open to more suggestions! cc @jpkrohling @pavankrish123
- Loading branch information
Showing
8 changed files
with
252 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Use this changelog template to create an entry for release notes. | ||
|
||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: enhancement | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) | ||
component: oauth2clientauthextension | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: Enable dynamically reading ClientID and ClientSecret from files | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [26117] | ||
|
||
# (Optional) One or more lines of additional information to render under the primary note. | ||
# These lines will be padded with 2 spaces and then inserted directly into the document. | ||
# Use pipe (|) for multiline entries. | ||
subtext: | | ||
- Read the client ID and/or secret from a file by specifying the file path to the ClientIDFile (`client_id_file`) and ClientSecretFile (`client_secret_file`) fields respectively. | ||
- The file is read every time the client issues a new token. This means that the corresponding value can change dynamically during the execution by modifying the file contents. | ||
# If your change doesn't affect end users or the exported elements of any package, | ||
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. | ||
# Optional: The change log or logs in which this entry should be included. | ||
# e.g. '[user]' or '[user, api]' | ||
# Include 'user' if the change is relevant to end users. | ||
# Include 'api' if there is a change to a library API. | ||
# Default: '[user]' | ||
change_logs: [user, api] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
extension/oauth2clientauthextension/clientcredentialsconfig.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package oauth2clientauthextension // import "github.com/open-telemetry/opentelemetry-collector-contrib/extension/oauth2clientauthextension" | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"go.uber.org/multierr" | ||
"golang.org/x/oauth2" | ||
"golang.org/x/oauth2/clientcredentials" | ||
) | ||
|
||
// clientCredentialsConfig is a clientcredentials.Config wrapper to allow | ||
// values read from files in the ClientID and ClientSecret fields. | ||
// | ||
// Values from files can be retrieved by populating the ClientIDFile or | ||
// the ClientSecretFile fields with the path to the file. | ||
// | ||
// Priority: File > Raw value | ||
// | ||
// Example - Retrieve secret from file: | ||
// | ||
// cfg := clientCredentialsConfig{ | ||
// Config: clientcredentials.Config{ | ||
// ClientID: "clientId", | ||
// ... | ||
// }, | ||
// ClientSecretFile: "/path/to/client/secret", | ||
// } | ||
type clientCredentialsConfig struct { | ||
clientcredentials.Config | ||
|
||
ClientIDFile string | ||
ClientSecretFile string | ||
} | ||
|
||
type clientCredentialsTokenSource struct { | ||
ctx context.Context | ||
config *clientCredentialsConfig | ||
} | ||
|
||
// clientCredentialsTokenSource implements TokenSource | ||
var _ oauth2.TokenSource = (*clientCredentialsTokenSource)(nil) | ||
|
||
func readCredentialsFile(path string) (string, error) { | ||
f, err := os.ReadFile(path) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to read credentials file %q: %w", path, err) | ||
} | ||
|
||
credential := strings.TrimSpace(string(f)) | ||
if credential == "" { | ||
return "", fmt.Errorf("empty credentials file %q", path) | ||
} | ||
return credential, nil | ||
} | ||
|
||
func getActualValue(value, filepath string) (string, error) { | ||
if len(filepath) > 0 { | ||
return readCredentialsFile(filepath) | ||
} | ||
|
||
return value, nil | ||
} | ||
|
||
// createConfig creates a proper clientcredentials.Config with values retrieved | ||
// from files, if the user has specified '*_file' values | ||
func (c *clientCredentialsConfig) createConfig() (*clientcredentials.Config, error) { | ||
clientID, err := getActualValue(c.ClientID, c.ClientIDFile) | ||
if err != nil { | ||
return nil, multierr.Combine(errNoClientIDProvided, err) | ||
} | ||
|
||
clientSecret, err := getActualValue(c.ClientSecret, c.ClientSecretFile) | ||
if err != nil { | ||
return nil, multierr.Combine(errNoClientSecretProvided, err) | ||
} | ||
|
||
return &clientcredentials.Config{ | ||
ClientID: clientID, | ||
ClientSecret: clientSecret, | ||
TokenURL: c.TokenURL, | ||
Scopes: c.Scopes, | ||
EndpointParams: c.EndpointParams, | ||
}, nil | ||
} | ||
|
||
func (c *clientCredentialsConfig) TokenSource(ctx context.Context) oauth2.TokenSource { | ||
return oauth2.ReuseTokenSource(nil, clientCredentialsTokenSource{ctx: ctx, config: c}) | ||
} | ||
|
||
func (ts clientCredentialsTokenSource) Token() (*oauth2.Token, error) { | ||
cfg, err := ts.config.createConfig() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return cfg.TokenSource(ts.ctx).Token() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
testcreds |