|
| 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.security.support; |
| 8 | + |
| 9 | +import org.apache.logging.log4j.Level; |
| 10 | +import org.apache.logging.log4j.LogManager; |
| 11 | +import org.apache.logging.log4j.Logger; |
| 12 | +import org.elasticsearch.common.logging.Loggers; |
| 13 | +import org.elasticsearch.license.License; |
| 14 | +import org.elasticsearch.license.XPackLicenseState; |
| 15 | +import org.elasticsearch.test.ESTestCase; |
| 16 | +import org.elasticsearch.test.MockLogAppender; |
| 17 | +import org.junit.After; |
| 18 | +import org.junit.Before; |
| 19 | +import org.mockito.Mockito; |
| 20 | + |
| 21 | +import static org.mockito.Mockito.when; |
| 22 | + |
| 23 | +public class SecurityStatusChangeListenerTests extends ESTestCase { |
| 24 | + |
| 25 | + private XPackLicenseState licenseState; |
| 26 | + private SecurityStatusChangeListener listener; |
| 27 | + private MockLogAppender logAppender; |
| 28 | + private Logger listenerLogger; |
| 29 | + |
| 30 | + @Before |
| 31 | + public void setup() throws IllegalAccessException { |
| 32 | + licenseState = Mockito.mock(XPackLicenseState.class); |
| 33 | + when(licenseState.isSecurityAvailable()).thenReturn(true); |
| 34 | + |
| 35 | + listener = new SecurityStatusChangeListener(licenseState); |
| 36 | + |
| 37 | + logAppender = new MockLogAppender(); |
| 38 | + logAppender.start(); |
| 39 | + listenerLogger = LogManager.getLogger(listener.getClass()); |
| 40 | + Loggers.addAppender(listenerLogger, logAppender); |
| 41 | + } |
| 42 | + |
| 43 | + @After |
| 44 | + public void cleanup() { |
| 45 | + Loggers.removeAppender(listenerLogger, logAppender); |
| 46 | + logAppender.stop(); |
| 47 | + } |
| 48 | + |
| 49 | + public void testSecurityEnabledToDisabled() { |
| 50 | + when(licenseState.isSecurityDisabledByLicenseDefaults()).thenReturn(false); |
| 51 | + |
| 52 | + when(licenseState.getOperationMode()).thenReturn(License.OperationMode.GOLD); |
| 53 | + logAppender.addExpectation(new MockLogAppender.SeenEventExpectation( |
| 54 | + "initial change", |
| 55 | + listener.getClass().getName(), |
| 56 | + Level.INFO, |
| 57 | + "Active license is now [GOLD]; Security is enabled" |
| 58 | + )); |
| 59 | + listener.licenseStateChanged(); |
| 60 | + |
| 61 | + when(licenseState.getOperationMode()).thenReturn(License.OperationMode.PLATINUM); |
| 62 | + logAppender.addExpectation(new MockLogAppender.UnseenEventExpectation( |
| 63 | + "no-op change", |
| 64 | + listener.getClass().getName(), |
| 65 | + Level.INFO, |
| 66 | + "Active license is now [PLATINUM]; Security is enabled" |
| 67 | + )); |
| 68 | + |
| 69 | + when(licenseState.isSecurityDisabledByLicenseDefaults()).thenReturn(true); |
| 70 | + when(licenseState.getOperationMode()).thenReturn(License.OperationMode.BASIC); |
| 71 | + logAppender.addExpectation(new MockLogAppender.SeenEventExpectation( |
| 72 | + "change to basic", |
| 73 | + listener.getClass().getName(), |
| 74 | + Level.INFO, |
| 75 | + "Active license is now [BASIC]; Security is disabled" |
| 76 | + )); |
| 77 | + listener.licenseStateChanged(); |
| 78 | + |
| 79 | + logAppender.assertAllExpectationsMatched(); |
| 80 | + } |
| 81 | + |
| 82 | + public void testSecurityDisabledToEnabled() { |
| 83 | + when(licenseState.isSecurityDisabledByLicenseDefaults()).thenReturn(true); |
| 84 | + |
| 85 | + when(licenseState.getOperationMode()).thenReturn(License.OperationMode.TRIAL); |
| 86 | + logAppender.addExpectation(new MockLogAppender.SeenEventExpectation( |
| 87 | + "initial change", |
| 88 | + listener.getClass().getName(), |
| 89 | + Level.INFO, |
| 90 | + "Active license is now [TRIAL]; Security is disabled" |
| 91 | + )); |
| 92 | + listener.licenseStateChanged(); |
| 93 | + |
| 94 | + when(licenseState.getOperationMode()).thenReturn(License.OperationMode.BASIC); |
| 95 | + logAppender.addExpectation(new MockLogAppender.UnseenEventExpectation( |
| 96 | + "no-op change", |
| 97 | + listener.getClass().getName(), |
| 98 | + Level.INFO, |
| 99 | + "Active license is now [BASIC]; Security is disabled" |
| 100 | + )); |
| 101 | + |
| 102 | + when(licenseState.isSecurityDisabledByLicenseDefaults()).thenReturn(false); |
| 103 | + when(licenseState.getOperationMode()).thenReturn(License.OperationMode.PLATINUM); |
| 104 | + logAppender.addExpectation(new MockLogAppender.SeenEventExpectation( |
| 105 | + "change to platinum", |
| 106 | + listener.getClass().getName(), |
| 107 | + Level.INFO, |
| 108 | + "Active license is now [PLATINUM]; Security is enabled" |
| 109 | + )); |
| 110 | + listener.licenseStateChanged(); |
| 111 | + |
| 112 | + logAppender.assertAllExpectationsMatched(); |
| 113 | + } |
| 114 | + |
| 115 | +} |
0 commit comments