1818
1919package  org .apache .cassandra .auth ;
2020
21- import  java .util .List ;
22- 
2321import  org .slf4j .Logger ;
2422import  org .slf4j .LoggerFactory ;
2523
2624import  org .apache .cassandra .config .Config ;
2725import  org .apache .cassandra .config .DatabaseDescriptor ;
28- import  org .apache .cassandra .config .ParameterizedClass ;
2926import  org .apache .cassandra .exceptions .ConfigurationException ;
27+ import  org .apache .cassandra .utils .FBUtilities ;
3028
3129/** 
3230 * Only purpose is to Initialize authentication/authorization via {@link #applyAuth()}. 
@@ -48,10 +46,11 @@ public static void applyAuth()
4846
4947        Config  conf  = DatabaseDescriptor .getRawConfig ();
5048
49+         IAuthenticator  authenticator  = new  AllowAllAuthenticator ();
5150
52-         /* Authentication, authorization and role management backend, implementing IAuthenticator, I*Authorizer  & IRoleManager  */ 
53- 
54-         IAuthenticator   authenticator  = authInstantiate (conf .authenticator ,  AllowAllAuthenticator . class );
51+         /* Authentication, authorization and role management backend, implementing IAuthenticator, IAuthorizer  & IRoleMapper */ 
52+          if  ( conf . authenticator  !=  null ) 
53+              authenticator  = FBUtilities . newAuthenticator (conf .authenticator );
5554
5655        // the configuration options regarding credentials caching are only guaranteed to 
5756        // work with PasswordAuthenticator, so log a message if some other authenticator 
@@ -70,39 +69,40 @@ public static void applyAuth()
7069
7170        // authorizer 
7271
73-         IAuthorizer  authorizer  = authInstantiate (conf .authorizer , AllowAllAuthorizer .class );
72+         IAuthorizer  authorizer  = new  AllowAllAuthorizer ();
73+ 
74+         if  (conf .authorizer  != null )
75+             authorizer  = FBUtilities .newAuthorizer (conf .authorizer );
7476
7577        if  (!authenticator .requireAuthentication () && authorizer .requireAuthorization ())
76-         {
77-             throw  new  ConfigurationException (authorizer .getClass ().getName () + " has authorization enabled which requires "  +
78-                                              authenticator .getClass ().getName () + " to enable authentication" , false );
79-         }
78+             throw  new  ConfigurationException (conf .authenticator  + " can't be used with "  + conf .authorizer , false );
8079
8180        DatabaseDescriptor .setAuthorizer (authorizer );
8281
8382        // role manager 
8483
85-         IRoleManager  roleManager  = authInstantiate (conf .role_manager , CassandraRoleManager .class );
84+         IRoleManager  roleManager ;
85+         if  (conf .role_manager  != null )
86+             roleManager  = FBUtilities .newRoleManager (conf .role_manager );
87+         else 
88+             roleManager  = new  CassandraRoleManager ();
8689
8790        if  (authenticator  instanceof  PasswordAuthenticator  && !(roleManager  instanceof  CassandraRoleManager ))
88-             throw  new  ConfigurationException (authenticator . getClass (). getName () +  " requires CassandraRoleManager "false );
91+             throw  new  ConfigurationException ("CassandraRoleManager must be used with PasswordAuthenticator "false );
8992
9093        DatabaseDescriptor .setRoleManager (roleManager );
9194
9295        // authenticator 
9396
94-         IInternodeAuthenticator  internodeAuthenticator  = authInstantiate (conf .internode_authenticator ,
95-                                                                          AllowAllInternodeAuthenticator .class );
96-         DatabaseDescriptor .setInternodeAuthenticator (internodeAuthenticator );
97+         if  (conf .internode_authenticator  != null )
98+             DatabaseDescriptor .setInternodeAuthenticator (FBUtilities .construct (conf .internode_authenticator , "internode_authenticator" ));
9799
98100        // network authorizer 
99- 
100-         INetworkAuthorizer  networkAuthorizer  = authInstantiate (conf .network_authorizer , AllowAllNetworkAuthorizer .class );
101+         INetworkAuthorizer  networkAuthorizer  = FBUtilities .newNetworkAuthorizer (conf .network_authorizer );
101102        DatabaseDescriptor .setNetworkAuthorizer (networkAuthorizer );
102- 
103103        if  (networkAuthorizer .requireAuthorization () && !authenticator .requireAuthentication ())
104104        {
105-             throw  new  ConfigurationException (conf .network_authorizer  + " can't be used with "  + conf .authenticator . class_name , false );
105+             throw  new  ConfigurationException (conf .network_authorizer  + " can't be used with "  + conf .authenticator , false );
106106        }
107107
108108        // Validate at last to have authenticator, authorizer, role-manager and internode-auth setup 
@@ -114,21 +114,4 @@ public static void applyAuth()
114114        networkAuthorizer .validateConfiguration ();
115115        DatabaseDescriptor .getInternodeAuthenticator ().validateConfiguration ();
116116    }
117- 
118-     private  static  <T > T  authInstantiate (ParameterizedClass  authCls , Class <T > defaultCls ) {
119-         if  (authCls  != null  && authCls .class_name  != null )
120-         {
121-             String  authPackage  = AuthConfig .class .getPackage ().getName ();
122-             return  ParameterizedClass .newInstance (authCls , List .of ("" , authPackage ));
123-         }
124- 
125-         try 
126-         {
127-             return  defaultCls .newInstance ();
128-         }
129-         catch  (InstantiationException  | IllegalAccessException   e )
130-         {
131-             throw  new  ConfigurationException ("Failed to instantiate "  + defaultCls .getName (), e );
132-         }
133-     }
134117}
0 commit comments