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

Shutdown Kibana on usages of PKCS12 truststore/keystore config #192627

Merged
merged 5 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -21,22 +21,22 @@ import { isFipsEnabled, checkFipsConfig } from './fips';
import { loggingSystemMock } from '@kbn/core-logging-server-mocks';

describe('fips', () => {
let config: SecurityServiceConfigType;
let securityConfig: SecurityServiceConfigType;
describe('#isFipsEnabled', () => {
it('should return `true` if config.experimental.fipsMode.enabled is `true`', () => {
config = { experimental: { fipsMode: { enabled: true } } };
securityConfig = { experimental: { fipsMode: { enabled: true } } };

expect(isFipsEnabled(config)).toBe(true);
expect(isFipsEnabled(securityConfig)).toBe(true);
});

it('should return `false` if config.experimental.fipsMode.enabled is `false`', () => {
config = { experimental: { fipsMode: { enabled: false } } };
securityConfig = { experimental: { fipsMode: { enabled: false } } };

expect(isFipsEnabled(config)).toBe(false);
expect(isFipsEnabled(securityConfig)).toBe(false);
});

it('should return `false` if config.experimental.fipsMode.enabled is `undefined`', () => {
expect(isFipsEnabled(config)).toBe(false);
expect(isFipsEnabled(securityConfig)).toBe(false);
});
});

Expand All @@ -54,10 +54,10 @@ describe('fips', () => {
});

it('should log an error message if FIPS mode is misconfigured - xpack.security.experimental.fipsMode.enabled true, Nodejs FIPS mode false', async () => {
config = { experimental: { fipsMode: { enabled: true } } };
securityConfig = { experimental: { fipsMode: { enabled: true } } };
const logger = loggingSystemMock.create().get();
try {
checkFipsConfig(config, logger);
checkFipsConfig(securityConfig, {}, {}, logger);
} catch (e) {
expect(mockExit).toHaveBeenNthCalledWith(1, 78);
}
Expand All @@ -76,11 +76,11 @@ describe('fips', () => {
return 1;
});

config = { experimental: { fipsMode: { enabled: false } } };
securityConfig = { experimental: { fipsMode: { enabled: false } } };
const logger = loggingSystemMock.create().get();

try {
checkFipsConfig(config, logger);
checkFipsConfig(securityConfig, {}, {}, logger);
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: what do you think about adding this on the next line?

fail('FIPS config check did not throw')

(ditto for other try catches like 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.

++ added in latest commit

} catch (e) {
expect(mockExit).toHaveBeenNthCalledWith(1, 78);
}
Expand All @@ -99,11 +99,11 @@ describe('fips', () => {
return 1;
});

config = { experimental: { fipsMode: { enabled: true } } };
securityConfig = { experimental: { fipsMode: { enabled: true } } };
const logger = loggingSystemMock.create().get();

try {
checkFipsConfig(config, logger);
checkFipsConfig(securityConfig, {}, {}, logger);
} catch (e) {
logger.error('Should not throw error!');
}
Expand All @@ -116,5 +116,72 @@ describe('fips', () => {
]
`);
});

describe('PKCS12 Config settings', function () {
let serverConfig = {};
let elasticsearchConfig = {};

beforeAll(function () {
mockGetFipsFn.mockImplementationOnce(() => {
return 1;
});

securityConfig = { experimental: { fipsMode: { enabled: true } } };
});

afterEach(function () {
serverConfig = {};
elasticsearchConfig = {};
});

it('should log an error message for each PKCS12 configuration option that is set', async () => {
elasticsearchConfig = {
ssl: {
keystore: {
path: '/test',
},
truststore: {
path: '/test',
},
},
};

serverConfig = {
ssl: {
keystore: {
path: '/test',
},
truststore: {
path: '/test',
},
},
};

const logger = loggingSystemMock.create().get();

try {
checkFipsConfig(securityConfig, elasticsearchConfig, serverConfig, logger);
} catch (e) {
expect(mockExit).toHaveBeenNthCalledWith(1, 78);
}

expect(loggingSystemMock.collect(logger).error).toMatchInlineSnapshot(`
Array [
Array [
"Configuration mismatch error: elasticsearch.ssl.keystore.path is set, PKCS12 configurations are not allowed while running in FIPS mode.",
],
Array [
"Configuration mismatch error: elasticsearch.ssl.truststore.path is set, PKCS12 configurations are not allowed while running in FIPS mode.",
],
Array [
"Configuration mismatch error: server.ssl.keystore.path is set, PKCS12 configurations are not allowed while running in FIPS mode.",
],
Array [
"Configuration mismatch error: server.ssl.truststore.path is set, PKCS12 configurations are not allowed while running in FIPS mode.",
],
]
`);
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,70 @@

import type { Logger } from '@kbn/logging';
import { getFips } from 'crypto';
import { SecurityServiceConfigType } from '../utils';
import { PKCS12ConfigType, SecurityServiceConfigType } from '../utils';

export function isFipsEnabled(config: SecurityServiceConfigType): boolean {
return config?.experimental?.fipsMode?.enabled ?? false;
}

export function checkFipsConfig(config: SecurityServiceConfigType, logger: Logger) {
export function checkFipsConfig(
config: SecurityServiceConfigType,
elasticsearchConfig: PKCS12ConfigType,
serverConfig: PKCS12ConfigType,
logger: Logger
) {
const isFipsConfigEnabled = isFipsEnabled(config);
const isNodeRunningWithFipsEnabled = getFips() === 1;

// Check if FIPS is enabled in either setting
if (isFipsConfigEnabled || isNodeRunningWithFipsEnabled) {
// FIPS must be enabled on both or log and error an exit Kibana
const definedPKCS12ConfigOptions = findDefinedPKCS12ConfigOptions(
elasticsearchConfig,
serverConfig
);
// FIPS must be enabled on both, or, log/error an exit Kibana
if (isFipsConfigEnabled !== isNodeRunningWithFipsEnabled) {
logger.error(
`Configuration mismatch error. xpack.security.experimental.fipsMode.enabled is set to ${isFipsConfigEnabled} and the configured Node.js environment has FIPS ${
isNodeRunningWithFipsEnabled ? 'enabled' : 'disabled'
}`
);

process.exit(78);
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: I know process.exit is pre-existing, but I thought we could leverage the "shutdown" listener by throwing a CriticalError like this that allows providing a message and an exit code.

Happy to stick with the current approach, my primary thought was just to give Core the chance to run the shutdown lifecycle and might make testing a little bit easier 🤷🏻

Copy link
Member

Choose a reason for hiding this comment

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

I like the CriticalError approach, thanks for showing us this, @jloleysens!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

++ added in latest commit - thank you for the guidance!!! It is way cleaner 🚀

} else if (definedPKCS12ConfigOptions.length > 0) {
definedPKCS12ConfigOptions.forEach((option) => {
logger.error(
`Configuration mismatch error: ${option} is set, PKCS12 configurations are not allowed while running in FIPS mode.`
);
});

process.exit(78);
} else {
logger.info('Kibana is running in FIPS mode.');
}
}
}

function findDefinedPKCS12ConfigOptions(
elasticsearchConfig: PKCS12ConfigType,
serverConfig: PKCS12ConfigType
): string[] {
const result = [];
if (elasticsearchConfig?.ssl?.keystore?.path) {
result.push('elasticsearch.ssl.keystore.path');
}

if (elasticsearchConfig?.ssl?.truststore?.path) {
result.push('elasticsearch.ssl.truststore.path');
}

if (serverConfig?.ssl?.keystore?.path) {
result.push('server.ssl.keystore.path');
}

if (serverConfig?.ssl?.truststore?.path) {
result.push('server.ssl.truststore.path');
}

return result;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
getDefaultSecurityImplementation,
convertSecurityApi,
SecurityServiceConfigType,
PKCS12ConfigType,
} from './utils';

export class SecurityService
Expand Down Expand Up @@ -50,8 +51,10 @@ export class SecurityService
public setup(): InternalSecurityServiceSetup {
const config = this.getConfig();
const securityConfig: SecurityServiceConfigType = config.get(['xpack', 'security']);
const elasticsearchConfig: PKCS12ConfigType = config.get(['elasticsearch']);
const serverConfig: PKCS12ConfigType = config.get(['server']);

checkFipsConfig(securityConfig, this.log);
checkFipsConfig(securityConfig, elasticsearchConfig, serverConfig, this.log);

return {
registerSecurityDelegate: (api) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,14 @@ export interface SecurityServiceConfigType {
};
};
}

export interface PKCS12ConfigType {
ssl?: {
keystore?: {
path?: string;
};
truststore?: {
path?: string;
};
};
}