Skip to content

Add filterSensitiveLog for Union types #190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 34 commits into from
Aug 25, 2020

Conversation

trivikr
Copy link
Contributor

@trivikr trivikr commented Jul 1, 2020

Issue #, if available:
Follow up #170

Description of changes:
This PR adds filterSensitiveLog for Union types

Testing done prior to code change on TypeScript playground

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@trivikr trivikr marked this pull request as ready for review July 6, 2020 16:55
@trivikr trivikr marked this pull request as draft July 6, 2020 21:01
@trivikr
Copy link
Contributor Author

trivikr commented Jul 6, 2020

Converted to draft as it's blocked on #191

@kstich kstich self-requested a review July 13, 2020 18:58
@trivikr trivikr force-pushed the union-filter-sensitive-log branch from 7b70ddf to 2f8681f Compare August 18, 2020 22:56
@trivikr trivikr marked this pull request as ready for review August 19, 2020 00:40
writer.write("};");
}
}
writer.write("if (${1L}.$$unknown !== undefined) return {[${1L}.$$unknown[0]]: ${1L}.$$unknown[1]};",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may want to not log the value of an unknown union member, as there's no way to know if it is sensitive or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logged the value of an unknown union member as 'UNKNOWN' in 0382f0e

Shape memberTarget = model.expectShape(member.getTarget());
String memberName = symbolProvider.toMemberName(member);

if (member.getMemberTrait(model, SensitiveTrait.class).isPresent()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A lot of this code, specifically the branching, looks similar to that of writing this function for structures (StructuredMemberWriter.writeFilterSensitiveLog.) Can it be consolidated cleanly at all?

Copy link
Contributor Author

@trivikr trivikr Aug 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can it be consolidated cleanly at all?

I'm open to suggestions for modifications.

The branching can't be consolidated between Union and Structure, as there's a check for SimpleShape in case of CollectionShape/MapShape members in case of Union which doesn't exist in latter.

Copy link
Contributor Author

@trivikr trivikr Aug 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consolidated by creating new method writeMemberFilterSensitiveLog in e1fd23c, and calling it from Union in e5361be 🎉

It was done by moving SimpleShape check inside write<Collection|Map>FilterSensitiveLog
It's redundant for Structure as it'll never reach because of isMemberOverwriteRequired call, but is required for Union.


@sensitive
sensitiveBar: String
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add newlines to the end of these files? There are quite a few without them.

If you use IntelliJ, you can set this automatically under Preferences -> Editor -> General -> Ensure an empty line at the end of a file on Save

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in commit 481a425

@trivikr trivikr force-pushed the union-filter-sensitive-log branch from b67715f to 0382f0e Compare August 21, 2020 16:09
String itemParam = "item";
Shape collectionMemberTarget = model.expectShape(collectionMember.getTarget());
writer.write("$L => ", itemParam);
if (collectionMemberTarget.isStructureShape() || collectionMemberTarget.isUnionShape()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks really similar to the code in writeMemberFilterSensitiveLog, is it possible to refactor this chunk of if/else to instead use that? I might be missing something, but the final else is the only thing that looks different to me here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The purpose of the else block is to throw CodegenException in case redundant code is being written for provided models at Runtime for SimpleShape.

But let me verify that in a Unit test instead, and reuse code from writeMemberFilterSensitiveLog

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But let me verify that in a Unit test instead, and reuse code from writeMemberFilterSensitiveLog

Code reused in commit 4d94416
Unit test test-insensitive-list already checks for redundant code not being present.

writer.write("...$L,", accParam);
Shape mapMemberTarget = model.expectShape(mapMember.getTarget());
writer.openBlock("[$L]: ", ",", keyParam, () -> {
if (mapMemberTarget.isStructureShape() || mapMemberTarget.isUnionShape()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks really similar to the code in writeMemberFilterSensitiveLog, is it possible to refactor this chunk of if/else to instead use that? I might be missing something, but the final else is the only thing that looks different to me here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in commit 4d94416

@trivikr trivikr force-pushed the union-filter-sensitive-log branch from e5361be to 4d94416 Compare August 25, 2020 02:37
@kstich kstich merged commit 4b001eb into smithy-lang:master Aug 25, 2020
@trivikr trivikr deleted the union-filter-sensitive-log branch October 11, 2022 18:10
srchase pushed a commit to srchase/smithy-typescript that referenced this pull request Mar 17, 2023
* Add writeFilterSensitiveLog to UnionGenerator with check for sensitive member

* Return $undefined if no value matched in filterSensitiveLog

* Remove fromEntries as TSConfig doesn't have it on type ObjectConstructor

Can be fixed by adding es2017 in lib https://stackoverflow.com/questions/45422573
But waiting for root TSConfig to be pushed

* Non-sensitive SimpleShape in Union.filterSensitiveLog

* Add check for obj.$unknown defined in Union.filterSensitiveLog

* Call writeStructureFilterSensitiveLog from Union

* Union.filterSensitiveLog for complex members

* Add check for UnionShape in filterSensitiveLog

* Update documentation for UnionGenerator

* Add newlines within Union components

* Fix checkstyle errors

* Test callsFilterForUnionWithSensitiveData

* Test callsFilterInUnionWithSensitiveData

* Test filtersSensitiveUnion

* Test callsFilterForUnionWithoutSensitiveData

* Test callsFilterInUnionWithoutSensitiveData

* rename test-insensitive-structure

* Test filtersSensitiveMemberPointingToUnion

* Test callsFilterForListWithUnionWithSensitiveData

* Test callsFilterInListWithUnionWithSensitiveData

* Test callsFilterForMapWithUnionWithSensitiveData

* Test callsFilterInMapWithUnionWithSensitiveData

* Fix ordering of objects in callsFilterInUnionWithoutSensitiveData

* Remove unused memberTarget instanceof UnionShape

* Disable filterSensitiveLog for event stream

* Rename STREAMING_TRAIT to STREAMING_CONTENT

* Log unknown union member as 'UNKNOWN'

* Use isStructureShape() and isUnionShape() instead of instanceof

* Added newline at the end of .smithy test files

* Add tests for complex shapes inside Union

* Create new function writeMemberFilterSensitiveLog

* Call writeMemberFilterSensitiveLog from Union

* Call writeMemberFilterSensitiveLog from Collection/Member

* Throw CodegenException in the else block of writeMemberFilterSensitiveLog
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants