Skip to content
This repository was archived by the owner on Apr 25, 2024. It is now read-only.

Commit b58f76a

Browse files
committed
401 Added ENDPOINT, REGION and SECRETNAME as parameters to the pod; edited documentation
1 parent 0e0b669 commit b58f76a

File tree

3 files changed

+35
-22
lines changed

3 files changed

+35
-22
lines changed

04-path-security-and-networking/401-configmaps-and-secrets/images/sec_mgr_app/server.js

+7-10
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
'use strict';
22

3+
require('dotenv').config();
4+
35
var AWS = require('aws-sdk'),
4-
endpoint = "https://secretsmanager.us-west-2.amazonaws.com",
5-
region = "us-west-2",
6-
secretName = "testsecret",
6+
endpoint = process.env.ENDPOINT,
7+
region = process.env.REGION,
8+
secretName = process.env.SECRETNAME,
79
secret = "",
810
binarySecretData = "";
911

12+
1013
// Constants
1114
var client = new AWS.SecretsManager({
1215
endpoint: endpoint,
@@ -36,11 +39,5 @@ client.getSecretValue({SecretId: secretName}, function(err, data) {
3639
}
3740

3841
// Your code goes here.
39-
console.log(`Secret retrieved from AWS SecretsManager: ${secretName} is ${secret}`);
42+
console.log(`Secret retrieved from AWS SecretsManager: The Secret ${secretName} has SecretString ${secret}`);
4043
});
41-
42-
43-
44-
45-
46-

04-path-security-and-networking/401-configmaps-and-secrets/readme.adoc

+21-12
Original file line numberDiff line numberDiff line change
@@ -678,14 +678,12 @@ This shows that the Java application has been able to read both the NAME and GRE
678678

679679
== Secrets using AWS Secrets Manager
680680

681-
This section will show how to create a secret using https://aws.amazon.com/secrets-manager/[AWS Secrets Manager] and access the secret in a Pod.
681+
In this section, we will create a secret using https://aws.amazon.com/secrets-manager/[AWS Secrets Manager] in the region of choice, and access the secret in a Node.js application deployed within Kubernetes pod. AWS Secrets Manager is available in https://docs.aws.amazon.com/general/latest/gr/rande.html#asm_region[most AWS regions].
682682

683683
AWS Secrets Manager enables you to easily rotate, manage, and retrieve database credentials, API keys, and other secrets throughout their lifecycle. The service integrates with KMS, which uses a https://aws.amazon.com/blogs/security/aws-key-management-service-now-offers-fips-140-2-validated-cryptographic-modules-enabling-easier-adoption-of-the-service-for-regulated-workloads/[FIPS 140-2 validated Hardware Security Module], to provide robust key management controls to secure the secret. AWS Secrets Manager also integrates with AWS IAM and AWS CloudTrail to provide fine-grained access, audit and alerting integration.
684684

685685
=== Update the IAM role for EKS or `kops` Kubernetes Cluster
686686

687-
In this guide, we will create the secret in the US-West (Oregon) `us-west-2` region. AWS Secrets Manager is available in most AWS regions
688-
689687
==== EKS Kubernetes Cluster
690688
EC2 worker nodes use `NodeInstanceRole` created in Step 3 of the https://docs.aws.amazon.com/eks/latest/userguide/getting-started.html[EKS Getting Started guide]. This role must be updated to allow the worked nodes to read the secrets from Secrets Manager.
691689

@@ -733,19 +731,23 @@ and click it. In the `Permissions` tab, expand the inline policy for `nodes.exam
733731

734732
=== Create secrets
735733

736-
. Create a secret key-value pair using https://docs.aws.amazon.com/cli/latest/reference/secretsmanager/create-secret.html[AWS Secrets Manager CLI].
734+
. Create a secret key-value pair using https://docs.aws.amazon.com/cli/latest/reference/secretsmanager/create-secret.html[AWS Secrets Manager CLI]. Replace `<SECRETNAME>` and `<REGION>` with your preference.
735+
736+
aws secretsmanager create-secret --name <SECRETNAME> --description "EKS/kops Demo Secret" --secret-string [{"testkey1":"testvalue1"},{"testkey2":"testvalue2"}] --region <REGION>
737+
738+
. Get the value of created secret using https://docs.aws.amazon.com/cli/latest/reference/secretsmanager/get-secret-value.html[GetSecretValue] API call.
737739

738-
aws secretsmanager create-secret --name testsecret --description "EKS/kops Demo Secret" --secret-string [{"testkey":"testvalue"}] --region us-west-2
740+
aws secretsmanager get-secret-value --secret-id <SECRETNAME> --region <REGION>
739741

740-
. Get the value of created secret using https://docs.aws.amazon.com/cli/latest/reference/secretsmanager/get-secret-value.html[GetSecretValue] API call
742+
. For the selected `<REGION>`, AWS Secrets Manager `<ENDPOINT>` can be determined from https://docs.aws.amazon.com/general/latest/gr/rande.html#asm_region[AWS Documentation].
741743

742-
aws secretsmanager get-secret-value --secret-id testsecret --region us-west-2
744+
. Note the `ENDPOINT`, `REGION` and `SECRETNAME` values. They will be passed as environment variables in a `.yaml` file described in the next section.
743745

744746
=== Consume secrets in a Pod
745747

746-
The directory images/sec_mgr_app contains a Node.js application that reads secrets from AWS Secrets Manager from the US-West (Oregon) `us-west-2` region. This application is then packaged as a Pod and deployed in the cluster.
748+
The Github repository directory `images/sec_mgr_app` contains a Node.js sample application that reads a secret from AWS Secrets Manager from specified region. This application is then packaged as a Pod and deployed in the cluster.
747749

748-
The Pod configuration is shown below:
750+
The Pod configuration is shown below. The `ENDPOINT`, `REGION` and `SECRETNAME` variables are passed as environment variables to the docker image. Change the values of these environment variables to match the values used during creation of secret in AWS Secrets Manager.
749751

750752
apiVersion: v1
751753
kind: Pod
@@ -755,22 +757,29 @@ The Pod configuration is shown below:
755757
containers:
756758
- name: pod-secretsmanager
757759
image: paavanmistry/node-aws-sm-demo:latest
760+
env:
761+
- name: ENDPOINT
762+
value: "https://secretsmanager.us-west-2.amazonaws.com"
763+
- name: REGION
764+
value: "us-west-2"
765+
- name: SECRETNAME
766+
value: "sm-demo-secret"
758767
restartPolicy: Never
759768

760769
Create the Pod:
761770

762771
$ kubectl apply -f templates/pod-secretsmanager.yaml
763-
pod "pod-parameter-store" configured
772+
pod "pod-secretsmanager" configured
764773

765774
Check the logs of the Pod:
766775

767776
$ kubectl logs pod-secretsmanager
768-
Secret retrieved from AWS SecretsManager: testsecret is {testkey}:{testvalue}
777+
Secret retrieved from AWS SecretsManager: The Secret <SECRETNAME> is [{testkey1}:{testvalue1},{testkey2:testvalue2}]
769778

770779
Clean up:
771780

772781
- `$ kubectl delete -f templates/pod-secretsmanager.yaml`
773-
- `$ aws secretsmanager delete-secret --secret-id testsecret --region us-west-2`
782+
- `$ aws secretsmanager delete-secret --secret-id $SECRETNAME --region $REGION`
774783
- Delete IAM role policy updates for AWS Secrets Manager
775784

776785
== Secrets using Vault

04-path-security-and-networking/401-configmaps-and-secrets/templates/pod-secretsmanager.yaml

+7
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,11 @@ spec:
66
containers:
77
- name: pod-secretsmanager
88
image: paavanmistry/node-aws-sm-demo:latest
9+
env:
10+
- name: ENDPOINT
11+
value: "https://secretsmanager.us-west-2.amazonaws.com"
12+
- name: REGION
13+
value: "us-west-2"
14+
- name: SECRETNAME
15+
value: "sm-demo-secret"
916
restartPolicy: Never

0 commit comments

Comments
 (0)