I have:
eks.Cluster.addResource()
I tried to deploy Secret this way:
import * as sm from "@aws-cdk/aws-secretsmanager";
getSecret(secretKey: string): string {
let secretTokens = sm.Secret.fromSecretArn(scope, "ImportedSecrets", awsSecretStorageArn);
return secretTokens.secretValueFromJson(secretKey).toString();
}
createKubernetesImagePullSecrets(k8s: eks.Cluster): void {
let eksSecretStorageName = this.env.awsResourcesConfig.k8sImagePullSecretStorageName;
k8s.addResource(eksSecretStorageName, {
apiVersion: "v1",
kind: "Secret",
metadata: {
name: eksSecretStorageName,
},
data: {
".dockerconfigjson": this.getSecret('hub-secret'),
},
type: "kubernetes.io/dockerconfigjson",
});
}
I'm getting an error from CloudFormation:
Secret in version "v1" cannot be handled as a Secret: v1.Secret.ObjectMeta: v1.ObjectMeta.TypeMeta: Kind: Data: decode base64: illegal base64 data at input byte 0
This happens because the secret token is not expanded and the ".dockerconfigjson" field value, in this case, looks like ${Token[TOKEN.417]}
Is there a way to deploy the EKS Secret resource and expand secret tokens correctly during deployment?
I created a temporary workaround for this, by downloading a plain-text version of secrets with aws-cli
. Not a safe way, but works. Do not use this if you have a more secure solution.
import { execSync } from "child_process";
extractSecretValues(awsSecretStorageArn: string) : Map<string, string> {
let map = new Map<string, string>();
let secretsContent = execSync(`aws secretsmanager get-secret-value --secret-id ${awsSecretStorageArn}`).toString();
let secrets = JSON.parse(secretsContent);
if (!secrets)
throw new Error(`Secret values could not be extracted from ${awsSecretStorageArn}`);
if (secrets.SecretString) {
let secretValuesObj = JSON.parse(secrets.SecretString);
for (let [secretKey, secretValue] of Object.entries<string>(secretValuesObj)) {
map.set(secretKey, secretValue);
}
}
return map;
}
let secretValueMap = extractSecretValues();
createKubernetesImagePullSecrets(k8s: eks.Cluster): void {
let eksSecretStorageName = this.env.awsResourcesConfig.k8sImagePullSecretStorageName;
k8s.addResource(eksSecretStorageName, {
apiVersion: "v1",
kind: "Secret",
metadata: {
name: eksSecretStorageName,
},
data: {
".dockerconfigjson": secretValueMap.get('hub-secret'),
},
type: "kubernetes.io/dockerconfigjson",
});
}