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 .settings .SecureSetting ;
26
27
import org .elasticsearch .common .settings .SecureString ;
@@ -53,6 +54,10 @@ final class AzureStorageSettings {
53
54
public static final AffixSetting <SecureString > KEY_SETTING = Setting .affixKeySetting (AZURE_CLIENT_PREFIX_KEY , "key" ,
54
55
key -> SecureSetting .secureString (key , null ));
55
56
57
+ /** Azure SAS token */
58
+ public static final AffixSetting <SecureString > SAS_TOKEN_SETTING = Setting .affixKeySetting (AZURE_CLIENT_PREFIX_KEY , "sas_token" ,
59
+ key -> SecureSetting .secureString (key , null ));
60
+
56
61
/** max_retries: Number of retries in case of Azure errors. Defaults to 3 (RetryPolicy.DEFAULT_CLIENT_RETRY_COUNT). */
57
62
public static final Setting <Integer > MAX_RETRIES_SETTING =
58
63
Setting .affixKeySetting (AZURE_CLIENT_PREFIX_KEY , "max_retries" ,
@@ -82,29 +87,29 @@ final class AzureStorageSettings {
82
87
PROXY_HOST_SETTING );
83
88
84
89
private final String account ;
85
- private final String key ;
90
+ private final String connectString ;
86
91
private final String endpointSuffix ;
87
92
private final TimeValue timeout ;
88
93
private final int maxRetries ;
89
94
private final Proxy proxy ;
90
95
private final LocationMode locationMode ;
91
96
92
97
// copy-constructor
93
- private AzureStorageSettings (String account , String key , String endpointSuffix , TimeValue timeout , int maxRetries , Proxy proxy ,
94
- LocationMode locationMode ) {
98
+ private AzureStorageSettings (String account , String connectString , String endpointSuffix , TimeValue timeout , int maxRetries ,
99
+ Proxy proxy , LocationMode locationMode ) {
95
100
this .account = account ;
96
- this .key = key ;
101
+ this .connectString = connectString ;
97
102
this .endpointSuffix = endpointSuffix ;
98
103
this .timeout = timeout ;
99
104
this .maxRetries = maxRetries ;
100
105
this .proxy = proxy ;
101
106
this .locationMode = locationMode ;
102
107
}
103
108
104
- AzureStorageSettings (String account , String key , String endpointSuffix , TimeValue timeout , int maxRetries ,
105
- Proxy .Type proxyType , String proxyHost , Integer proxyPort ) {
109
+ private AzureStorageSettings (String account , String key , String sasToken , String endpointSuffix , TimeValue timeout , int maxRetries ,
110
+ Proxy .Type proxyType , String proxyHost , Integer proxyPort ) {
106
111
this .account = account ;
107
- this .key = key ;
112
+ this .connectString = buildConnectString ( account , key , sasToken , endpointSuffix ) ;
108
113
this .endpointSuffix = endpointSuffix ;
109
114
this .timeout = timeout ;
110
115
this .maxRetries = maxRetries ;
@@ -145,13 +150,26 @@ public Proxy getProxy() {
145
150
return proxy ;
146
151
}
147
152
148
- public String buildConnectionString () {
153
+ public String getConnectString () {
154
+ return connectString ;
155
+ }
156
+
157
+ private static String buildConnectString (String account , @ Nullable String key , @ Nullable String sasToken , String endpointSuffix ) {
158
+ final boolean hasSasToken = Strings .hasText (sasToken );
159
+ final boolean hasKey = Strings .hasText (key );
160
+ if (hasSasToken == false && hasKey == false ) {
161
+ throw new SettingsException ("Neither a secret key nor a shared access token was set." );
162
+ }
163
+ if (hasSasToken && hasKey ) {
164
+ throw new SettingsException ("Both a secret as well as a shared access token were set." );
165
+ }
149
166
final StringBuilder connectionStringBuilder = new StringBuilder ();
150
- connectionStringBuilder .append ("DefaultEndpointsProtocol=https" )
151
- .append (";AccountName=" )
152
- .append (account )
153
- .append (";AccountKey=" )
154
- .append (key );
167
+ connectionStringBuilder .append ("DefaultEndpointsProtocol=https" ).append (";AccountName=" ).append (account );
168
+ if (hasKey ) {
169
+ connectionStringBuilder .append (";AccountKey=" ).append (key );
170
+ } else {
171
+ connectionStringBuilder .append (";SharedAccessSignature=" ).append (sasToken );
172
+ }
155
173
if (Strings .hasText (endpointSuffix )) {
156
174
connectionStringBuilder .append (";EndpointSuffix=" ).append (endpointSuffix );
157
175
}
@@ -166,7 +184,6 @@ public LocationMode getLocationMode() {
166
184
public String toString () {
167
185
final StringBuilder sb = new StringBuilder ("AzureStorageSettings{" );
168
186
sb .append ("account='" ).append (account ).append ('\'' );
169
- sb .append (", key='" ).append (key ).append ('\'' );
170
187
sb .append (", timeout=" ).append (timeout );
171
188
sb .append (", endpointSuffix='" ).append (endpointSuffix ).append ('\'' );
172
189
sb .append (", maxRetries=" ).append (maxRetries );
@@ -201,8 +218,9 @@ public static Map<String, AzureStorageSettings> load(Settings settings) {
201
218
/** Parse settings for a single client. */
202
219
private static AzureStorageSettings getClientSettings (Settings settings , String clientName ) {
203
220
try (SecureString account = getConfigValue (settings , clientName , ACCOUNT_SETTING );
204
- SecureString key = getConfigValue (settings , clientName , KEY_SETTING )) {
205
- return new AzureStorageSettings (account .toString (), key .toString (),
221
+ SecureString key = getConfigValue (settings , clientName , KEY_SETTING );
222
+ SecureString sasToken = getConfigValue (settings , clientName , SAS_TOKEN_SETTING )) {
223
+ return new AzureStorageSettings (account .toString (), key .toString (), sasToken .toString (),
206
224
getValue (settings , clientName , ENDPOINT_SUFFIX_SETTING ),
207
225
getValue (settings , clientName , TIMEOUT_SETTING ),
208
226
getValue (settings , clientName , MAX_RETRIES_SETTING ),
@@ -228,10 +246,9 @@ static Map<String, AzureStorageSettings> overrideLocationMode(Map<String, AzureS
228
246
LocationMode locationMode ) {
229
247
final var map = new HashMap <String , AzureStorageSettings >();
230
248
for (final Map .Entry <String , AzureStorageSettings > entry : clientsSettings .entrySet ()) {
231
- final AzureStorageSettings azureSettings = new AzureStorageSettings (entry .getValue ().account , entry .getValue ().key ,
232
- entry .getValue ().endpointSuffix , entry .getValue ().timeout , entry .getValue ().maxRetries , entry .getValue ().proxy ,
233
- locationMode );
234
- map .put (entry .getKey (), azureSettings );
249
+ map .put (entry .getKey (),
250
+ new AzureStorageSettings (entry .getValue ().account , entry .getValue ().connectString , entry .getValue ().endpointSuffix ,
251
+ entry .getValue ().timeout , entry .getValue ().maxRetries , entry .getValue ().proxy , locationMode ));
235
252
}
236
253
return Map .copyOf (map );
237
254
}
0 commit comments