|
24 | 24 | import static org.hamcrest.CoreMatchers.not; |
25 | 25 | import static org.hamcrest.CoreMatchers.nullValue; |
26 | 26 | import static org.hamcrest.MatcherAssert.assertThat; |
| 27 | +import static org.mockito.ArgumentMatchers.anyString; |
| 28 | +import static org.mockito.Mockito.mock; |
| 29 | +import static org.mockito.Mockito.never; |
| 30 | +import static org.mockito.Mockito.verify; |
| 31 | +import static org.mockito.Mockito.when; |
27 | 32 |
|
28 | 33 | import com.google.common.net.HttpHeaders; |
29 | 34 | import java.io.IOException; |
@@ -121,6 +126,79 @@ public void testDownloadDaemonLogFilePathOutsideLogRoot() throws IOException { |
121 | 126 | } |
122 | 127 | } |
123 | 128 |
|
| 129 | + @Test |
| 130 | + public void testDownloadLogFileUnauthorizedUserDoesNotChangeLogFilePermission() throws IOException { |
| 131 | + try (TmpPath rootPath = new TmpPath()) { |
| 132 | + Path daemonLogRoot = rootPath.getFile().toPath().resolve("logs"); |
| 133 | + Path workerLogRoot = daemonLogRoot.resolve("workers-artifacts"); |
| 134 | + Path file = workerLogRoot.resolve("topoA").resolve("1111").resolve("worker.log"); |
| 135 | + Files.createDirectories(file.getParent()); |
| 136 | + Files.createFile(file); |
| 137 | + |
| 138 | + ResourceAuthorizer resourceAuthorizer = mock(ResourceAuthorizer.class); |
| 139 | + when(resourceAuthorizer.isUserAllowedToAccessFile(anyString(), anyString())).thenReturn(false); |
| 140 | + WorkerLogs workerLogs = mock(WorkerLogs.class); |
| 141 | + |
| 142 | + LogviewerLogDownloadHandler handler = new LogviewerLogDownloadHandler(workerLogRoot.toString(), |
| 143 | + daemonLogRoot.toString(), workerLogs, resourceAuthorizer, new StormMetricsRegistry()); |
| 144 | + |
| 145 | + Response response = handler.downloadLogFile("host", "topoA/1111/worker.log", "user"); |
| 146 | + |
| 147 | + Utils.forceDelete(rootPath.toString()); |
| 148 | + |
| 149 | + assertThat(response.getStatus(), is(Response.Status.FORBIDDEN.getStatusCode())); |
| 150 | + verify(workerLogs, never()).setLogFilePermission(anyString()); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + @Test |
| 155 | + public void testDownloadLogFileAuthorizedUserSetsLogFilePermission() throws IOException { |
| 156 | + try (TmpPath rootPath = new TmpPath()) { |
| 157 | + Path daemonLogRoot = rootPath.getFile().toPath().resolve("logs"); |
| 158 | + Path workerLogRoot = daemonLogRoot.resolve("workers-artifacts"); |
| 159 | + Path file = workerLogRoot.resolve("topoA").resolve("1111").resolve("worker.log"); |
| 160 | + Files.createDirectories(file.getParent()); |
| 161 | + Files.createFile(file); |
| 162 | + |
| 163 | + ResourceAuthorizer resourceAuthorizer = mock(ResourceAuthorizer.class); |
| 164 | + when(resourceAuthorizer.isUserAllowedToAccessFile(anyString(), anyString())).thenReturn(true); |
| 165 | + WorkerLogs workerLogs = mock(WorkerLogs.class); |
| 166 | + |
| 167 | + LogviewerLogDownloadHandler handler = new LogviewerLogDownloadHandler(workerLogRoot.toString(), |
| 168 | + daemonLogRoot.toString(), workerLogs, resourceAuthorizer, new StormMetricsRegistry()); |
| 169 | + |
| 170 | + Response response = handler.downloadLogFile("host", "topoA/1111/worker.log", "user"); |
| 171 | + |
| 172 | + Utils.forceDelete(rootPath.toString()); |
| 173 | + |
| 174 | + assertThat(response.getStatus(), is(Response.Status.OK.getStatusCode())); |
| 175 | + verify(workerLogs).setLogFilePermission("topoA/1111/worker.log"); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + @Test |
| 180 | + public void testDownloadDaemonLogFileDoesNotChangeLogFilePermission() throws IOException { |
| 181 | + try (TmpPath rootPath = new TmpPath()) { |
| 182 | + Path daemonLogRoot = rootPath.getFile().toPath().resolve("logs"); |
| 183 | + Path workerLogRoot = daemonLogRoot.resolve("workers-artifacts"); |
| 184 | + Path daemonFile = daemonLogRoot.resolve("nimbus.log"); |
| 185 | + Files.createDirectories(workerLogRoot); |
| 186 | + Files.createFile(daemonFile); |
| 187 | + |
| 188 | + WorkerLogs workerLogs = mock(WorkerLogs.class); |
| 189 | + |
| 190 | + LogviewerLogDownloadHandler handler = new LogviewerLogDownloadHandler(workerLogRoot.toString(), |
| 191 | + daemonLogRoot.toString(), workerLogs, new ResourceAuthorizer(Utils.readStormConfig()), new StormMetricsRegistry()); |
| 192 | + |
| 193 | + Response response = handler.downloadDaemonLogFile("host", "nimbus.log", "user"); |
| 194 | + |
| 195 | + Utils.forceDelete(rootPath.toString()); |
| 196 | + |
| 197 | + assertThat(response.getStatus(), is(Response.Status.OK.getStatusCode())); |
| 198 | + verify(workerLogs, never()).setLogFilePermission(anyString()); |
| 199 | + } |
| 200 | + } |
| 201 | + |
124 | 202 | private LogviewerLogDownloadHandler createHandlerTraversalTests(Path rootPath) throws IOException { |
125 | 203 | Path daemonLogRoot = rootPath.resolve("logs"); |
126 | 204 | Path fileOutsideDaemonRoot = rootPath.resolve("evil.sh"); |
|
0 commit comments