Skip to content
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
merged 6 commits into from
May 31, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public UnseenEventExpectation(String name, String logger, Level level, String me

@Override
public void assertMatched() {
assertThat("expected to see " + name + " but did not", saw, equalTo(false));
assertThat("expected not to see " + name + " but did", saw, equalTo(false));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@
import org.elasticsearch.xpack.security.rest.action.user.RestPutUserAction;
import org.elasticsearch.xpack.security.rest.action.user.RestSetEnabledAction;
import org.elasticsearch.xpack.security.support.SecurityIndexManager;
import org.elasticsearch.xpack.security.support.SecurityStatusChangeListener;
import org.elasticsearch.xpack.security.transport.SecurityHttpSettings;
import org.elasticsearch.xpack.security.transport.SecurityServerTransportInterceptor;
import org.elasticsearch.xpack.security.transport.filter.IPFilter;
Expand Down Expand Up @@ -446,6 +447,7 @@ Collection<Object> createComponents(Client client, ThreadPool threadPool, Cluste
// to keep things simple, just invalidate all cached entries on license change. this happens so rarely that the impact should be
// minimal
getLicenseState().addListener(allRolesStore::invalidateAll);
getLicenseState().addListener(new SecurityStatusChangeListener(getLicenseState()));

final AuthenticationFailureHandler failureHandler = createAuthenticationFailureHandler(realms);
authcService.set(new AuthenticationService(settings, realms, auditTrailService, failureHandler, threadPool,
Expand Down
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If securityEnabled is null and newState is false, won't this produce a misleading log entry saying

disabling security for licence [X]

?

Copy link
Contributor Author

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?

Copy link
Member

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.

Copy link
Contributor Author

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).

Copy link
Contributor Author

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.

Copy link
Member

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 ?

logger.info("{} security for license [{}]", newState ? "is enabled" : "is disabled", licenseState.getOperationMode());

but I'm not strongly positive this is so much better.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about this?

Active license is now [{}] ; Security is {enabled|disabled}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WFM !

logger.info("{} security for license [{}]", newState ? "enabling" : "disabling", licenseState.getOperationMode());
this.securityEnabled = newState;
}
}
}
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();
}

}