2020
2121import java .io .IOException ;
2222
23+ import org .assertj .core .api .Assertions ;
24+ import org .junit .Test ;
25+
2326import org .apache .hadoop .conf .Configuration ;
2427import org .apache .hadoop .fs .azurebfs .contracts .exceptions .InvalidConfigurationValueException ;
28+ import org .apache .hadoop .fs .azurebfs .oauth2 .ClientCredsTokenProvider ;
29+ import org .apache .hadoop .fs .azurebfs .oauth2 .CustomTokenProviderAdapter ;
30+ import org .apache .hadoop .fs .azurebfs .services .AuthType ;
2531
2632import static org .junit .Assert .assertEquals ;
2733import static org .junit .Assert .assertNull ;
2834
29- import org .junit .Test ;
35+ import static org .apache .hadoop .fs .azurebfs .constants .ConfigurationKeys .FS_AZURE_ACCOUNT_AUTH_TYPE_PROPERTY_NAME ;
36+ import static org .apache .hadoop .fs .azurebfs .constants .ConfigurationKeys .FS_AZURE_ACCOUNT_OAUTH_CLIENT_ENDPOINT ;
37+ import static org .apache .hadoop .fs .azurebfs .constants .ConfigurationKeys .FS_AZURE_ACCOUNT_OAUTH_CLIENT_ID ;
38+ import static org .apache .hadoop .fs .azurebfs .constants .ConfigurationKeys .FS_AZURE_ACCOUNT_OAUTH_CLIENT_SECRET ;
39+ import static org .apache .hadoop .fs .azurebfs .constants .ConfigurationKeys .FS_AZURE_ACCOUNT_TOKEN_PROVIDER_TYPE_PROPERTY_NAME ;
40+ import static org .apache .hadoop .fs .azurebfs .constants .ConfigurationKeys .FS_AZURE_SAS_TOKEN_PROVIDER_TYPE ;
3041
3142/**
3243 * Tests correct precedence of various configurations that might be returned.
4051 * that do allow default values (all others) follow another form.
4152 */
4253public class TestAccountConfiguration {
54+ private static final String TEST_OAUTH_PROVIDER_CLASS_CONFIG = "org.apache.hadoop.fs.azurebfs.oauth2.ClientCredsTokenProvider" ;
55+ private static final String TEST_CUSTOM_PROVIDER_CLASS_CONFIG = "org.apache.hadoop.fs.azurebfs.oauth2.RetryTestTokenProvider" ;
56+ private static final String TEST_SAS_PROVIDER_CLASS_CONFIG_1 = "org.apache.hadoop.fs.azurebfs.extensions.MockErrorSASTokenProvider" ;
57+ private static final String TEST_SAS_PROVIDER_CLASS_CONFIG_2 = "org.apache.hadoop.fs.azurebfs.extensions.MockSASTokenProvider" ;
58+
59+ private static final String TEST_OAUTH_ENDPOINT = "oauthEndpoint" ;
60+ private static final String TEST_CLIENT_ID = "clientId" ;
61+ private static final String TEST_CLIENT_SECRET = "clientSecret" ;
4362
4463 @ Test
4564 public void testStringPrecedence ()
@@ -248,7 +267,7 @@ private class GetClassImpl1 implements GetClassInterface {
248267 }
249268
250269 @ Test
251- public void testClassPrecedence ()
270+ public void testClass ()
252271 throws IllegalAccessException , IOException , InvalidConfigurationValueException {
253272
254273 final String accountName = "account" ;
@@ -264,22 +283,182 @@ public void testClassPrecedence()
264283
265284 conf .setClass (globalKey , class0 , xface );
266285 assertEquals ("Default value returned even though account-agnostic config was set" ,
267- abfsConf .getClass (globalKey , class1 , xface ), class0 );
286+ abfsConf .getAccountAgnosticClass (globalKey , class1 , xface ), class0 );
268287 conf .unset (globalKey );
269288 assertEquals ("Default value not returned even though config was unset" ,
270- abfsConf .getClass (globalKey , class1 , xface ), class1 );
289+ abfsConf .getAccountAgnosticClass (globalKey , class1 , xface ), class1 );
271290
272291 conf .setClass (accountKey , class0 , xface );
273292 assertEquals ("Default value returned even though account-specific config was set" ,
274- abfsConf .getClass (globalKey , class1 , xface ), class0 );
293+ abfsConf .getAccountSpecificClass (globalKey , class1 , xface ), class0 );
275294 conf .unset (accountKey );
276295 assertEquals ("Default value not returned even though config was unset" ,
277- abfsConf .getClass (globalKey , class1 , xface ), class1 );
296+ abfsConf .getAccountSpecificClass (globalKey , class1 , xface ), class1 );
278297
279298 conf .setClass (accountKey , class1 , xface );
280299 conf .setClass (globalKey , class0 , xface );
281300 assertEquals ("Account-agnostic or default value returned even though account-specific config was set" ,
282- abfsConf .getClass (globalKey , class0 , xface ), class1 );
301+ abfsConf .getAccountSpecificClass (globalKey , class0 , xface ), class1 );
302+ }
303+
304+ @ Test
305+ public void testSASProviderPrecedence ()
306+ throws IOException , IllegalAccessException {
307+ final String accountName = "account" ;
308+
309+ final Configuration conf = new Configuration ();
310+ final AbfsConfiguration abfsConf = new AbfsConfiguration (conf , accountName );
311+
312+ // AccountSpecific: SAS with provider set as SAS_Provider_1
313+ abfsConf .set (FS_AZURE_ACCOUNT_AUTH_TYPE_PROPERTY_NAME + "." + accountName ,
314+ "SAS" );
315+ abfsConf .set (FS_AZURE_SAS_TOKEN_PROVIDER_TYPE + "." + accountName ,
316+ TEST_SAS_PROVIDER_CLASS_CONFIG_1 );
317+
318+ // Global: SAS with provider set as SAS_Provider_2
319+ abfsConf .set (FS_AZURE_ACCOUNT_AUTH_TYPE_PROPERTY_NAME ,
320+ AuthType .SAS .toString ());
321+ abfsConf .set (FS_AZURE_SAS_TOKEN_PROVIDER_TYPE ,
322+ TEST_SAS_PROVIDER_CLASS_CONFIG_2 );
323+
324+ Assertions .assertThat (
325+ abfsConf .getSASTokenProvider ().getClass ().getName ())
326+ .describedAs (
327+ "Account-specific SAS token provider should be in effect." )
328+ .isEqualTo (TEST_SAS_PROVIDER_CLASS_CONFIG_1 );
329+ }
330+
331+ @ Test
332+ public void testAccessTokenProviderPrecedence ()
333+ throws IllegalAccessException , IOException {
334+ final String accountName = "account" ;
335+
336+ final Configuration conf = new Configuration ();
337+ final AbfsConfiguration abfsConf = new AbfsConfiguration (conf , accountName );
338+
339+ // Global: Custom , AccountSpecific: OAuth
340+ testGlobalAndAccountOAuthPrecedence (abfsConf , AuthType .Custom ,
341+ AuthType .OAuth );
342+
343+ // Global: OAuth , AccountSpecific: Custom
344+ testGlobalAndAccountOAuthPrecedence (abfsConf , AuthType .OAuth ,
345+ AuthType .Custom );
346+
347+ // Global: (non-oAuth) SAS , AccountSpecific: Custom
348+ testGlobalAndAccountOAuthPrecedence (abfsConf , AuthType .SAS ,
349+ AuthType .Custom );
350+
351+ // Global: Custom , AccountSpecific: -
352+ testGlobalAndAccountOAuthPrecedence (abfsConf , AuthType .Custom , null );
353+
354+ // Global: OAuth , AccountSpecific: -
355+ testGlobalAndAccountOAuthPrecedence (abfsConf , AuthType .OAuth , null );
356+
357+ // Global: - , AccountSpecific: Custom
358+ testGlobalAndAccountOAuthPrecedence (abfsConf , null , AuthType .Custom );
359+
360+ // Global: - , AccountSpecific: OAuth
361+ testGlobalAndAccountOAuthPrecedence (abfsConf , null , AuthType .OAuth );
362+ }
363+
364+ public void testGlobalAndAccountOAuthPrecedence (AbfsConfiguration abfsConf ,
365+ AuthType globalAuthType ,
366+ AuthType accountSpecificAuthType )
367+ throws IOException {
368+ if (globalAuthType == null ) {
369+ unsetAuthConfig (abfsConf , false );
370+ } else {
371+ setAuthConfig (abfsConf , false , globalAuthType );
372+ }
373+
374+ if (accountSpecificAuthType == null ) {
375+ unsetAuthConfig (abfsConf , true );
376+ } else {
377+ setAuthConfig (abfsConf , true , accountSpecificAuthType );
378+ }
379+
380+ // If account specific AuthType is present, precedence is always for it.
381+ AuthType expectedEffectiveAuthType ;
382+ if (accountSpecificAuthType != null ) {
383+ expectedEffectiveAuthType = accountSpecificAuthType ;
384+ } else {
385+ expectedEffectiveAuthType = globalAuthType ;
386+ }
387+
388+ Class <?> expectedEffectiveTokenProviderClassType =
389+ (expectedEffectiveAuthType == AuthType .OAuth )
390+ ? ClientCredsTokenProvider .class
391+ : CustomTokenProviderAdapter .class ;
392+
393+ Assertions .assertThat (
394+ abfsConf .getTokenProvider ().getClass ().getTypeName ())
395+ .describedAs (
396+ "Account-specific settings takes precendence to global"
397+ + " settings. In absence of Account settings, global settings "
398+ + "should take effect." )
399+ .isEqualTo (expectedEffectiveTokenProviderClassType .getTypeName ());
400+
401+
402+ unsetAuthConfig (abfsConf , false );
403+ unsetAuthConfig (abfsConf , true );
404+ }
405+
406+ public void setAuthConfig (AbfsConfiguration abfsConf ,
407+ boolean isAccountSetting ,
408+ AuthType authType ) {
409+ final String accountNameSuffix = "." + abfsConf .getAccountName ();
410+ String authKey = FS_AZURE_ACCOUNT_AUTH_TYPE_PROPERTY_NAME
411+ + (isAccountSetting ? accountNameSuffix : "" );
412+ String providerClassKey = "" ;
413+ String providerClassValue = "" ;
414+
415+ switch (authType ) {
416+ case OAuth :
417+ providerClassKey = FS_AZURE_ACCOUNT_TOKEN_PROVIDER_TYPE_PROPERTY_NAME
418+ + (isAccountSetting ? accountNameSuffix : "" );
419+ providerClassValue = TEST_OAUTH_PROVIDER_CLASS_CONFIG ;
420+
421+ abfsConf .set (FS_AZURE_ACCOUNT_OAUTH_CLIENT_ENDPOINT
422+ + ((isAccountSetting ) ? accountNameSuffix : "" ),
423+ TEST_OAUTH_ENDPOINT );
424+ abfsConf .set (FS_AZURE_ACCOUNT_OAUTH_CLIENT_ID
425+ + ((isAccountSetting ) ? accountNameSuffix : "" ),
426+ TEST_CLIENT_ID );
427+ abfsConf .set (FS_AZURE_ACCOUNT_OAUTH_CLIENT_SECRET
428+ + ((isAccountSetting ) ? accountNameSuffix : "" ),
429+ TEST_CLIENT_SECRET );
430+ break ;
431+
432+ case Custom :
433+ providerClassKey = FS_AZURE_ACCOUNT_TOKEN_PROVIDER_TYPE_PROPERTY_NAME
434+ + (isAccountSetting ? accountNameSuffix : "" );
435+ providerClassValue = TEST_CUSTOM_PROVIDER_CLASS_CONFIG ;
436+ break ;
437+
438+ case SAS :
439+ providerClassKey = FS_AZURE_SAS_TOKEN_PROVIDER_TYPE
440+ + (isAccountSetting ? accountNameSuffix : "" );
441+ providerClassValue = TEST_SAS_PROVIDER_CLASS_CONFIG_1 ;
442+ break ;
443+
444+ default : // set nothing
445+ }
446+
447+ abfsConf .set (authKey , authType .toString ());
448+ abfsConf .set (providerClassKey , providerClassValue );
449+ }
450+
451+ private void unsetAuthConfig (AbfsConfiguration abfsConf , boolean isAccountSettings ) {
452+ String accountNameSuffix =
453+ isAccountSettings ? ("." + abfsConf .getAccountName ()) : "" ;
454+
455+ abfsConf .unset (FS_AZURE_ACCOUNT_AUTH_TYPE_PROPERTY_NAME + accountNameSuffix );
456+ abfsConf .unset (FS_AZURE_ACCOUNT_TOKEN_PROVIDER_TYPE_PROPERTY_NAME + accountNameSuffix );
457+ abfsConf .unset (FS_AZURE_SAS_TOKEN_PROVIDER_TYPE + accountNameSuffix );
458+
459+ abfsConf .unset (FS_AZURE_ACCOUNT_OAUTH_CLIENT_ENDPOINT + accountNameSuffix );
460+ abfsConf .unset (FS_AZURE_ACCOUNT_OAUTH_CLIENT_ID + accountNameSuffix );
461+ abfsConf .unset (FS_AZURE_ACCOUNT_OAUTH_CLIENT_SECRET + accountNameSuffix );
283462 }
284463
285464}
0 commit comments