Skip to content

Commit

Permalink
Remove hard-coded version and name from clients (#6557)
Browse files Browse the repository at this point in the history
* Remove hard-coded version and name from clients
  • Loading branch information
sima-zhu authored Dec 5, 2019
1 parent 6819bcc commit 5e37d79
Show file tree
Hide file tree
Showing 39 changed files with 247 additions and 143 deletions.
2 changes: 1 addition & 1 deletion eng/versioning/external_dependencies.txt
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,4 @@ media_com.microsoft.azure:adal4j;1.2.0
servicebus_com.microsoft.azure:azure-client-authentication;1.6.7

# sdk\storage\azure-storage-blob-cryptography\pom.xml
storage_com.microsoft.azure:azure-storage;8.4.0
storage_com.microsoft.azure:azure-storage;8.4.0
9 changes: 0 additions & 9 deletions eng/versioning/version_client_java_files.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,3 @@

# The file format here should be the relative path from the root of the azure-sdk-for-java
sdk/core/azure-core-amqp/src/main/java/com/azure/core/amqp/implementation/ClientConstants.java
sdk/keyvault/azure-security-keyvault-certificates/src/main/java/com/azure/security/keyvault/certificates/AzureKeyVaultConfiguration.java
sdk/keyvault/azure-security-keyvault-keys/src/main/java/com/azure/security/keyvault/keys/implementation/AzureKeyVaultConfiguration.java
sdk/keyvault/azure-security-keyvault-secrets/src/main/java/com/azure/security/keyvault/secrets/AzureKeyVaultConfiguration.java
sdk/storage/azure-storage-blob/src/main/java/com/azure/storage/blob/implementation/util/BuilderHelper.java
sdk/storage/azure-storage-blob-cryptography/src/main/java/com/azure/storage/blob/specialized/cryptography/BlobCryptographyConfiguration.java
sdk/storage/azure-storage-file-share/src/main/java/com/azure/storage/file/share/implementation/util/BuilderHelper.java
sdk/storage/azure-storage-file-share/src/main/java/com/azure/storage/file/share/FileConfiguration.java
sdk/storage/azure-storage-queue/src/main/java/com/azure/storage/queue/implementation/util/BuilderHelper.java
sdk/storage/azure-storage-queue/src/main/java/com/azure/storage/queue/QueueConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ public ConfigurationSetting setReadOnly(String key, String label, boolean isRead
*
* <p>Set the setting to read-only with the key-label "prodDBConnection"-"westUS".</p>
*
* {@codesnippet com.azure.data.applicationconfig.configurationclient.setReadOnlyWithResponse#ConfigurationSetting-boolean-Context}
* {@codesnippet com.azure.data.applicationconfig.configurationclient.setReadOnlyWithResponse#ConfigurationSetting-Boolean-Context}
*
* <p>Clear read-only of the setting with the key-label "prodDBConnection"-"westUS".</p>
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.azure.core.http.policy.UserAgentPolicy;
import com.azure.core.util.Configuration;
import com.azure.core.util.CoreUtils;
import com.azure.core.util.UserAgentProperties;
import com.azure.core.util.logging.ClientLogger;
import com.azure.data.appconfiguration.implementation.ConfigurationClientCredentials;
import com.azure.data.appconfiguration.implementation.ConfigurationCredentialsPolicy;
Expand All @@ -34,7 +35,6 @@
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
Expand Down Expand Up @@ -76,16 +76,14 @@ public final class ConfigurationClientBuilder {
private static final String CONTENT_TYPE_HEADER_VALUE = "application/json";
private static final String ACCEPT_HEADER = "Accept";
private static final String ACCEPT_HEADER_VALUE = "application/vnd.microsoft.azconfig.kv+json";
// This is properties file's name.
private static final String APP_CONFIG_PROPERTIES = "azure-appconfig.properties";
private static final String NAME = "name";
private static final String VERSION = "version";
private static final RetryPolicy DEFAULT_RETRY_POLICY = new RetryPolicy("retry-after-ms", ChronoUnit.MILLIS);

private final ClientLogger logger = new ClientLogger(ConfigurationClientBuilder.class);
private final List<HttpPipelinePolicy> policies;
private final HttpHeaders headers;
private final String clientName;
private final String clientVersion;
private final UserAgentProperties properties;

private ConfigurationClientCredentials credential;
private TokenCredential tokenCredential;
Expand All @@ -105,9 +103,7 @@ public ConfigurationClientBuilder() {
policies = new ArrayList<>();
httpLogOptions = new HttpLogOptions();

Map<String, String> properties = CoreUtils.getProperties(APP_CONFIG_PROPERTIES);
clientName = properties.getOrDefault(NAME, "UnknownName");
clientVersion = properties.getOrDefault(VERSION, "UnknownVersion");
properties = CoreUtils.getUserAgentProperties(APP_CONFIG_PROPERTIES);

headers = new HttpHeaders()
.put(ECHO_REQUEST_ID_HEADER, "true")
Expand Down Expand Up @@ -175,8 +171,8 @@ public ConfigurationAsyncClient buildAsyncClient() {
// Closest to API goes first, closest to wire goes last.
final List<HttpPipelinePolicy> policies = new ArrayList<>();

policies.add(new UserAgentPolicy(httpLogOptions.getApplicationId(), clientName, clientVersion,
buildConfiguration));
policies.add(new UserAgentPolicy(httpLogOptions.getApplicationId(), properties.getName(),
properties.getVersion(), buildConfiguration));
policies.add(new RequestIdPolicy());
policies.add(new AddHeadersPolicy(headers));
policies.add(new AddDatePolicy());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
*/
public final class CoreUtils {
private static final String COMMA = ",";
private static final String NAME = "name";
private static final String VERSION = "version";
private static final String UNKNOWN_NAME = "UnknownName";
private static final String UNKNOWN_VERSION = "UnknownVersion";

private CoreUtils() {
// Exists only to defeat instantiation.
Expand Down Expand Up @@ -161,6 +165,19 @@ public static <T> Publisher<T> extractAndFetch(PagedResponse<T> page, Context co
}


/**
* Helper method that returns {@link UserAgentProperties} from properties defined in {@code propertiesFileName}.
*
* @param propertiesFileName The file name defining the properties.
* @return {@link UserAgentProperties}.
*/
public static UserAgentProperties getUserAgentProperties(String propertiesFileName) {
Map<String, String> propertyMap = getProperties(propertiesFileName);
String name = propertyMap.getOrDefault(NAME, UNKNOWN_NAME);
String version = propertyMap.getOrDefault(VERSION, UNKNOWN_VERSION);
return new UserAgentProperties(name, version);
}

/**
* Helper method that returns an immutable {@link Map} of properties defined in {@code propertiesFileName}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

package com.azure.core.util;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

Expand Down Expand Up @@ -40,16 +40,18 @@ public void findFirstOfTypeWithNoneOfType() {

@Test
public void testProperties() {
assertNotNull(CoreUtils.getProperties("azure-core.properties").get("version"));
assertNotNull(CoreUtils.getProperties("azure-core.properties").get("name"));
assertTrue(CoreUtils.getProperties("azure-core.properties").get("version")
UserAgentProperties properties =
CoreUtils.getUserAgentProperties("azure-core.properties");
assertFalse(properties.getName().matches("UnknownName"));
assertTrue(CoreUtils.getUserAgentProperties("azure-core.properties").getVersion()
.matches("\\d.\\d.\\d([-a-zA-Z0-9.])*"));
}

@Test
public void testMissingProperties() {
assertNotNull(CoreUtils.getProperties("foo.properties"));
assertTrue(CoreUtils.getProperties("foo.properties").isEmpty());
assertNull(CoreUtils.getProperties("azure-core.properties").get("foo"));
assertTrue(CoreUtils.getUserAgentProperties("foo.properties")
.getVersion().matches("UnknownVersion"));
assertTrue(CoreUtils.getUserAgentProperties("foo.properties")
.getName().matches("UnknownName"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name=${project.artifactId}
version=${project.version}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.azure.core.http.policy.UserAgentPolicy;
import com.azure.core.util.CoreUtils;
import com.azure.core.util.Configuration;
import com.azure.core.util.UserAgentProperties;
import com.azure.core.util.logging.ClientLogger;
import com.azure.security.keyvault.certificates.implementation.KeyVaultCredentialPolicy;

Expand Down Expand Up @@ -57,8 +58,10 @@
@ServiceClientBuilder(serviceClients = {CertificateClient.class, CertificateAsyncClient.class})
public final class CertificateClientBuilder {
private final ClientLogger logger = new ClientLogger(CertificateClientBuilder.class);

// This is properties file's name.
private static final String AZURE_KEY_VAULT_CERTIFICATES_PROPERTIES = "azure-key-vault-certificates.properties";
private final List<HttpPipelinePolicy> policies;
private final UserAgentProperties properties;
private TokenCredential credential;
private HttpPipeline pipeline;
private URL vaultUrl;
Expand All @@ -75,6 +78,7 @@ public CertificateClientBuilder() {
retryPolicy = new RetryPolicy();
httpLogOptions = new HttpLogOptions();
policies = new ArrayList<>();
properties = CoreUtils.getUserAgentProperties(AZURE_KEY_VAULT_CERTIFICATES_PROPERTIES);
}

/**
Expand Down Expand Up @@ -110,11 +114,13 @@ public CertificateClient buildClient() {
* {@link CertificateClientBuilder#vaultUrl(String)} have not been set.
*/
public CertificateAsyncClient buildAsyncClient() {
Configuration buildConfiguration = (configuration == null) ? Configuration.getGlobalConfiguration().clone() : configuration;
Configuration buildConfiguration = (configuration == null) ? Configuration.getGlobalConfiguration().clone()
: configuration;
URL buildEndpoint = getBuildEndpoint(buildConfiguration);

if (buildEndpoint == null) {
throw logger.logExceptionAsError(new IllegalStateException(KeyVaultErrorCodeStrings.getErrorString(KeyVaultErrorCodeStrings.VAULT_END_POINT_REQUIRED)));
throw logger.logExceptionAsError(new IllegalStateException(
KeyVaultErrorCodeStrings.getErrorString(KeyVaultErrorCodeStrings.VAULT_END_POINT_REQUIRED)));
}
CertificateServiceVersion serviceVersion = version != null ? version : CertificateServiceVersion.getLatest();

Expand All @@ -123,12 +129,14 @@ public CertificateAsyncClient buildAsyncClient() {
}

if (credential == null) {
throw logger.logExceptionAsError(new IllegalStateException(KeyVaultErrorCodeStrings.getErrorString(KeyVaultErrorCodeStrings.CREDENTIALS_REQUIRED)));
throw logger.logExceptionAsError(new IllegalStateException(
KeyVaultErrorCodeStrings.getErrorString(KeyVaultErrorCodeStrings.CREDENTIALS_REQUIRED)));
}

// Closest to API goes first, closest to wire goes last.
final List<HttpPipelinePolicy> policies = new ArrayList<>();
policies.add(new UserAgentPolicy(httpLogOptions.getApplicationId(), AzureKeyVaultConfiguration.SDK_NAME, AzureKeyVaultConfiguration.SDK_VERSION, buildConfiguration));
policies.add(new UserAgentPolicy(httpLogOptions.getApplicationId(), properties.getName(),
properties.getVersion(), buildConfiguration));
HttpPolicyProviders.addBeforeRetryPolicies(policies);
policies.add(retryPolicy);
policies.add(new KeyVaultCredentialPolicy(credential));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name=${project.artifactId}
version=${project.version}
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
credentials_required=Azure Key Vault credentials are required.
vault_endpoint_required=Azure Key Vault endpoint url is required.

Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
import static org.junit.jupiter.api.Assertions.fail;

public abstract class CertificateClientTestBase extends TestBase {
private static final String SDK_NAME = "client_name";
private static final String SDK_VERSION = "client_version";

@Override
protected String getTestName() {
Expand All @@ -80,7 +82,7 @@ <T> T clientSetup(Function<HttpPipeline, T> clientBuilder) {
HttpClient httpClient;
// Closest to API goes first, closest to wire goes last.
final List<HttpPipelinePolicy> policies = new ArrayList<>();
policies.add(new UserAgentPolicy(AzureKeyVaultConfiguration.SDK_NAME, AzureKeyVaultConfiguration.SDK_VERSION,
policies.add(new UserAgentPolicy(SDK_NAME, SDK_VERSION,
Configuration.getGlobalConfiguration().clone(), CertificateServiceVersion.getLatest()));
HttpPolicyProviders.addBeforeRetryPolicies(policies);
policies.add(new RetryPolicy());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
import com.azure.core.http.policy.RetryPolicy;
import com.azure.core.http.policy.UserAgentPolicy;
import com.azure.core.annotation.ServiceClientBuilder;
import com.azure.core.util.UserAgentProperties;
import com.azure.core.util.logging.ClientLogger;
import com.azure.security.keyvault.keys.implementation.AzureKeyVaultConfiguration;
import com.azure.security.keyvault.keys.implementation.KeyVaultCredentialPolicy;

import java.net.MalformedURLException;
Expand Down Expand Up @@ -60,8 +60,11 @@
@ServiceClientBuilder(serviceClients = KeyClient.class)
public final class KeyClientBuilder {
private final ClientLogger logger = new ClientLogger(KeyClientBuilder.class);
// This is properties file's name.
private static final String AZURE_KEY_VAULT_KEYS = "azure-key-vault-keys.properties";

private final List<HttpPipelinePolicy> policies;
private final UserAgentProperties properties;
private TokenCredential credential;
private HttpPipeline pipeline;
private URL vaultUrl;
Expand All @@ -78,6 +81,7 @@ public KeyClientBuilder() {
retryPolicy = new RetryPolicy();
httpLogOptions = new HttpLogOptions();
policies = new ArrayList<>();
properties = CoreUtils.getUserAgentProperties(AZURE_KEY_VAULT_KEYS);
}

/**
Expand Down Expand Up @@ -136,7 +140,7 @@ public KeyAsyncClient buildAsyncClient() {

// Closest to API goes first, closest to wire goes last.
final List<HttpPipelinePolicy> policies = new ArrayList<>();
policies.add(new UserAgentPolicy(httpLogOptions.getApplicationId(), AzureKeyVaultConfiguration.SDK_NAME, AzureKeyVaultConfiguration.SDK_VERSION,
policies.add(new UserAgentPolicy(httpLogOptions.getApplicationId(), properties.getName(), properties.getVersion(),
buildConfiguration));
HttpPolicyProviders.addBeforeRetryPolicies(policies);
policies.add(retryPolicy);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
import com.azure.core.annotation.ServiceClientBuilder;
import com.azure.core.http.policy.HttpPolicyProviders;
import com.azure.core.util.Configuration;
import com.azure.core.util.CoreUtils;
import com.azure.core.util.UserAgentProperties;
import com.azure.core.util.logging.ClientLogger;
import com.azure.security.keyvault.keys.implementation.KeyVaultCredentialPolicy;
import com.azure.security.keyvault.keys.implementation.AzureKeyVaultConfiguration;
import com.azure.security.keyvault.keys.models.JsonWebKey;

import java.util.ArrayList;
Expand Down Expand Up @@ -63,7 +64,10 @@
@ServiceClientBuilder(serviceClients = CryptographyClient.class)
public final class CryptographyClientBuilder {
final List<HttpPipelinePolicy> policies;
final UserAgentProperties properties;
private final ClientLogger logger = new ClientLogger(CryptographyClientBuilder.class);
// This is properties file's name.
private static final String AZURE_KEY_VAULT_KEYS = "azure-key-vault-keys.properties";
private TokenCredential credential;
private HttpPipeline pipeline;
private JsonWebKey jsonWebKey;
Expand All @@ -81,6 +85,7 @@ public CryptographyClientBuilder() {
retryPolicy = new RetryPolicy();
httpLogOptions = new HttpLogOptions();
policies = new ArrayList<>();
properties = CoreUtils.getUserAgentProperties(AZURE_KEY_VAULT_KEYS);
}

/**
Expand Down Expand Up @@ -153,8 +158,8 @@ HttpPipeline setupPipeline() {

// Closest to API goes first, closest to wire goes last.
final List<HttpPipelinePolicy> policies = new ArrayList<>();
policies.add(new UserAgentPolicy(httpLogOptions.getApplicationId(), AzureKeyVaultConfiguration.SDK_NAME, AzureKeyVaultConfiguration.SDK_VERSION,
buildConfiguration));
policies.add(new UserAgentPolicy(httpLogOptions.getApplicationId(), properties.getName(),
properties.getVersion(), buildConfiguration));
HttpPolicyProviders.addBeforeRetryPolicies(policies);
policies.add(retryPolicy);
policies.add(new KeyVaultCredentialPolicy(credential));
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
name=${project.artifactId}
version=${project.version}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.azure.core.test.TestBase;
import com.azure.core.util.Configuration;
import com.azure.identity.DefaultAzureCredentialBuilder;
import com.azure.security.keyvault.keys.implementation.AzureKeyVaultConfiguration;
import com.azure.security.keyvault.keys.models.CreateKeyOptions;
import com.azure.security.keyvault.keys.models.KeyType;
import com.azure.security.keyvault.keys.models.KeyVaultKey;
Expand All @@ -47,6 +46,8 @@ public abstract class KeyClientTestBase extends TestBase {
private static final String KEY_NAME = "javaKeyTemp";
private static final KeyType RSA_KEY_TYPE = KeyType.RSA;
private static final KeyType EC_KEY_TYPE = KeyType.EC;
private static final String SDK_NAME = "client_name";
private static final String SDK_VERSION = "client_version";

@Override
protected String getTestName() {
Expand All @@ -70,7 +71,7 @@ <T> T clientSetup(Function<HttpPipeline, T> clientBuilder) {
HttpClient httpClient;
// Closest to API goes first, closest to wire goes last.
final List<HttpPipelinePolicy> policies = new ArrayList<>();
policies.add(new UserAgentPolicy(AzureKeyVaultConfiguration.SDK_NAME, AzureKeyVaultConfiguration.SDK_VERSION, Configuration.getGlobalConfiguration().clone(), KeyServiceVersion.getLatest()));
policies.add(new UserAgentPolicy(SDK_NAME, SDK_VERSION, Configuration.getGlobalConfiguration().clone(), KeyServiceVersion.getLatest()));
HttpPolicyProviders.addBeforeRetryPolicies(policies);
policies.add(new RetryPolicy());
if (credential != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.security.keyvault.keys;

import com.azure.core.util.CoreUtils;
import com.azure.core.util.UserAgentProperties;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class KeyVaultKeysUserAgentPropertiesTest {

@Test
public void testAzureConfiguration() {
UserAgentProperties properties = CoreUtils.getUserAgentProperties("azure-key-vault-keys.properties");
assertFalse(properties.getName().matches("UnknownName"));
assertTrue(properties.getVersion().matches("(\\d)+.(\\d)+.(\\d)+([-a-zA-Z0-9.])*"));
}
}
Loading

0 comments on commit 5e37d79

Please sign in to comment.