|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.livy.server.recovery |
| 19 | + |
| 20 | +import javax.servlet.http.HttpServletRequest |
| 21 | + |
| 22 | +import org.mockito.Mockito.when |
| 23 | +import org.scalatestplus.mockito.MockitoSugar.mock |
| 24 | + |
| 25 | +import org.apache.livy.LivyConf |
| 26 | +import org.apache.livy.server.{AccessManager, BaseJsonServletSpec} |
| 27 | +import org.apache.livy.server.batch.BatchRecoveryMetadata |
| 28 | +import org.apache.livy.server.interactive.InteractiveRecoveryMetadata |
| 29 | +import org.apache.livy.sessions.{BatchSessionManager, InteractiveSessionManager} |
| 30 | +import org.apache.livy.sessions.SessionManager.{SESSION_RECOVERY_MODE_OFF, |
| 31 | + SESSION_RECOVERY_MODE_RECOVERY} |
| 32 | + |
| 33 | +object RecoveryServletSpec { |
| 34 | + val REMOTE_USER_HEADER = "X-Livy-RecoveryServlet-User" |
| 35 | + val ADMIN = "__admin__" |
| 36 | + val REGULAR_USER = "__user__" |
| 37 | +} |
| 38 | + |
| 39 | +/** Reads the test-only remote-user header instead of the (unavailable in tests) container user. */ |
| 40 | +class TestRecoveryServlet( |
| 41 | + livyConf: LivyConf, |
| 42 | + accessManager: AccessManager, |
| 43 | + interactiveSessionManager: InteractiveSessionManager, |
| 44 | + batchSessionManager: BatchSessionManager) |
| 45 | + extends RecoveryServlet(livyConf, accessManager, interactiveSessionManager, batchSessionManager) { |
| 46 | + |
| 47 | + override protected def remoteUser(req: HttpServletRequest): String = { |
| 48 | + req.getHeader(RecoveryServletSpec.REMOTE_USER_HEADER) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +trait RecoveryServletSpecBase extends BaseJsonServletSpec { |
| 53 | + |
| 54 | + import RecoveryServletSpec._ |
| 55 | + |
| 56 | + protected def recoveryMode: String |
| 57 | + |
| 58 | + protected def headersFor(user: String): Map[String, String] = |
| 59 | + defaultHeaders ++ Map(REMOTE_USER_HEADER -> user) |
| 60 | + |
| 61 | + private def mockSessionStore(): SessionStore = { |
| 62 | + val sessionStore = mock[SessionStore] |
| 63 | + when(sessionStore.getAllSessions[BatchRecoveryMetadata]("batch")).thenReturn(Seq.empty) |
| 64 | + when(sessionStore.getAllSessions[InteractiveRecoveryMetadata]("interactive")) |
| 65 | + .thenReturn(Seq.empty) |
| 66 | + when(sessionStore.getNextSessionId("batch")).thenReturn(0) |
| 67 | + when(sessionStore.getNextSessionId("interactive")).thenReturn(0) |
| 68 | + sessionStore |
| 69 | + } |
| 70 | + |
| 71 | + private val livyConf = new LivyConf() |
| 72 | + .set(LivyConf.SUPERUSERS, ADMIN) |
| 73 | + .set(LivyConf.RECOVERY_MODE, recoveryMode) |
| 74 | + private val accessManager = new AccessManager(livyConf) |
| 75 | + private val sessionStore = mockSessionStore() |
| 76 | + private val batchSessionManager = new BatchSessionManager(livyConf, sessionStore) |
| 77 | + private val interactiveSessionManager = new InteractiveSessionManager(livyConf, sessionStore) |
| 78 | + |
| 79 | + addServlet( |
| 80 | + new TestRecoveryServlet(livyConf, accessManager, interactiveSessionManager, |
| 81 | + batchSessionManager), |
| 82 | + "/*") |
| 83 | +} |
| 84 | + |
| 85 | +class RecoveryServletEnabledSpec extends RecoveryServletSpecBase { |
| 86 | + |
| 87 | + import RecoveryServletSpec._ |
| 88 | + |
| 89 | + override protected def recoveryMode: String = SESSION_RECOVERY_MODE_RECOVERY |
| 90 | + |
| 91 | + describe("RecoveryServlet with recovery enabled") { |
| 92 | + |
| 93 | + it("rejects non-superusers with 403") { |
| 94 | + post("/refresh", headers = headersFor(REGULAR_USER)) { |
| 95 | + status should be (403) |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + it("allows superusers and returns refresh counts with 200") { |
| 100 | + post("/refresh", headers = headersFor(ADMIN)) { |
| 101 | + status should be (200) |
| 102 | + body should include ("\"sessions\"") |
| 103 | + body should include ("\"batches\"") |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + it("allows superusers on the single-manager endpoints with 200") { |
| 108 | + post("/sessions/refresh", headers = headersFor(ADMIN)) { |
| 109 | + status should be (200) |
| 110 | + body should include ("\"added\"") |
| 111 | + } |
| 112 | + post("/batches/refresh", headers = headersFor(ADMIN)) { |
| 113 | + status should be (200) |
| 114 | + body should include ("\"added\"") |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +class RecoveryServletDisabledSpec extends RecoveryServletSpecBase { |
| 121 | + |
| 122 | + import RecoveryServletSpec._ |
| 123 | + |
| 124 | + override protected def recoveryMode: String = SESSION_RECOVERY_MODE_OFF |
| 125 | + |
| 126 | + describe("RecoveryServlet with recovery disabled") { |
| 127 | + |
| 128 | + it("returns 409 when recovery is disabled, even for superusers") { |
| 129 | + post("/refresh", headers = headersFor(ADMIN)) { |
| 130 | + status should be (409) |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + it("checks authorization before the recovery-mode check") { |
| 135 | + post("/refresh", headers = headersFor(REGULAR_USER)) { |
| 136 | + status should be (403) |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments