|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +package org.elasticsearch.xpack.core.security.authc.support; |
| 8 | + |
| 9 | +import org.elasticsearch.Version; |
| 10 | +import org.elasticsearch.action.ActionListener; |
| 11 | +import org.elasticsearch.action.support.ContextPreservingActionListener; |
| 12 | +import org.elasticsearch.common.settings.Settings; |
| 13 | +import org.elasticsearch.common.util.concurrent.ThreadContext; |
| 14 | +import org.elasticsearch.test.ESTestCase; |
| 15 | +import org.elasticsearch.threadpool.TestThreadPool; |
| 16 | +import org.elasticsearch.threadpool.ThreadPool; |
| 17 | +import org.elasticsearch.xpack.core.security.SecurityContext; |
| 18 | +import org.elasticsearch.xpack.core.security.authc.Authentication; |
| 19 | +import org.elasticsearch.xpack.core.security.user.User; |
| 20 | +import org.elasticsearch.xpack.core.security.authc.support.SecondaryAuthentication; |
| 21 | +import org.junit.After; |
| 22 | +import org.junit.Before; |
| 23 | + |
| 24 | +import java.util.concurrent.Future; |
| 25 | +import java.util.concurrent.Semaphore; |
| 26 | +import java.util.concurrent.TimeUnit; |
| 27 | +import java.util.concurrent.atomic.AtomicReference; |
| 28 | + |
| 29 | +import static org.hamcrest.Matchers.equalTo; |
| 30 | +import static org.hamcrest.Matchers.notNullValue; |
| 31 | +import static org.hamcrest.Matchers.sameInstance; |
| 32 | + |
| 33 | +public class SecondaryAuthenticationTests extends ESTestCase { |
| 34 | + |
| 35 | + private SecurityContext securityContext; |
| 36 | + private ThreadPool threadPool; |
| 37 | + |
| 38 | + @Before |
| 39 | + public void setupObjects() { |
| 40 | + this.threadPool = new TestThreadPool(getTestName()); |
| 41 | + this.securityContext = new SecurityContext(Settings.EMPTY, threadPool.getThreadContext()); |
| 42 | + } |
| 43 | + |
| 44 | + @After |
| 45 | + public void cleanup() { |
| 46 | + this.threadPool.shutdownNow(); |
| 47 | + } |
| 48 | + |
| 49 | + public void testCannotCreateObjectWithNullAuthentication() { |
| 50 | + expectThrows(NullPointerException.class, () -> new SecondaryAuthentication(securityContext, null)); |
| 51 | + } |
| 52 | + |
| 53 | + public void testSynchronousExecuteInSecondaryContext() { |
| 54 | + final User user1 = new User("u1", "role1"); |
| 55 | + securityContext.setUser(user1, Version.CURRENT); |
| 56 | + assertThat(securityContext.getUser().principal(), equalTo("u1")); |
| 57 | + |
| 58 | + final Authentication authentication2 = new Authentication(new User("u2", "role2"), realm(), realm()); |
| 59 | + final SecondaryAuthentication secondaryAuth = new SecondaryAuthentication(securityContext, authentication2); |
| 60 | + |
| 61 | + assertThat(securityContext.getUser().principal(), equalTo("u1")); |
| 62 | + String result = secondaryAuth.execute(original -> { |
| 63 | + assertThat(securityContext.getUser().principal(), equalTo("u2")); |
| 64 | + assertThat(securityContext.getAuthentication(), sameInstance(authentication2)); |
| 65 | + return "xyzzy"; |
| 66 | + }); |
| 67 | + assertThat(securityContext.getUser().principal(), equalTo("u1")); |
| 68 | + assertThat(result, equalTo("xyzzy")); |
| 69 | + } |
| 70 | + |
| 71 | + public void testSecondaryContextCanBeRestored() { |
| 72 | + final User user1 = new User("u1", "role1"); |
| 73 | + securityContext.setUser(user1, Version.CURRENT); |
| 74 | + assertThat(securityContext.getUser().principal(), equalTo("u1")); |
| 75 | + |
| 76 | + final Authentication authentication2 = new Authentication(new User("u2", "role2"), realm(), realm()); |
| 77 | + final SecondaryAuthentication secondaryAuth = new SecondaryAuthentication(securityContext, authentication2); |
| 78 | + |
| 79 | + assertThat(securityContext.getUser().principal(), equalTo("u1")); |
| 80 | + final AtomicReference<ThreadContext.StoredContext> secondaryContext = new AtomicReference<>(); |
| 81 | + secondaryAuth.execute(storedContext -> { |
| 82 | + assertThat(securityContext.getUser().principal(), equalTo("u2")); |
| 83 | + assertThat(securityContext.getAuthentication(), sameInstance(authentication2)); |
| 84 | + secondaryContext.set(threadPool.getThreadContext().newStoredContext(false)); |
| 85 | + storedContext.restore(); |
| 86 | + assertThat(securityContext.getUser().principal(), equalTo("u1")); |
| 87 | + return null; |
| 88 | + }); |
| 89 | + assertThat(securityContext.getUser().principal(), equalTo("u1")); |
| 90 | + secondaryContext.get().restore(); |
| 91 | + assertThat(securityContext.getUser().principal(), equalTo("u2")); |
| 92 | + } |
| 93 | + |
| 94 | + public void testWrapRunnable() throws Exception { |
| 95 | + final User user1 = new User("u1", "role1"); |
| 96 | + securityContext.setUser(user1, Version.CURRENT); |
| 97 | + assertThat(securityContext.getUser().principal(), equalTo("u1")); |
| 98 | + |
| 99 | + final Authentication authentication2 = new Authentication(new User("u2", "role2"), realm(), realm()); |
| 100 | + final SecondaryAuthentication secondaryAuth = new SecondaryAuthentication(securityContext, authentication2); |
| 101 | + |
| 102 | + assertThat(securityContext.getUser().principal(), equalTo("u1")); |
| 103 | + final Semaphore semaphore = new Semaphore(0); |
| 104 | + final Future<?> future = threadPool.generic().submit(secondaryAuth.wrap(() -> { |
| 105 | + try { |
| 106 | + assertThat(securityContext.getUser().principal(), equalTo("u2")); |
| 107 | + semaphore.acquire(); |
| 108 | + assertThat(securityContext.getUser().principal(), equalTo("u2")); |
| 109 | + semaphore.acquire(); |
| 110 | + assertThat(securityContext.getUser().principal(), equalTo("u2")); |
| 111 | + } catch (InterruptedException e) { |
| 112 | + Thread.currentThread().interrupt(); |
| 113 | + throw new RuntimeException(e); |
| 114 | + } |
| 115 | + })); |
| 116 | + assertThat(securityContext.getUser().principal(), equalTo("u1")); |
| 117 | + semaphore.release(); |
| 118 | + assertThat(securityContext.getUser().principal(), equalTo("u1")); |
| 119 | + semaphore.release(); |
| 120 | + assertThat(securityContext.getUser().principal(), equalTo("u1")); |
| 121 | + |
| 122 | + // ensure that the runnable didn't throw any exceptions / assertions |
| 123 | + future.get(1, TimeUnit.SECONDS); |
| 124 | + } |
| 125 | + |
| 126 | + public void testPreserveSecondaryContextAcrossThreads() throws Exception { |
| 127 | + final User user1 = new User("u1", "role1"); |
| 128 | + securityContext.setUser(user1, Version.CURRENT); |
| 129 | + assertThat(securityContext.getUser().principal(), equalTo("u1")); |
| 130 | + |
| 131 | + final Authentication authentication2 = new Authentication(new User("u2", "role2"), realm(), realm()); |
| 132 | + final SecondaryAuthentication secondaryAuth = new SecondaryAuthentication(securityContext, authentication2); |
| 133 | + |
| 134 | + assertThat(securityContext.getUser().principal(), equalTo("u1")); |
| 135 | + |
| 136 | + final AtomicReference<User> threadUser = new AtomicReference<>(); |
| 137 | + final AtomicReference<User> listenerUser = new AtomicReference<>(); |
| 138 | + |
| 139 | + final ThreadContext threadContext = threadPool.getThreadContext(); |
| 140 | + secondaryAuth.execute(originalContext -> { |
| 141 | + assertThat(securityContext.getUser().principal(), equalTo("u2")); |
| 142 | + ActionListener<Void> listener = new ContextPreservingActionListener<>(threadContext.newRestorableContext(false), |
| 143 | + ActionListener.wrap(() -> listenerUser.set(securityContext.getUser()))); |
| 144 | + originalContext.restore(); |
| 145 | + threadPool.generic().execute(() -> { |
| 146 | + threadUser.set(securityContext.getUser()); |
| 147 | + listener.onResponse(null); |
| 148 | + }); |
| 149 | + return null; |
| 150 | + }); |
| 151 | + assertThat(securityContext.getUser().principal(), equalTo("u1")); |
| 152 | + assertBusy(() -> assertThat(listenerUser.get(), notNullValue()), 1, TimeUnit.SECONDS); |
| 153 | + assertThat(threadUser.get(), notNullValue()); |
| 154 | + assertThat(threadUser.get().principal(), equalTo("u1")); |
| 155 | + assertThat(listenerUser.get().principal(), equalTo("u2")); |
| 156 | + } |
| 157 | + |
| 158 | + private Authentication.RealmRef realm() { |
| 159 | + return new Authentication.RealmRef(randomAlphaOfLengthBetween(4, 8), randomAlphaOfLengthBetween(2, 4), randomAlphaOfLength(12)); |
| 160 | + } |
| 161 | + |
| 162 | +} |
0 commit comments