Skip to content

Commit 856e3b1

Browse files
authored
feat(opensearch): add logic to only log specific field when less verbosity is needed for opensearch access policy custom resource (#34701)
### Issue # (if applicable) Closes #29093 ### Reason for this change Opensearch access policy defined via `OpenSearchAccessPolicy` (custom-resource) return failures in case of large policy documents, even if the policy change is successful Issue comes for the CFN limit of 4k on the response size ### Description of changes Added an optional parameter `verboseOutput` in `OpenSearchAccessPolicyProps` to allow users of the custom resource to optionally toggle on/off the verbose option : On turning it `false` only `["DomainConfig.AccessPolicies.Status.State", "DomainConfig.AccessPolicies.Status.UpdateVersion"]` are shown ***NOTE : Default behavior of verbose output is retained*** ### Describe any new or updated permissions being added NONE ### Description of how you validated changes - Adding unit tests for the changes ### Checklist - [ ] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent fc46733 commit 856e3b1

File tree

8 files changed

+406
-90
lines changed

8 files changed

+406
-90
lines changed

packages/@aws-cdk-testing/framework-integ/test/aws-opensearchservice/test/integ.opensearch.custom-kms-key.js.snapshot/OpenSearchCustomKmsIntegDefaultTestDeployAssertA1F4FD6B.assets.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-opensearchservice/test/integ.opensearch.custom-kms-key.js.snapshot/OpenSearchCustomKmsIntegDefaultTestDeployAssertA1F4FD6B.template.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 137 additions & 40 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-opensearchservice/test/integ.opensearch.custom-kms-key.js.snapshot/cdk-integ-opensearch-custom-kms-key.assets.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-opensearchservice/test/integ.opensearch.custom-kms-key.js.snapshot/manifest.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-opensearchservice/test/integ.opensearch.https.js.snapshot/asset.97484721f29e34bf38d7a459804dd2d2a8dea6f8c27d7531e215bf4274fbc895.bundle/index.js

Lines changed: 137 additions & 40 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/aws-cdk-lib/aws-opensearchservice/lib/opensearch-access-policy.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ export interface OpenSearchAccessPolicyProps {
2121
* The access policy statements for the OpenSearch cluster
2222
*/
2323
readonly accessPolicies: iam.PolicyStatement[];
24+
25+
/**
26+
* Flag to control verbosity of OpenSearch policy custom resource result
27+
* If verbose output is actively disabled it will only output specific fields
28+
* This is can be used to limit the response body of the custom resource, in cases it exceeds the CFN 4k limit
29+
* @default true
30+
*/
31+
readonly verboseOutput?: boolean;
2432
}
2533

2634
/**
@@ -47,7 +55,8 @@ export class OpenSearchAccessPolicy extends cr.AwsCustomResource {
4755
}),
4856
},
4957
// this is needed to limit the response body, otherwise it exceeds the CFN 4k limit
50-
outputPaths: ['DomainConfig.AccessPolicies'],
58+
// If verbose output is actively disabled it will only output specific fields
59+
outputPaths: (props.verboseOutput === undefined || props.verboseOutput) ? ['DomainConfig.AccessPolicies'] : ['DomainConfig.AccessPolicies.Status.State', 'DomainConfig.AccessPolicies.Status.UpdateVersion'],
5160
physicalResourceId: cr.PhysicalResourceId.of(`${props.domainName}AccessPolicy`),
5261
},
5362
policy: cr.AwsCustomResourcePolicy.fromStatements([new iam.PolicyStatement({ actions: ['es:UpdateDomainConfig'], resources: [props.domainArn] })]),

packages/aws-cdk-lib/aws-opensearchservice/test/opensearch-access-policy.test.ts

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,116 @@ test('support access policy added inline and later', () => {
118118
}),
119119
});
120120
});
121+
122+
test('handling of verbose output via flag explicitly set', () => {
123+
const domainArn = 'test:arn';
124+
125+
new OpenSearchAccessPolicy(stack, 'OpenSearchAccessPolicy', {
126+
domainName: 'TestDomain',
127+
domainArn: 'test:arn',
128+
accessPolicies: [
129+
new iam.PolicyStatement({
130+
effect: iam.Effect.ALLOW,
131+
actions: ['es:ESHttp*'],
132+
principals: [new iam.AnyPrincipal()],
133+
resources: ['test:arn'],
134+
}),
135+
],
136+
verboseOutput: true,
137+
});
138+
139+
Template.fromStack(stack).hasResourceProperties('Custom::OpenSearchAccessPolicy', {
140+
ServiceToken: {
141+
'Fn::GetAtt': [
142+
'AWS679f53fac002430cb0da5b7982bd22872D164C4C',
143+
'Arn',
144+
],
145+
},
146+
Create: JSON.stringify({
147+
action: 'updateDomainConfig',
148+
service: 'OpenSearch',
149+
parameters: {
150+
DomainName: 'TestDomain',
151+
AccessPolicies: '{"Statement":[{"Action":"es:ESHttp*","Effect":"Allow","Principal":{"AWS":"*"},"Resource":"test:arn"}],"Version":"2012-10-17"}',
152+
},
153+
outputPaths: ['DomainConfig.AccessPolicies'],
154+
physicalResourceId: { id: 'TestDomainAccessPolicy' },
155+
}),
156+
Update: JSON.stringify({
157+
action: 'updateDomainConfig',
158+
service: 'OpenSearch',
159+
parameters: {
160+
DomainName: 'TestDomain',
161+
AccessPolicies: '{"Statement":[{"Action":"es:ESHttp*","Effect":"Allow","Principal":{"AWS":"*"},"Resource":"test:arn"}],"Version":"2012-10-17"}',
162+
},
163+
outputPaths: ['DomainConfig.AccessPolicies'],
164+
physicalResourceId: { id: 'TestDomainAccessPolicy' },
165+
}),
166+
});
167+
Template.fromStack(stack).hasResourceProperties('AWS::IAM::Policy', {
168+
PolicyDocument: {
169+
Statement: [{
170+
Action: 'es:UpdateDomainConfig',
171+
Effect: 'Allow',
172+
Resource: domainArn,
173+
}],
174+
},
175+
});
176+
});
177+
178+
test('handling of less verbose output', () => {
179+
const domainArn = 'test:arn';
180+
181+
new OpenSearchAccessPolicy(stack, 'OpenSearchAccessPolicy', {
182+
domainName: 'TestDomain',
183+
domainArn: 'test:arn',
184+
accessPolicies: [
185+
new iam.PolicyStatement({
186+
effect: iam.Effect.ALLOW,
187+
actions: ['es:ESHttp*'],
188+
principals: [new iam.AnyPrincipal()],
189+
resources: ['test:arn'],
190+
}),
191+
],
192+
verboseOutput: false,
193+
});
194+
195+
Template.fromStack(stack).hasResourceProperties('Custom::OpenSearchAccessPolicy', {
196+
ServiceToken: {
197+
'Fn::GetAtt': [
198+
'AWS679f53fac002430cb0da5b7982bd22872D164C4C',
199+
'Arn',
200+
],
201+
},
202+
Create: JSON.stringify({
203+
action: 'updateDomainConfig',
204+
service: 'OpenSearch',
205+
parameters: {
206+
DomainName: 'TestDomain',
207+
AccessPolicies: '{"Statement":[{"Action":"es:ESHttp*","Effect":"Allow","Principal":{"AWS":"*"},"Resource":"test:arn"}],"Version":"2012-10-17"}',
208+
},
209+
outputPaths: ['DomainConfig.AccessPolicies.Status.State', 'DomainConfig.AccessPolicies.Status.UpdateVersion'],
210+
physicalResourceId: { id: 'TestDomainAccessPolicy' },
211+
}),
212+
Update: JSON.stringify({
213+
action: 'updateDomainConfig',
214+
service: 'OpenSearch',
215+
parameters: {
216+
DomainName: 'TestDomain',
217+
AccessPolicies: '{"Statement":[{"Action":"es:ESHttp*","Effect":"Allow","Principal":{"AWS":"*"},"Resource":"test:arn"}],"Version":"2012-10-17"}',
218+
},
219+
outputPaths: ['DomainConfig.AccessPolicies.Status.State', 'DomainConfig.AccessPolicies.Status.UpdateVersion'],
220+
physicalResourceId: { id: 'TestDomainAccessPolicy' },
221+
}),
222+
});
223+
Template.fromStack(stack).hasResourceProperties('AWS::IAM::Policy', {
224+
PolicyDocument: {
225+
Statement: [{
226+
Action: 'es:UpdateDomainConfig',
227+
Effect: 'Allow',
228+
Resource: domainArn,
229+
}],
230+
},
231+
});
232+
});
233+

0 commit comments

Comments
 (0)