-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create pkg/datadog/api and move static and full API validation here
- Loading branch information
1 parent
6c94334
commit fd3df3c
Showing
10 changed files
with
568 additions
and
83 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: pkg/datadog | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: add pkg/datadog/api submodule; refactor online API key validation for shared Datadog components | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [38223, 38234] | ||
|
||
# (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: | | ||
StaticAPIKeyCheck and FullAPIKeyCheck are now available in pkg/datadog/api for use by shared Datadog components. | ||
The online API key validation logic has been refactored to use these new functions. | ||
# 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: [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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Copyright The OpenTelemetryAuthors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package api // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/datadog/api" | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/DataDog/datadog-api-client-go/v2/api/datadogV2" | ||
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/datadog/clientutil" | ||
"go.opentelemetry.io/collector/component" | ||
"go.opentelemetry.io/collector/config/confighttp" | ||
"go.uber.org/zap" | ||
zorkian "gopkg.in/zorkian/go-datadog-api.v2" | ||
) | ||
|
||
const ( | ||
// NonHexChars is a regex of characters that are always invalid in a Datadog API key | ||
NonHexChars = "[^0-9a-fA-F]" | ||
) | ||
|
||
var ( | ||
// ErrAPIKeyFormat is returned if API key contains invalid characters | ||
ErrAPIKeyFormat = errors.New("api::key contains invalid characters") | ||
// ErrUnsetAPIKey is returned when the API key is not set. | ||
ErrUnsetAPIKey = errors.New("api.key is not set") | ||
// NonHexRegex is a regex of characters that are always invalid in a Datadog API key | ||
NonHexRegex = regexp.MustCompile(NonHexChars) | ||
) | ||
|
||
// StaticAPIKeyCheck does offline validation of API Key, checking for non-empty | ||
// and ensuring all values are valid hex characters. | ||
func StaticAPIKeyCheck(key string) error { | ||
if key == "" { | ||
return ErrUnsetAPIKey | ||
} | ||
invalidAPIKeyChars := NonHexRegex.FindAllString(key, -1) | ||
if len(invalidAPIKeyChars) > 0 { | ||
return fmt.Errorf("%w: invalid characters: %s", ErrAPIKeyFormat, strings.Join(invalidAPIKeyChars, ", ")) | ||
} | ||
return nil | ||
} | ||
|
||
// FullAPIKeyCheck is a helper function that validates the API key online | ||
// and returns an API client as well as an error via a passed channel if it is invalid. | ||
// Note: The Zorkian client portion is deprecated and will be removed in a future version | ||
func FullAPIKeyCheck( | ||
ctx context.Context, | ||
apiKey string, | ||
errchan *chan error, | ||
buildInfo component.BuildInfo, | ||
isMetricExportV2Enabled bool, | ||
failOnInvalidKey bool, | ||
endpoint string, | ||
logger *zap.Logger, | ||
clientConfig confighttp.ClientConfig, | ||
) (apiClient *datadogV2.MetricsApi, zorkianClient *zorkian.Client, err error) { | ||
if isMetricExportV2Enabled { | ||
client := clientutil.CreateAPIClient( | ||
buildInfo, | ||
endpoint, | ||
clientConfig, | ||
) | ||
go func() { *errchan <- clientutil.ValidateAPIKey(ctx, apiKey, logger, client) }() | ||
apiClient = datadogV2.NewMetricsApi(client) | ||
zorkianClient = nil | ||
} else { | ||
zorkianClient = clientutil.CreateZorkianClient(apiKey, endpoint) | ||
go func() { *errchan <- clientutil.ValidateAPIKeyZorkian(logger, zorkianClient) }() | ||
apiClient = nil | ||
} | ||
// validate apiKey | ||
if failOnInvalidKey { | ||
err = <-*errchan | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
} | ||
return apiClient, zorkianClient, nil | ||
} |
Oops, something went wrong.