Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[exporter/datadog] Add api.fail_on_invalid_key to fail fast if API Key is invalid #9426

Merged
merged 11 commits into from
May 5, 2022
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
- `attributesprocessor`: Support filter by severity (#9132)
- `processor/transform`: Add transformation of logs (#9368)
- `datadogexporter`: Add `metrics::summaries::mode` to specify export mode for summaries (#8846)
- `datadogexporter`: Add `api.fail_on_invalid_key` to fail fast if api key is invalid (#9426)

### 🧰 Bug fixes 🧰

Expand Down
7 changes: 5 additions & 2 deletions exporter/datadogexporter/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@ import (
"regexp"
"strings"

"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/metadata/valid"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/confignet"
"go.opentelemetry.io/collector/exporter/exporterhelper"
"go.uber.org/zap"

"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/metadata/valid"
)

var (
Expand Down Expand Up @@ -58,6 +57,10 @@ type APIConfig struct {
// It can also be set through the `DD_SITE` environment variable (Deprecated: [v0.47.0] set environment variable explicitly on configuration instead).
// The default value is "datadoghq.com".
Site string `mapstructure:"site"`

// FailOnInvalidKey states whether to exit on invalid api key.
keisku marked this conversation as resolved.
Show resolved Hide resolved
// The default value is false.
FailOnInvalidKey bool `mapstructure:"fail_on_invalid_key"`
}

// MetricsConfig defines the metrics exporter specific configuration options
Expand Down
5 changes: 5 additions & 0 deletions exporter/datadogexporter/example/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ exporters:
#
# site: datadoghq.com

## @param fail_on_invalid_key - boolean - optional - default: false
## Whether to exit on invalid api key.
keisku marked this conversation as resolved.
Show resolved Hide resolved
#
# fail_on_invalid_key: false

## @param tls - custom object - optional
# TLS settings for HTTPS communications.
# tls:
Expand Down
24 changes: 15 additions & 9 deletions exporter/datadogexporter/internal/utils/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,22 @@ func CreateClient(APIKey string, endpoint string) *datadog.Client {
}

// ValidateAPIKey checks that the provided client was given a correct API key.
func ValidateAPIKey(logger *zap.Logger, client *datadog.Client) {
// If `api.fail_on_invalid_key` is enabled,
func ValidateAPIKey(logger *zap.Logger, client *datadog.Client, failOnInvalidKey bool) {
logger.Info("Validating API key.")
res, err := client.Validate()
if err != nil {
logger.Warn("Error while validating API key.", zap.Error(err))
}

if res {
valid, err := client.Validate()
if err == nil && valid {
logger.Info("API key validation successful.")
} else {
logger.Warn("API key validation failed.")
return
}
switch {
case err != nil && failOnInvalidKey:
logger.Fatal("Error while validating API key.", zap.Error(err))
keisku marked this conversation as resolved.
Show resolved Hide resolved
case err != nil && !failOnInvalidKey:
logger.Warn("Error while validating API key.", zap.Error(err))
case !valid && failOnInvalidKey:
logger.Fatal("API Key validation failed.")
keisku marked this conversation as resolved.
Show resolved Hide resolved
case !valid && !failOnInvalidKey:
logger.Warn("API Key validation failed.")
}
}
2 changes: 1 addition & 1 deletion exporter/datadogexporter/metrics_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func newMetricsExporter(ctx context.Context, params component.ExporterCreateSett
client.ExtraHeader["User-Agent"] = utils.UserAgent(params.BuildInfo)
client.HttpClient = utils.NewHTTPClient(cfg.TimeoutSettings, cfg.LimitedHTTPClientSettings.TLSSetting.InsecureSkipVerify)

utils.ValidateAPIKey(params.Logger, client)
utils.ValidateAPIKey(params.Logger, client, cfg.API.FailOnInvalidKey)

tr, err := translatorFromConfig(params.Logger, cfg)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion exporter/datadogexporter/traces_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ var (
func newTracesExporter(ctx context.Context, params component.ExporterCreateSettings, cfg *config.Config, onceMetadata *sync.Once) *traceExporter {
// client to send running metric to the backend & perform API key validation
client := utils.CreateClient(cfg.API.Key, cfg.Metrics.TCPAddr.Endpoint)
utils.ValidateAPIKey(params.Logger, client)
utils.ValidateAPIKey(params.Logger, client, cfg.API.FailOnInvalidKey)

// removes potentially sensitive info and PII, approach taken from serverless approach
// https://github.com/DataDog/datadog-serverless-functions/blob/11f170eac105d66be30f18eda09eca791bc0d31b/aws/logs_monitoring/trace_forwarder/cmd/trace/main.go#L43
Expand Down