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

Add audience for multi-cloud support #24237

Merged
merged 6 commits into from
Oct 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.azure.core.util.Configuration;
import com.azure.core.util.logging.ClientLogger;
import com.azure.monitor.query.implementation.logs.AzureLogAnalyticsImplBuilder;
import com.azure.monitor.query.models.LogsQueryClientAudience;

/**
* Fluent builder for creating instances of {@link LogsQueryClient} and {@link LogsQueryAsyncClient}.
Expand Down Expand Up @@ -116,17 +117,29 @@ public LogsQueryClientBuilder credential(TokenCredential tokenCredential) {
/**
* Set the {@link ClientOptions} used for creating the client.
* @param clientOptions The {@link ClientOptions}.
* @return the {@link LogsQueryClientBuilder}
* @return the {@link LogsQueryClientBuilder}.
*/
public LogsQueryClientBuilder clientOptions(ClientOptions clientOptions) {
this.clientOptions = clientOptions;
return this;
}

/**
* Sets the audience to use for authentication with Azure Active Directory. The Azure Public Cloud audience will be
* used if the property is null.
* @param audience audience to use for authentication with Azure Active Directory. The Azure Public Cloud audience
* will be used if the property is null.
* @return the {@link LogsQueryClientBuilder}.
*/
public LogsQueryClientBuilder audience(LogsQueryClientAudience audience) {
innerLogBuilder.audience(audience == null ? null : audience.toString());
return this;
}

/**
* The service version to use when creating the client.
* @param serviceVersion The {@link LogsQueryServiceVersion}.
* @return the {@link LogsQueryClientBuilder}
* @return the {@link LogsQueryClientBuilder}.
*/
public LogsQueryClientBuilder serviceVersion(LogsQueryServiceVersion serviceVersion) {
this.serviceVersion = serviceVersion;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.azure.monitor.query.implementation.metrics.MonitorManagementClientImplBuilder;
import com.azure.monitor.query.implementation.metricsdefinitions.MetricsDefinitionsClientImplBuilder;
import com.azure.monitor.query.implementation.metricsnamespaces.MetricsNamespacesClientImplBuilder;
import com.azure.monitor.query.models.MetricsQueryClientAudience;

/**
* Fluent builder for creating instances of {@link MetricsQueryClient} and {@link MetricsQueryAsyncClient}.
Expand Down Expand Up @@ -137,6 +138,20 @@ public MetricsQueryClientBuilder credential(TokenCredential tokenCredential) {
return this;
}

/**
* Sets the audience to use for authentication with Azure Active Directory. The Azure Public Cloud audience will be
* used if the property is null.
* @param audience audience to use for authentication with Azure Active Directory. The Azure Public Cloud audience
* will be used if the property is null.
* @return the {@link MetricsQueryClientBuilder}.
*/
public MetricsQueryClientBuilder audience(MetricsQueryClientAudience audience) {
innerMetricsBuilder.audience(audience == null ? null : audience.toString());
innerMetricsDefinitionsBuilder.audience(audience == null ? null : audience.toString());
innerMetricsNamespaceBuilder.audience(audience == null ? null : audience.toString());
return this;
}

/**
* Set the {@link ClientOptions} used for creating the client.
* @param clientOptions The {@link ClientOptions}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
import com.azure.core.util.CoreUtils;
import com.azure.core.util.serializer.JacksonAdapter;
import com.azure.core.util.serializer.SerializerAdapter;
import com.azure.monitor.query.LogsQueryClientBuilder;
import com.azure.monitor.query.models.LogsQueryClientAudience;
import com.azure.monitor.query.models.MetricsQueryClientAudience;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand All @@ -33,6 +37,7 @@ public final class AzureLogAnalyticsImplBuilder {
private static final String SDK_VERSION = "version";

private final Map<String, String> properties = CoreUtils.getProperties("azure-monitor-query.properties");
private String audience;

/** Create an instance of the AzureLogAnalyticsImplBuilder. */
public AzureLogAnalyticsImplBuilder() {
Expand Down Expand Up @@ -185,22 +190,35 @@ public AzureLogAnalyticsImplBuilder addPolicy(HttpPipelinePolicy customPolicy) {
return this;
}

/**
* Sets the audience to use for authentication with Azure Active Directory. The Azure Public Cloud audience will be
* used if the property is null.
* @param audience audience to use for authentication with Azure Active Directory. The Azure Public Cloud audience
* will be used if the property is null.
* @return the {@link AzureLogAnalyticsImplBuilder}.
*/
public AzureLogAnalyticsImplBuilder audience(String audience) {
this.audience = audience;
return this;
}
srnagar marked this conversation as resolved.
Show resolved Hide resolved

/**
* Builds an instance of AzureLogAnalyticsImpl with the provided parameters.
*
* @return an instance of AzureLogAnalyticsImpl.
*/
public AzureLogAnalyticsImpl buildClient() {
if (host == null) {
this.host = "https://api.loganalytics.io/v1";
this.host = "https://api.loganalytics.io";
}
String hostVersion = this.host + "/v1";
if (pipeline == null) {
this.pipeline = createHttpPipeline();
}
if (serializerAdapter == null) {
this.serializerAdapter = JacksonAdapter.createDefaultSerializerAdapter();
}
AzureLogAnalyticsImpl client = new AzureLogAnalyticsImpl(pipeline, serializerAdapter, host);
AzureLogAnalyticsImpl client = new AzureLogAnalyticsImpl(pipeline, serializerAdapter, hostVersion);
return client;
}

Expand All @@ -217,8 +235,14 @@ private HttpPipeline createHttpPipeline() {
new UserAgentPolicy(httpLogOptions.getApplicationId(), clientName, clientVersion, buildConfiguration));
HttpPolicyProviders.addBeforeRetryPolicies(policies);

String resolvedAudience = this.audience;
if (resolvedAudience == null) {
resolvedAudience = LogsQueryClientAudience.AZURE_PUBLIC_CLOUD.toString();
}
resolvedAudience += "/.default";
BearerTokenAuthenticationPolicy tokenPolicy = new BearerTokenAuthenticationPolicy(this.tokenCredential,
"https://api.loganalytics.io/.default");
resolvedAudience);

policies.add(tokenPolicy);
policies.add(retryPolicy == null ? new RetryPolicy() : retryPolicy);
policies.add(new CookiePolicy());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import com.azure.core.util.CoreUtils;
import com.azure.core.util.serializer.JacksonAdapter;
import com.azure.core.util.serializer.SerializerAdapter;
import com.azure.monitor.query.models.MetricsQueryClientAudience;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand All @@ -33,6 +35,7 @@ public final class MonitorManagementClientImplBuilder {
private static final String SDK_VERSION = "version";

private final Map<String, String> properties = CoreUtils.getProperties("azure-monitor-query.properties");
private String audience;

/** Create an instance of the MonitorManagementClientImplBuilder. */
public MonitorManagementClientImplBuilder() {
Expand Down Expand Up @@ -201,6 +204,18 @@ public MonitorManagementClientImplBuilder addPolicy(HttpPipelinePolicy customPol
return this;
}

/**
* Sets the audience to use for authentication with Azure Active Directory. The Azure Public Cloud audience will be
* used if the property is null.
* @param audience audience to use for authentication with Azure Active Directory. The Azure Public Cloud audience
* will be used if the property is null.
* @return the {@link MonitorManagementClientImplBuilder}.
*/
public MonitorManagementClientImplBuilder audience(String audience) {
this.audience = audience;
return this;
}

/**
* Builds an instance of MonitorManagementClientImpl with the provided parameters.
*
Expand All @@ -210,6 +225,7 @@ public MonitorManagementClientImpl buildClient() {
if (host == null) {
this.host = "https://management.azure.com";
}

if (apiVersion == null) {
this.apiVersion = "2018-01-01";
}
Expand Down Expand Up @@ -237,8 +253,13 @@ private HttpPipeline createHttpPipeline() {
new UserAgentPolicy(httpLogOptions.getApplicationId(), clientName, clientVersion, buildConfiguration));
HttpPolicyProviders.addBeforeRetryPolicies(policies);
policies.add(retryPolicy == null ? new RetryPolicy() : retryPolicy);
BearerTokenAuthenticationPolicy tokenPolicy = new BearerTokenAuthenticationPolicy(this.tokenCredential, " https://management.azure.com" +
"/.default");
String resolvedAudience = this.audience;
if (resolvedAudience == null) {
resolvedAudience = MetricsQueryClientAudience.AZURE_RESOURCE_MANAGER_PUBLIC_CLOUD.toString();
}
resolvedAudience += "/.default";
BearerTokenAuthenticationPolicy tokenPolicy = new BearerTokenAuthenticationPolicy(this.tokenCredential,
resolvedAudience);
policies.add(tokenPolicy);
policies.add(new CookiePolicy());
policies.addAll(this.pipelinePolicies);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
import com.azure.core.annotation.ServiceInterface;
import com.azure.core.annotation.ServiceMethod;
import com.azure.core.annotation.UnexpectedResponseExceptionType;
import com.azure.core.exception.HttpResponseException;
import com.azure.core.http.rest.PagedFlux;
import com.azure.core.http.rest.PagedIterable;
import com.azure.core.http.rest.PagedResponse;
import com.azure.core.http.rest.PagedResponseBase;
import com.azure.core.http.rest.Response;
import com.azure.core.http.rest.RestProxy;
import com.azure.core.models.ResponseError;
import com.azure.core.util.Context;
import com.azure.core.util.FluxUtil;
import com.azure.monitor.query.implementation.metricsdefinitions.models.ErrorResponseException;
Expand Down Expand Up @@ -96,6 +98,8 @@ public Mono<PagedResponse<MetricDefinition>> listSinglePageAsync(String resource
metricnamespace,
accept,
context))
.onErrorMap(ErrorResponseException.class, ex -> new HttpResponseException(ex.getMessage(),
ex.getResponse(), new ResponseError(ex.getValue().getCode(), ex.getValue().getMessage())))
.map(
res ->
new PagedResponseBase<>(
Expand Down Expand Up @@ -136,6 +140,8 @@ public Mono<PagedResponse<MetricDefinition>> listSinglePageAsync(
metricnamespace,
accept,
context)
.onErrorMap(ErrorResponseException.class, ex -> new HttpResponseException(ex.getMessage(),
ex.getResponse(), new ResponseError(ex.getValue().getCode(), ex.getValue().getMessage())))
.map(
res ->
new PagedResponseBase<>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import com.azure.core.util.CoreUtils;
import com.azure.core.util.serializer.JacksonAdapter;
import com.azure.core.util.serializer.SerializerAdapter;
import com.azure.monitor.query.models.MetricsQueryClientAudience;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand All @@ -33,6 +35,7 @@ public final class MetricsDefinitionsClientImplBuilder {
private static final String SDK_VERSION = "version";

private final Map<String, String> properties = CoreUtils.getProperties("azure-monitor-query.properties");
private String audience;

/** Create an instance of the MetricsDefinitionsClientImplBuilder. */
public MetricsDefinitionsClientImplBuilder() {
Expand Down Expand Up @@ -201,6 +204,18 @@ public MetricsDefinitionsClientImplBuilder addPolicy(HttpPipelinePolicy customPo
return this;
}

/**
* Sets the audience to use for authentication with Azure Active Directory. The Azure Public Cloud audience will be
* used if the property is null.
* @param audience audience to use for authentication with Azure Active Directory. The Azure Public Cloud audience
* will be used if the property is null.
* @return the {@link MetricsDefinitionsClientImplBuilder}.
*/
public MetricsDefinitionsClientImplBuilder audience(String audience) {
this.audience = audience;
return this;
}

/**
* Builds an instance of MetricsDefinitionsClientImpl with the provided parameters.
*
Expand Down Expand Up @@ -236,8 +251,13 @@ private HttpPipeline createHttpPipeline() {
policies.add(
new UserAgentPolicy(httpLogOptions.getApplicationId(), clientName, clientVersion, buildConfiguration));
HttpPolicyProviders.addBeforeRetryPolicies(policies);
BearerTokenAuthenticationPolicy tokenPolicy = new BearerTokenAuthenticationPolicy(this.tokenCredential, "https://management.azure.com" +
"/.default");
String resolvedAudience = this.audience;
if (resolvedAudience == null) {
resolvedAudience = MetricsQueryClientAudience.AZURE_RESOURCE_MANAGER_PUBLIC_CLOUD.toString();
}
resolvedAudience += "/.default";
BearerTokenAuthenticationPolicy tokenPolicy = new BearerTokenAuthenticationPolicy(this.tokenCredential,
resolvedAudience);
policies.add(tokenPolicy);
policies.add(retryPolicy == null ? new RetryPolicy() : retryPolicy);
policies.add(new CookiePolicy());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
import com.azure.core.annotation.ServiceInterface;
import com.azure.core.annotation.ServiceMethod;
import com.azure.core.annotation.UnexpectedResponseExceptionType;
import com.azure.core.exception.HttpResponseException;
import com.azure.core.http.rest.PagedFlux;
import com.azure.core.http.rest.PagedIterable;
import com.azure.core.http.rest.PagedResponse;
import com.azure.core.http.rest.PagedResponseBase;
import com.azure.core.http.rest.Response;
import com.azure.core.http.rest.RestProxy;
import com.azure.core.models.ResponseError;
import com.azure.core.util.Context;
import com.azure.core.util.FluxUtil;
import com.azure.monitor.query.implementation.metricsnamespaces.models.ErrorResponseException;
Expand Down Expand Up @@ -88,14 +90,16 @@ public Mono<PagedResponse<MetricNamespace>> listSinglePageAsync(String resourceU
}
final String accept = "application/json";
return FluxUtil.withContext(
context ->
service.list(
this.client.getHost(),
resourceUri,
this.client.getApiVersion(),
startTime,
accept,
context))
context ->
service.list(
this.client.getHost(),
resourceUri,
this.client.getApiVersion(),
startTime,
accept,
context))
.onErrorMap(ErrorResponseException.class, ex -> new HttpResponseException(ex.getMessage(),
ex.getResponse(), new ResponseError(ex.getValue().getCode(), ex.getValue().getMessage())))
.map(
res ->
new PagedResponseBase<>(
Expand Down Expand Up @@ -130,6 +134,8 @@ public Mono<PagedResponse<MetricNamespace>> listSinglePageAsync(
}
final String accept = "application/json";
return service.list(this.client.getHost(), resourceUri, this.client.getApiVersion(), startTime, accept, context)
.onErrorMap(ErrorResponseException.class, ex -> new HttpResponseException(ex.getMessage(),
ex.getResponse(), new ResponseError(ex.getValue().getCode(), ex.getValue().getMessage())))
.map(
res ->
new PagedResponseBase<>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import com.azure.core.util.CoreUtils;
import com.azure.core.util.serializer.JacksonAdapter;
import com.azure.core.util.serializer.SerializerAdapter;
import com.azure.monitor.query.models.MetricsQueryClientAudience;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand All @@ -33,6 +35,7 @@ public final class MetricsNamespacesClientImplBuilder {
private static final String SDK_VERSION = "version";

private final Map<String, String> properties = CoreUtils.getProperties("azure-monitor-query.properties");
private String audience;

/** Create an instance of the MetricsNamespacesClientImplBuilder. */
public MetricsNamespacesClientImplBuilder() {
Expand Down Expand Up @@ -201,6 +204,18 @@ public MetricsNamespacesClientImplBuilder addPolicy(HttpPipelinePolicy customPol
return this;
}

/**
* Sets the audience to use for authentication with Azure Active Directory. The Azure Public Cloud audience will be
* used if the property is null.
* @param audience audience to use for authentication with Azure Active Directory. The Azure Public Cloud audience
* will be used if the property is null.
* @return the {@link MetricsNamespacesClientImplBuilder}.
*/
public MetricsNamespacesClientImplBuilder audience(String audience) {
this.audience = audience;
return this;
}

/**
* Builds an instance of MetricsNamespacesClientImpl with the provided parameters.
*
Expand Down Expand Up @@ -236,8 +251,13 @@ private HttpPipeline createHttpPipeline() {
policies.add(
new UserAgentPolicy(httpLogOptions.getApplicationId(), clientName, clientVersion, buildConfiguration));
HttpPolicyProviders.addBeforeRetryPolicies(policies);
BearerTokenAuthenticationPolicy tokenPolicy = new BearerTokenAuthenticationPolicy(this.tokenCredential, "https://management.azure.com" +
"/.default");
String resolvedAudience = this.audience;
if (resolvedAudience == null) {
resolvedAudience = MetricsQueryClientAudience.AZURE_RESOURCE_MANAGER_PUBLIC_CLOUD.toString();
}
resolvedAudience += "/.default";
BearerTokenAuthenticationPolicy tokenPolicy = new BearerTokenAuthenticationPolicy(this.tokenCredential,
resolvedAudience);
policies.add(tokenPolicy);
policies.add(retryPolicy == null ? new RetryPolicy() : retryPolicy);
policies.add(new CookiePolicy());
Expand Down
Loading