2323import org .apache .iotdb .commons .auth .entity .PrivilegeType ;
2424import org .apache .iotdb .commons .auth .entity .PrivilegeUnion ;
2525import org .apache .iotdb .commons .exception .MetadataException ;
26+ import org .apache .iotdb .commons .exception .auth .AccessDeniedException ;
27+ import org .apache .iotdb .commons .path .PartialPath ;
28+ import org .apache .iotdb .commons .path .PathPatternTree ;
29+ import org .apache .iotdb .commons .schema .template .Template ;
2630import org .apache .iotdb .commons .utils .StatusUtils ;
2731import org .apache .iotdb .confignode .consensus .request .ConfigPhysicalPlanType ;
2832import org .apache .iotdb .confignode .consensus .request .write .auth .AuthorTreePlan ;
2933import org .apache .iotdb .confignode .consensus .request .write .database .DatabaseSchemaPlan ;
34+ import org .apache .iotdb .confignode .consensus .request .write .database .SetTTLPlan ;
35+ import org .apache .iotdb .confignode .consensus .request .write .pipe .payload .PipeDeactivateTemplatePlan ;
36+ import org .apache .iotdb .confignode .consensus .request .write .pipe .payload .PipeDeleteLogicalViewPlan ;
37+ import org .apache .iotdb .confignode .consensus .request .write .pipe .payload .PipeDeleteTimeSeriesPlan ;
3038import org .apache .iotdb .confignode .manager .ConfigManager ;
3139import org .apache .iotdb .confignode .manager .PermissionManager ;
3240import org .apache .iotdb .confignode .rpc .thrift .TDatabaseSchema ;
4250import org .junit .Test ;
4351
4452import java .io .IOException ;
53+ import java .nio .ByteBuffer ;
54+ import java .util .Collections ;
55+ import java .util .HashMap ;
56+ import java .util .List ;
4557import java .util .function .BiFunction ;
4658
4759public class PipeConfigTreePrivilegeParseVisitorTest {
@@ -140,6 +152,73 @@ public void testAuthPrivilege() {
140152 .isPresent ());
141153 }
142154
155+ @ Test
156+ public void testPatternRelatedPrivilege () throws IOException {
157+ final PartialPath matchedPath =
158+ new PartialPath (new String [] {"root" , "db" , "device" , "measurement" });
159+ final PartialPath unmatchedPath =
160+ new PartialPath (new String [] {"root" , "db" , "device2" , "measurement" });
161+
162+ final PathPatternTree originalTree = new PathPatternTree ();
163+ originalTree .appendPathPattern (matchedPath );
164+ originalTree .appendPathPattern (unmatchedPath );
165+ originalTree .constructTree ();
166+ final ByteBuffer buffer = originalTree .serialize ();
167+
168+ Assert .assertEquals (
169+ Collections .singletonList (matchedPath ),
170+ PathPatternTree .deserialize (
171+ ((PipeDeleteTimeSeriesPlan )
172+ skipVisitor
173+ .visitPipeDeleteTimeSeries (new PipeDeleteTimeSeriesPlan (buffer ), null )
174+ .get ())
175+ .getPatternTreeBytes ())
176+ .getAllPathPatterns ());
177+ Assert .assertEquals (
178+ Collections .singletonList (matchedPath ),
179+ PathPatternTree .deserialize (
180+ ((PipeDeleteLogicalViewPlan )
181+ skipVisitor
182+ .visitPipeDeleteLogicalView (new PipeDeleteLogicalViewPlan (buffer ), null )
183+ .get ())
184+ .getPatternTreeBytes ())
185+ .getAllPathPatterns ());
186+ Assert .assertThrows (
187+ AccessDeniedException .class ,
188+ () -> throwVisitor .visitPipeDeleteTimeSeries (new PipeDeleteTimeSeriesPlan (buffer ), null ));
189+ Assert .assertThrows (
190+ AccessDeniedException .class ,
191+ () -> throwVisitor .visitPipeDeleteLogicalView (new PipeDeleteLogicalViewPlan (buffer ), null ));
192+
193+ Assert .assertEquals (
194+ Collections .singleton (matchedPath ),
195+ ((PipeDeactivateTemplatePlan )
196+ skipVisitor
197+ .visitPipeDeactivateTemplate (
198+ new PipeDeactivateTemplatePlan (
199+ new HashMap <PartialPath , List <Template >>() {
200+ {
201+ put (matchedPath , Collections .singletonList (new Template ()));
202+ put (unmatchedPath , Collections .singletonList (new Template ()));
203+ }
204+ }),
205+ null )
206+ .get ())
207+ .getTemplateSetInfo ()
208+ .keySet ());
209+
210+ Assert .assertTrue (
211+ skipVisitor
212+ .visitTTL (
213+ new SetTTLPlan (new String [] {"root" , "db" , "device" , "measurement" }, 100 ), null )
214+ .isPresent ());
215+ Assert .assertFalse (
216+ skipVisitor
217+ .visitTTL (
218+ new SetTTLPlan (new String [] {"root" , "db2" , "device" , "measurement" }, 100 ), null )
219+ .isPresent ());
220+ }
221+
143222 private static class TestPermissionManager extends PermissionManager {
144223
145224 private BiFunction <String , PrivilegeUnion , Boolean > checkUserPrivileges =
@@ -157,8 +236,18 @@ public TPermissionInfoResp checkUserPrivileges(
157236 : new TPermissionInfoResp (new TSStatus (TSStatusCode .NO_PERMISSION .getStatusCode ()));
158237 }
159238
160- void setUserPrivilege (final BiFunction <String , PrivilegeUnion , Boolean > checkUserPrivileges ) {
239+ private void setUserPrivilege (
240+ final BiFunction <String , PrivilegeUnion , Boolean > checkUserPrivileges ) {
161241 this .checkUserPrivileges = checkUserPrivileges ;
162242 }
243+
244+ @ Override
245+ public PathPatternTree fetchRawAuthorizedPTree (
246+ final String userName , final PrivilegeType type ) {
247+ final PathPatternTree tree = new PathPatternTree ();
248+ tree .appendPathPattern (new PartialPath (new String [] {"root" , "db" , "device" , "**" }));
249+ tree .constructTree ();
250+ return tree ;
251+ }
163252 }
164253}
0 commit comments