3232import org .apache .hadoop .fs .azurebfs .services .AuthType ;
3333import org .apache .hadoop .fs .azurebfs .services .FixedSASTokenProvider ;
3434import org .apache .hadoop .fs .azurebfs .utils .AccountSASGenerator ;
35+ import org .apache .hadoop .fs .azurebfs .utils .ServiceSASGenerator ;
3536import org .apache .hadoop .fs .azurebfs .utils .Base64 ;
3637
38+ import static org .apache .hadoop .fs .azurebfs .constants .AbfsHttpConstants .EMPTY_STRING ;
3739import static org .apache .hadoop .fs .azurebfs .constants .ConfigurationKeys .FS_AZURE_SAS_FIXED_TOKEN ;
3840import static org .apache .hadoop .fs .azurebfs .constants .ConfigurationKeys .FS_AZURE_SAS_TOKEN_PROVIDER_TYPE ;
3941import static org .apache .hadoop .fs .azurebfs .constants .ConfigurationKeys .accountProperty ;
42+ import static org .apache .hadoop .fs .azurebfs .constants .ConfigurationKeys .containerProperty ;
4043import static org .apache .hadoop .fs .azurebfs .constants .TestConfigurationKeys .FS_AZURE_TEST_APP_ID ;
4144import static org .apache .hadoop .fs .azurebfs .constants .TestConfigurationKeys .FS_AZURE_TEST_APP_SECRET ;
4245import static org .apache .hadoop .fs .azurebfs .constants .TestConfigurationKeys .FS_AZURE_TEST_APP_SERVICE_PRINCIPAL_OBJECT_ID ;
5053public class ITestAzureBlobFileSystemChooseSAS extends AbstractAbfsIntegrationTest {
5154
5255 private String accountSAS = null ;
56+ private String containerSAS = null ;
57+ private String accountAgnosticSAS = null ;
5358 private static final String TEST_PATH = "testPath" ;
5459
5560 /**
@@ -69,6 +74,8 @@ public void setup() throws Exception {
6974 super .setup ();
7075 createFilesystemWithTestFileForSASTests (new Path (TEST_PATH ));
7176 generateAccountSAS ();
77+ generateAccountAgnosticSAS ();
78+ generateContainerSAS ();
7279 }
7380
7481 /**
@@ -85,6 +92,37 @@ private void generateAccountSAS() throws AzureBlobFileSystemException {
8592 accountSAS = configAccountSASGenerator .getAccountSAS (getAccountName ());
8693 }
8794
95+ /**
96+ * Generates an Account SAS Token (for account-agnostic config) using the Account Shared Key to
97+ * be used as a fixed SAS Token.
98+ * Account SAS used here will only have write permissions to resources.
99+ * This will be used by individual tests to set in the configurations.
100+ * @throws AzureBlobFileSystemException
101+ */
102+ private void generateAccountAgnosticSAS () throws AzureBlobFileSystemException {
103+ final String accountKey = getConfiguration ().getStorageAccountKey ();
104+ AccountSASGenerator configAccountSASGenerator = new AccountSASGenerator (Base64 .decode (accountKey ));
105+ // Setting only write permissions.
106+ configAccountSASGenerator .setPermissions ("w" );
107+ accountAgnosticSAS = configAccountSASGenerator .getAccountSAS (getAccountName ());
108+ }
109+
110+ /**
111+ * Generates a Container SAS Token using the Account Shared Key to be used as a fixed SAS Token.
112+ * Container SAS used here will have only read permissions to resources.
113+ * This will be used by individual tests to set in the configurations.
114+ * @throws AzureBlobFileSystemException
115+ */
116+ private void generateContainerSAS () throws AzureBlobFileSystemException {
117+ final byte [] accountKey = Base64 .decode (
118+ getConfiguration ().getStorageAccountKey ());
119+ ServiceSASGenerator configServiceSASGenerator = new ServiceSASGenerator (
120+ accountKey );
121+ // Setting only read permissions.
122+ configServiceSASGenerator .setPermissions ("r" );
123+ containerSAS = configServiceSASGenerator .getContainerSASWithFullControl (
124+ getAccountName (), getFileSystemName ());
125+ }
88126 /**
89127 * Tests the scenario where both the custom SASTokenProvider and a fixed SAS token are configured.
90128 * Custom implementation of SASTokenProvider class should be chosen and User Delegation SAS should be used.
@@ -126,6 +164,58 @@ public void testBothProviderFixedTokenConfigured() throws Exception {
126164 }
127165 }
128166
167+ /**
168+ * Helper method to get the Fixed SAS token value
169+ */
170+ private String getFixedSASToken (AbfsConfiguration config ) throws Exception {
171+ return config .getSASTokenProvider ()
172+ .getSASToken (this .getAccountName (), this .getFileSystemName (),
173+ getMethodName (),
174+ EMPTY_STRING );
175+ }
176+
177+ /**
178+ * Tests the implementation sequence if all fixed SAS configs are set.
179+ * The expected sequence is Container Specific Fixed SAS, Account Specific Fixed SAS, Account Agnostic Fixed SAS.
180+ * @throws IOException
181+ */
182+ @ Test
183+ public void testFixedSASTokenProviderPreference () throws Exception {
184+ AbfsConfiguration testAbfsConfig = new AbfsConfiguration (
185+ getRawConfiguration (), this .getAccountName (), this .getFileSystemName (),
186+ getAbfsServiceType ());
187+
188+ // setting all types of Fixed SAS configs (container-specific, account-specific, account-agnostic)
189+ removeAnyPresetConfiguration (testAbfsConfig );
190+ testAbfsConfig .set (
191+ containerProperty (FS_AZURE_SAS_FIXED_TOKEN , this .getFileSystemName (),
192+ this .getAccountName ()), containerSAS );
193+ testAbfsConfig .set (
194+ accountProperty (FS_AZURE_SAS_FIXED_TOKEN , this .getAccountName ()),
195+ accountSAS );
196+ testAbfsConfig .set (FS_AZURE_SAS_FIXED_TOKEN , accountAgnosticSAS );
197+
198+ // Assert that Container Specific Fixed SAS is used
199+ Assertions .assertThat (getFixedSASToken (testAbfsConfig ))
200+ .describedAs ("Container-specific fixed SAS should've been used." )
201+ .isEqualTo (containerSAS );
202+
203+ // Assert that Account Specific Fixed SAS is used if container SAS isn't set
204+ testAbfsConfig .unset (
205+ containerProperty (FS_AZURE_SAS_FIXED_TOKEN , this .getFileSystemName (),
206+ this .getAccountName ()));
207+ Assertions .assertThat (getFixedSASToken (testAbfsConfig ))
208+ .describedAs ("Account-specific fixed SAS should've been used." )
209+ .isEqualTo (accountSAS );
210+
211+ //Assert that Account-Agnostic fixed SAS is used if no other fixed SAS configs are set.
212+ testAbfsConfig .unset (
213+ accountProperty (FS_AZURE_SAS_FIXED_TOKEN , this .getAccountName ()));
214+ Assertions .assertThat (getFixedSASToken (testAbfsConfig ))
215+ .describedAs ("Account-agnostic fixed SAS should've been used." )
216+ .isEqualTo (accountAgnosticSAS );
217+ }
218+
129219 /**
130220 * Tests the scenario where only the fixed token is configured, and no token provider class is set.
131221 * Account SAS Token configured as fixed SAS should be used.
@@ -189,5 +279,6 @@ private void removeAnyPresetConfiguration(AbfsConfiguration testAbfsConfig) {
189279 testAbfsConfig .unset (FS_AZURE_SAS_FIXED_TOKEN );
190280 testAbfsConfig .unset (accountProperty (FS_AZURE_SAS_TOKEN_PROVIDER_TYPE , this .getAccountName ()));
191281 testAbfsConfig .unset (accountProperty (FS_AZURE_SAS_FIXED_TOKEN , this .getAccountName ()));
282+ testAbfsConfig .unset (containerProperty (FS_AZURE_SAS_FIXED_TOKEN , this .getFileSystemName (), this .getAccountName ()));
192283 }
193284}
0 commit comments