21
21
22
22
import com .microsoft .azure .storage .LocationMode ;
23
23
import com .microsoft .azure .storage .RetryPolicy ;
24
+ import org .elasticsearch .common .Nullable ;
24
25
import org .elasticsearch .common .Strings ;
25
26
import org .elasticsearch .common .collect .MapBuilder ;
26
27
import org .elasticsearch .common .collect .Tuple ;
@@ -57,6 +58,10 @@ public final class AzureStorageSettings {
57
58
public static final AffixSetting <SecureString > KEY_SETTING = Setting .affixKeySetting (AZURE_CLIENT_PREFIX_KEY , "key" ,
58
59
key -> SecureSetting .secureString (key , null ));
59
60
61
+ /** Azure SAS token */
62
+ public static final AffixSetting <SecureString > SAS_TOKEN_SETTING = Setting .affixKeySetting (AZURE_CLIENT_PREFIX_KEY , "sas_token" ,
63
+ key -> SecureSetting .secureString (key , null ));
64
+
60
65
/** max_retries: Number of retries in case of Azure errors. Defaults to 3 (RetryPolicy.DEFAULT_CLIENT_RETRY_COUNT). */
61
66
public static final Setting <Integer > MAX_RETRIES_SETTING =
62
67
Setting .affixKeySetting (AZURE_CLIENT_PREFIX_KEY , "max_retries" ,
@@ -118,7 +123,7 @@ public interface Storage {
118
123
@ Deprecated
119
124
private final String name ;
120
125
private final String account ;
121
- private final String key ;
126
+ private final String connectString ;
122
127
private final String endpointSuffix ;
123
128
private final TimeValue timeout ;
124
129
@ Deprecated
@@ -128,11 +133,11 @@ public interface Storage {
128
133
private final LocationMode locationMode ;
129
134
130
135
// copy-constructor
131
- private AzureStorageSettings (String name , String account , String key , String endpointSuffix , TimeValue timeout , boolean activeByDefault ,
132
- int maxRetries , Proxy proxy , LocationMode locationMode ) {
136
+ private AzureStorageSettings (String name , String account , String connectString , String endpointSuffix , TimeValue timeout ,
137
+ boolean activeByDefault , int maxRetries , Proxy proxy , LocationMode locationMode ) {
133
138
this .name = name ;
134
139
this .account = account ;
135
- this .key = key ;
140
+ this .connectString = connectString ;
136
141
this .endpointSuffix = endpointSuffix ;
137
142
this .timeout = timeout ;
138
143
this .activeByDefault = activeByDefault ;
@@ -145,7 +150,7 @@ private AzureStorageSettings(String name, String account, String key, String end
145
150
public AzureStorageSettings (String name , String account , String key , TimeValue timeout , boolean activeByDefault , int maxRetries ) {
146
151
this .name = name ;
147
152
this .account = account ;
148
- this .key = key ;
153
+ this .connectString = buildConnectString ( account , key , null , null ) ;
149
154
this .endpointSuffix = null ;
150
155
this .timeout = timeout ;
151
156
this .activeByDefault = activeByDefault ;
@@ -154,11 +159,11 @@ public AzureStorageSettings(String name, String account, String key, TimeValue t
154
159
this .locationMode = LocationMode .PRIMARY_ONLY ;
155
160
}
156
161
157
- AzureStorageSettings (String account , String key , String endpointSuffix , TimeValue timeout , int maxRetries ,
158
- Proxy .Type proxyType , String proxyHost , Integer proxyPort ) {
162
+ private AzureStorageSettings (String account , String key , String sasToken , String endpointSuffix , TimeValue timeout , int maxRetries ,
163
+ Proxy .Type proxyType , String proxyHost , Integer proxyPort ) {
159
164
this .name = null ;
160
165
this .account = account ;
161
- this .key = key ;
166
+ this .connectString = buildConnectString ( account , key , sasToken , endpointSuffix ) ;
162
167
this .endpointSuffix = endpointSuffix ;
163
168
this .timeout = timeout ;
164
169
this .activeByDefault = false ;
@@ -189,10 +194,6 @@ public String getName() {
189
194
return name ;
190
195
}
191
196
192
- public String getKey () {
193
- return key ;
194
- }
195
-
196
197
public String getAccount () {
197
198
return account ;
198
199
}
@@ -218,13 +219,26 @@ public Proxy getProxy() {
218
219
return proxy ;
219
220
}
220
221
221
- public String buildConnectionString () {
222
+ public String getConnectString () {
223
+ return connectString ;
224
+ }
225
+
226
+ private static String buildConnectString (String account , @ Nullable String key , @ Nullable String sasToken , String endpointSuffix ) {
227
+ final boolean hasSasToken = Strings .hasText (sasToken );
228
+ final boolean hasKey = Strings .hasText (key );
229
+ if (hasSasToken == false && hasKey == false ) {
230
+ throw new SettingsException ("Neither a secret key nor a shared access token was set." );
231
+ }
232
+ if (hasSasToken && hasKey ) {
233
+ throw new SettingsException ("Both a secret as well as a shared access token were set." );
234
+ }
222
235
final StringBuilder connectionStringBuilder = new StringBuilder ();
223
- connectionStringBuilder .append ("DefaultEndpointsProtocol=https" )
224
- .append (";AccountName=" )
225
- .append (account )
226
- .append (";AccountKey=" )
227
- .append (key );
236
+ connectionStringBuilder .append ("DefaultEndpointsProtocol=https" ).append (";AccountName=" ).append (account );
237
+ if (hasKey ) {
238
+ connectionStringBuilder .append (";AccountKey=" ).append (key );
239
+ } else {
240
+ connectionStringBuilder .append (";SharedAccessSignature=" ).append (sasToken );
241
+ }
228
242
if (Strings .hasText (endpointSuffix )) {
229
243
connectionStringBuilder .append (";EndpointSuffix=" ).append (endpointSuffix );
230
244
}
@@ -239,7 +253,6 @@ public LocationMode getLocationMode() {
239
253
public String toString () {
240
254
final StringBuilder sb = new StringBuilder ("AzureStorageSettings{" );
241
255
sb .append ("account='" ).append (account ).append ('\'' );
242
- sb .append (", key='" ).append (key ).append ('\'' );
243
256
sb .append (", activeByDefault='" ).append (activeByDefault ).append ('\'' );
244
257
sb .append (", timeout=" ).append (timeout );
245
258
sb .append (", endpointSuffix='" ).append (endpointSuffix ).append ('\'' );
@@ -309,8 +322,9 @@ static Map<String, AzureStorageSettings> loadRegular(Settings settings) {
309
322
/** Parse settings for a single client. */
310
323
static AzureStorageSettings getClientSettings (Settings settings , String clientName ) {
311
324
try (SecureString account = getConfigValue (settings , clientName , ACCOUNT_SETTING );
312
- SecureString key = getConfigValue (settings , clientName , KEY_SETTING )) {
313
- return new AzureStorageSettings (account .toString (), key .toString (),
325
+ SecureString key = getConfigValue (settings , clientName , KEY_SETTING );
326
+ SecureString sasToken = getConfigValue (settings , clientName , SAS_TOKEN_SETTING )) {
327
+ return new AzureStorageSettings (account .toString (), key .toString (), sasToken .toString (),
314
328
getValue (settings , clientName , ENDPOINT_SUFFIX_SETTING ),
315
329
getValue (settings , clientName , TIMEOUT_SETTING ),
316
330
getValue (settings , clientName , MAX_RETRIES_SETTING ),
@@ -358,8 +372,8 @@ private static AzureStorageSettings getPrimary(List<AzureStorageSettings> settin
358
372
} else if (settings .size () == 1 ) {
359
373
// the only storage settings belong (implicitly) to the default primary storage
360
374
AzureStorageSettings storage = settings .get (0 );
361
- return new AzureStorageSettings (storage .getName (), storage .getAccount (), storage .getKey (), storage .getTimeout (), true ,
362
- storage .getMaxRetries ());
375
+ return new AzureStorageSettings (storage .getName (), storage .getAccount (), storage .connectString , null , storage .getTimeout (),
376
+ true , storage .getMaxRetries (), null , LocationMode . PRIMARY_ONLY );
363
377
} else {
364
378
AzureStorageSettings primary = null ;
365
379
for (AzureStorageSettings setting : settings ) {
@@ -398,8 +412,8 @@ public static Map<String, AzureStorageSettings> overrideLocationMode(Map<String,
398
412
final MapBuilder <String , AzureStorageSettings > mapBuilder = new MapBuilder <>();
399
413
for (final Map .Entry <String , AzureStorageSettings > entry : clientsSettings .entrySet ()) {
400
414
final AzureStorageSettings azureSettings = new AzureStorageSettings (entry .getValue ().name , entry .getValue ().account ,
401
- entry .getValue ().key , entry .getValue ().endpointSuffix , entry .getValue ().timeout , entry . getValue (). activeByDefault ,
402
- entry .getValue ().maxRetries , entry .getValue ().proxy , locationMode );
415
+ entry .getValue ().connectString , entry .getValue ().endpointSuffix , entry .getValue ().timeout ,
416
+ entry . getValue (). activeByDefault , entry .getValue ().maxRetries , entry .getValue ().proxy , locationMode );
403
417
mapBuilder .put (entry .getKey (), azureSettings );
404
418
}
405
419
return mapBuilder .immutableMap ();
0 commit comments