Block ability to remove allowed TLDs from the registrar console

This is a temporary measure until we implement access control for Support.

Once we implement access control, we will only block Support from removing TLDs
on production.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=222180321
This commit is contained in:
guyben 2018-11-19 19:50:02 -08:00 committed by jianglai
parent f46f817f9e
commit 274b7115d4
2 changed files with 36 additions and 5 deletions

View file

@ -29,6 +29,7 @@ import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Multimap; import com.google.common.collect.Multimap;
import com.google.common.collect.Sets;
import com.google.common.collect.Streams; import com.google.common.collect.Streams;
import com.google.common.flogger.FluentLogger; import com.google.common.flogger.FluentLogger;
import google.registry.config.RegistryConfig.Config; import google.registry.config.RegistryConfig.Config;
@ -298,11 +299,19 @@ public class RegistrarSettingsAction implements Runnable, JsonActionRunner.JsonA
RegistrarFormFields.ALLOWED_TLDS_FIELD.extractUntyped(args).orElse(ImmutableSet.of()); RegistrarFormFields.ALLOWED_TLDS_FIELD.extractUntyped(args).orElse(ImmutableSet.of());
if (!updatedAllowedTlds.equals(existingRegistrarObj.getAllowedTlds())) { if (!updatedAllowedTlds.equals(existingRegistrarObj.getAllowedTlds())) {
// Only admin is allowed to update allowed TLDs // Only admin is allowed to update allowed TLDs
if (roles.contains(Role.ADMIN)) { if (!roles.contains(Role.ADMIN)) {
builder.setAllowedTlds(updatedAllowedTlds);
} else {
throw new ForbiddenException("Only admin can update allowed TLDs."); throw new ForbiddenException("Only admin can update allowed TLDs.");
} }
// Temporarily block anyone from removing an allowed TLD.
// This is so we can start having Support users use the console in production before we finish
// implementing configurable access control.
// TODO(b/119549884): remove this code once configurable access control is implemented.
Set<String> removedTlds =
Sets.difference(existingRegistrarObj.getAllowedTlds(), updatedAllowedTlds);
if (!removedTlds.isEmpty()) {
throw new ForbiddenException("Can't remove allowed TLDs using the console.");
}
builder.setAllowedTlds(updatedAllowedTlds);
} }
} }

View file

@ -302,7 +302,7 @@ public class RegistrarSettingsActionTest extends RegistrarSettingsActionTestCase
setUserAdmin(); setUserAdmin();
doTestUpdate( doTestUpdate(
Registrar::getAllowedTlds, Registrar::getAllowedTlds,
ImmutableSet.of("newtld"), ImmutableSet.of("newtld", "currenttld"),
(builder, s) -> builder.setAllowedTlds(s)); (builder, s) -> builder.setAllowedTlds(s));
assertMetric(CLIENT_ID, "update", "[ADMIN]", "SUCCESS"); assertMetric(CLIENT_ID, "update", "[ADMIN]", "SUCCESS");
} }
@ -340,7 +340,7 @@ public class RegistrarSettingsActionTest extends RegistrarSettingsActionTestCase
ImmutableMap.of( ImmutableMap.of(
"lastUpdateTime", getLastUpdateTime(), "lastUpdateTime", getLastUpdateTime(),
"emailAddress", "abc@def.com", "emailAddress", "abc@def.com",
"allowedTlds", ImmutableList.of("invalidtld")))); "allowedTlds", ImmutableList.of("invalidtld", "currenttld"))));
assertThat(response) assertThat(response)
.containsExactly( .containsExactly(
"status", "ERROR", "status", "ERROR",
@ -350,6 +350,28 @@ public class RegistrarSettingsActionTest extends RegistrarSettingsActionTestCase
assertNoTasksEnqueued("sheet"); assertNoTasksEnqueued("sheet");
} }
@Test
public void testUpdate_allowedTlds_failedWhenRemovingTld() {
setUserAdmin();
Map<String, Object> response =
action.handleJsonRequest(
ImmutableMap.of(
"op", "update",
"id", CLIENT_ID,
"args",
ImmutableMap.of(
"lastUpdateTime", getLastUpdateTime(),
"emailAddress", "abc@def.com",
"allowedTlds", ImmutableList.of("newTld"))));
assertThat(response)
.containsExactly(
"status", "ERROR",
"results", ImmutableList.of(),
"message", "Can't remove allowed TLDs using the console.");
assertMetric(CLIENT_ID, "update", "[ADMIN]", "ERROR: ForbiddenException");
assertNoTasksEnqueued("sheet");
}
@Test @Test
public void testUpdate_allowedTlds_noChange_successWhenUserIsNotAdmin() { public void testUpdate_allowedTlds_noChange_successWhenUserIsNotAdmin() {
Map<String, Object> response = Map<String, Object> response =