|
| 1 | +/* |
| 2 | + * Copyright (c) 2023 Oracle and/or its affiliates. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.helidon.webserver.tests.observe.metrics; |
| 18 | + |
| 19 | +import java.net.URI; |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.Optional; |
| 24 | +import java.util.Set; |
| 25 | + |
| 26 | +import io.helidon.common.media.type.MediaTypes; |
| 27 | +import io.helidon.http.Status; |
| 28 | +import io.helidon.openapi.OpenApiFeature; |
| 29 | +import io.helidon.security.EndpointConfig; |
| 30 | +import io.helidon.security.Security; |
| 31 | +import io.helidon.security.providers.httpauth.HttpBasicAuthProvider; |
| 32 | +import io.helidon.security.providers.httpauth.SecureUserStore; |
| 33 | +import io.helidon.webclient.http1.Http1Client; |
| 34 | +import io.helidon.webclient.http1.Http1ClientResponse; |
| 35 | +import io.helidon.webclient.security.WebClientSecurity; |
| 36 | +import io.helidon.webserver.WebServerConfig; |
| 37 | +import io.helidon.webserver.context.ContextFeature; |
| 38 | +import io.helidon.webserver.observe.ObserveFeature; |
| 39 | +import io.helidon.webserver.security.SecurityFeature; |
| 40 | +import io.helidon.webserver.testing.junit5.ServerTest; |
| 41 | +import io.helidon.webserver.testing.junit5.SetUpServer; |
| 42 | + |
| 43 | +import org.junit.jupiter.api.Test; |
| 44 | + |
| 45 | +import static org.hamcrest.CoreMatchers.is; |
| 46 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 47 | + |
| 48 | +@ServerTest |
| 49 | +class ObserveSecurityTest { |
| 50 | + private static final Map<String, MyUser> USERS = new HashMap<>(); |
| 51 | + |
| 52 | + private final Http1Client client; |
| 53 | + |
| 54 | + ObserveSecurityTest(URI uri) { |
| 55 | + USERS.put("jack", new MyUser("jack", "password".toCharArray(), Set.of("user"))); |
| 56 | + |
| 57 | + Security security = Security.builder() |
| 58 | + .addProvider(HttpBasicAuthProvider.builder()) |
| 59 | + .build(); |
| 60 | + |
| 61 | + WebClientSecurity securityService = WebClientSecurity.create(security); |
| 62 | + |
| 63 | + client = Http1Client.builder() |
| 64 | + .baseUri(uri) |
| 65 | + .addService(securityService) |
| 66 | + .build(); |
| 67 | + } |
| 68 | + |
| 69 | + @SetUpServer |
| 70 | + static void setup(WebServerConfig.Builder server) { |
| 71 | + server.routing(routing -> routing |
| 72 | + .addFeature(ObserveFeature.create()) |
| 73 | + .addFeature(OpenApiFeature.create()) |
| 74 | + .addFeature(ContextFeature.create()) |
| 75 | + .addFeature(buildWebSecurity().securityDefaults(SecurityFeature.authenticate())) |
| 76 | + .get("/observe/metrics", SecurityFeature.rolesAllowed("user")) |
| 77 | + .get("/openapi", SecurityFeature.rolesAllowed("user"))); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + void testMetrics() { |
| 82 | + testSecureEndpoint("/observe/metrics"); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + void testOpenApi() { |
| 87 | + testSecureEndpoint("/openapi"); |
| 88 | + } |
| 89 | + |
| 90 | + void testSecureEndpoint(String uri) { |
| 91 | + try (Http1ClientResponse response = client.get().uri(uri).request()) { |
| 92 | + assertThat(response.status(), is(Status.UNAUTHORIZED_401)); |
| 93 | + } |
| 94 | + |
| 95 | + try (Http1ClientResponse response = client.get() |
| 96 | + .uri(uri) |
| 97 | + .property(EndpointConfig.PROPERTY_OUTBOUND_ID, "jack") |
| 98 | + .property(EndpointConfig.PROPERTY_OUTBOUND_SECRET, "password") |
| 99 | + .request()) { |
| 100 | + assertThat(response.status(), is(Status.OK_200)); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + private static SecurityFeature buildWebSecurity() { |
| 105 | + Security security = Security.builder() |
| 106 | + .addAuthenticationProvider( |
| 107 | + HttpBasicAuthProvider.builder() |
| 108 | + .realm("helidon") |
| 109 | + .userStore(buildUserStore()), |
| 110 | + "http-basic-auth") |
| 111 | + .build(); |
| 112 | + return SecurityFeature.create(security); |
| 113 | + } |
| 114 | + |
| 115 | + private static SecureUserStore buildUserStore() { |
| 116 | + return login -> Optional.ofNullable(USERS.get(login)); |
| 117 | + } |
| 118 | + |
| 119 | + private record MyUser(String login, char[] password, Set<String> roles) implements SecureUserStore.User { |
| 120 | + |
| 121 | + @Override |
| 122 | + public boolean isPasswordValid(char[] password) { |
| 123 | + return Arrays.equals(password(), password); |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments