Skip to content

Commit

Permalink
Modify README.md files and unit tests (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhuXiaoBing-cn authored Dec 18, 2020
1 parent b7507cd commit f2fcab9
Show file tree
Hide file tree
Showing 24 changed files with 184 additions and 156 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2285,6 +2285,7 @@
<Or>
<Class name="com.azure.spring.utils.GetHashMac"/> <!-- false positive -->
<Class name="com.azure.spring.utils.PropertyLoader"/> <!-- false positive -->
<Class name="com.azure.spring.cloud.telemetry.GetHashMac"/> <!-- false positive -->
</Or>
<Bug pattern="RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE,
NP_LOAD_OF_KNOWN_NULL_VALUE,
Expand Down Expand Up @@ -2386,6 +2387,7 @@
<Or>
<Class name="com.azure.identity.implementation.IdentityClientTests"/>
<Class name="com.azure.identity.implementation.SynchronizedAccessorTests"/>
<Class name="com.azure.spring.identity.SpringEnvironmentTokenBuilder"/>
</Or>
<Or>
<Bug pattern="NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE"/> <!-- false positive -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.identity.spring;
package com.azure.spring.identity;

import java.util.HashMap;

Expand All @@ -14,12 +14,11 @@

/**
* A helper class to deal with credentials in a Spring environment.
*
*
* <p>
* This helper class makes it possible to configure credentials to be used
* within a Spring context.
* This helper class makes it possible to configure credentials to be used within a Spring context.
* </p>
*
*
* <table summary="">
* <tr>
* <th>Property Tuples</th>
Expand All @@ -43,7 +42,7 @@
* the path to the PEM client certificate</td>
* </tr>
* </table>
*
* <p>
* where name is the <code>name</code> of the credential. Note if
* <code>name</code> is entirely omitted it is taken to be the default
* credential. Note if the default credential is omitted it is configure to use
Expand All @@ -65,8 +64,7 @@ public class SpringEnvironmentTokenBuilder {
private final HashMap<String, TokenCredential> credentials;

/**
* Stores the name of the credential to be returned. If omitted, the default
* credential will be returned.
* Stores the name of the credential to be returned. If omitted, the default credential will be returned.
*/
private String name = "";

Expand All @@ -82,7 +80,6 @@ public SpringEnvironmentTokenBuilder() {
* Populate from Environment.
*
* @param environment the environment.
*
* @return this builder.
*/
public SpringEnvironmentTokenBuilder fromEnvironment(Environment environment) {
Expand All @@ -98,9 +95,9 @@ public SpringEnvironmentTokenBuilder fromEnvironment(Environment environment) {
}

/**
* Sets a credential to override a named credential. If this credential fails to produce a token,
* the original token credential will be used.
*
* Sets a credential to override a named credential. If this credential fails to produce a token, the original token
* credential will be used.
*
* @param name the name for the credential.
* @param credential the token credential.
* @return this builder.
Expand All @@ -123,7 +120,7 @@ public SpringEnvironmentTokenBuilder overrideNamedCredential(String name, TokenC
* Populate a named credential.
*
* @param environment the environment
* @param name the name.
* @param name the name.
*/
private void populateNamedCredential(Environment environment, String name) {
String standardizedName = name;
Expand All @@ -142,7 +139,7 @@ private void populateNamedCredential(Environment environment, String name) {

if (tenantId != null && clientId != null && clientSecret != null) {
TokenCredential credential = new ClientSecretCredentialBuilder().tenantId(tenantId).clientId(clientId)
.clientSecret(clientSecret).build();
.clientSecret(clientSecret).build();
credentials.put(name, credential);
return;
}
Expand All @@ -152,7 +149,7 @@ private void populateNamedCredential(Environment environment, String name) {

if (tenantId != null && clientId != null && clientCertificatePath != null) {
TokenCredential credential = new ClientCertificateCredentialBuilder().tenantId(tenantId).clientId(clientId)
.pemCertificate(clientCertificatePath).build();
.pemCertificate(clientCertificatePath).build();
credentials.put(name, credential);
return;
}
Expand All @@ -164,7 +161,7 @@ private void populateNamedCredential(Environment environment, String name) {

/**
* Sets the builder to return a credential named <code>name</code>
*
*
* @param name the name of the credential.
* @return this builder.
*/
Expand All @@ -175,6 +172,7 @@ public SpringEnvironmentTokenBuilder namedCredential(String name) {

/**
* Sets the builder to return the default credential.
*
* @return the credential builder with default name.
*/
public SpringEnvironmentTokenBuilder defaultCredential() {
Expand All @@ -185,14 +183,13 @@ public SpringEnvironmentTokenBuilder defaultCredential() {
* Builds an Azure TokenCredential.
*
* @return the built token credential.
* @throws IllegalArgumentException if attempting to retrieve a named credential
* not defined in the environment.
* @throws IllegalArgumentException if attempting to retrieve a named credential not defined in the environment.
*/
public TokenCredential build() {
TokenCredential result = credentials.get(name);
if (result == null) {
throw new IllegalArgumentException(
"Attempting to retrieve Azure credential not configured in the environment. (name=" + name + ")");
"Attempting to retrieve Azure credential not configured in the environment. (name=" + name + ")");
} else {
return result;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

/**
* Package com.azure.identity.spring;
*/
package com.azure.spring.identity;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.identity.spring;
package com.azure.spring.identity;

import com.azure.identity.ClientSecretCredential;
import org.junit.jupiter.api.Test;
Expand All @@ -24,20 +24,20 @@
public class SpringEnvironmentTokenBuilderTest {

/**
* Test getDefaultCredential method.
*/
@Test
public void testGetDefaultCredential() {
SpringEnvironmentTokenBuilder builder = new SpringEnvironmentTokenBuilder();
assertNotNull(builder.build());
assertEquals(builder.build(), builder.defaultCredential().build());
}

/**
* Test populate method.
*/
@Test
public void testPopulate() {
* Test getDefaultCredential method.
*/
@Test
public void testGetDefaultCredential() {
SpringEnvironmentTokenBuilder builder = new SpringEnvironmentTokenBuilder();
assertNotNull(builder.build());
assertEquals(builder.build(), builder.defaultCredential().build());
}

/**
* Test populate method.
*/
@Test
public void testPopulate() {
Properties properties = new Properties();
properties.put("azure.credential.names", "");
properties.put("azure.credential.tenantId", "tenantId");
Expand All @@ -51,54 +51,54 @@ public void testPopulate() {
assertTrue(builder.build() instanceof ClientSecretCredential);
assertEquals(builder.build(), builder.defaultCredential().build());

}
}

/**
* Test populate method.
*/
@Test
public void testPopulate2() {
/**
* Test populate method.
*/
@Test
public void testPopulate2() {
Properties properties = new Properties();
properties.put("azure.credential.names", "myname");
properties.put("azure.credential.myname.tenantId", "tenantId");
properties.put("azure.credential.myname.clientId", "clientId");
properties.put("azure.credential.myname.clientSecret", "clientSecret");

SpringEnvironmentTokenBuilder builder = new SpringEnvironmentTokenBuilder();
builder.fromEnvironment(buildEnvironment(properties));
assertNotNull(builder.namedCredential("myname").build());
assertTrue(builder.build() instanceof ClientSecretCredential);
assertNotEquals(builder.build(), builder.defaultCredential().build());

}

/**
* Test populate method.
*/
@Test
public void testPopulate3() {
properties.put("azure.credential.names", "myname");
properties.put("azure.credential.myname.tenantId", "tenantId");
properties.put("azure.credential.myname.clientId", "clientId");
properties.put("azure.credential.myname.clientSecret", "clientSecret");

SpringEnvironmentTokenBuilder builder = new SpringEnvironmentTokenBuilder();
builder.fromEnvironment(buildEnvironment(properties));
assertNotNull(builder.namedCredential("myname").build());
assertTrue(builder.build() instanceof ClientSecretCredential);
assertNotEquals(builder.build(), builder.defaultCredential().build());

}

/**
* Test populate method.
*/
@Test
public void testPopulate3() {
Properties properties = new Properties();
properties.put("azure.credential.names", "myname2");
properties.put("azure.credential.myname2.tenantId", "tenantId");
properties.put("azure.credential.myname2.clientSecret", "clientSecret");
properties.put("azure.credential.myname2.tenantId", "tenantId");
properties.put("azure.credential.myname2.clientSecret", "clientSecret");

SpringEnvironmentTokenBuilder builder = new SpringEnvironmentTokenBuilder();
try {
builder.fromEnvironment(buildEnvironment(properties));
fail();
} catch (Throwable t) {
assertEquals(IllegalStateException.class, t.getClass(),
"Unexpected exception class on missing configuration field.");
}

}
private StandardEnvironment buildEnvironment(Properties properties) {
try {
builder.fromEnvironment(buildEnvironment(properties));
fail();
} catch (Throwable t) {
assertEquals(IllegalStateException.class, t.getClass(),
"Unexpected exception class on missing configuration field.");
}

}

private StandardEnvironment buildEnvironment(Properties properties) {
StandardEnvironment environment = new StandardEnvironment();
final MutablePropertySources propertySources = environment.getPropertySources();
propertySources.addFirst(new PropertiesPropertySource("test", properties));

return environment;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ We need to ensure that this [environment checklist][ready-to-run-checklist] is c

<!-- LINKS -->
[azure-account]: https://azure.microsoft.com/account/
[azure-portal]: http://ms.portal.azure.com/
[azure-portal]: https://ms.portal.azure.com/
[create-azure-cache-for-redis]: https://docs.microsoft.com/azure/azure-cache-for-redis/quickstart-create-redis
[create-sp-using-azure-cli]: https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/spring/azure-spring-boot-samples/create-sp-using-azure-cli.md
[ready-to-run-checklist]: https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/spring/azure-spring-boot-samples/README.md#ready-to-run-checklist
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Event Hub. You can choose anyone of them.

1. Create [Azure Storage][create-azure-storage] for checkpoint use.

1. Update [application.yaml](src/main/resources/application.yaml).
1. Update [application.yaml][application.yaml].

```yaml
spring:
Expand Down Expand Up @@ -64,7 +64,7 @@ Event Hub. You can choose anyone of them.

1. Create [Azure Storage][create-azure-storage] for checkpoint use.

1. Update [application-sp.yaml](src/main/resources/application-sp.yaml).
1. Update [application-sp.yaml][application-sp.yaml].
```yaml
spring:
cloud:
Expand Down Expand Up @@ -111,7 +111,7 @@ Please follow [create managed identity][create-managed-identity] to set up manag

##### Update MSI related properties

1. Update [application-mi.yaml](src/main/resources/application-mi.yaml)
1. Update [application-mi.yaml][application-mi.yaml]
```yaml
spring:
cloud:
Expand Down Expand Up @@ -193,12 +193,14 @@ spring:
<!-- LINKS -->

[azure-account]: https://azure.microsoft.com/account/
[azure-portal]: http://ms.portal.azure.com/
[azure-portal]: https://ms.portal.azure.com/
[create-event-hubs]: https://docs.microsoft.com/azure/event-hubs/
[create-azure-storage]: https://docs.microsoft.com/azure/storage/
[create-sp-using-azure-cli]: https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/spring/azure-spring-boot-samples/create-sp-using-azure-cli.md
[create-managed-identity]: https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/spring/azure-spring-boot-samples/create-managed-identity.md
[deploy-spring-boot-application-to-app-service]: https://docs.microsoft.com/java/azure/spring-framework/deploy-spring-boot-java-app-with-maven-plugin?toc=%2Fazure%2Fapp-service%2Fcontainers%2Ftoc.json&view=azure-java-stable
[ready-to-run-checklist]: https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/spring/azure-spring-boot-samples/README.md#ready-to-run-checklist
[role-assignment]: https://docs.microsoft.com/azure/role-based-access-control/role-assignments-portal

[application-mi.yaml]: https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/spring/azure-spring-boot-samples/azure-spring-cloud-sample-eventhubs-binder/src/main/resources/application-mi.yaml
[application.yaml]: https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/spring/azure-spring-boot-samples/azure-spring-cloud-sample-eventhubs-binder/src/main/resources/application.yaml
[application-sp.yaml]: https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/spring/azure-spring-boot-samples/azure-spring-cloud-sample-eventhubs-binder/src/main/resources/application-sp.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ completed before the run.
## Examples

1. Update
[application.yaml](src/main/resources/application.yaml)
[application.yaml][application.yaml]
file

```yaml
Expand Down Expand Up @@ -74,7 +74,8 @@ completed before the run.

<!-- LINKS -->
[azure-account]: https://azure.microsoft.com/account/
[azure-portal]: http://ms.portal.azure.com/
[azure-portal]: https://ms.portal.azure.com/
[create-event-hubs]: https://docs.microsoft.com/azure/event-hubs/
[create-sp-using-azure-cli]: https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/spring/azure-spring-boot-samples/create-sp-using-azure-cli.md
[ready-to-run-checklist]: https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/spring/azure-spring-boot-samples/README.md#ready-to-run-checklist
[application.yaml]: https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/spring/azure-spring-boot-samples/azure-spring-cloud-sample-eventhubs-kafka/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ is completed before the run.
## Examples

1. Update stream binding related properties in
[application.yaml](src/main/resources/application.yaml). If you choose to use
[application.yaml][application.yaml]. If you choose to use
service principal or managed identity, update the `application-sp.yaml` or
`application-mi.yaml` respectively.

Expand Down Expand Up @@ -119,7 +119,7 @@ processing.
[2] New message received: 'hello'
[2] Message 'hello' successfully checkpointed

6. Delete the resources on [Azure Portal](http://ms.portal.azure.com/)
6. Delete the resources on [Azure Portal][azure-portal]
to avoid unexpected charges.

## Troubleshooting
Expand All @@ -131,7 +131,7 @@ processing.

<!-- LINKS -->
[azure-account]: https://azure.microsoft.com/account/
[azure-portal]: http://ms.portal.azure.com/
[azure-portal]: https://ms.portal.azure.com/
[create-event-hubs]: https://docs.microsoft.com/azure/event-hubs/
[create-azure-storage]: https://docs.microsoft.com/azure/storage/
[create-sp-using-azure-cli]: https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/spring/azure-spring-boot-samples/create-sp-using-azure-cli.md
Expand All @@ -141,4 +141,4 @@ processing.
[managed-identities]: https://docs.microsoft.com/azure/active-directory/managed-identities-azure-resources/
[ready-to-run-checklist]: https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/spring/azure-spring-boot-samples/README.md#ready-to-run-checklist
[role-assignment]: https://docs.microsoft.com/azure/role-based-access-control/role-assignments-portal

[application.yaml]: https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/spring/azure-spring-boot-samples/azure-spring-cloud-sample-eventhubs-multibinders/src/main/resources/application.yaml
Loading

0 comments on commit f2fcab9

Please sign in to comment.