|
23 | 23 | import java.util.Locale;
|
24 | 24 |
|
25 | 25 | import static org.elasticsearch.common.Strings.capitalize;
|
| 26 | +import static org.hamcrest.Matchers.allOf; |
26 | 27 | import static org.hamcrest.Matchers.containsInAnyOrder;
|
27 | 28 | import static org.hamcrest.Matchers.containsString;
|
28 | 29 | import static org.hamcrest.Matchers.emptyIterable;
|
| 30 | +import static org.hamcrest.Matchers.endsWith; |
29 | 31 | import static org.hamcrest.Matchers.equalTo;
|
30 | 32 | import static org.hamcrest.Matchers.is;
|
| 33 | +import static org.hamcrest.Matchers.startsWith; |
31 | 34 |
|
32 | 35 | /**
|
33 | 36 | * JWT realm settings unit tests. These are low-level tests against ES settings parsers.
|
@@ -588,4 +591,59 @@ public void testRequiredClaimsCannotBeEmpty() {
|
588 | 591 |
|
589 | 592 | assertThat(e.getMessage(), containsString("required claim [" + fullSettingKey + "] cannot be empty"));
|
590 | 593 | }
|
| 594 | + |
| 595 | + public void testInvalidProxySchemeThrowsError() { |
| 596 | + final String scheme = randomBoolean() ? "https" : randomAlphaOfLengthBetween(3, 8); |
| 597 | + final String realmName = randomAlphaOfLengthBetween(3, 8); |
| 598 | + final String proxySchemeSettingKey = RealmSettings.getFullSettingKey(realmName, JwtRealmSettings.HTTP_PROXY_SCHEME); |
| 599 | + final Settings settings = Settings.builder().put(proxySchemeSettingKey, scheme).build(); |
| 600 | + |
| 601 | + final RealmConfig realmConfig = buildRealmConfig(JwtRealmSettings.TYPE, realmName, settings, randomInt()); |
| 602 | + final IllegalArgumentException e = expectThrows( |
| 603 | + IllegalArgumentException.class, |
| 604 | + () -> realmConfig.getSetting(JwtRealmSettings.HTTP_PROXY_SCHEME) |
| 605 | + ); |
| 606 | + |
| 607 | + assertThat( |
| 608 | + e.getMessage(), |
| 609 | + equalTo(Strings.format("Invalid value [%s] for [%s]. Allowed values are [http].", scheme, proxySchemeSettingKey)) |
| 610 | + ); |
| 611 | + } |
| 612 | + |
| 613 | + public void testInvalidProxyHostThrowsError() { |
| 614 | + final int proxyPort = randomIntBetween(1, 65535); |
| 615 | + final String realmName = randomAlphaOfLengthBetween(3, 8); |
| 616 | + final String proxyPortSettingKey = RealmSettings.getFullSettingKey(realmName, JwtRealmSettings.HTTP_PROXY_PORT); |
| 617 | + final String proxyHostSettingKey = RealmSettings.getFullSettingKey(realmName, JwtRealmSettings.HTTP_PROXY_HOST); |
| 618 | + final Settings settings = Settings.builder().put(proxyHostSettingKey, "not a url").put(proxyPortSettingKey, proxyPort).build(); |
| 619 | + |
| 620 | + final RealmConfig realmConfig = buildRealmConfig(JwtRealmSettings.TYPE, realmName, settings, randomInt()); |
| 621 | + final IllegalArgumentException e = expectThrows( |
| 622 | + IllegalArgumentException.class, |
| 623 | + () -> realmConfig.getSetting(JwtRealmSettings.HTTP_PROXY_HOST) |
| 624 | + ); |
| 625 | + |
| 626 | + assertThat( |
| 627 | + e.getMessage(), |
| 628 | + allOf(startsWith(Strings.format("HTTP host for hostname [not a url] (from [%s])", proxyHostSettingKey)), endsWith("is invalid")) |
| 629 | + ); |
| 630 | + } |
| 631 | + |
| 632 | + public void testInvalidProxyPortThrowsError() { |
| 633 | + final int proxyPort = randomFrom(randomIntBetween(Integer.MIN_VALUE, -1), randomIntBetween(65536, Integer.MAX_VALUE)); |
| 634 | + final String realmName = randomAlphaOfLengthBetween(3, 8); |
| 635 | + final String proxyPortSettingKey = RealmSettings.getFullSettingKey(realmName, JwtRealmSettings.HTTP_PROXY_PORT); |
| 636 | + final Settings settings = Settings.builder().put(proxyPortSettingKey, proxyPort).build(); |
| 637 | + |
| 638 | + final RealmConfig realmConfig = buildRealmConfig(JwtRealmSettings.TYPE, realmName, settings, randomInt()); |
| 639 | + final IllegalArgumentException e = expectThrows( |
| 640 | + IllegalArgumentException.class, |
| 641 | + () -> realmConfig.getSetting(JwtRealmSettings.HTTP_PROXY_PORT) |
| 642 | + ); |
| 643 | + |
| 644 | + assertThat( |
| 645 | + e.getMessage(), |
| 646 | + startsWith(Strings.format("Failed to parse value [%d] for setting [%s]", proxyPort, proxyPortSettingKey)) |
| 647 | + ); |
| 648 | + } |
591 | 649 | }
|
0 commit comments