Skip to content

Added ClientEncryption methods to scala #1779

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public final class RewrapManyDataKeyOptions {
private String provider;
private BsonDocument masterKey;

/**
* Construct a new instance
*/
public RewrapManyDataKeyOptions() {
}

/**
* Sets the provider name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@

package org.mongodb.scala.model

import com.mongodb.annotations.{ Beta, Reason }
import com.mongodb.client.model.vault.{ DataKeyOptions => JDataKeyOptions }
import com.mongodb.client.model.vault.{ EncryptOptions => JEncryptOptions }
import com.mongodb.client.model.vault.{ RangeOptions => JRangeOptions }
import com.mongodb.client.model.vault.{ RewrapManyDataKeyResult => JRewrapManyDataKeyResult }
import com.mongodb.client.model.vault.{ RewrapManyDataKeyOptions => JRewrapManyDataKeyOptions }

/**
* This package contains options classes for the key vault API
Expand Down Expand Up @@ -65,4 +66,28 @@ package object vault {
def apply(): RangeOptions = new JRangeOptions()
}

/**
* The result of the rewrapping of data keys
*
* @since 5.6
*/
type RewrapManyDataKeyResult = JRewrapManyDataKeyResult

/**
* The result of the rewrapping of data keys
*
* @since 5.6
*/
type RewrapManyDataKeyOptions = JRewrapManyDataKeyOptions

/**
* The rewrap many data key options
*
* The `getMasterKey` document MUST have the fields corresponding to the given provider as specified in masterKey.
*
* @since 5.6
*/
object RewrapManyDataKeyOptions {
def apply() = new JRewrapManyDataKeyOptions()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,15 @@ import com.mongodb.client.model.{ CreateCollectionOptions, CreateEncryptedCollec
import java.io.Closeable
import com.mongodb.reactivestreams.client.vault.{ ClientEncryption => JClientEncryption }
import org.bson.{ BsonBinary, BsonDocument, BsonValue }
import org.mongodb.scala.{ Document, MongoDatabase, SingleObservable, ToSingleObservablePublisher }
import org.mongodb.scala.model.vault.{ DataKeyOptions, EncryptOptions }
import org.mongodb.scala.bson.conversions.Bson
import org.mongodb.scala.{ Document, FindObservable, MongoDatabase, SingleObservable, ToSingleObservablePublisher }
import org.mongodb.scala.model.vault.{
DataKeyOptions,
EncryptOptions,
RewrapManyDataKeyOptions,
RewrapManyDataKeyResult
}
import org.mongodb.scala.result.DeleteResult

/**
* The Key vault.
Expand All @@ -40,7 +47,7 @@ case class ClientEncryption(private val wrapped: JClientEncryption) extends Clos
* Creates a new key document and inserts into the key vault collection.
*
* @param kmsProvider the KMS provider
* @return a Publisher containing the identifier for the created data key
* @return an Observable containing the identifier for the created data key
*/
def createDataKey(kmsProvider: String): SingleObservable[BsonBinary] = createDataKey(kmsProvider, DataKeyOptions())

Expand All @@ -51,7 +58,7 @@ case class ClientEncryption(private val wrapped: JClientEncryption) extends Clos
*
* @param kmsProvider the KMS provider
* @param dataKeyOptions the options for data key creation
* @return a Publisher containing the identifier for the created data key
* @return an Observable containing the identifier for the created data key
*/
def createDataKey(kmsProvider: String, dataKeyOptions: DataKeyOptions): SingleObservable[BsonBinary] =
wrapped.createDataKey(kmsProvider, dataKeyOptions)
Expand All @@ -62,7 +69,7 @@ case class ClientEncryption(private val wrapped: JClientEncryption) extends Clos
*
* @param value the value to encrypt
* @param options the options for data encryption
* @return a Publisher containing the encrypted value, a BSON binary of subtype 6
* @return an Observable containing the encrypted value, a BSON binary of subtype 6
*/
def encrypt(value: BsonValue, options: EncryptOptions): SingleObservable[BsonBinary] =
wrapped.encrypt(value, options)
Expand All @@ -86,7 +93,7 @@ case class ClientEncryption(private val wrapped: JClientEncryption) extends Clos
* @note Requires MongoDB 8.0 or greater
* @param expression the Match Expression or Aggregate Expression
* @param options the options
* @return a Publisher containing the queryable encrypted range expression
* @return an Observable containing the queryable encrypted range expression
* @since 4.9
*/
def encryptExpression(
Expand All @@ -99,10 +106,87 @@ case class ClientEncryption(private val wrapped: JClientEncryption) extends Clos
* Decrypt the given value.
*
* @param value the value to decrypt, which must be of subtype 6
* @return a Publisher containing the decrypted value
* @return an Observable containing the decrypted value
*/
def decrypt(value: BsonBinary): SingleObservable[BsonValue] = wrapped.decrypt(value)

/**
* Finds a single key document with the given UUID (BSON binary subtype 0x04).
*
* @param id the data key UUID (BSON binary subtype 0x04)
* @return an Observable containing the single key document or an empty publisher if there is no match
* @since 5.6
*/
def getKey(id: BsonBinary): SingleObservable[BsonDocument] = wrapped.getKey(id)

/**
* Returns a key document in the key vault collection with the given keyAltName.
*
* @param keyAltName the alternative key name
* @return an Observable containing the matching key document or an empty publisher if there is no match
* @since 5.6
*/
def getKeyByAltName(keyAltName: String): SingleObservable[BsonDocument] = wrapped.getKeyByAltName(keyAltName)

/**
* Finds all documents in the key vault collection.
*
* @return a find Observable for the documents in the key vault collection
* @since 5.6
*/
def keys: FindObservable[BsonDocument] = FindObservable(wrapped.getKeys)

/**
* Adds a keyAltName to the keyAltNames array of the key document in the key vault collection with the given UUID.
*
* @param id the data key UUID (BSON binary subtype 0x04)
* @param keyAltName the alternative key name to add to the keyAltNames array
* @return an Observable containing the previous version of the key document or an empty publisher if no match
* @since 5.6
*/
def addKeyAltName(id: BsonBinary, keyAltName: String): SingleObservable[BsonDocument] =
wrapped.addKeyAltName(id, keyAltName)

/**
* Removes the key document with the given data key from the key vault collection.
*
* @param id the data key UUID (BSON binary subtype 0x04)
* @return an Observable containing the delete result
* @since 5.6
*/
def deleteKey(id: BsonBinary): SingleObservable[DeleteResult] = wrapped.deleteKey(id)

/**
* Removes a keyAltName from the keyAltNames array of the key document in the key vault collection with the given id.
*
* @param id the data key UUID (BSON binary subtype 0x04)
* @param keyAltName the alternative key name
* @return an Observable containing the previous version of the key document or an empty publisher if there is no match
* @since 5.6
*/
def removeKeyAltName(id: BsonBinary, keyAltName: String): SingleObservable[BsonDocument] =
wrapped.removeKeyAltName(id, keyAltName)

/**
* Decrypts multiple data keys and (re-)encrypts them with the current masterKey.
*
* @param filter the filter
* @return an Observable containing the result
* @since 5.6
*/
def rewrapManyDataKey(filter: Bson): SingleObservable[RewrapManyDataKeyResult] = wrapped.rewrapManyDataKey(filter)

/**
* Decrypts multiple data keys and (re-)encrypts them with a new masterKey, or with their current masterKey if a new one is not given.
*
* @param filter the filter
* @param options the options
* @return an Observable containing the result
* @since 5.6
*/
def rewrapManyDataKey(filter: Bson, options: RewrapManyDataKeyOptions): SingleObservable[RewrapManyDataKeyResult] =
wrapped.rewrapManyDataKey(filter, options)

/**
* Create a new collection with encrypted fields,
* automatically creating
Expand All @@ -115,7 +199,7 @@ case class ClientEncryption(private val wrapped: JClientEncryption) extends Clos
* @param collectionName The name for the collection to create.
* @param createCollectionOptions Options for creating the collection.
* @param createEncryptedCollectionParams Auxiliary parameters for creating an encrypted collection.
* @return A publisher of the (potentially updated) `encryptedFields` configuration that was used to create the collection.
* @return An Observable of the (potentially updated) `encryptedFields` configuration that was used to create the collection.
* A user may use this document to configure `com.mongodb.AutoEncryptionSettings.getEncryptedFieldsMap`.
*
* Produces MongoUpdatedEncryptedFieldsException` if an exception happens after creating at least one data key.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class AggregateObservableSpec extends BaseSpec with MockitoSugar {

wrapped.foreach((name: String) => {
val cleanedName = name.stripPrefix("get")
assert(local.contains(name) | local.contains(cleanedName.head.toLower + cleanedName.tail), s"Missing: $name")
assert(local.contains(name) || local.contains(cleanedName.head.toLower + cleanedName.tail), s"Missing: $name")
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ChangeStreamObservableSpec extends BaseSpec with MockitoSugar {

wrapped.foreach((name: String) => {
val cleanedName = name.stripPrefix("get")
assert(local.contains(name) | local.contains(cleanedName.head.toLower + cleanedName.tail), s"Missing: $name")
assert(local.contains(name) || local.contains(cleanedName.head.toLower + cleanedName.tail), s"Missing: $name")
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class DistinctObservableSpec extends BaseSpec with MockitoSugar {

wrapped.foreach((name: String) => {
val cleanedName = name.stripPrefix("get")
assert(local.contains(name) | local.contains(cleanedName.head.toLower + cleanedName.tail), s"Missing: $name")
assert(local.contains(name) || local.contains(cleanedName.head.toLower + cleanedName.tail), s"Missing: $name")
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class FindObservableSpec extends BaseSpec with MockitoSugar {

wrapped.foreach((name: String) => {
val cleanedName = name.stripPrefix("get")
assert(local.contains(name) | local.contains(cleanedName.head.toLower + cleanedName.tail), s"Missing: $name")
assert(local.contains(name) || local.contains(cleanedName.head.toLower + cleanedName.tail), s"Missing: $name")
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ListCollectionNamesObservableSpec extends BaseSpec with MockitoSugar {

wrapped.foreach((name: String) => {
val cleanedName = name.stripPrefix("get")
assert(local.contains(name) | local.contains(cleanedName.head.toLower + cleanedName.tail), s"Missing: $name")
assert(local.contains(name) || local.contains(cleanedName.head.toLower + cleanedName.tail), s"Missing: $name")
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ListCollectionsObservableSpec extends BaseSpec with MockitoSugar {

wrapped.foreach((name: String) => {
val cleanedName = name.stripPrefix("get")
assert(local.contains(name) | local.contains(cleanedName.head.toLower + cleanedName.tail), s"Missing: $name")
assert(local.contains(name) || local.contains(cleanedName.head.toLower + cleanedName.tail), s"Missing: $name")
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ListDatabasesObservableSpec extends BaseSpec with MockitoSugar {

wrapped.foreach((name: String) => {
val cleanedName = name.stripPrefix("get")
assert(local.contains(name) | local.contains(cleanedName.head.toLower + cleanedName.tail), s"Missing: $name")
assert(local.contains(name) || local.contains(cleanedName.head.toLower + cleanedName.tail), s"Missing: $name")
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ListIndexesObservableSpec extends BaseSpec with MockitoSugar {

wrapped.foreach((name: String) => {
val cleanedName = name.stripPrefix("get")
assert(local.contains(name) | local.contains(cleanedName.head.toLower + cleanedName.tail), s"Missing: $name")
assert(local.contains(name) || local.contains(cleanedName.head.toLower + cleanedName.tail), s"Missing: $name")
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class MapReduceObservableSpec extends BaseSpec with MockitoSugar {

wrapped.foreach((name: String) => {
val cleanedName = name.stripPrefix("get")
assert(local.contains(name) | local.contains(cleanedName.head.toLower + cleanedName.tail), s"Missing: $name")
assert(local.contains(name) || local.contains(cleanedName.head.toLower + cleanedName.tail), s"Missing: $name")
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class MongoClientSpec extends BaseSpec with MockitoSugar {

wrapped.foreach((name: String) => {
val cleanedName = name.stripPrefix("get")
assert(local.contains(name) | local.contains(cleanedName.head.toLower + cleanedName.tail), s"Missing: $name")
assert(local.contains(name) || local.contains(cleanedName.head.toLower + cleanedName.tail), s"Missing: $name")
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class MongoCollectionSpec extends BaseSpec with MockitoSugar {

wrapped.foreach((name: String) => {
val cleanedName = name.stripPrefix("get")
assert(local.contains(name) | local.contains(cleanedName.head.toLower + cleanedName.tail), s"Missing: $name")
assert(local.contains(name) || local.contains(cleanedName.head.toLower + cleanedName.tail), s"Missing: $name")
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class MongoCredentialSpec extends BaseSpec {

wrapped.foreach((name: String) => {
val cleanedName = name.stripPrefix("get")
assert(local.contains(name) | local.contains(cleanedName.head.toLower + cleanedName.tail), s"Missing: $name")
assert(local.contains(name) || local.contains(cleanedName.head.toLower + cleanedName.tail), s"Missing: $name")
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class MongoDatabaseSpec extends BaseSpec with MockitoSugar {

wrapped.foreach((name: String) => {
val cleanedName = name.stripPrefix("get")
assert(local.contains(name) | local.contains(cleanedName.head.toLower + cleanedName.tail), s"Missing: $name")
assert(local.contains(name) || local.contains(cleanedName.head.toLower + cleanedName.tail), s"Missing: $name")
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class GridFSBucketSpec extends BaseSpec with MockitoSugar {

wrapped.foreach((name: String) => {
val cleanedName = name.stripPrefix("get").replace("Publisher", "Observable")
assert(local.contains(name) | local.contains(cleanedName.head.toLower + cleanedName.tail), s"Missing: $name")
assert(local.contains(name) || local.contains(cleanedName.head.toLower + cleanedName.tail), s"Missing: $name")
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class GridFSDownloadObservableSpec extends BaseSpec with MockitoSugar {

wrapped.foreach((name: String) => {
val cleanedName = name.stripPrefix("get")
assert(local.contains(name) | local.contains(cleanedName.head.toLower + cleanedName.tail), s"Missing: $name")
assert(local.contains(name) || local.contains(cleanedName.head.toLower + cleanedName.tail), s"Missing: $name")
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class GridFSFindObservableSpec extends BaseSpec with MockitoSugar {

wrapped.foreach((name: String) => {
val cleanedName = name.stripPrefix("get")
assert(local.contains(name) | local.contains(cleanedName.head.toLower + cleanedName.tail), s"Missing: $name")
assert(local.contains(name) || local.contains(cleanedName.head.toLower + cleanedName.tail), s"Missing: $name")
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class GridFSUploadPublisherSpec extends BaseSpec with MockitoSugar {

wrapped.foreach((name: String) => {
val cleanedName = name.stripPrefix("get")
assert(local.contains(name) | local.contains(cleanedName.head.toLower + cleanedName.tail), s"Missing: $name")
assert(local.contains(name) || local.contains(cleanedName.head.toLower + cleanedName.tail), s"Missing: $name")
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class AggregatesSpec extends BaseSpec {

"Aggregates" should "have the same methods as the wrapped Aggregates" in {
val wrapped = classOf[com.mongodb.client.model.Aggregates].getDeclaredMethods
.filter(f => isStatic(f.getModifiers) && isPublic(f.getModifiers))
.filter(f => isPublic(f.getModifiers))
.map(_.getName)
.toSet
val aliases = Set("filter")
Expand All @@ -66,7 +66,7 @@ class AggregatesSpec extends BaseSpec {

it should "have the same methods as the wrapped Accumulators" in {
val wrapped = classOf[com.mongodb.client.model.Accumulators].getDeclaredMethods
.filter(f => isStatic(f.getModifiers) && isPublic(f.getModifiers))
.filter(f => isPublic(f.getModifiers))
.map(_.getName)
.toSet
val local = Accumulators.getClass.getDeclaredMethods.filter(f => isPublic(f.getModifiers)).map(_.getName).toSet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class FiltersSpec extends BaseSpec {

"Filters" should "have the same methods as the wrapped Filters" in {
val wrapped = classOf[com.mongodb.client.model.Filters].getDeclaredMethods
.filter(f => isStatic(f.getModifiers) && isPublic(f.getModifiers))
.filter(f => isPublic(f.getModifiers))
.map(_.getName)
.toSet
val aliases = Set("equal", "notEqual", "bsonType")
Expand Down
Loading