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 MI test code into sample app #80

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion java-17-sample/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@ az account set --subscription $SUBSCRIPTION
```

# Standard Plan
## Provision ASA Service Instance
```shell
az spring create --resource-group $RESOURCE_GROUP --name $SERVICE_NAME --sku standard --location $LOCATION
az spring app create -n java17sample --service $SERVICE_NAME -g $RESOURCE_GROUP --runtime-version Java_17 --assign-endpoint true
```

## Deploy Application
```shell
mvn clean package
az spring app create -n java17sample --service $SERVICE_NAME -g $RESOURCE_GROUP --runtime-version Java_17 --assign-endpoint true
az spring app deploy -n java17sample --service $SERVICE_NAME -g $RESOURCE_GROUP --artifact-path ./target/java-17-sample-1.0-SNAPSHOT.jar
```

17 changes: 14 additions & 3 deletions java-17-sample/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
Expand All @@ -43,7 +47,7 @@
<dependency>
<groupId>com.fasterxml.woodstox</groupId>
<artifactId>woodstox-core</artifactId>
<version>6.4.0</version>
<version>7.1.0</version>
</dependency>
</dependencies>

Expand All @@ -52,14 +56,21 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>3.3.3</version>
<version>3.4.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2023.0.3</version>
<version>2024.0.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-sdk-bom</artifactId>
<version>1.2.30</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
@RestController
public class EurekaController {

@Autowired
@Autowired(required = false)
private EurekaClient discoveryClient;

@GetMapping("/eureka")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.azure.asa.sample;

import com.azure.core.credential.AccessToken;
import com.azure.core.credential.TokenRequestContext;
import com.azure.identity.ManagedIdentityCredential;
import com.azure.identity.ManagedIdentityCredentialBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ManagedIdentityTestController {

@GetMapping("/mi/token")
public String getAccessToken(@RequestParam(name = "client-id", required = false) String clientId) {
ManagedIdentityCredentialBuilder managedIdentityCredentialBuilder = new ManagedIdentityCredentialBuilder();
if(clientId != null) {
managedIdentityCredentialBuilder.clientId(clientId);
}
ManagedIdentityCredential managedIdentityCredential = managedIdentityCredentialBuilder.build();
AccessToken accessToken = managedIdentityCredential.getToken(new TokenRequestContext().addScopes("https://management.core.windows.net/")).block();

return accessToken.getToken();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.azure.asa.sample;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

@ControllerAdvice
public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {

@ExceptionHandler(value = {RuntimeException.class})
protected ResponseEntity<Object> handleConflict(
RuntimeException ex, WebRequest request) {
String bodyOfResponse = ex.getMessage();
return handleExceptionInternal(ex, bodyOfResponse,
new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR, request);
}
}
1 change: 1 addition & 0 deletions java-17-sample/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ management.endpoints.web.exposure.include=*
management.endpoint.env.show-values=ALWAYS
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
eureka.client.healthcheck.enabled=true
#eureka.client.enabled=false
spring.cloud.config.enabled=false
Loading