|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.elasticsearch.repositories.gcs; |
| 20 | + |
| 21 | +import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; |
| 22 | +import com.google.api.services.storage.StorageScopes; |
| 23 | +import org.elasticsearch.common.settings.SecureSetting; |
| 24 | +import org.elasticsearch.common.settings.Setting; |
| 25 | +import org.elasticsearch.common.settings.Settings; |
| 26 | +import org.elasticsearch.common.unit.TimeValue; |
| 27 | + |
| 28 | +import java.io.IOException; |
| 29 | +import java.io.InputStream; |
| 30 | +import java.io.UncheckedIOException; |
| 31 | +import java.util.Collections; |
| 32 | +import java.util.HashMap; |
| 33 | +import java.util.Map; |
| 34 | + |
| 35 | +import static org.elasticsearch.common.settings.Setting.timeSetting; |
| 36 | + |
| 37 | +/** |
| 38 | + * Container for Google Cloud Storage clients settings. |
| 39 | + */ |
| 40 | +public class GoogleCloudStorageClientSettings { |
| 41 | + |
| 42 | + private static final String PREFIX = "gcs.client."; |
| 43 | + |
| 44 | + /** A json Service Account file loaded from secure settings. */ |
| 45 | + static final Setting.AffixSetting<InputStream> CREDENTIALS_FILE_SETTING = Setting.affixKeySetting(PREFIX, "credentials_file", |
| 46 | + key -> SecureSetting.secureFile(key, null)); |
| 47 | + |
| 48 | + /** An override for the Storage endpoint to connect to. */ |
| 49 | + static final Setting.AffixSetting<String> ENDPOINT_SETTING = Setting.affixKeySetting(PREFIX, "endpoint", |
| 50 | + key -> new Setting<>(key, "", s -> s, Setting.Property.NodeScope)); |
| 51 | + |
| 52 | + /** |
| 53 | + * The timeout to establish a connection. A value of {@code -1} corresponds to an infinite timeout. A value of {@code 0} |
| 54 | + * corresponds to the default timeout of the Google Cloud Storage Java Library. |
| 55 | + */ |
| 56 | + static final Setting.AffixSetting<TimeValue> CONNECT_TIMEOUT_SETTING = Setting.affixKeySetting(PREFIX, "connect_timeout", |
| 57 | + key -> timeSetting(key, TimeValue.ZERO, TimeValue.MINUS_ONE, Setting.Property.NodeScope)); |
| 58 | + |
| 59 | + /** |
| 60 | + * The timeout to read data from an established connection. A value of {@code -1} corresponds to an infinite timeout. A value of |
| 61 | + * {@code 0} corresponds to the default timeout of the Google Cloud Storage Java Library. |
| 62 | + */ |
| 63 | + static final Setting.AffixSetting<TimeValue> READ_TIMEOUT_SETTING = Setting.affixKeySetting(PREFIX, "read_timeout", |
| 64 | + key -> timeSetting(key, TimeValue.ZERO, TimeValue.MINUS_ONE, Setting.Property.NodeScope)); |
| 65 | + |
| 66 | + /** Name used by the client when it uses the Google Cloud JSON API. **/ |
| 67 | + static final Setting.AffixSetting<String> APPLICATION_NAME_SETTING = Setting.affixKeySetting(PREFIX, "application_name", |
| 68 | + key -> new Setting<>(key, "repository-gcs", s -> s, Setting.Property.NodeScope)); |
| 69 | + |
| 70 | + /** The credentials used by the client to connect to the Storage endpoint **/ |
| 71 | + private final GoogleCredential credential; |
| 72 | + |
| 73 | + /** The Storage root URL the client should talk to, or empty string to use the default. **/ |
| 74 | + private final String endpoint; |
| 75 | + |
| 76 | + /** The timeout to establish a connection **/ |
| 77 | + private final TimeValue connectTimeout; |
| 78 | + |
| 79 | + /** The timeout to read data from an established connection **/ |
| 80 | + private final TimeValue readTimeout; |
| 81 | + |
| 82 | + /** The Storage client application name **/ |
| 83 | + private final String applicationName; |
| 84 | + |
| 85 | + GoogleCloudStorageClientSettings(final GoogleCredential credential, |
| 86 | + final String endpoint, |
| 87 | + final TimeValue connectTimeout, |
| 88 | + final TimeValue readTimeout, |
| 89 | + final String applicationName) { |
| 90 | + this.credential = credential; |
| 91 | + this.endpoint = endpoint; |
| 92 | + this.connectTimeout = connectTimeout; |
| 93 | + this.readTimeout = readTimeout; |
| 94 | + this.applicationName = applicationName; |
| 95 | + } |
| 96 | + |
| 97 | + public GoogleCredential getCredential() { |
| 98 | + return credential; |
| 99 | + } |
| 100 | + |
| 101 | + public String getEndpoint() { |
| 102 | + return endpoint; |
| 103 | + } |
| 104 | + |
| 105 | + public TimeValue getConnectTimeout() { |
| 106 | + return connectTimeout; |
| 107 | + } |
| 108 | + |
| 109 | + public TimeValue getReadTimeout() { |
| 110 | + return readTimeout; |
| 111 | + } |
| 112 | + |
| 113 | + public String getApplicationName() { |
| 114 | + return applicationName; |
| 115 | + } |
| 116 | + |
| 117 | + public static Map<String, GoogleCloudStorageClientSettings> load(final Settings settings) { |
| 118 | + final Map<String, GoogleCloudStorageClientSettings> clients = new HashMap<>(); |
| 119 | + for (String clientName: settings.getGroups(PREFIX).keySet()) { |
| 120 | + clients.put(clientName, getClientSettings(settings, clientName)); |
| 121 | + } |
| 122 | + if (clients.containsKey("default") == false) { |
| 123 | + // this won't find any settings under the default client, |
| 124 | + // but it will pull all the fallback static settings |
| 125 | + clients.put("default", getClientSettings(settings, "default")); |
| 126 | + } |
| 127 | + return Collections.unmodifiableMap(clients); |
| 128 | + } |
| 129 | + |
| 130 | + static GoogleCloudStorageClientSettings getClientSettings(final Settings settings, final String clientName) { |
| 131 | + return new GoogleCloudStorageClientSettings( |
| 132 | + loadCredential(settings, clientName), |
| 133 | + getConfigValue(settings, clientName, ENDPOINT_SETTING), |
| 134 | + getConfigValue(settings, clientName, CONNECT_TIMEOUT_SETTING), |
| 135 | + getConfigValue(settings, clientName, READ_TIMEOUT_SETTING), |
| 136 | + getConfigValue(settings, clientName, APPLICATION_NAME_SETTING) |
| 137 | + ); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Loads the service account file corresponding to a given client name. If no file is defined for the client, |
| 142 | + * a {@code null} credential is returned. |
| 143 | + * |
| 144 | + * @param settings the {@link Settings} |
| 145 | + * @param clientName the client name |
| 146 | + * |
| 147 | + * @return the {@link GoogleCredential} to use for the given client, {@code null} if no service account is defined. |
| 148 | + */ |
| 149 | + static GoogleCredential loadCredential(final Settings settings, final String clientName) { |
| 150 | + try { |
| 151 | + if (CREDENTIALS_FILE_SETTING.getConcreteSettingForNamespace(clientName).exists(settings) == false) { |
| 152 | + // explicitly returning null here so that the default credential |
| 153 | + // can be loaded later when creating the Storage client |
| 154 | + return null; |
| 155 | + } |
| 156 | + try (InputStream credStream = CREDENTIALS_FILE_SETTING.getConcreteSettingForNamespace(clientName).get(settings)) { |
| 157 | + GoogleCredential credential = GoogleCredential.fromStream(credStream); |
| 158 | + if (credential.createScopedRequired()) { |
| 159 | + credential = credential.createScoped(Collections.singleton(StorageScopes.DEVSTORAGE_FULL_CONTROL)); |
| 160 | + } |
| 161 | + return credential; |
| 162 | + } |
| 163 | + } catch (IOException e) { |
| 164 | + throw new UncheckedIOException(e); |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + private static <T> T getConfigValue(final Settings settings, final String clientName, final Setting.AffixSetting<T> clientSetting) { |
| 169 | + Setting<T> concreteSetting = clientSetting.getConcreteSettingForNamespace(clientName); |
| 170 | + return concreteSetting.get(settings); |
| 171 | + } |
| 172 | +} |
0 commit comments