Skip to content

Commit 1787e78

Browse files
authored
[azingest] add sovereign support (Azure#21689)
* add sovereign support * readme * configure utils_test.go * fix env var * update doc comment * update docs
1 parent 6ebe91e commit 1787e78

File tree

5 files changed

+59
-11
lines changed

5 files changed

+59
-11
lines changed

sdk/monitor/azingest/CHANGELOG.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
# Release History
22

3-
## 0.1.1 (Unreleased)
4-
5-
### Features Added
6-
7-
### Breaking Changes
3+
## 0.1.1 (2023-10-11)
84

95
### Bugs Fixed
6+
* Added sovereign cloud support
107

118
### Other Changes
129
* Added troubleshooting guide.

sdk/monitor/azingest/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ The [azidentity][azure_identity] module is used for Azure Active Directory authe
3131

3232
An authenticated client object is required to upload logs. The examples demonstrate using [azidentity.NewDefaultAzureCredential][default_cred_ref] to authenticate; however, the client accepts any [azidentity][azure_identity] credential. See the [azidentity][azure_identity] documentation for more information about other credential types.
3333

34+
The clients defaults to the Azure public cloud. For other cloud configurations, see the [cloud][cloud_documentation] package documentation.
35+
3436
#### Create a client
3537

3638
[Example client][azingest_pkg_go_example_client]
@@ -92,6 +94,7 @@ This project has adopted the [Microsoft Open Source Code of Conduct][code_of_con
9294
[azure_monitor_overview]: https://learn.microsoft.com/azure/azure-monitor/
9395
[azure_monitor_query]: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/monitor/azquery
9496
[azure_subscription]: https://azure.microsoft.com/free/
97+
[cloud_documentation]: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azcore/cloud
9598
[data_collection_endpoint]: https://learn.microsoft.com/azure/azure-monitor/essentials/data-collection-endpoint-overview
9699
[data_collection_rule]: https://learn.microsoft.com/azure/azure-monitor/essentials/data-collection-rule-overview
97100
[data_collection_rule_structure]: https://learn.microsoft.com/azure/azure-monitor/essentials/data-collection-rule-structure

sdk/monitor/azingest/cloud_config.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//go:build go1.18
2+
// +build go1.18
3+
4+
// Copyright (c) Microsoft Corporation. All rights reserved.
5+
// Licensed under the MIT License. See License.txt in the project root for license information.
6+
7+
package azingest
8+
9+
import "github.com/Azure/azure-sdk-for-go/sdk/azcore/cloud"
10+
11+
// Cloud Service Names for Monitor Ingestion, used to identify the respective cloud.ServiceConfiguration
12+
const (
13+
ServiceNameIngestion cloud.ServiceName = "azingest"
14+
)
15+
16+
func init() {
17+
cloud.AzureChina.Services[ServiceNameIngestion] = cloud.ServiceConfiguration{
18+
Audience: "https://monitor.azure.cn/",
19+
}
20+
cloud.AzureGovernment.Services[ServiceNameIngestion] = cloud.ServiceConfiguration{
21+
Audience: "https://monitor.azure.us/",
22+
}
23+
cloud.AzurePublic.Services[ServiceNameIngestion] = cloud.ServiceConfiguration{
24+
Audience: "https://monitor.azure.com/",
25+
}
26+
}

sdk/monitor/azingest/custom_client.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ package azingest
99
// this file contains handwritten additions to the generated code
1010

1111
import (
12+
"errors"
13+
"reflect"
14+
1215
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
16+
"github.com/Azure/azure-sdk-for-go/sdk/azcore/cloud"
1317
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
1418
"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
1519
)
@@ -24,8 +28,15 @@ func NewClient(endpoint string, credential azcore.TokenCredential, options *Clie
2428
if options == nil {
2529
options = &ClientOptions{}
2630
}
31+
if reflect.ValueOf(options.Cloud).IsZero() {
32+
options.Cloud = cloud.AzurePublic
33+
}
34+
c, ok := options.Cloud.Services[ServiceNameIngestion]
35+
if !ok || c.Audience == "" {
36+
return nil, errors.New("provided Cloud field is missing Azure Monitor Ingestion configuration")
37+
}
2738

28-
authPolicy := runtime.NewBearerTokenPolicy(credential, []string{"https://monitor.azure.com/" + "/.default"}, nil)
39+
authPolicy := runtime.NewBearerTokenPolicy(credential, []string{c.Audience + "/.default"}, nil)
2940
azcoreClient, err := azcore.NewClient("azingest.Client", version, runtime.PipelineOptions{PerRetry: []policy.Policy{authPolicy}}, &options.ClientOptions)
3041
if err != nil {
3142
return nil, err

sdk/monitor/azingest/utils_test.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ import (
1010
"context"
1111
"fmt"
1212
"os"
13+
"strings"
1314
"testing"
1415
"time"
1516

1617
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
18+
"github.com/Azure/azure-sdk-for-go/sdk/azcore/cloud"
1719
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
1820
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
1921
"github.com/Azure/azure-sdk-for-go/sdk/internal/recording"
@@ -26,10 +28,11 @@ const fakeRuleID = "Custom-TestTable_CL"
2628
const fakeStreamName = "dcr-testing"
2729

2830
var (
29-
credential azcore.TokenCredential
30-
endpoint string
31-
ruleID string
32-
streamName string
31+
credential azcore.TokenCredential
32+
endpoint string
33+
ruleID string
34+
streamName string
35+
clientCloud cloud.Configuration
3336
)
3437

3538
func TestMain(m *testing.M) {
@@ -47,6 +50,14 @@ func TestMain(m *testing.M) {
4750
if err != nil {
4851
panic(err)
4952
}
53+
if cloudEnv, ok := os.LookupEnv("AZINGEST_ENVIRONMENT"); ok {
54+
if strings.EqualFold(cloudEnv, "AzureUSGovernment") {
55+
clientCloud = cloud.AzureGovernment
56+
}
57+
if strings.EqualFold(cloudEnv, "AzureChinaCloud") {
58+
clientCloud = cloud.AzureChina
59+
}
60+
}
5061
}
5162
endpoint = getEnvVar("AZURE_MONITOR_DCE", fakeEndpoint)
5263
ruleID = getEnvVar("AZURE_MONITOR_DCR_ID", fakeRuleID)
@@ -69,7 +80,7 @@ func startTest(t *testing.T) *azingest.Client {
6980
startRecording(t)
7081
transport, err := recording.NewRecordingHTTPClient(t, nil)
7182
require.NoError(t, err)
72-
opts := &azingest.ClientOptions{ClientOptions: azcore.ClientOptions{Transport: transport}}
83+
opts := &azingest.ClientOptions{ClientOptions: azcore.ClientOptions{Transport: transport, Cloud: clientCloud}}
7384

7485
client, err := azingest.NewClient(endpoint, credential, opts)
7586
if err != nil {

0 commit comments

Comments
 (0)