Skip to content

Conversation

@mikereiche
Copy link
Collaborator

@mikereiche mikereiche commented Dec 16, 2022

The key changes are at lines 141 and 145 of CryptoConverter.

(a) use objectMapper to encode strings instead of just escaping and adding quotes; and
(b) use context.getConverter().writeInternalRoot() to serialize instead of
relying on context.read(value).toString()) which relies on the entity object having a toString() that produces json.

Closes #1621.

  • You have read the Spring Data contribution guidelines.
  • There is a ticket in the bug tracker for the project in our JIRA.
  • You use the code formatters provided here and have them applied to your changes. Don’t submit any formatting related changes.
  • You submit test cases (unit or integration tests) that back your changes.
  • You added yourself as author in the headers of the classes you touched. Amend the date range in the Apache license header if needed. For new types, add the license header (copy from another file and set the current year only).

@mikereiche
Copy link
Collaborator Author

@dnault

@mikereiche mikereiche changed the title Fix serialiation/deserialization for field level encryption. Fix serialization for field level encryption. Dec 16, 2022
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The CryptoConverter now needs the ObjectMapper.

TypeInformation<?> type = ClassTypeInformation.from(value.getClass());
((MappingCouchbaseConverter) this).writeInternalRoot(value, embeddedDoc, type, false, null);
((MappingCouchbaseConverter) this).writeInternalRoot(value, embeddedDoc, type, false, null, true);
value = embeddedDoc;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

writeInternalRoot needs a flag of whether or not to process PropertyValueConverters (specifically the CryptoConverter) so it doesn't recursively encrypt such an annotated property.

import org.springframework.data.convert.ValueConversionContext;
import org.springframework.data.mapping.PersistentProperty;

import com.couchbase.client.core.encryption.CryptoManager;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Addition of ObjectMapper.

return plaintextParser.readValueAs(beanPropertyTypeRef);
*/

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The deleted comment is a bit misleading. The conversions do take place in context.getConverter.read() and getPotentiallyConvertedSimpleRead() below.

CouchbaseDocument doc = new CouchbaseDocument();
context.getConverter().writeInternalRoot(value, doc, property.getTypeInformation(), false, property, false);
plainText = JsonObject.from(doc.export()).toBytes();
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The old code relied on the entity (Airport, Address etc) produced by context.read(value) have a nice toString() method that gives the json representation of the object. It's a bad assumption.

* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Add one of our @jsonvalue Enums to Address which gets used in the FLE tests and elsewhere.

T10("\"10%"), T20("\"20%"), T30("\"30%");

private final String code;

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Add a quote to the content of the Enum serialization.

@Encrypted public DateTime encDateTime;

@Encrypted public ETurbulenceCategory turbulence;
@Encrypted public Address encAddress = new Address();
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Add an encrypted @jsonvalue Enum.

encString = "myString";
encStrings = new String[] { "myString" };
encString = "myS\"tring";
encStrings = new String[] { "mySt\"ring" };
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

make a tricky String and String array

encDateTime = NOW_DateTime;

encAddress = new Address();

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

let the caller (test case) initialize this.

@mikereiche mikereiche merged commit 1fcafab into main Dec 16, 2022
@mikereiche mikereiche deleted the datacouch_1621_fixes_to_fle_encryption_serialization_deserialization branch January 24, 2023 21:09
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.

revisit serialization/deserialization in CryptoConverter

2 participants