Skip to content

Commit 8a18110

Browse files
committed
added Secrets Manager documentation under aws-samples#401 - issue aws-samples#475
1 parent 7471ea5 commit 8a18110

File tree

6 files changed

+176
-1
lines changed

6 files changed

+176
-1
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
npm-debug.log
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
FROM node:carbon
2+
# Create app directory
3+
WORKDIR /usr/src/app
4+
5+
# Install app dependencies
6+
# A wildcard is used to ensure both package.json AND package-lock.json are copied
7+
# where available (npm@5+)
8+
COPY package*.json ./
9+
10+
RUN npm install
11+
# If you are building your code for production
12+
# RUN npm install --only=production
13+
# Bundle app source
14+
COPY . .
15+
16+
CMD [ "npm", "start" ]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "docker_web_app",
3+
"version": "1.0.0",
4+
"description": "Node.js on Docker",
5+
"author": "First Last <[email protected]>",
6+
"main": "server.js",
7+
"scripts": {
8+
"start": "node server.js"
9+
},
10+
"dependencies": {
11+
"aws-sdk": "latest",
12+
"npm": "^6.1.0"
13+
}
14+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'use strict';
2+
3+
var AWS = require('aws-sdk'),
4+
endpoint = "https://secretsmanager.us-west-2.amazonaws.com",
5+
region = "us-west-2",
6+
secretName = "testsecret",
7+
secret = "",
8+
binarySecretData = "";
9+
10+
// Constants
11+
var client = new AWS.SecretsManager({
12+
endpoint: endpoint,
13+
region: region
14+
});
15+
16+
17+
// App
18+
client.getSecretValue({SecretId: secretName}, function(err, data) {
19+
if(err) {
20+
if(err.code === 'ResourceNotFoundException')
21+
console.log("The requested secret " + secretName + " was not found");
22+
else if(err.code === 'InvalidRequestException')
23+
console.log("The request was invalid due to: " + err.message);
24+
else if(err.code === 'InvalidParameterException')
25+
console.log("The request had invalid params: " + err.message);
26+
}
27+
else {
28+
// Decrypted secret using the associated KMS CMK
29+
// Depending on whether the secret was a string or binary, one of these fields will be populated
30+
if(data.SecretString !== "") {
31+
secret = data.SecretString;
32+
// console.log(secret);
33+
} else {
34+
binarySecretData = data.SecretBinary;
35+
}
36+
}
37+
38+
// Your code goes here.
39+
console.log(`Secret retrieved from AWS SecretsManager: ${secretName} is ${secret}`);
40+
});
41+
42+
43+
44+
45+
46+

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

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -678,10 +678,98 @@ 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-
=== Update the IAM role
681+
This section will show how to create a secret using AWS Secrets Manager and access it in a Pod.
682+
683+
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 for protection of the secret. It also integrates with AWS IAM and AWS CloudTrail to provide fine-grained access, audit and alerting integration.
684+
685+
=== Update the IAM role for EKS or `kops` Kubernetes Cluster
686+
687+
==== EKS Kubernetes Cluster
688+
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.
689+
690+
In the IAM Console, click `roles` and type `NodeInstanceRole` and click it. In the Permissions tab, expand the inline policy and click `Edit policy`. Add the `secretsManager:GetSecretValue` permission to the policy so the policy looks similar to the one below.
691+
692+
{
693+
"Version": "2012-10-17",
694+
"Statement": [
695+
{
696+
"Effect": "Allow",
697+
"Action": [
698+
"secretsmanager:GetSecretValue",
699+
"secretsmanager:DescribeSecret"
700+
],
701+
"Resource": [
702+
"arn:aws:secretsmanager:<region>:<account-id>:secret:<secret-name>"
703+
]
704+
}
705+
]
706+
}
707+
708+
==== `kops` Kubernetes Cluster
709+
710+
EC2 worker nodes use an instance profile to allow the EC2 node instances to access other AWS services. This role must be updated to allow the worker nodes to read the secrets from Secrets Manager.
711+
712+
In the IAM Console click `roles` and type `nodes` into the search box. Find the `nodes.example.cluster.k8s.local` role
713+
and click it. In the Permissions tab, expand the inline policy for `nodes.example.cluster.k8s.local` and click
714+
`Edit policy`.
715+
716+
{
717+
"Version": "2012-10-17",
718+
"Statement": [
719+
{
720+
"Effect": "Allow",
721+
"Action": [
722+
"secretsmanager:GetSecretValue",
723+
"secretsmanager:DescribeSecret"
724+
],
725+
"Resource": [
726+
"arn:aws:secretsmanager:<region>:<account-id>:secret:<secret-name>"
727+
]
728+
}
729+
]
730+
}
731+
682732
=== Create secrets
733+
734+
. Create a secret key-value pair using https://docs.aws.amazon.com/cli/latest/reference/secretsmanager/create-secret.html[AWS Secrets Manager CLI].
735+
736+
aws secretsmanager create-secret --name testsecret --description "EKS/kops Demo Secret" --secret-string [{"testkey":"testvalue"}] --region us-west-2
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
739+
740+
aws secretsmanager get-secret-value --secret-id testsecret --region us-west-2
741+
683742
=== Consume secrets in a Pod
684743

744+
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.
745+
746+
The Pod configuration is shown below:
747+
748+
apiVersion: v1
749+
kind: Pod
750+
metadata:
751+
name: pod-secretsmanager
752+
spec:
753+
containers:
754+
- name: pod-secretsmanager
755+
image: paavanmistry/node-aws-sm-demo:latest
756+
restartPolicy: Never
757+
758+
Create the Pod:
759+
760+
$ kubectl apply -f templates/pod-secretsmanager.yaml
761+
pod "pod-parameter-store" configured
762+
763+
Check the logs of the Pod:
764+
765+
$ kubectl logs pod-secretsmanager
766+
Secret retrieved from AWS SecretsManager: testsecret is {testkey}:{testvalue}
767+
768+
Clean up:
769+
770+
$ kubectl delete -f templates/pod-secretsmanager.yaml
771+
$ aws secretsmanager delete-secret --secret-id testsecret --region us-west-2
772+
685773
== Secrets using Vault
686774

687775
https://www.vaultproject.io/[Hashicorp Vault] is a tool for managing secrets. It secures, stores and tightly controls access to tokens, passwords, certificates, API keys and other secrets.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
apiVersion: v1
2+
kind: Pod
3+
metadata:
4+
name: pod-secretsmanager
5+
spec:
6+
containers:
7+
- name: pod-secretsmanager
8+
image: paavanmistry/node-aws-sm-demo:latest
9+
restartPolicy: Never

0 commit comments

Comments
 (0)