Skip to content

Commit 66c7e57

Browse files
rozzavbabanin
andauthored
Added ClientEncryption methods to scala (#1779)
* Added ClientEncryption methods to scala - Added explicit public constructor for `RewrapManyDataKeyOptions` - Added missing methods to scala's `ClientEncryption` - Added type alias and builder objects for `RewrapManyDataKeyResult` - Added type alias and builder objects for `RewrapManyDataKeyOptions` - Updated logic for API tests - checking all public methods - Fixed the `ClientEncryptionSpec` to test for scala class methods as opposed to scala object methods. JAVA-5933 Co-authored-by: Viacheslav Babanin <[email protected]>
1 parent 49866cb commit 66c7e57

28 files changed

+215
-46
lines changed

driver-core/src/main/com/mongodb/client/model/vault/RewrapManyDataKeyOptions.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ public final class RewrapManyDataKeyOptions {
3333
private String provider;
3434
private BsonDocument masterKey;
3535

36+
/**
37+
* Construct a new instance
38+
*/
39+
public RewrapManyDataKeyOptions() {
40+
}
3641

3742
/**
3843
* Sets the provider name

driver-scala/src/main/scala/org/mongodb/scala/model/vault/package.scala

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@
1616

1717
package org.mongodb.scala.model
1818

19-
import com.mongodb.annotations.{ Beta, Reason }
2019
import com.mongodb.client.model.vault.{ DataKeyOptions => JDataKeyOptions }
2120
import com.mongodb.client.model.vault.{ EncryptOptions => JEncryptOptions }
2221
import com.mongodb.client.model.vault.{ RangeOptions => JRangeOptions }
22+
import com.mongodb.client.model.vault.{ RewrapManyDataKeyResult => JRewrapManyDataKeyResult }
23+
import com.mongodb.client.model.vault.{ RewrapManyDataKeyOptions => JRewrapManyDataKeyOptions }
2324

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

69+
/**
70+
* The result of the rewrapping of data keys
71+
*
72+
* @since 5.6
73+
*/
74+
type RewrapManyDataKeyResult = JRewrapManyDataKeyResult
75+
76+
/**
77+
* The result of the rewrapping of data keys
78+
*
79+
* @since 5.6
80+
*/
81+
type RewrapManyDataKeyOptions = JRewrapManyDataKeyOptions
82+
83+
/**
84+
* The rewrap many data key options
85+
*
86+
* The `getMasterKey` document MUST have the fields corresponding to the given provider as specified in masterKey.
87+
*
88+
* @since 5.6
89+
*/
90+
object RewrapManyDataKeyOptions {
91+
def apply() = new JRewrapManyDataKeyOptions()
92+
}
6893
}

driver-scala/src/main/scala/org/mongodb/scala/vault/ClientEncryption.scala

Lines changed: 92 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,15 @@ import com.mongodb.client.model.{ CreateCollectionOptions, CreateEncryptedCollec
2222
import java.io.Closeable
2323
import com.mongodb.reactivestreams.client.vault.{ ClientEncryption => JClientEncryption }
2424
import org.bson.{ BsonBinary, BsonDocument, BsonValue }
25-
import org.mongodb.scala.{ Document, MongoDatabase, SingleObservable, ToSingleObservablePublisher }
26-
import org.mongodb.scala.model.vault.{ DataKeyOptions, EncryptOptions }
25+
import org.mongodb.scala.bson.conversions.Bson
26+
import org.mongodb.scala.{ Document, FindObservable, MongoDatabase, SingleObservable, ToSingleObservablePublisher }
27+
import org.mongodb.scala.model.vault.{
28+
DataKeyOptions,
29+
EncryptOptions,
30+
RewrapManyDataKeyOptions,
31+
RewrapManyDataKeyResult
32+
}
33+
import org.mongodb.scala.result.DeleteResult
2734

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

@@ -51,7 +58,7 @@ case class ClientEncryption(private val wrapped: JClientEncryption) extends Clos
5158
*
5259
* @param kmsProvider the KMS provider
5360
* @param dataKeyOptions the options for data key creation
54-
* @return a Publisher containing the identifier for the created data key
61+
* @return an Observable containing the identifier for the created data key
5562
*/
5663
def createDataKey(kmsProvider: String, dataKeyOptions: DataKeyOptions): SingleObservable[BsonBinary] =
5764
wrapped.createDataKey(kmsProvider, dataKeyOptions)
@@ -62,7 +69,7 @@ case class ClientEncryption(private val wrapped: JClientEncryption) extends Clos
6269
*
6370
* @param value the value to encrypt
6471
* @param options the options for data encryption
65-
* @return a Publisher containing the encrypted value, a BSON binary of subtype 6
72+
* @return an Observable containing the encrypted value, a BSON binary of subtype 6
6673
*/
6774
def encrypt(value: BsonValue, options: EncryptOptions): SingleObservable[BsonBinary] =
6875
wrapped.encrypt(value, options)
@@ -86,7 +93,7 @@ case class ClientEncryption(private val wrapped: JClientEncryption) extends Clos
8693
* @note Requires MongoDB 8.0 or greater
8794
* @param expression the Match Expression or Aggregate Expression
8895
* @param options the options
89-
* @return a Publisher containing the queryable encrypted range expression
96+
* @return an Observable containing the queryable encrypted range expression
9097
* @since 4.9
9198
*/
9299
def encryptExpression(
@@ -99,10 +106,87 @@ case class ClientEncryption(private val wrapped: JClientEncryption) extends Clos
99106
* Decrypt the given value.
100107
*
101108
* @param value the value to decrypt, which must be of subtype 6
102-
* @return a Publisher containing the decrypted value
109+
* @return an Observable containing the decrypted value
103110
*/
104111
def decrypt(value: BsonBinary): SingleObservable[BsonValue] = wrapped.decrypt(value)
105112

113+
/**
114+
* Finds a single key document with the given UUID (BSON binary subtype 0x04).
115+
*
116+
* @param id the data key UUID (BSON binary subtype 0x04)
117+
* @return an Observable containing the single key document or an empty Observable if there is no match
118+
* @since 5.6
119+
*/
120+
def getKey(id: BsonBinary): SingleObservable[BsonDocument] = wrapped.getKey(id)
121+
122+
/**
123+
* Returns a key document in the key vault collection with the given keyAltName.
124+
*
125+
* @param keyAltName the alternative key name
126+
* @return an Observable containing the matching key document or an empty Observable if there is no match
127+
* @since 5.6
128+
*/
129+
def getKeyByAltName(keyAltName: String): SingleObservable[BsonDocument] = wrapped.getKeyByAltName(keyAltName)
130+
131+
/**
132+
* Finds all documents in the key vault collection.
133+
*
134+
* @return a find Observable for the documents in the key vault collection
135+
* @since 5.6
136+
*/
137+
def keys: FindObservable[BsonDocument] = FindObservable(wrapped.getKeys)
138+
139+
/**
140+
* Adds a keyAltName to the keyAltNames array of the key document in the key vault collection with the given UUID.
141+
*
142+
* @param id the data key UUID (BSON binary subtype 0x04)
143+
* @param keyAltName the alternative key name to add to the keyAltNames array
144+
* @return an Observable containing the previous version of the key document or an empty Observable if no match
145+
* @since 5.6
146+
*/
147+
def addKeyAltName(id: BsonBinary, keyAltName: String): SingleObservable[BsonDocument] =
148+
wrapped.addKeyAltName(id, keyAltName)
149+
150+
/**
151+
* Removes the key document with the given data key from the key vault collection.
152+
*
153+
* @param id the data key UUID (BSON binary subtype 0x04)
154+
* @return an Observable containing the delete result
155+
* @since 5.6
156+
*/
157+
def deleteKey(id: BsonBinary): SingleObservable[DeleteResult] = wrapped.deleteKey(id)
158+
159+
/**
160+
* Removes a keyAltName from the keyAltNames array of the key document in the key vault collection with the given id.
161+
*
162+
* @param id the data key UUID (BSON binary subtype 0x04)
163+
* @param keyAltName the alternative key name
164+
* @return an Observable containing the previous version of the key document or an empty Observable if there is no match
165+
* @since 5.6
166+
*/
167+
def removeKeyAltName(id: BsonBinary, keyAltName: String): SingleObservable[BsonDocument] =
168+
wrapped.removeKeyAltName(id, keyAltName)
169+
170+
/**
171+
* Decrypts multiple data keys and (re-)encrypts them with the current masterKey.
172+
*
173+
* @param filter the filter
174+
* @return an Observable containing the result
175+
* @since 5.6
176+
*/
177+
def rewrapManyDataKey(filter: Bson): SingleObservable[RewrapManyDataKeyResult] = wrapped.rewrapManyDataKey(filter)
178+
179+
/**
180+
* Decrypts multiple data keys and (re-)encrypts them with a new masterKey, or with their current masterKey if a new one is not given.
181+
*
182+
* @param filter the filter
183+
* @param options the options
184+
* @return an Observable containing the result
185+
* @since 5.6
186+
*/
187+
def rewrapManyDataKey(filter: Bson, options: RewrapManyDataKeyOptions): SingleObservable[RewrapManyDataKeyResult] =
188+
wrapped.rewrapManyDataKey(filter, options)
189+
106190
/**
107191
* Create a new collection with encrypted fields,
108192
* automatically creating
@@ -115,7 +199,7 @@ case class ClientEncryption(private val wrapped: JClientEncryption) extends Clos
115199
* @param collectionName The name for the collection to create.
116200
* @param createCollectionOptions Options for creating the collection.
117201
* @param createEncryptedCollectionParams Auxiliary parameters for creating an encrypted collection.
118-
* @return A publisher of the (potentially updated) `encryptedFields` configuration that was used to create the collection.
202+
* @return An Observable of the (potentially updated) `encryptedFields` configuration that was used to create the collection.
119203
* A user may use this document to configure `com.mongodb.AutoEncryptionSettings.getEncryptedFieldsMap`.
120204
*
121205
* Produces MongoUpdatedEncryptedFieldsException` if an exception happens after creating at least one data key.

driver-scala/src/test/scala/org/mongodb/scala/AggregateObservableSpec.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class AggregateObservableSpec extends BaseSpec with MockitoSugar {
3434

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

driver-scala/src/test/scala/org/mongodb/scala/ChangeStreamObservableSpec.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class ChangeStreamObservableSpec extends BaseSpec with MockitoSugar {
4040

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

driver-scala/src/test/scala/org/mongodb/scala/DistinctObservableSpec.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class DistinctObservableSpec extends BaseSpec with MockitoSugar {
3333

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

driver-scala/src/test/scala/org/mongodb/scala/FindObservableSpec.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class FindObservableSpec extends BaseSpec with MockitoSugar {
3636

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

driver-scala/src/test/scala/org/mongodb/scala/ListCollectionNamesObservableSpec.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class ListCollectionNamesObservableSpec extends BaseSpec with MockitoSugar {
3333

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

driver-scala/src/test/scala/org/mongodb/scala/ListCollectionsObservableSpec.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class ListCollectionsObservableSpec extends BaseSpec with MockitoSugar {
3434

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

driver-scala/src/test/scala/org/mongodb/scala/ListDatabasesObservableSpec.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class ListDatabasesObservableSpec extends BaseSpec with MockitoSugar {
3333

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

0 commit comments

Comments
 (0)