2020package org .apache .iotdb .confignode .manager .pipe .receiver .protocol ;
2121
2222import org .apache .iotdb .common .rpc .thrift .TSStatus ;
23+ import org .apache .iotdb .commons .audit .IAuditEntity ;
2324import org .apache .iotdb .commons .auth .entity .PrivilegeType ;
2425import org .apache .iotdb .commons .auth .entity .PrivilegeUnion ;
2526import org .apache .iotdb .commons .conf .CommonDescriptor ;
4647import org .apache .iotdb .commons .schema .ttl .TTLCache ;
4748import org .apache .iotdb .commons .utils .PathUtils ;
4849import org .apache .iotdb .commons .utils .StatusUtils ;
50+ import org .apache .iotdb .confignode .audit .CNAuditLogger ;
4951import org .apache .iotdb .confignode .conf .ConfigNodeDescriptor ;
5052import org .apache .iotdb .confignode .consensus .request .ConfigPhysicalPlan ;
5153import org .apache .iotdb .confignode .consensus .request .ConfigPhysicalPlanType ;
143145import java .util .Set ;
144146import java .util .concurrent .atomic .AtomicInteger ;
145147
148+ import static org .apache .iotdb .confignode .manager .pipe .source .PipeConfigTreePrivilegeParseVisitor .checkGlobalStatus ;
149+
146150public class IoTDBConfigNodeReceiver extends IoTDBFileReceiver {
147151
148152 private static final Logger LOGGER = LoggerFactory .getLogger (IoTDBConfigNodeReceiver .class );
@@ -157,6 +161,7 @@ public class IoTDBConfigNodeReceiver extends IoTDBFileReceiver {
157161 new PipeConfigPhysicalPlanExceptionVisitor ();
158162
159163 private final ConfigManager configManager = ConfigNode .getInstance ().getConfigManager ();
164+ private final CNAuditLogger auditLogger = configManager .getAuditLogger ();
160165
161166 @ Override
162167 public TPipeTransferResp receive (final TPipeTransferReq req ) {
@@ -290,43 +295,41 @@ private TSStatus checkPermission(final ConfigPhysicalPlan plan) throws IOExcepti
290295 return status ;
291296 }
292297
298+ String database ;
293299 switch (plan .getType ()) {
294300 case CreateDatabase :
295- return PathUtils .isTableModelDatabase (((DatabaseSchemaPlan ) plan ).getSchema ().getName ())
296- ? configManager
297- .checkUserPrivileges (
298- username ,
299- new PrivilegeUnion (
300- ((DatabaseSchemaPlan ) plan ).getSchema ().getName (), PrivilegeType .CREATE ))
301- .getStatus ()
302- : configManager
303- .checkUserPrivileges (username , new PrivilegeUnion (PrivilegeType .MANAGE_DATABASE ))
304- .getStatus ();
301+ database = ((DatabaseSchemaPlan ) plan ).getSchema ().getName ();
302+ if (PathUtils .isTableModelDatabase (database )) {
303+ status = checkDatabaseStatus (userEntity , PrivilegeType .CREATE , database , false );
304+ if (status .getCode () != TSStatusCode .SUCCESS_STATUS .getStatusCode ()) {
305+ return checkGlobalStatus (userEntity , PrivilegeType .SYSTEM , database , true );
306+ }
307+ }
308+ return checkGlobalStatus (userEntity , PrivilegeType .MANAGE_DATABASE , database , true );
305309 case AlterDatabase :
306- return PathUtils .isTableModelDatabase (((DatabaseSchemaPlan ) plan ).getSchema ().getName ())
307- ? configManager
308- .checkUserPrivileges (
309- username ,
310- new PrivilegeUnion (
311- ((DatabaseSchemaPlan ) plan ).getSchema ().getName (), PrivilegeType .ALTER ))
312- .getStatus ()
313- : configManager
314- .checkUserPrivileges (username , new PrivilegeUnion (PrivilegeType .MANAGE_DATABASE ))
315- .getStatus ();
310+ database = ((DatabaseSchemaPlan ) plan ).getSchema ().getName ();
311+ if (PathUtils .isTableModelDatabase (database )) {
312+ status = checkDatabaseStatus (userEntity , PrivilegeType .ALTER , database , false );
313+ if (status .getCode () != TSStatusCode .SUCCESS_STATUS .getStatusCode ()) {
314+ return checkGlobalStatus (userEntity , PrivilegeType .SYSTEM , database , true );
315+ }
316+ }
317+ return checkGlobalStatus (userEntity , PrivilegeType .MANAGE_DATABASE , database , true );
316318 case DeleteDatabase :
317- return PathUtils .isTableModelDatabase (((DeleteDatabasePlan ) plan ).getName ())
318- ? configManager
319- .checkUserPrivileges (
320- username ,
321- new PrivilegeUnion (((DeleteDatabasePlan ) plan ).getName (), PrivilegeType .DROP ))
322- .getStatus ()
323- : configManager
324- .checkUserPrivileges (username , new PrivilegeUnion (PrivilegeType .MANAGE_DATABASE ))
325- .getStatus ();
319+ database = ((DeleteDatabasePlan ) plan ).getName ();
320+ if (PathUtils .isTableModelDatabase (database )) {
321+ status = checkDatabaseStatus (userEntity , PrivilegeType .DELETE , database , false );
322+ if (status .getCode () != TSStatusCode .SUCCESS_STATUS .getStatusCode ()) {
323+ return checkGlobalStatus (userEntity , PrivilegeType .SYSTEM , database , true );
324+ }
325+ }
326+ return checkGlobalStatus (userEntity , PrivilegeType .MANAGE_DATABASE , database , true );
326327 case ExtendSchemaTemplate :
327- return configManager
328- .checkUserPrivileges (username , new PrivilegeUnion (PrivilegeType .EXTEND_TEMPLATE ))
329- .getStatus ();
328+ return checkGlobalStatus (
329+ userEntity ,
330+ PrivilegeType .EXTEND_TEMPLATE ,
331+ ((ExtendSchemaTemplatePlan ) plan ).getTemplateExtendInfo ().getTemplateName (),
332+ true );
330333 case CreateSchemaTemplate :
331334 case CommitSetSchemaTemplate :
332335 case PipeUnsetTemplate :
@@ -618,6 +621,29 @@ username, new PrivilegeUnion(PrivilegeType.values()[permission], true))
618621 }
619622 }
620623
624+ public static TSStatus checkDatabaseStatus (
625+ final IAuditEntity userEntity ,
626+ final PrivilegeType privilegeType ,
627+ final String database ,
628+ final boolean isLastCheck ) {
629+ final ConfigManager configManager = ConfigNode .getInstance ().getConfigManager ();
630+ final CNAuditLogger logger = configManager .getAuditLogger ();
631+ final TSStatus result =
632+ configManager
633+ .getPermissionManager ()
634+ .checkUserPrivileges (
635+ userEntity .getUsername (), new PrivilegeUnion (database , privilegeType ))
636+ .getStatus ();
637+ if (result .getCode () == TSStatusCode .SUCCESS_STATUS .getStatusCode () || isLastCheck ) {
638+ logger .recordAuditLog (
639+ userEntity
640+ .setPrivilegeType (privilegeType )
641+ .setResult (result .getCode () == TSStatusCode .SUCCESS_STATUS .getStatusCode ()),
642+ () -> database );
643+ }
644+ return result ;
645+ }
646+
621647 private TSStatus executePlan (final ConfigPhysicalPlan plan ) throws ConsensusException {
622648 final String queryId = generatePseudoQueryId ();
623649 switch (plan .getType ()) {
0 commit comments