-
Notifications
You must be signed in to change notification settings - Fork 2.9k
[receiver/azuremonitor] Add support for azureauthextension #39658
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
Changes from 22 commits
e3fb879
68973bc
68f7a7d
b4754e7
e7d4e05
a61af8d
b10a121
51608a2
afdcc0c
c7d50da
356c20c
2635e87
b8a1b30
c2bd28a
ffdebc2
fec9048
e22fe39
a7700c7
0c0f917
9dcf0aa
2fcab1e
09f43f4
f168f86
ab2c7e4
b875e10
5504ce4
74e8e36
3443493
701b7a8
a17c799
3b71df3
2a1c55f
613a4f2
2026205
671c3ec
7681a30
4a77bc0
0f9df1d
4727cd1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# 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: azuremonitorreceiver | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: Add support for azureauthextension as a token provider for azuremonitorreceiver. | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [39048] | ||
|
||
# (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: | ||
|
||
# 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] |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ import ( | |
"errors" | ||
"fmt" | ||
|
||
"go.opentelemetry.io/collector/component" | ||
"go.opentelemetry.io/collector/scraper/scraperhelper" | ||
"go.uber.org/multierr" | ||
|
||
|
@@ -242,11 +243,7 @@ type Config struct { | |
Cloud string `mapstructure:"cloud"` | ||
SubscriptionIDs []string `mapstructure:"subscription_ids"` | ||
DiscoverSubscriptions bool `mapstructure:"discover_subscriptions"` | ||
Credentials string `mapstructure:"credentials"` | ||
TenantID string `mapstructure:"tenant_id"` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you keep just TenantID at this place? Just a nit but this will be clearer when we'll remove the deprecated part There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Of course |
||
ClientID string `mapstructure:"client_id"` | ||
ClientSecret string `mapstructure:"client_secret"` | ||
FederatedTokenFile string `mapstructure:"federated_token_file"` | ||
ResourceGroups []string `mapstructure:"resource_groups"` | ||
Services []string `mapstructure:"services"` | ||
Metrics NestedListAlias `mapstructure:"metrics"` | ||
|
@@ -257,6 +254,25 @@ type Config struct { | |
AppendTagsAsAttributes bool `mapstructure:"append_tags_as_attributes"` | ||
UseBatchAPI bool `mapstructure:"use_batch_api"` | ||
Dimensions DimensionsConfig `mapstructure:"dimensions"` | ||
|
||
// Authentication accepts the component azureauthextension, | ||
// and uses it to get an access token to make requests. | ||
// Using this, `credentials` and any related fields become | ||
// useless. | ||
Authentication *AuthConfig `mapstructure:"auth"` | ||
|
||
// Credentials is deprecated. | ||
Credentials string `mapstructure:"credentials"` | ||
ClientID string `mapstructure:"client_id"` | ||
ClientSecret string `mapstructure:"client_secret"` | ||
FederatedTokenFile string `mapstructure:"federated_token_file"` | ||
} | ||
|
||
// AuthConfig defines the auth settings for the azure receiver. | ||
type AuthConfig struct { | ||
// AuthenticatorID specifies the name of the azure extension to authenticate the | ||
// requests to azure monitor. | ||
AuthenticatorID component.ID `mapstructure:"authenticator,omitempty"` | ||
} | ||
|
||
const ( | ||
|
@@ -272,36 +288,39 @@ func (c Config) Validate() (err error) { | |
err = multierr.Append(err, errMissingSubscriptionIDs) | ||
} | ||
|
||
switch c.Credentials { | ||
case servicePrincipal: | ||
if c.TenantID == "" { | ||
err = multierr.Append(err, errMissingTenantID) | ||
} | ||
if c.Authentication == nil { | ||
// only matters if there is no auth specified | ||
switch c.Credentials { | ||
case servicePrincipal: | ||
if c.TenantID == "" { | ||
err = multierr.Append(err, errMissingTenantID) | ||
} | ||
|
||
if c.ClientID == "" { | ||
err = multierr.Append(err, errMissingClientID) | ||
} | ||
if c.ClientID == "" { | ||
err = multierr.Append(err, errMissingClientID) | ||
} | ||
|
||
if c.ClientSecret == "" { | ||
err = multierr.Append(err, errMissingClientSecret) | ||
} | ||
case workloadIdentity: | ||
if c.TenantID == "" { | ||
err = multierr.Append(err, errMissingTenantID) | ||
} | ||
if c.ClientSecret == "" { | ||
err = multierr.Append(err, errMissingClientSecret) | ||
} | ||
case workloadIdentity: | ||
if c.TenantID == "" { | ||
err = multierr.Append(err, errMissingTenantID) | ||
} | ||
|
||
if c.ClientID == "" { | ||
err = multierr.Append(err, errMissingClientID) | ||
} | ||
if c.ClientID == "" { | ||
err = multierr.Append(err, errMissingClientID) | ||
} | ||
|
||
if c.FederatedTokenFile == "" { | ||
err = multierr.Append(err, errMissingFedTokenFile) | ||
} | ||
if c.FederatedTokenFile == "" { | ||
err = multierr.Append(err, errMissingFedTokenFile) | ||
} | ||
|
||
case managedIdentity: | ||
case defaultCredentials: | ||
default: | ||
return fmt.Errorf("credentials %v is not supported. supported authentications include [%v,%v,%v,%v]", c.Credentials, servicePrincipal, workloadIdentity, managedIdentity, defaultCredentials) | ||
case managedIdentity: | ||
case defaultCredentials: | ||
default: | ||
return fmt.Errorf("credentials %q is not supported. supported authentications include [%v,%v,%v,%v]", c.Credentials, servicePrincipal, workloadIdentity, managedIdentity, defaultCredentials) | ||
} | ||
} | ||
|
||
if c.Cloud != azureCloud && c.Cloud != azureGovernmentCloud && c.Cloud != azureChinaCloud { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package azuremonitorreceiver | ||
|
||
import ( | ||
"fmt" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"go.opentelemetry.io/collector/component" | ||
"go.opentelemetry.io/collector/confmap/confmaptest" | ||
"go.opentelemetry.io/collector/confmap/xconfmap" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/azuremonitorreceiver/internal/metadata" | ||
) | ||
|
||
func TestLoadConfig(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for that! So cool to see some tests of this quality for the config :) |
||
t.Parallel() | ||
|
||
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml")) | ||
require.NoError(t, err) | ||
|
||
tests := []struct { | ||
id component.ID | ||
expected component.Config | ||
expectedErr string | ||
}{ | ||
{ | ||
id: component.NewIDWithName(metadata.Type, "valid_subscription_ids"), | ||
expected: func() component.Config { | ||
cfg := createDefaultConfig().(*Config) | ||
cfg.SubscriptionIDs = []string{"test"} | ||
cfg.Credentials = defaultCredentials | ||
return cfg | ||
}(), | ||
}, | ||
{ | ||
id: component.NewIDWithName(metadata.Type, "valid_discover_subscription"), | ||
expected: func() component.Config { | ||
cfg := createDefaultConfig().(*Config) | ||
cfg.DiscoverSubscriptions = true | ||
cfg.Credentials = defaultCredentials | ||
return cfg | ||
}(), | ||
}, | ||
{ | ||
id: component.NewIDWithName(metadata.Type, "missing_subscription"), | ||
expectedErr: errMissingSubscriptionIDs.Error(), | ||
}, | ||
{ | ||
id: component.NewIDWithName(metadata.Type, "invalid_cloud"), | ||
expectedErr: errInvalidCloud.Error(), | ||
}, | ||
{ | ||
id: component.NewIDWithName(metadata.Type, "missing_service_principal"), | ||
expectedErr: fmt.Sprintf( | ||
"%s; %s; %s", | ||
errMissingTenantID.Error(), | ||
errMissingClientID.Error(), | ||
errMissingClientSecret.Error(), | ||
), | ||
}, | ||
{ | ||
id: component.NewIDWithName(metadata.Type, "missing_service_principal"), | ||
expectedErr: fmt.Sprintf( | ||
"%s; %s; %s", | ||
errMissingTenantID.Error(), | ||
errMissingClientID.Error(), | ||
errMissingClientSecret.Error(), | ||
), | ||
}, | ||
{ | ||
id: component.NewIDWithName(metadata.Type, "missing_workload_identity"), | ||
expectedErr: fmt.Sprintf( | ||
"%s; %s; %s", | ||
errMissingTenantID.Error(), | ||
errMissingClientID.Error(), | ||
errMissingFedTokenFile.Error(), | ||
), | ||
}, | ||
{ | ||
id: component.NewIDWithName(metadata.Type, "invalid_credentials"), | ||
expectedErr: `credentials "invalid" is not supported`, | ||
}, | ||
{ | ||
id: component.NewIDWithName(metadata.Type, "valid_authenticator"), | ||
expected: func() component.Config { | ||
cfg := createDefaultConfig().(*Config) | ||
cfg.DiscoverSubscriptions = true | ||
cfg.Authentication = &AuthConfig{ | ||
AuthenticatorID: component.MustNewIDWithName("azureauth", "monitor"), | ||
} | ||
cfg.Credentials = "does-not-matter" | ||
return cfg | ||
}(), | ||
}, | ||
{ | ||
id: component.NewIDWithName(metadata.Type, "valid_authenticator_2"), | ||
expected: func() component.Config { | ||
cfg := createDefaultConfig().(*Config) | ||
cfg.DiscoverSubscriptions = true | ||
cfg.Authentication = &AuthConfig{ | ||
AuthenticatorID: component.MustNewID("azureauth"), | ||
} | ||
cfg.Credentials = "does-not-matter" | ||
return cfg | ||
}(), | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.id.Name(), func(t *testing.T) { | ||
factory := NewFactory() | ||
cfg := factory.CreateDefaultConfig() | ||
|
||
sub, err := cm.Sub(tt.id.String()) | ||
require.NoError(t, err) | ||
require.NoError(t, sub.Unmarshal(cfg)) | ||
|
||
err = xconfmap.Validate(cfg) | ||
if tt.expectedErr != "" { | ||
assert.ErrorContains(t, err, tt.expectedErr) | ||
} else { | ||
assert.NoError(t, err) | ||
assert.Equal(t, tt.expected, cfg) | ||
} | ||
}) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.