aws / aws/aws-cdk

aws-cloudfront: SecretValue not resolved inside Fn.base64() passed to FunctionCode

Open
#21,980 2 comments 0 reactions 0 assignees View on GitHub
@aws-cdk/aws-cloudfront bug effort/medium p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

### Describe the bug

When creating a CloudFront Function for simple HTTP basic authentication, you can insert a SecretValue into the inline FunctionCode. But if you wrap the SecretValue into Fn.base64(), the actually deployed value is not the resolved SecretValue, but a base64-encoded reference string that looks like `{{resolve:secretsmanager:...`.

In the case of HTTP basic auth, we need to pass a base64-encoded value that contains `username:password`, where username and password come from Secrets Manager. The workaround is to do the base64-encoding at runtime inside the CloudFront function, but it would be clearer to encode it at deployment time with Fn.base64().

### Expected Behavior

Passing a SecretValue wrapped with Fn.base64() should resolve into the base64-encoded secret value.

### Current Behavior

Passing a SecretValue wrapped with Fn.base64() is deployed as a base64-encoded unresolved `{{resolve:secretsmanager:...` string.

### Reproduction Steps

Untested quick reference code:
```
const username = SecretValue.secretsManager(props.secretName, { jsonField: 'username' }).unsafeUnwrap();
const password = SecretValue.secretsManager(props.secretName, { jsonField: 'password' }).unsafeUnwrap();
const authValue = `${username }:${password }`;
const basicAuthFunction = new CloudFrontFunction(this, 'BasicAuthFunction', {
code: FunctionCode.fromInline(`
...
if (event.request.headers.authorization && event.request.headers.authorization.value === '${Fn.base64(authValue)}')
...
`),
});
```

### Possible Solution

_No response_

### Additional Information/Context

_No response_

### CDK CLI Version

2.40.0 (build 56ba2ab)

### Framework Version

_No response_

### Node.js Version

v16.13.1

### OS

Windows 11 WSL2

### Language

Typescript

### Language Version

_No response_

### Other information

_No response_

Contributor guide

Open the contributing guide

Research direction

Start by tracing how Fn.base64() handles the SecretValue tokens used by SecretValue.secretsManager(), then follow their use in FunctionCode.fromInline() for CloudFrontFunction. Reproduce the issue with the provided basic-auth example and verify that deployment produces a base64-encoded resolved secret rather than an encoded Secrets Manager reference.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, typescript
Domain
cloud, infrastructure
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.