This repository was archived by the owner on Apr 19, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathServiceConfigSupplier.java
More file actions
243 lines (220 loc) · 9.12 KB
/
Copy pathServiceConfigSupplier.java
File metadata and controls
243 lines (220 loc) · 9.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.api.config;
import com.google.api.Service;
import com.google.api.Service.Builder;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.HttpResponse;
import com.google.api.client.http.HttpStatusCodes;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.servicemanagement.ServiceManagement;
import com.google.api.services.servicemanagement.model.ListServiceConfigsResponse;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.protobuf.util.JsonFormat;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.List;
import javax.annotation.Nullable;
/**
* Supplies a service configuration fetched from Google Service Management APIs.
*/
public final class ServiceConfigSupplier implements Supplier<Service> {
private static final String SERVICE_NAME_KEY = "ENDPOINTS_SERVICE_NAME";
private static final String SERVICE_VERSION_KEY = "ENDPOINTS_SERVICE_VERSION";
private static final ImmutableMap<Integer, String> ERROR_CODE_DETAILS =
ImmutableMap.<Integer, String>builder()
.put(
403 /* forbidden */,
"This may occur if the App Engine service account has been deleted or does not " +
"have correct permissions. Visit https://goo.gl/UEKik4 and verify that the " +
"App Engine default service account has either the Editor or Service Controller " +
"role.")
.put(
404 /* not found */,
"The service config name and config id could not be found. Double check that " +
"filter initialization parameters endpoints.projectId and endpoints.serviceName " +
"are correctly set. See https://goo.gl/yp8QUN for details.")
.put(
429 /* too many requests */,
"Too many requests are being made to fetch the service config. Check to see if " +
"instances are crashing prematurely. If not, you may need to increase your " +
"Google Service Management API quota. See https://goo.gl/JjBQMu to manage quota.")
.build();
private static final List<String> SCOPES =
ImmutableList.of("https://www.googleapis.com/auth/cloud-platform");
private static final String FIELD_MASKS = Joiner.on(',').join(
"authentication",
"http",
"id",
"logging",
"logs",
"metrics",
"monitored_resources",
"monitoring",
"name",
"producer_project_id",
"quota",
"system_parameters",
"usage");
private final Environment environment;
private final ServiceManagement serviceManagement;
@VisibleForTesting
ServiceConfigSupplier(
Environment environment,
HttpTransport httpTransport,
JsonFactory jsonFactory,
final GoogleCredential credential) {
this.environment = environment;
HttpRequestInitializer requestInitializer = new HttpRequestInitializer() {
@Override
public void initialize(HttpRequest request) throws IOException {
request.setThrowExceptionOnExecuteError(false);
credential.initialize(request);
}
};
this.serviceManagement =
new ServiceManagement.Builder(httpTransport, jsonFactory, requestInitializer)
.setApplicationName("Endpoints Frameworks Java")
.build();
}
/**
* Fetches the service configuration using the service name and the service
* version read from the environment variables.
*
* @return a {@link Service} object generated by the JSON response from Google
* Service Management.
*/
@Override
public Service get() {
String serviceName = this.environment.getVariable(SERVICE_NAME_KEY);
if (Strings.isNullOrEmpty(serviceName)) {
String errorMessage =
String.format("Environment variable '%s' is not set", SERVICE_NAME_KEY);
throw new IllegalArgumentException(errorMessage);
}
String serviceVersion = this.environment.getVariable(SERVICE_VERSION_KEY);
return fetch(serviceName, serviceVersion);
}
/**
* Fetches the service configuration with the given service name and service version.
*
* @param serviceName the given service name
* @param serviceVersion the given service version
* @return a {@link Service} object generated by the JSON response from Google Service Management.
*/
private Service fetch(String serviceName, @Nullable String serviceVersion) {
Preconditions.checkArgument(
!Strings.isNullOrEmpty(serviceName), "service name must be specified");
if (serviceVersion == null) {
serviceVersion = fetchLatestServiceVersion(serviceName);
}
final HttpResponse httpResponse;
try {
httpResponse = serviceManagement.services().configs().get(serviceName, serviceVersion)
.setFields(FIELD_MASKS)
.executeUnparsed();
} catch (IOException exception) {
throw new ServiceConfigException(exception);
}
int statusCode = httpResponse.getStatusCode();
if (statusCode != HttpStatusCodes.STATUS_CODE_OK) {
String extendedMessage = ERROR_CODE_DETAILS.get(statusCode);
String message = MessageFormat.format(
"Failed to fetch service config (status code {0})",
statusCode);
if (extendedMessage != null) {
message = String.format("%s: %s", message, extendedMessage);
}
throw new ServiceConfigException(message);
}
Service service = parseHttpResponse(httpResponse);
validateServiceConfig(service, serviceName, serviceVersion);
return service;
}
private String fetchLatestServiceVersion(String serviceName) {
try {
ListServiceConfigsResponse response =
serviceManagement.services().configs().list(serviceName).execute();
if (response.getServiceConfigs() == null || response.getServiceConfigs().isEmpty()) {
throw new ServiceConfigException(MessageFormat.format(
"Failed to fetch default config version for service ''{0}''. No versions exist!",
serviceName));
}
return response.getServiceConfigs().get(0).getId();
} catch (IOException e) {
throw new ServiceConfigException(e);
}
}
private static Service parseHttpResponse(HttpResponse httpResponse) {
try {
Builder builder = Service.newBuilder();
JsonFormat.parser().ignoringUnknownFields().merge(httpResponse.parseAsString(), builder);
return builder.build();
} catch (IOException exception) {
throw new ServiceConfigException(
"Failed to parse the HTTP response as service configuration",
exception);
}
}
private static void validateServiceConfig(
Service service,
String expectedServiceName,
@Nullable String expectedServiceVersion) {
String serviceName = service.getName();
if (!expectedServiceName.equals(serviceName)) {
throw new ServiceConfigException("Unexpected service name in service config: " + serviceName);
}
String serviceVersion = service.getId();
if (expectedServiceVersion != null && !expectedServiceVersion.equals(serviceVersion)) {
throw new ServiceConfigException("Unexpected service version in service config: " + serviceVersion);
}
}
/**
* Create a {@link ServiceConfigSupplier} instance.
*
* @return a {@code ServiceConfigSuppler}
*/
public static ServiceConfigSupplier create() {
NetHttpTransport httpTransport = new NetHttpTransport();
JacksonFactory jsonFactory = new JacksonFactory();
GoogleCredential credential;
try {
credential = GoogleCredential.getApplicationDefault(httpTransport, jsonFactory)
.createScoped(SCOPES);
} catch (IOException e) {
throw new IllegalStateException("could not get credentials for fetching service config!");
}
return new ServiceConfigSupplier(
new SystemEnvironment(), httpTransport, jsonFactory, credential);
}
private static final class SystemEnvironment implements Environment {
@Override
public String getVariable(String variableName) {
return System.getenv(variableName);
}
}
}