32
32
import org .apache .hadoop .fs .azurebfs .services .AuthType ;
33
33
import org .apache .hadoop .fs .azurebfs .services .FixedSASTokenProvider ;
34
34
import org .apache .hadoop .fs .azurebfs .utils .AccountSASGenerator ;
35
+ import org .apache .hadoop .fs .azurebfs .utils .ServiceSASGenerator ;
35
36
import org .apache .hadoop .fs .azurebfs .utils .Base64 ;
36
37
37
38
import static org .apache .hadoop .fs .azurebfs .constants .ConfigurationKeys .FS_AZURE_SAS_FIXED_TOKEN ;
38
39
import static org .apache .hadoop .fs .azurebfs .constants .ConfigurationKeys .FS_AZURE_SAS_TOKEN_PROVIDER_TYPE ;
39
40
import static org .apache .hadoop .fs .azurebfs .constants .ConfigurationKeys .accountProperty ;
41
+ import static org .apache .hadoop .fs .azurebfs .constants .ConfigurationKeys .containerProperty ;
40
42
import static org .apache .hadoop .fs .azurebfs .constants .TestConfigurationKeys .FS_AZURE_TEST_APP_ID ;
41
43
import static org .apache .hadoop .fs .azurebfs .constants .TestConfigurationKeys .FS_AZURE_TEST_APP_SECRET ;
42
44
import static org .apache .hadoop .fs .azurebfs .constants .TestConfigurationKeys .FS_AZURE_TEST_APP_SERVICE_PRINCIPAL_OBJECT_ID ;
50
52
public class ITestAzureBlobFileSystemChooseSAS extends AbstractAbfsIntegrationTest {
51
53
52
54
private String accountSAS = null ;
55
+ private String containerSAS = null ;
53
56
private static final String TEST_PATH = "testPath" ;
54
57
55
58
/**
@@ -69,6 +72,7 @@ public void setup() throws Exception {
69
72
super .setup ();
70
73
createFilesystemWithTestFileForSASTests (new Path (TEST_PATH ));
71
74
generateAccountSAS ();
75
+ generateContainerSAS ();
72
76
}
73
77
74
78
/**
@@ -85,6 +89,22 @@ private void generateAccountSAS() throws AzureBlobFileSystemException {
85
89
accountSAS = configAccountSASGenerator .getAccountSAS (getAccountName ());
86
90
}
87
91
92
+ /**
93
+ * Generates a Container SAS Token using the Account Shared Key to be used as a fixed SAS Token.
94
+ * Container SAS used here will have only read permissions to resources.
95
+ * This will be used by individual tests to set in the configurations.
96
+ * @throws AzureBlobFileSystemException
97
+ */
98
+ private void generateContainerSAS () throws AzureBlobFileSystemException {
99
+ final byte [] accountKey = Base64 .decode (
100
+ getConfiguration ().getStorageAccountKey ());
101
+ ServiceSASGenerator configServiceSASGenerator = new ServiceSASGenerator (
102
+ accountKey );
103
+ // Setting only read permissions.
104
+ configServiceSASGenerator .setPermissions ("r" );
105
+ containerSAS = configServiceSASGenerator .getContainerSASWithFullControl (
106
+ getAccountName (), getFileSystemName ());
107
+ }
88
108
/**
89
109
* Tests the scenario where both the custom SASTokenProvider and a fixed SAS token are configured.
90
110
* Custom implementation of SASTokenProvider class should be chosen and User Delegation SAS should be used.
@@ -126,6 +146,38 @@ public void testBothProviderFixedTokenConfigured() throws Exception {
126
146
}
127
147
}
128
148
149
+ /**
150
+ * Tests the implementation sequence if all fixed SAS configs are set.
151
+ * The expected sequence is Container Specific Fixed SAS, Account Specific Fixed SAS, Account Agnostic Fixed SAS.
152
+ * @throws IOException
153
+ */
154
+ @ Test
155
+ public void testFixedTokenPreference () throws Exception {
156
+ AbfsConfiguration testAbfsConfig = new AbfsConfiguration (
157
+ getRawConfiguration (), this .getAccountName (), this .getFileSystemName (), getAbfsServiceType ());
158
+
159
+ // setting all types of Fixed SAS configs (container-specific, account-specific, account-agnostic)
160
+ removeAnyPresetConfiguration (testAbfsConfig );
161
+ testAbfsConfig .set (containerProperty (FS_AZURE_SAS_FIXED_TOKEN , this .getFileSystemName (), this .getAccountName ()), containerSAS );
162
+ testAbfsConfig .set (accountProperty (FS_AZURE_SAS_FIXED_TOKEN , this .getAccountName ()), accountSAS );
163
+ testAbfsConfig .set (FS_AZURE_SAS_FIXED_TOKEN , accountSAS );
164
+ String ContainerSASExpected = testAbfsConfig .getSASTokenProvider ().getSASToken (this .getAccountName (), this .getFileSystemName (), getMethodName (), "read" );
165
+
166
+ // Assert that Container Specific Fixed SAS is used
167
+ Assertions .assertThat (ContainerSASExpected ).contains ("sr=c" );
168
+
169
+ // Assert that Account Specific Fixed SAS is used if container SAS isn't set
170
+ testAbfsConfig .unset (containerProperty (FS_AZURE_SAS_FIXED_TOKEN , this .getFileSystemName (), this .getAccountName ()));
171
+ String AccountSASExpected = testAbfsConfig .getSASTokenProvider ().getSASToken (this .getAccountName (), this .getFileSystemName (), getMethodName (), "read" );
172
+ Assertions .assertThat (AccountSASExpected ).contains ("ss=bf" );
173
+
174
+ //Assert that Account-Agnostic fixed SAS is used if no other fixed SAS configs are set.
175
+ // The token is the same as the Account Specific Fixed SAS.
176
+ testAbfsConfig .unset (FS_AZURE_SAS_FIXED_TOKEN );
177
+ String AccountAgnosticSASExpected = testAbfsConfig .getSASTokenProvider ().getSASToken (this .getAccountName (), this .getFileSystemName (), getMethodName (), "read" );
178
+ Assertions .assertThat (AccountAgnosticSASExpected ).contains ("ss=bf" );
179
+ }
180
+
129
181
/**
130
182
* Tests the scenario where only the fixed token is configured, and no token provider class is set.
131
183
* Account SAS Token configured as fixed SAS should be used.
@@ -189,5 +241,6 @@ private void removeAnyPresetConfiguration(AbfsConfiguration testAbfsConfig) {
189
241
testAbfsConfig .unset (FS_AZURE_SAS_FIXED_TOKEN );
190
242
testAbfsConfig .unset (accountProperty (FS_AZURE_SAS_TOKEN_PROVIDER_TYPE , this .getAccountName ()));
191
243
testAbfsConfig .unset (accountProperty (FS_AZURE_SAS_FIXED_TOKEN , this .getAccountName ()));
244
+ testAbfsConfig .unset (containerProperty (FS_AZURE_SAS_FIXED_TOKEN , this .getFileSystemName (), this .getAccountName ()));
192
245
}
193
246
}
0 commit comments