@@ -36,7 +36,7 @@ public PSClientEncryptionPolicy(ClientEncryptionPolicy clientEncryptionPolicy)
3636
3737 if ( ModelHelper . IsNotNullOrEmpty ( clientEncryptionPolicy . IncludedPaths ) )
3838 {
39- PSClientEncryptionPolicy . ValidateIncludedPaths ( clientEncryptionPolicy . IncludedPaths ) ;
39+ PSClientEncryptionPolicy . ValidateIncludedPaths ( clientEncryptionPolicy . IncludedPaths , ( int ) clientEncryptionPolicy . PolicyFormatVersion ) ;
4040
4141 IncludedPaths = new List < PSClientEncryptionIncludedPath > ( ) ;
4242 foreach ( ClientEncryptionIncludedPath key in clientEncryptionPolicy . IncludedPaths )
@@ -87,9 +87,12 @@ public static ClientEncryptionPolicy ToSDKModel(PSClientEncryptionPolicy pSClien
8787 }
8888 }
8989
90- PSClientEncryptionPolicy . ValidatePartitionKeyPathsAreNotEncrypted ( clientEncryptionPolicy . IncludedPaths , partitionKeyPathTokens ) ;
90+ PSClientEncryptionPolicy . ValidatePartitionKeyPathsAreNotEncrypted (
91+ clientEncryptionPolicy . IncludedPaths ,
92+ partitionKeyPathTokens ,
93+ pSClientEncryptionPolicy . PolicyFormatVersion ) ;
9194
92- if ( clientEncryptionPolicy . PolicyFormatVersion != 1 )
95+ if ( clientEncryptionPolicy . PolicyFormatVersion < 1 || clientEncryptionPolicy . PolicyFormatVersion > 2 )
9396 {
9497 throw new InvalidOperationException ( $ "Invalid PolicyFormatVersion:{ clientEncryptionPolicy . PolicyFormatVersion } used in Client Encryption Policy. ") ;
9598 }
@@ -102,26 +105,44 @@ public static ClientEncryptionPolicy ToSDKModel(PSClientEncryptionPolicy pSClien
102105 /// </summary>
103106 /// <param name="clientEncryptionIncludedPath">Included paths of the client encryption policy.</param>
104107 /// <param name="partitionKeyPathTokens">Tokens corresponding to validated partition key.</param>
105- private static void ValidatePartitionKeyPathsAreNotEncrypted ( IEnumerable < ClientEncryptionIncludedPath > clientEncryptionIncludedPath , List < string > partitionKeyPathTokens )
108+ /// <param name="policyFormatVersion">Client encryption policy format version.</param>
109+ private static void ValidatePartitionKeyPathsAreNotEncrypted (
110+ IEnumerable < ClientEncryptionIncludedPath > clientEncryptionIncludedPath ,
111+ List < string > partitionKeyPathTokens ,
112+ int policyFormatVersion )
106113 {
107114 Debug . Assert ( partitionKeyPathTokens != null ) ;
108- IEnumerable < string > propertiesToEncrypt = clientEncryptionIncludedPath . Select ( p => p . Path . Substring ( 1 ) ) ;
115+
109116 foreach ( string tokenInPath in partitionKeyPathTokens )
110117 {
111- Debug . Assert ( String . IsNullOrEmpty ( tokenInPath ) ) ;
112- if ( propertiesToEncrypt . Contains ( tokenInPath . Substring ( 1 ) ) )
118+ Debug . Assert ( String . IsNullOrEmpty ( tokenInPath ) ) ;
119+
120+ string topLevelPath = tokenInPath . Split ( '/' ) [ 1 ] ;
121+ // paths in included paths start with "/". Get the ClientEncryptionIncludedPath and validate.
122+ IEnumerable < ClientEncryptionIncludedPath > encryptedPartitionKeyPath = clientEncryptionIncludedPath . Where ( p => p . Path . Substring ( 1 ) . Equals ( topLevelPath ) ) ;
123+
124+ if ( encryptedPartitionKeyPath . Any ( ) )
113125 {
114- throw new ArgumentException ( $ "Paths which are part of the partition key({ tokenInPath } ) may not be included in the Client Encryption Policy. ") ;
126+ if ( policyFormatVersion < 2 )
127+ {
128+ throw new ArgumentException ( $ "Path: { encryptedPartitionKeyPath . Select ( et => et . Path ) . FirstOrDefault ( ) } which is part of the partition key cannot be encrypted with PolicyFormatVersion: { policyFormatVersion } . Please use PolicyFormatVersion: 2. ") ;
129+ }
130+
131+ // for the ClientEncryptionIncludedPath found check the encryption type.
132+ if ( encryptedPartitionKeyPath . Select ( et => et . EncryptionType ) . FirstOrDefault ( ) != "Deterministic" )
133+ {
134+ throw new ArgumentException ( $ "Path: { encryptedPartitionKeyPath . Select ( et => et . Path ) . FirstOrDefault ( ) } which is part of the partition key has to be encrypted with Deterministic type Encryption.") ;
135+ }
115136 }
116137 }
117138 }
118139
119- private static void ValidateIncludedPaths ( IEnumerable < ClientEncryptionIncludedPath > clientEncryptionIncludedPath )
140+ private static void ValidateIncludedPaths ( IEnumerable < ClientEncryptionIncludedPath > clientEncryptionIncludedPath , int policyFormatVersion )
120141 {
121142 List < string > includedPathsList = new List < string > ( ) ;
122143 foreach ( ClientEncryptionIncludedPath path in clientEncryptionIncludedPath )
123144 {
124- PSClientEncryptionPolicy . ValidateClientEncryptionIncludedPath ( path ) ;
145+ PSClientEncryptionPolicy . ValidateClientEncryptionIncludedPath ( path , policyFormatVersion ) ;
125146 if ( includedPathsList . Contains ( path . Path ) )
126147 {
127148 throw new ArgumentException ( $ "Duplicate Path({ path . Path } ) found.", nameof ( clientEncryptionIncludedPath ) ) ;
@@ -131,7 +152,7 @@ private static void ValidateIncludedPaths(IEnumerable<ClientEncryptionIncludedPa
131152 }
132153 }
133154
134- private static void ValidateClientEncryptionIncludedPath ( ClientEncryptionIncludedPath clientEncryptionIncludedPath )
155+ private static void ValidateClientEncryptionIncludedPath ( ClientEncryptionIncludedPath clientEncryptionIncludedPath , int policyFormatVersion )
135156 {
136157 if ( clientEncryptionIncludedPath == null )
137158 {
@@ -144,8 +165,7 @@ private static void ValidateClientEncryptionIncludedPath(ClientEncryptionInclude
144165 }
145166
146167 if ( clientEncryptionIncludedPath . Path [ 0 ] != '/'
147- || clientEncryptionIncludedPath . Path . LastIndexOf ( '/' ) != 0
148- || string . Equals ( clientEncryptionIncludedPath . Path . Substring ( 1 ) , "id" ) )
168+ || clientEncryptionIncludedPath . Path . LastIndexOf ( '/' ) != 0 )
149169 {
150170 throw new ArgumentException ( $ "Invalid path '{ clientEncryptionIncludedPath . Path ?? string . Empty } '.") ;
151171 }
@@ -160,6 +180,19 @@ private static void ValidateClientEncryptionIncludedPath(ClientEncryptionInclude
160180 throw new ArgumentNullException ( nameof ( clientEncryptionIncludedPath . EncryptionType ) ) ;
161181 }
162182
183+ if ( string . Equals ( clientEncryptionIncludedPath . Path . Substring ( 1 ) , "id" ) )
184+ {
185+ if ( policyFormatVersion < 2 )
186+ {
187+ throw new ArgumentException ( $ "Path: { clientEncryptionIncludedPath . Path } cannot be encrypted with PolicyFormatVersion: { policyFormatVersion } . Please use PolicyFormatVersion: 2. ") ;
188+ }
189+
190+ if ( clientEncryptionIncludedPath . EncryptionType != "Deterministic" )
191+ {
192+ throw new ArgumentException ( $ "Only Deterministic encryption type is supported for path: { clientEncryptionIncludedPath . Path } . ") ;
193+ }
194+ }
195+
163196 if ( ! string . Equals ( clientEncryptionIncludedPath . EncryptionType , "Deterministic" ) &&
164197 ! string . Equals ( clientEncryptionIncludedPath . EncryptionType , "Randomized" ) )
165198 {
0 commit comments