-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add azure-identity node example (#580)
Signed-off-by: Anish Ramasekar <[email protected]>
- Loading branch information
Showing
8 changed files
with
1,075 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
ARG BUILDER=mcr.microsoft.com/cbl-mariner/base/nodejs:16 | ||
ARG BASEIMAGE=mcr.microsoft.com/mirror/gcr/distroless/nodejs-debian11:16 | ||
|
||
# ref: https://github.com/GoogleContainerTools/distroless/blob/main/examples/nodejs/Dockerfile | ||
FROM ${BUILDER} AS build-env | ||
ADD . /app | ||
WORKDIR /app | ||
RUN npm install | ||
|
||
FROM ${BASEIMAGE} | ||
COPY --from=build-env /app /app | ||
WORKDIR /app | ||
# Kubernetes runAsNonRoot requires USER to be numeric | ||
USER 65532:65532 | ||
CMD ["index.js"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
REGISTRY ?= ghcr.io/azure/azure-workload-identity | ||
IMAGE_NAME := azid-node | ||
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* @summary Uses a SecretClient and DefaultAzureCredential to get a secret from a Key Vault. | ||
*/ | ||
|
||
import { DefaultAzureCredential } from "@azure/identity"; | ||
import { SecretClient } from "@azure/keyvault-secrets"; | ||
|
||
const main = async () => { | ||
const keyvaultURL = process.env["KEYVAULT_URL"]; | ||
const secretName = process.env["SECRET_NAME"]; | ||
|
||
// DefaultAzureCredential will use the environment variables injected by the Azure Workload Identity | ||
// mutating webhook to authenticate with Azure Key Vault. | ||
const credential = new DefaultAzureCredential(); | ||
const client = new SecretClient(keyvaultURL, credential); | ||
|
||
const secret = await client.getSecret(secretName); | ||
console.log(`successfully got secret, secret=${secret.value}`); | ||
} | ||
|
||
main().catch((error) => { | ||
console.error("An error occurred:", error); | ||
process.exit(1); | ||
}); |
Oops, something went wrong.