-
Notifications
You must be signed in to change notification settings - Fork 24.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Log the status of security on license change #42488
Merged
tvernum
merged 6 commits into
elastic:master
from
tvernum:security-status-change-listener
May 31, 2019
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b09245c
Log the status of security on license change
tvernum fe72d41
Merge branch 'master' into security-status-change-listener
tvernum a6ac218
Improve error message
tvernum 4e10bc1
Merge branch 'master' into security-status-change-listener
tvernum 6c65c05
Merge branch 'master' into security-status-change-listener
tvernum 9539fb9
Update test for message text change
tvernum File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
.../src/main/java/org/elasticsearch/xpack/security/support/SecurityStatusChangeListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.security.support; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.elasticsearch.license.LicenseStateListener; | ||
import org.elasticsearch.license.XPackLicenseState; | ||
|
||
import java.util.Objects; | ||
|
||
/** | ||
* A listener for license state changes that provides log messages when a license change | ||
* causes security to switch between enable and disabled (or vice versa). | ||
*/ | ||
public class SecurityStatusChangeListener implements LicenseStateListener { | ||
|
||
private final Logger logger; | ||
private final XPackLicenseState licenseState; | ||
private Boolean securityEnabled; | ||
|
||
public SecurityStatusChangeListener(XPackLicenseState licenseState) { | ||
this.logger = LogManager.getLogger(getClass()); | ||
this.licenseState = licenseState; | ||
this.securityEnabled = null; | ||
} | ||
|
||
/** | ||
* This listener will not be registered if security has been explicitly disabled, so we only need to account for dynamic changes due | ||
* to changes in the applied license. | ||
*/ | ||
@Override | ||
public synchronized void licenseStateChanged() { | ||
final boolean newState = licenseState.isSecurityAvailable() && licenseState.isSecurityDisabledByLicenseDefaults() == false; | ||
// old state might be null (undefined) so do Object comparison | ||
if (Objects.equals(newState, securityEnabled) == false) { | ||
logger.info("{} security for license [{}]", newState ? "enabling" : "disabling", licenseState.getOperationMode()); | ||
this.securityEnabled = newState; | ||
} | ||
} | ||
} |
115 changes: 115 additions & 0 deletions
115
...test/java/org/elasticsearch/xpack/security/support/SecurityStatusChangeListenerTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.security.support; | ||
|
||
import org.apache.logging.log4j.Level; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.elasticsearch.common.logging.Loggers; | ||
import org.elasticsearch.license.License; | ||
import org.elasticsearch.license.XPackLicenseState; | ||
import org.elasticsearch.test.ESTestCase; | ||
import org.elasticsearch.test.MockLogAppender; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.mockito.Mockito; | ||
|
||
import static org.mockito.Mockito.when; | ||
|
||
public class SecurityStatusChangeListenerTests extends ESTestCase { | ||
|
||
private XPackLicenseState licenseState; | ||
private SecurityStatusChangeListener listener; | ||
private MockLogAppender logAppender; | ||
private Logger listenerLogger; | ||
|
||
@Before | ||
public void setup() throws IllegalAccessException { | ||
licenseState = Mockito.mock(XPackLicenseState.class); | ||
when(licenseState.isSecurityAvailable()).thenReturn(true); | ||
|
||
listener = new SecurityStatusChangeListener(licenseState); | ||
|
||
logAppender = new MockLogAppender(); | ||
logAppender.start(); | ||
listenerLogger = LogManager.getLogger(listener.getClass()); | ||
Loggers.addAppender(listenerLogger, logAppender); | ||
} | ||
|
||
@After | ||
public void cleanup() { | ||
Loggers.removeAppender(listenerLogger, logAppender); | ||
logAppender.stop(); | ||
} | ||
|
||
public void testSecurityEnabledToDisabled() { | ||
when(licenseState.isSecurityDisabledByLicenseDefaults()).thenReturn(false); | ||
|
||
when(licenseState.getOperationMode()).thenReturn(License.OperationMode.GOLD); | ||
logAppender.addExpectation(new MockLogAppender.SeenEventExpectation( | ||
"initial change", | ||
listener.getClass().getName(), | ||
Level.INFO, | ||
"enabling security for license [GOLD]" | ||
)); | ||
listener.licenseStateChanged(); | ||
|
||
when(licenseState.getOperationMode()).thenReturn(License.OperationMode.PLATINUM); | ||
logAppender.addExpectation(new MockLogAppender.UnseenEventExpectation( | ||
"no-op change", | ||
listener.getClass().getName(), | ||
Level.INFO, | ||
"enabling security for license [PLATINUM]" | ||
)); | ||
|
||
when(licenseState.isSecurityDisabledByLicenseDefaults()).thenReturn(true); | ||
when(licenseState.getOperationMode()).thenReturn(License.OperationMode.BASIC); | ||
logAppender.addExpectation(new MockLogAppender.SeenEventExpectation( | ||
"change to basic", | ||
listener.getClass().getName(), | ||
Level.INFO, | ||
"disabling security for license [BASIC]" | ||
)); | ||
listener.licenseStateChanged(); | ||
|
||
logAppender.assertAllExpectationsMatched(); | ||
} | ||
|
||
public void testSecurityDisabledToEnabled() { | ||
when(licenseState.isSecurityDisabledByLicenseDefaults()).thenReturn(true); | ||
|
||
when(licenseState.getOperationMode()).thenReturn(License.OperationMode.TRIAL); | ||
logAppender.addExpectation(new MockLogAppender.SeenEventExpectation( | ||
"initial change", | ||
listener.getClass().getName(), | ||
Level.INFO, | ||
"disabling security for license [TRIAL]" | ||
)); | ||
listener.licenseStateChanged(); | ||
|
||
when(licenseState.getOperationMode()).thenReturn(License.OperationMode.BASIC); | ||
logAppender.addExpectation(new MockLogAppender.UnseenEventExpectation( | ||
"no-op change", | ||
listener.getClass().getName(), | ||
Level.INFO, | ||
"disabling security for license [BASIC]" | ||
)); | ||
|
||
when(licenseState.isSecurityDisabledByLicenseDefaults()).thenReturn(false); | ||
when(licenseState.getOperationMode()).thenReturn(License.OperationMode.PLATINUM); | ||
logAppender.addExpectation(new MockLogAppender.SeenEventExpectation( | ||
"change to platinum", | ||
listener.getClass().getName(), | ||
Level.INFO, | ||
"enabling security for license [PLATINUM]" | ||
)); | ||
listener.licenseStateChanged(); | ||
|
||
logAppender.assertAllExpectationsMatched(); | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
securityEnabled
is null andnewState
is false, won't this produce a misleading log entry saying?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you say that is misleading?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because we transition from a "Security not enabled" state to a "Security not enabled" state and we log a message saying "disabling security for license [x]" that sounds like it indicates that it was the change in license that disabled security - when it might well be the existing configuration that disables this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I reject all of your assertions :)
In my mind we transition from an uninitialised state (for which security is partially enabled - this is how uninitialised nodes can join a cluster with security enabled) to a "security not enabled" state.
Secondly, I intentionally wrote the log message to strike a balance between vague and informative. It simply states whether security is enabled or not, and what the license is, without explicitly stating anything more than that.
Thirdly, per the method doc, if existing configuration explicitly disables security than this listener is never registered, and we don't log anything, so the message is always indicating that a dynamic decision was made based on the license.
The case where it could be argued to be misleading is if security is explicitly enabled, in which case every license type will trigger a "enabling security for license [x]" message, when clearly the license mode was irrelevant (but the message is still technically true, we are enabling security for your license whether it be basic through to platinum).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In seriousness, I do think we want to print a message on startup because it's nice to have a clear confirmation as to whether security is enabled.
I'm happy to change the wording if there is a version that you feel would be less misleading.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see your point(s). I don't have much to offer in terms of less ambiguity. Maybe passive voice instead of gerund, in order to focus more on the license state for the cluster instead of the act of changing it ?
but I'm not strongly positive this is so much better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WFM !