-
Notifications
You must be signed in to change notification settings - Fork 1.5k
CSFLE auto encryption tests improvements #1788
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* Copyright 2008-present MongoDB, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* 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. | ||
*/ | ||
|
||
package org.mongodb.scala.syncadapter | ||
|
||
import com.mongodb.ClusterFixture.TIMEOUT_DURATION | ||
import com.mongodb.client.model.{ CreateCollectionOptions, CreateEncryptedCollectionParams } | ||
import com.mongodb.client.model.vault.{ | ||
DataKeyOptions, | ||
EncryptOptions, | ||
RewrapManyDataKeyOptions, | ||
RewrapManyDataKeyResult | ||
} | ||
import com.mongodb.client.result.DeleteResult | ||
import com.mongodb.client.vault.{ ClientEncryption => JClientEncryption } | ||
import com.mongodb.client.{ MongoDatabase => JMongoDatabase } | ||
import org.bson.{ BsonBinary, BsonDocument, BsonValue } | ||
import org.bson.conversions.Bson | ||
import org.mongodb.scala.vault.ClientEncryption | ||
import reactor.core.publisher.Mono | ||
|
||
import java.util.Objects.requireNonNull | ||
|
||
case class SyncClientEncryption(wrapped: ClientEncryption) extends JClientEncryption { | ||
|
||
override def createDataKey(kmsProvider: String): BsonBinary = | ||
requireNonNull(Mono.from(wrapped.createDataKey(kmsProvider, new DataKeyOptions)).block(TIMEOUT_DURATION)) | ||
|
||
override def createDataKey(kmsProvider: String, dataKeyOptions: DataKeyOptions): BsonBinary = | ||
requireNonNull(Mono.from(wrapped.createDataKey(kmsProvider, dataKeyOptions)).block(TIMEOUT_DURATION)) | ||
|
||
override def encrypt(value: BsonValue, options: EncryptOptions): BsonBinary = | ||
requireNonNull(Mono.from(wrapped.encrypt(value, options)).block(TIMEOUT_DURATION)) | ||
|
||
override def encryptExpression(expression: Bson, options: EncryptOptions): BsonDocument = | ||
requireNonNull(Mono.from(wrapped | ||
.encryptExpression(expression.toBsonDocument, options)).block(TIMEOUT_DURATION).toBsonDocument) | ||
|
||
override def decrypt(value: BsonBinary): BsonValue = | ||
requireNonNull(Mono.from(wrapped.decrypt(value)).block(TIMEOUT_DURATION)) | ||
|
||
override def deleteKey(id: BsonBinary): DeleteResult = | ||
requireNonNull(Mono.from(wrapped.deleteKey(id)).block(TIMEOUT_DURATION)) | ||
|
||
override def getKey(id: BsonBinary): BsonDocument = Mono.from(wrapped.getKey(id)).block(TIMEOUT_DURATION) | ||
|
||
override def getKeys = new SyncFindIterable[BsonDocument](wrapped.keys) | ||
|
||
override def addKeyAltName(id: BsonBinary, keyAltName: String): BsonDocument = | ||
Mono.from(wrapped.addKeyAltName(id, keyAltName)).block(TIMEOUT_DURATION) | ||
|
||
override def removeKeyAltName(id: BsonBinary, keyAltName: String): BsonDocument = | ||
Mono.from(wrapped.removeKeyAltName(id, keyAltName)).block(TIMEOUT_DURATION) | ||
|
||
override def getKeyByAltName(keyAltName: String): BsonDocument = | ||
Mono.from(wrapped.getKeyByAltName(keyAltName)).block(TIMEOUT_DURATION) | ||
|
||
override def rewrapManyDataKey(filter: Bson): RewrapManyDataKeyResult = | ||
requireNonNull(Mono.from(wrapped.rewrapManyDataKey(filter)).block(TIMEOUT_DURATION)) | ||
|
||
override def rewrapManyDataKey(filter: Bson, options: RewrapManyDataKeyOptions): RewrapManyDataKeyResult = | ||
requireNonNull(Mono.from(wrapped.rewrapManyDataKey(filter, options)).block(TIMEOUT_DURATION)) | ||
|
||
override def createEncryptedCollection( | ||
database: JMongoDatabase, | ||
collectionName: String, | ||
createCollectionOptions: CreateCollectionOptions, | ||
createEncryptedCollectionParams: CreateEncryptedCollectionParams | ||
): BsonDocument = { | ||
database match { | ||
case syncMongoDatabase: SyncMongoDatabase => | ||
requireNonNull(Mono.from(wrapped.createEncryptedCollection( | ||
syncMongoDatabase.wrapped, | ||
collectionName, | ||
createCollectionOptions, | ||
createEncryptedCollectionParams | ||
)).block(TIMEOUT_DURATION)) | ||
case _ => throw new AssertionError(s"Unexpected database type: ${database.getClass}") | ||
} | ||
} | ||
|
||
override def close(): Unit = { | ||
wrapped.close() | ||
} | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given that the legacy CSFLE tests under the /legacy folder in the specification repository have been converted to the unified format under the /unified tests folder in this PR, do we still need AbstractClientSideEncryptionTest that runs the legacy tests? Or are there still some tests that haven’t yet been converted to the unified format? By a quick check, looks like all file names in legacy folder are present in unified folder. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Copyright 2008-present MongoDB, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* 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. | ||
*/ | ||
|
||
package org.mongodb.scala.unified | ||
|
||
object ClientEncryptionTest extends UnifiedTest { | ||
val directory = "client-side-encryption/tests/unified" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Copyright 2008-present MongoDB, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* 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. | ||
*/ | ||
|
||
package org.mongodb.scala.unified | ||
|
||
object UnifiedCrudTest extends UnifiedTest { | ||
val directory = "crud" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright 2008-present MongoDB, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* 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. | ||
*/ | ||
|
||
package org.mongodb.scala.unified | ||
|
||
import com.mongodb.client.gridfs.{ GridFSBucket => JGridFSBucket } | ||
import com.mongodb.client.unified.UnifiedTest.Language | ||
import com.mongodb.client.unified.{ UnifiedTest, UnifiedTest => JUnifiedTest } | ||
import com.mongodb.client.vault.{ ClientEncryption => JClientEncryption } | ||
import com.mongodb.client.{ MongoClient => JMongoClient, MongoDatabase => JMongoDatabase } | ||
import com.mongodb.reactivestreams.client.internal.vault.ClientEncryptionImpl | ||
import com.mongodb.{ ClientEncryptionSettings => JClientEncryptionSettings, MongoClientSettings } | ||
import org.junit.jupiter.api.TestInstance | ||
import org.junit.jupiter.api.TestInstance.Lifecycle | ||
import org.junit.jupiter.params.provider.Arguments | ||
import org.mongodb.scala.MongoClient | ||
import org.mongodb.scala.MongoClient.DEFAULT_CODEC_REGISTRY | ||
import org.mongodb.scala.syncadapter.{ SyncClientEncryption, SyncMongoClient } | ||
import org.mongodb.scala.vault.ClientEncryption | ||
|
||
import java.util | ||
|
||
@TestInstance(Lifecycle.PER_CLASS) | ||
abstract class UnifiedTest extends JUnifiedTest { | ||
|
||
val directory: String | ||
|
||
def data(): util.Collection[Arguments] = JUnifiedTest.getTestData(directory, true, Language.SCALA) | ||
|
||
override def createMongoClient(settings: MongoClientSettings): JMongoClient = | ||
SyncMongoClient(MongoClient(MongoClientSettings.builder(settings).codecRegistry(DEFAULT_CODEC_REGISTRY).build())) | ||
|
||
override def createGridFSBucket(database: JMongoDatabase): JGridFSBucket = | ||
throw new NotImplementedError("Not implemented") | ||
|
||
override def createClientEncryption( | ||
keyVaultClient: JMongoClient, | ||
clientEncryptionSettings: JClientEncryptionSettings | ||
): JClientEncryption = { | ||
keyVaultClient match { | ||
case client: SyncMongoClient => | ||
SyncClientEncryption(ClientEncryption(new ClientEncryptionImpl( | ||
client.wrapped.wrapped, | ||
clientEncryptionSettings | ||
))) | ||
case _ => throw new IllegalArgumentException(s"Invalid keyVaultClient type: ${keyVaultClient.getClass}") | ||
} | ||
} | ||
|
||
override protected def isReactive: Boolean = true | ||
|
||
override protected def getLanguage: Language = Language.SCALA | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nit] Could we match the formatting used below for consistency, unless this is from Spotless auto-formatting?