From 461d6c96f2802baeada161613c82819b62bd63fe Mon Sep 17 00:00:00 2001 From: Anish Ramasekar Date: Thu, 6 Oct 2022 23:20:19 +0000 Subject: [PATCH] feat: add azure-identity java example Signed-off-by: Anish Ramasekar --- .../azure-identity-sdk.md | 1 + examples/azure-identity/java/Dockerfile | 15 +++ examples/azure-identity/java/Makefile | 39 ++++++++ examples/azure-identity/java/pom.xml | 97 +++++++++++++++++++ .../com/example/azureidentity/java/App.java | 26 +++++ 5 files changed, 178 insertions(+) create mode 100644 examples/azure-identity/java/Dockerfile create mode 100644 examples/azure-identity/java/Makefile create mode 100644 examples/azure-identity/java/pom.xml create mode 100644 examples/azure-identity/java/src/main/java/com/example/azureidentity/java/App.java diff --git a/docs/book/src/topics/language-specific-examples/azure-identity-sdk.md b/docs/book/src/topics/language-specific-examples/azure-identity-sdk.md index c391967d5..c9f8501fb 100644 --- a/docs/book/src/topics/language-specific-examples/azure-identity-sdk.md +++ b/docs/book/src/topics/language-specific-examples/azure-identity-sdk.md @@ -19,3 +19,4 @@ The following client libraries are the **minimum** version required | Python | [azure-sdk-for-python](https://github.com/Azure/azure-sdk-for-python) | [Link](https://github.com/Azure/azure-workload-identity/tree/main/examples/azure-identity/python) | | JavaScript/TypeScript | [azure-sdk-for-js](https://github.com/Azure/azure-sdk-for-js) | [Link](https://github.com/Azure/azure-workload-identity/tree/main/examples/azure-identity/node) | | C# | [azure-sdk-for-net](https://github.com/Azure/azure-sdk-for-net) | [Link](https://github.com/Azure/azure-workload-identity/tree/main/examples/azure-identity/dotnet) | +| Java | [azure-sdk-for-java](https://github.com/Azure/azure-sdk-for-java) | [Link](https://github.com/Azure/azure-workload-identity/tree/main/examples/azure-identity/java) | diff --git a/examples/azure-identity/java/Dockerfile b/examples/azure-identity/java/Dockerfile new file mode 100644 index 000000000..015f62435 --- /dev/null +++ b/examples/azure-identity/java/Dockerfile @@ -0,0 +1,15 @@ +ARG BUILDER=maven:3.8.4-jdk-11 +ARG BASEIMAGE=gcr.io/distroless/java:11-nonroot + +FROM ${BUILDER} as builder +WORKDIR /app +COPY pom.xml . +RUN mvn -e -B dependency:resolve +COPY src ./src +RUN mvn -e -B package + +FROM ${BASEIMAGE} +COPY --from=builder /app/target/azid-java-*.jar /app.jar +# Kubernetes runAsNonRoot requires USER to be numeric +USER 65532:65532 +CMD ["/app.jar"] diff --git a/examples/azure-identity/java/Makefile b/examples/azure-identity/java/Makefile new file mode 100644 index 000000000..29a97be2a --- /dev/null +++ b/examples/azure-identity/java/Makefile @@ -0,0 +1,39 @@ +REGISTRY ?= ghcr.io/azure/azure-workload-identity +IMAGE_NAME := azid-java +IMAGE_VERSION ?= latest + +DEMO_IMAGE := $(REGISTRY)/$(IMAGE_NAME):$(IMAGE_VERSION) + +## -------------------------------------- +## Images +## -------------------------------------- + +# Output type of docker buildx build +OUTPUT_TYPE ?= type=registry + +ALL_OS = linux +ALL_ARCH.linux = amd64 arm64 +ALL_OS_ARCH.linux = $(foreach arch, ${ALL_ARCH.linux}, linux-$(arch)) +ALL_OS_ARCH = $(foreach os, $(ALL_OS), ${ALL_OS_ARCH.${os}}) + +# The architecture of the image +ARCH ?= amd64 + +.PHONY: container-linux +container-linux: + docker buildx build \ + --output=$(OUTPUT_TYPE) \ + --platform="linux/$(ARCH)" \ + --tag=$(DEMO_IMAGE)-linux-$(ARCH) . + +.PHONY: container-all +container-all: + for arch in $(ALL_ARCH.linux); do \ + ARCH=$${arch} $(MAKE) container-linux; \ + done + +.PHONY: push-manifest +push-manifest: + docker manifest create --amend $(DEMO_IMAGE) $(foreach osarch, $(ALL_OS_ARCH), $(DEMO_IMAGE)-${osarch}) + for arch in $(ALL_ARCH.linux); do docker manifest annotate --os linux --arch $${arch} $(DEMO_IMAGE) $(DEMO_IMAGE)-linux-$${arch}; done; \ + docker manifest push --purge $(DEMO_IMAGE) diff --git a/examples/azure-identity/java/pom.xml b/examples/azure-identity/java/pom.xml new file mode 100644 index 000000000..4729afdf6 --- /dev/null +++ b/examples/azure-identity/java/pom.xml @@ -0,0 +1,97 @@ + + + + 4.0.0 + + com.example.azureidentity.java + azid-java + 1.0-SNAPSHOT + + azid-java + + http://www.example.com + + + UTF-8 + 1.8 + 1.8 + + + + + com.azure + azure-security-keyvault-secrets + 4.5.0 + + + com.azure + azure-identity + 1.6.0 + + + com.azure + azure-core + 1.32.0 + + + + org.slf4j + slf4j-simple + 1.7.32 + + + + + + org.apache.maven.plugins + maven-shade-plugin + 3.2.4 + + + package + + shade + + + + + com.example.azureidentity.java.App + + + + + *:* + + META-INF/*.SF + META-INF/*.DSA + META-INF/*.RSA + + + + + + + + + org.apache.maven.plugins + maven-dependency-plugin + 2.6 + + + unpack-dependencies + package + + unpack-dependencies + + + system + META-INF/*.SF,META-INF/*.DSA,META-INF/*.RSA + junit,org.mockito,org.hamcrest + ${project.build.directory}/classes + + + + + + + diff --git a/examples/azure-identity/java/src/main/java/com/example/azureidentity/java/App.java b/examples/azure-identity/java/src/main/java/com/example/azureidentity/java/App.java new file mode 100644 index 000000000..47db7117d --- /dev/null +++ b/examples/azure-identity/java/src/main/java/com/example/azureidentity/java/App.java @@ -0,0 +1,26 @@ +package com.example.azureidentity.java; + +import java.util.Map; + +import com.azure.security.keyvault.secrets.SecretClient; +import com.azure.security.keyvault.secrets.SecretClientBuilder; +import com.azure.security.keyvault.secrets.models.KeyVaultSecret; +import com.azure.identity.DefaultAzureCredentialBuilder; +import com.azure.identity.DefaultAzureCredential; + +public class App { + public static void main(String[] args) { + Map env = System.getenv(); + String keyvaultURL = env.get("KEYVAULT_URL"); + String secretName = env.get("SECRET_NAME"); + + DefaultAzureCredential defaultCredential = new DefaultAzureCredentialBuilder().build(); + + SecretClient secretClient = new SecretClientBuilder() + .vaultUrl(keyvaultURL) + .credential(defaultCredential) + .buildClient(); + KeyVaultSecret secret = secretClient.getSecret(secretName); + System.out.printf("successfully got secret, secret=%s", secret.getValue()); + } +}