Remove AppEngineServiceUtils (#2003)

The only method that is called from this class is setNumInstances. However we
don't current use `nomulus set_num_instances` anywhere. If we need to change
the number of instances, it is either done by updating appengine-web.xml, which
is deployed by Spinnaker, or doing it manually as a break-glass fix via gcloud
or on Pantheon.
This commit is contained in:
Lai Jiang 2023-04-21 10:11:12 -04:00 committed by GitHub
parent 23fb69a682
commit bd0cea0d87
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 1 additions and 712 deletions

View file

@ -1,63 +0,0 @@
// Copyright 2018 The Nomulus Authors. 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 google.registry.util;
/**
* A wrapper for {@link com.google.appengine.api.modules.ModulesService} that provides a saner API.
*/
public interface AppEngineServiceUtils {
/**
* Returns a host name to use for the given service.
*
* <p>Note that this host name will not include a version, so it will always be whatever the live
* version is at the time that you hit the URL.
*/
String getServiceHostname(String service);
/**
* Returns a host name to use for the given service and current version.
*
* <p>Note that this host name will include the current version now at time of URL generation,
* which will not be the live version in the future.
*/
String getCurrentVersionHostname(String service);
/** Returns a host name to use for the given service and version. */
String getVersionHostname(String service, String version);
/**
* Converts a multi-level App Engine host name (not URL) to the -dot- single subdomain format.
*
* <p>This is needed because appspot.com only has a single wildcard SSL certificate, so the native
* App Engine URLs of the form service.projectid.appspot.com or
* version.service.projectid.appspot.com won't work over HTTPS when being fetched from outside of
* GCP. The work-around is to change all of the "." subdomain markers to "-dot-". E.g.:
*
* <ul>
* <li>tools.projectid.appspot.com --&gt; tools-dot-projectid.appspot.com
* <li>version.backend.projectid.appspot.com --&gt;
* version-dot-backend-dot-projectid.appspot.com
* </ul>
*
* @see <a
* href="https://cloud.google.com/appengine/docs/standard/java/how-requests-are-routed">How
* App Engine requests are routed</a>
*/
String convertToSingleSubdomain(String hostname);
/** Set the number of instances at runtime for a given service and version. */
void setNumInstances(String service, String version, long numInstances);
}

View file

@ -1,77 +0,0 @@
// Copyright 2018 The Nomulus Authors. 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 google.registry.util;
import static com.google.common.base.Preconditions.checkArgument;
import static google.registry.util.PreconditionsUtils.checkArgumentNotNull;
import com.google.appengine.api.modules.ModulesService;
import com.google.common.flogger.FluentLogger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.inject.Inject;
/** A wrapper for {@link ModulesService} that provides a saner API. */
public class AppEngineServiceUtilsImpl implements AppEngineServiceUtils {
private static final Pattern APPSPOT_HOSTNAME_PATTERN =
Pattern.compile("^(.*)\\.appspot\\.com$");
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
private final ModulesService modulesService;
@Inject
public AppEngineServiceUtilsImpl(ModulesService modulesService) {
this.modulesService = modulesService;
}
@Override
public String getServiceHostname(String service) {
// This will be in the format "version.service.projectid.appspot.com"
String hostnameWithVersion = modulesService.getVersionHostname(service, null);
// Strip off the version and return just "service.projectid.appspot.com"
return hostnameWithVersion.replaceFirst("^[^.]+\\.", "");
}
@Override
public String getCurrentVersionHostname(String service) {
return modulesService.getVersionHostname(service, null);
}
@Override
public String getVersionHostname(String service, String version) {
checkArgumentNotNull(version, "Must specify the version");
return modulesService.getVersionHostname(service, version);
}
@Override
public void setNumInstances(String service, String version, long numInstances) {
checkArgumentNotNull(service, "Must specify the service");
checkArgumentNotNull(version, "Must specify the version");
checkArgument(numInstances > 0, "Number of instances must be greater than 0");
modulesService.setNumInstances(service, version, numInstances);
}
@Override
public String convertToSingleSubdomain(String hostname) {
Matcher matcher = APPSPOT_HOSTNAME_PATTERN.matcher(hostname);
if (!matcher.matches()) {
logger.atWarning().log(
"Skipping conversion because hostname '%s' can't be parsed.", hostname);
return hostname;
}
return matcher.group(1).replace(".", "-dot-") + ".appspot.com";
}
}

View file

@ -14,8 +14,6 @@
package google.registry.util;
import com.google.appengine.api.modules.ModulesService;
import com.google.appengine.api.modules.ModulesServiceFactory;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import dagger.Binds;
@ -41,17 +39,6 @@ public abstract class UtilsModule {
@Singleton
abstract Clock provideClock(SystemClock clock);
@Provides
@Singleton
static ModulesService provideModulesService() {
return ModulesServiceFactory.getModulesService();
}
@Binds
@Singleton
abstract AppEngineServiceUtils provideAppEngineServiceUtils(
AppEngineServiceUtilsImpl appEngineServiceUtilsImpl);
@Singleton
@Provides
public static SecureRandom provideSecureRandom() {

View file

@ -1,144 +0,0 @@
// Copyright 2018 The Nomulus Authors. 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 google.registry.util;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import com.google.appengine.api.modules.ModulesService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
/** Unit tests for {@link AppEngineServiceUtilsImpl}. */
@ExtendWith(MockitoExtension.class)
class AppEngineServiceUtilsImplTest {
@Mock private ModulesService modulesService;
private AppEngineServiceUtils appEngineServiceUtils;
@BeforeEach
void beforeEach() {
appEngineServiceUtils = new AppEngineServiceUtilsImpl(modulesService);
}
@Test
void test_getServiceHostname_doesntIncludeVersionId() {
when(modulesService.getVersionHostname(anyString(), isNull()))
.thenReturn("1234.servicename.projectid.appspot.fake");
assertThat(appEngineServiceUtils.getServiceHostname("servicename"))
.isEqualTo("servicename.projectid.appspot.fake");
}
@Test
void test_getVersionHostname_doesIncludeVersionId() {
when(modulesService.getVersionHostname(anyString(), isNull()))
.thenReturn("1234.servicename.projectid.appspot.fake");
assertThat(appEngineServiceUtils.getCurrentVersionHostname("servicename"))
.isEqualTo("1234.servicename.projectid.appspot.fake");
}
@Test
void test_getVersionHostname_worksWithVersionId() {
when(modulesService.getVersionHostname(anyString(), eq("2345")))
.thenReturn("2345.servicename.projectid.appspot.fake");
assertThat(appEngineServiceUtils.getVersionHostname("servicename", "2345"))
.isEqualTo("2345.servicename.projectid.appspot.fake");
}
@Test
void test_getVersionHostname_throwsWhenVersionIdIsNull() {
IllegalArgumentException thrown =
assertThrows(
IllegalArgumentException.class,
() -> appEngineServiceUtils.getVersionHostname("servicename", null));
assertThat(thrown).hasMessageThat().isEqualTo("Must specify the version");
}
@Test
void test_setNumInstances_worksWithValidParameters() {
appEngineServiceUtils.setNumInstances("service", "version", 10L);
verify(modulesService, times(1)).setNumInstances("service", "version", 10L);
}
@Test
void test_setNumInstances_throwsWhenServiceIsNull() {
IllegalArgumentException thrown =
assertThrows(
IllegalArgumentException.class,
() -> appEngineServiceUtils.setNumInstances(null, "version", 10L));
assertThat(thrown).hasMessageThat().isEqualTo("Must specify the service");
}
@Test
void test_setNumInstances_throwsWhenVersionIsNull() {
IllegalArgumentException thrown =
assertThrows(
IllegalArgumentException.class,
() -> appEngineServiceUtils.setNumInstances("service", null, 10L));
assertThat(thrown).hasMessageThat().isEqualTo("Must specify the version");
}
@Test
void test_setNumInstances_throwsWhenNumInstancesIsInvalid() {
IllegalArgumentException thrown =
assertThrows(
IllegalArgumentException.class,
() -> appEngineServiceUtils.setNumInstances("service", "version", -10L));
assertThat(thrown).hasMessageThat().isEqualTo("Number of instances must be greater than 0");
}
@Test
void test_convertToSingleSubdomain_doesNothingWithoutServiceOrHostname() {
assertThat(appEngineServiceUtils.convertToSingleSubdomain("projectid.appspot.com"))
.isEqualTo("projectid.appspot.com");
}
@Test
void test_convertToSingleSubdomain_doesNothingWhenItCannotParseCorrectly() {
assertThat(appEngineServiceUtils.convertToSingleSubdomain("garbage.notrealhost.example"))
.isEqualTo("garbage.notrealhost.example");
}
@Test
void test_convertToSingleSubdomain_convertsWithServiceName() {
assertThat(appEngineServiceUtils.convertToSingleSubdomain("service.projectid.appspot.com"))
.isEqualTo("service-dot-projectid.appspot.com");
}
@Test
void test_convertToSingleSubdomain_convertsWithVersionAndServiceName() {
assertThat(
appEngineServiceUtils.convertToSingleSubdomain("version.service.projectid.appspot.com"))
.isEqualTo("version-dot-service-dot-projectid.appspot.com");
}
@Test
void test_convertToSingleSubdomain_convertsWithInstanceAndVersionAndServiceName() {
assertThat(
appEngineServiceUtils.convertToSingleSubdomain(
"instanceid.version.service.projectid.appspot.com"))
.isEqualTo("instanceid-dot-version-dot-service-dot-projectid.appspot.com");
}
}