Skip to content

Commit 1475ec2

Browse files
committed
SCANJLIB-263 Don't let people use the region value 'global'
1 parent 4b06cfc commit 1475ec2

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

lib/src/main/java/org/sonarsource/scanner/lib/internal/endpoint/OfficialSonarQubeCloudInstance.java

+16-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.Optional;
2525
import java.util.Set;
2626
import java.util.stream.Collectors;
27+
import java.util.stream.Stream;
2728
import javax.annotation.Nullable;
2829
import org.apache.commons.lang3.StringUtils;
2930

@@ -38,16 +39,28 @@ public enum OfficialSonarQubeCloudInstance {
3839
this.endpoint = new ScannerEndpoint(webEndpoint, apiEndpoint, true);
3940
}
4041

41-
public static Set<String> getRegionCodes() {
42-
return Arrays.stream(OfficialSonarQubeCloudInstance.values()).filter(r -> r != GLOBAL).map(Enum::name).map(s -> s.toLowerCase(Locale.ENGLISH)).collect(Collectors.toSet());
42+
public static Set<String> getRegionCodesWithoutGlobal() {
43+
return getRegionsWithoutGlobal().map(Enum::name).map(s -> s.toLowerCase(Locale.ENGLISH)).collect(Collectors.toSet());
44+
}
45+
46+
/**
47+
* For now, we are not sure the default region will be called "global" so don't let users use this enum value
48+
*/
49+
private static Stream<OfficialSonarQubeCloudInstance> getRegionsWithoutGlobal() {
50+
return Arrays.stream(OfficialSonarQubeCloudInstance.values()).filter(r -> r != GLOBAL);
4351
}
4452

4553
public static Optional<OfficialSonarQubeCloudInstance> fromRegionCode(@Nullable String regionCode) {
4654
if (StringUtils.isBlank(regionCode)) {
4755
return Optional.of(GLOBAL);
4856
}
4957
try {
50-
return Optional.of(OfficialSonarQubeCloudInstance.valueOf(regionCode.toUpperCase(Locale.ENGLISH)));
58+
var value = OfficialSonarQubeCloudInstance.valueOf(regionCode.toUpperCase(Locale.ENGLISH));
59+
if (value == GLOBAL) {
60+
// For now, we are not sure the default region will be called "global" so don't let users use this enum value
61+
return Optional.empty();
62+
}
63+
return Optional.of(value);
5164
} catch (IllegalArgumentException e) {
5265
return Optional.empty();
5366
}

lib/src/main/java/org/sonarsource/scanner/lib/internal/endpoint/ScannerEndpointResolver.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ private static ScannerEndpoint resolveSonarQubeCloudEndpoint(Map<String, String>
5151
return OfficialSonarQubeCloudInstance.fromRegionCode(regionCode)
5252
.orElseThrow(() -> new MessageException(
5353
format("Invalid region '%s'. Valid regions are: %s. Please check the '%s' property or the '%s' environment variable.",
54-
regionCode, StringUtils.join(OfficialSonarQubeCloudInstance.getRegionCodes().stream().map(r -> "'" + r + "'").collect(toList()), ", "),
54+
regionCode, StringUtils.join(OfficialSonarQubeCloudInstance.getRegionCodesWithoutGlobal().stream().map(r -> "'" + r + "'").collect(toList()), ", "),
5555
ScannerProperties.SONAR_REGION, EnvironmentConfig.REGION_ENV_VARIABLE)))
5656
.getEndpoint();
5757
}

lib/src/test/java/org/sonarsource/scanner/lib/internal/endpoint/OfficialSonarQubeCloudInstanceTest.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class OfficialSonarQubeCloudInstanceTest {
2727

2828
@Test
2929
void shouldListRegionsCodes() {
30-
assertThat(OfficialSonarQubeCloudInstance.getRegionCodes()).containsExactlyInAnyOrder("us");
30+
assertThat(OfficialSonarQubeCloudInstance.getRegionCodesWithoutGlobal()).containsExactlyInAnyOrder("us");
3131
}
3232

3333
@Test
@@ -37,6 +37,8 @@ void shouldGetInstanceFromRegionCodeIgnoringCase() {
3737
assertThat(OfficialSonarQubeCloudInstance.fromRegionCode("")).contains(OfficialSonarQubeCloudInstance.GLOBAL);
3838
assertThat(OfficialSonarQubeCloudInstance.fromRegionCode(null)).contains(OfficialSonarQubeCloudInstance.GLOBAL);
3939
assertThat(OfficialSonarQubeCloudInstance.fromRegionCode("foo")).isEmpty();
40+
// For now, we are not sure the default region will be called "global" so don't let users use this enum value
41+
assertThat(OfficialSonarQubeCloudInstance.fromRegionCode("global")).isEmpty();
4042
}
4143

4244
@Test

0 commit comments

Comments
 (0)