|
| 1 | +/* |
| 2 | + * Copyright (C) 2020 Graylog, Inc. |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the Server Side Public License, version 1, |
| 6 | + * as published by MongoDB, Inc. |
| 7 | + * |
| 8 | + * This program is distributed in the hope that it will be useful, |
| 9 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | + * Server Side Public License for more details. |
| 12 | + * |
| 13 | + * You should have received a copy of the Server Side Public License |
| 14 | + * along with this program. If not, see |
| 15 | + * <http://www.mongodb.com/licensing/server-side-public-license>. |
| 16 | + */ |
| 17 | +package org.graylog2.inputs; |
| 18 | + |
| 19 | +import net.bytebuddy.utility.RandomString; |
| 20 | +import org.assertj.core.api.Assertions; |
| 21 | +import org.graylog.testing.completebackend.Lifecycle; |
| 22 | +import org.graylog.testing.completebackend.apis.GraylogApiResponse; |
| 23 | +import org.graylog.testing.completebackend.apis.GraylogApis; |
| 24 | +import org.graylog.testing.completebackend.apis.Users; |
| 25 | +import org.graylog.testing.containermatrix.SearchServer; |
| 26 | +import org.graylog.testing.containermatrix.annotations.ContainerMatrixTest; |
| 27 | +import org.graylog.testing.containermatrix.annotations.ContainerMatrixTestsConfiguration; |
| 28 | +import org.graylog2.shared.security.RestPermissions; |
| 29 | +import org.junit.jupiter.api.AfterAll; |
| 30 | +import org.junit.jupiter.api.BeforeAll; |
| 31 | + |
| 32 | +import java.util.Arrays; |
| 33 | +import java.util.List; |
| 34 | +import java.util.Map; |
| 35 | +import java.util.Set; |
| 36 | + |
| 37 | +import static org.hamcrest.CoreMatchers.equalTo; |
| 38 | + |
| 39 | +@ContainerMatrixTestsConfiguration(serverLifecycle = Lifecycle.VM, searchVersions = SearchServer.DATANODE_DEV) |
| 40 | +public class InputPermissionsIT { |
| 41 | + |
| 42 | + private final GraylogApis apis; |
| 43 | + |
| 44 | + private GraylogApiResponse roleInputsReader; |
| 45 | + private GraylogApiResponse roleInputsCreator; |
| 46 | + private GraylogApiResponse roleRestrictedInputsCreator; |
| 47 | + |
| 48 | + private Users.User inputsReader; |
| 49 | + private Users.User inputsCreator; |
| 50 | + private Users.User restrictedInputsCreator; |
| 51 | + |
| 52 | + public InputPermissionsIT(GraylogApis apis) { |
| 53 | + this.apis = apis; |
| 54 | + } |
| 55 | + |
| 56 | + @BeforeAll |
| 57 | + void setUp() { |
| 58 | + roleInputsReader = apis.roles().create("custom_inputs_reader", "inputs reader can only see inputs", Set.of( |
| 59 | + RestPermissions.INPUTS_READ |
| 60 | + ), false); |
| 61 | + inputsReader = createUser("inputs.reader", roleInputsReader); |
| 62 | + |
| 63 | + roleInputsCreator = apis.roles().create("custom_inputs_creator", "inputs creator can only create inputs", Set.of( |
| 64 | + RestPermissions.INPUTS_READ, |
| 65 | + RestPermissions.INPUTS_CREATE, |
| 66 | + RestPermissions.INPUT_TYPES_CREATE |
| 67 | + ), false); |
| 68 | + inputsCreator = createUser("inputs.creator", roleInputsCreator); |
| 69 | + |
| 70 | + roleRestrictedInputsCreator = apis.roles().create("custom_restricted_inputs_creator", "inputs creator can only create certain inputs", Set.of( |
| 71 | + RestPermissions.INPUTS_READ, |
| 72 | + RestPermissions.INPUTS_CREATE, |
| 73 | + RestPermissions.INPUT_TYPES_CREATE + ":org.graylog2.inputs.random.FakeHttpMessageInput", |
| 74 | + RestPermissions.INPUT_TYPES_CREATE + ":org.graylog2.inputs.gelf.tcp.GELFTCPInput" |
| 75 | + |
| 76 | + ), false); |
| 77 | + restrictedInputsCreator = createUser("restricted.inputs.creator", roleRestrictedInputsCreator); |
| 78 | + |
| 79 | + waitForRolesCacheRefresh(); |
| 80 | + |
| 81 | + apis.users().createUser(inputsReader); |
| 82 | + apis.users().createUser(inputsCreator); |
| 83 | + apis.users().createUser(restrictedInputsCreator); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Roles are stored in mongodb, but the auth backend is refreshing those only once every second. If we trigger a call |
| 88 | + * before the role is refreshed, we may get weird results. |
| 89 | + * @see org.graylog2.security.InMemoryRolePermissionResolver |
| 90 | + */ |
| 91 | + private static void waitForRolesCacheRefresh() { |
| 92 | + try { |
| 93 | + // This is naive and wrong, but very simple to implement. Another approach would be to have a fingerprint of |
| 94 | + // roles cache, similarly to StreamRouterEngine and its fingerprint. |
| 95 | + Thread.sleep(1000); |
| 96 | + } catch (InterruptedException e) { |
| 97 | + throw new RuntimeException(e); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private Users.User createUser(String username, GraylogApiResponse... roles) { |
| 102 | + return new Users.User(username, RandomString.make(), "<Generated>", username, |
| 103 | + username + "@graylog", false, 30_0000, "Europe/Vienna", |
| 104 | + Arrays.stream(roles).map(role -> role.properJSONPath().read("name", String.class)).toList(), List.of()); |
| 105 | + } |
| 106 | + |
| 107 | + @AfterAll |
| 108 | + void tearDown() { |
| 109 | + apis.users().deleteUser(inputsReader.username()); |
| 110 | + apis.users().deleteUser(inputsCreator.username()); |
| 111 | + apis.users().deleteUser(restrictedInputsCreator.username()); |
| 112 | + |
| 113 | + apis.roles().delete(roleInputsReader.properJSONPath().read("name", String.class)); |
| 114 | + apis.roles().delete(roleInputsCreator.properJSONPath().read("name", String.class)); |
| 115 | + apis.roles().delete(roleRestrictedInputsCreator.properJSONPath().read("name", String.class)); |
| 116 | + } |
| 117 | + |
| 118 | + @ContainerMatrixTest |
| 119 | + void testPermittedInputCreationAndReading() { |
| 120 | + String inputId = apis.forUser(inputsCreator).inputs().createGlobalInput("testInput", |
| 121 | + "org.graylog2.inputs.random.FakeHttpMessageInput", |
| 122 | + Map.of("sleep", 30, |
| 123 | + "sleep_deviation", 30, |
| 124 | + "source", "example.org")); |
| 125 | + apis.waitFor(() -> |
| 126 | + apis.inputs().getInputState(inputId) |
| 127 | + .extract().body().jsonPath().get("state") |
| 128 | + .equals("RUNNING"), |
| 129 | + "Timed out waiting for HTTP Random Message Input to become available"); |
| 130 | + |
| 131 | + apis.forUser(inputsReader).inputs().getInput(inputId).assertThat().body("id", equalTo(inputId)); |
| 132 | + |
| 133 | + apis.inputs().deleteInput(inputId); |
| 134 | + |
| 135 | + String inputId2 = apis.forUser(restrictedInputsCreator).inputs().createGlobalInput("testInput", |
| 136 | + "org.graylog2.inputs.random.FakeHttpMessageInput", |
| 137 | + Map.of("sleep", 30, |
| 138 | + "sleep_deviation", 30, |
| 139 | + "source", "example.org")); |
| 140 | + apis.waitFor(() -> |
| 141 | + apis.inputs().getInputState(inputId2) |
| 142 | + .extract().body().jsonPath().get("state") |
| 143 | + .equals("RUNNING"), |
| 144 | + "Timed out waiting for HTTP Random Message Input to become available"); |
| 145 | + |
| 146 | + apis.forUser(inputsReader).inputs().getInput(inputId2).assertThat().body("id", equalTo(inputId2)); |
| 147 | + |
| 148 | + apis.inputs().deleteInput(inputId2); |
| 149 | + } |
| 150 | + |
| 151 | + @ContainerMatrixTest |
| 152 | + void testRestrictedInputCreationAndReading() { |
| 153 | + String inputId = apis.forUser(inputsCreator).inputs().createGlobalInput("testInput", |
| 154 | + "org.graylog2.inputs.misc.jsonpath.JsonPathInput", |
| 155 | + Map.of("target_url", "https://example.org", |
| 156 | + "interval", 10, |
| 157 | + "timeunit", "MINUTES", |
| 158 | + "path", "$.data", |
| 159 | + "source", "messagesource")); |
| 160 | + apis.waitFor(() -> |
| 161 | + apis.inputs().getInputState(inputId) |
| 162 | + .extract().body().jsonPath().get("state") |
| 163 | + .equals("RUNNING"), |
| 164 | + "Timed out waiting for Json Input to become available"); |
| 165 | + |
| 166 | + apis.forUser(inputsReader).inputs().getInput(inputId).assertThat().body("id", equalTo(inputId)); |
| 167 | + |
| 168 | + apis.inputs().deleteInput(inputId); |
| 169 | + |
| 170 | + Assertions.assertThatThrownBy(() -> apis.forUser(restrictedInputsCreator).inputs().createGlobalInput("testInput", |
| 171 | + "org.graylog2.inputs.misc.jsonpath.JsonPathInput", |
| 172 | + Map.of("target_url", "https://example.org", |
| 173 | + "interval", 10, |
| 174 | + "timeunit", "MINUTES", |
| 175 | + "path", "$.data", |
| 176 | + "source", "messagesource"))) |
| 177 | + .isInstanceOf(AssertionError.class) |
| 178 | + .hasMessageContaining("Expected status code <201> but was <403>"); |
| 179 | + |
| 180 | + } |
| 181 | + |
| 182 | + @ContainerMatrixTest |
| 183 | + void testInputTypesRead() { |
| 184 | + final GraylogApiResponse inputTypesForReader = apis.forUser(inputsReader).inputs().getInputTypes(); |
| 185 | + final Map<String, String> typesReader = inputTypesForReader.properJSONPath().read("types"); |
| 186 | + Assertions.assertThat(typesReader).isEmpty(); |
| 187 | + |
| 188 | + final GraylogApiResponse inputTypesForRestrictedCreator = apis.forUser(restrictedInputsCreator).inputs().getInputTypes(); |
| 189 | + final Map<String, String> typesRestricted = inputTypesForRestrictedCreator.properJSONPath().read("types"); |
| 190 | + Assertions.assertThat(typesRestricted).containsOnlyKeys( |
| 191 | + "org.graylog2.inputs.random.FakeHttpMessageInput", |
| 192 | + "org.graylog2.inputs.gelf.tcp.GELFTCPInput" |
| 193 | + ); |
| 194 | + |
| 195 | + final GraylogApiResponse inputTypesForCreator = apis.forUser(inputsCreator).inputs().getInputTypes(); |
| 196 | + final Map<String, String> typesCreator = inputTypesForCreator.properJSONPath().read("types"); |
| 197 | + Assertions.assertThat(typesCreator).hasSizeGreaterThan(5); |
| 198 | + } |
| 199 | +} |
0 commit comments