Skip to content

Commit

Permalink
Shutdown Kibana on usages of PKCS12 truststore/keystore config (elast…
Browse files Browse the repository at this point in the history
…ic#192627)

## Summary

Closes elastic#169741

PKCS12 truststores/keystores are not FIPS compliant and should not be
used when running if FIPS mode.

Users will be notified when they try to start KB in FIPS mode and are
using the flagged settings and KB will exit.

## Testing
You will need to generate a PKCS12 container (*.p12) file and have it
stored somewhere that your local KB can access.

To generate a PKCS12 to use:

- `openssl req -x509 -newkey rsa:4096 -keyout myPrivateKey.pem -out
myCertificate.crt`
- `openssl pkcs12 -export -out keyStore.p12 -inkey myPrivateKey.pem -in
myCertificate.crt`
  - Set password to `test`

Put the `.p12` file in your `config` directory (not required, but you
can copy and paste these commands easier)

Start an ES instance in a method of your choosing, but not using yarn es
snapshot. I like to use an 8.16.0-snapshot from the .es/cache directory
by running tar -xzvf elasticsearch-8.16.0-SNAPSHOT-darwin-aarch64.tar.gz
and cd into the new directory's bin folder to run ./elasticsearch

In a new terminal window, navigate to your the top level of your
elasticsearch folder and run:
`curl -X POST --cacert config/certs/http_ca.crt -u
elastic:YOUR_PASSWORD_HERE
"https://localhost:9200/_license/start_trial?acknowledge=true&pretty"`

This will enable the trial license for ES.

Ensure you have Docker running locally.

From any command line, run:
```
docker run --rm -it \
-v "$(pwd)"/config/keyStore.p12:/keyStore.p12:ro \
-e XPACK_SECURITY_FIPSMODE_ENABLED='true' \
-e ELASTICSEARCH_SSL_TRUSTSTORE_PATH='/keyStore.p12' \
-e ELASTICSEARCH_SSL_TRUSTSTORE_PASSWORD='test' \
-e ELASTICSEARCH_SSL_KEYSTORE_PATH='/keyStore.p12' \
-e ELASTICSEARCH_SSL_KEYSTORE_PASSWORD='test' \
-e SERVER_SSL_TRUSTSTORE_PATH='/keyStore.p12' \
-e SERVER_SSL_TRUSTSTORE_PASSWORD='test' \
-e SERVER_SSL_KEYSTORE_PATH='/keyStore.p12' \
-e SERVER_SSL_KEYSTORE_PASSWORD='test' \
-p 5601:5601/tcp docker.elastic.co/kibana-ci/kibana-ubi-fips:9.0.0-SNAPSHOT-92aeabf477867dc1768f9048b159f01f2ab1fcc3
```
This will start Kibana into Interactive Setup mode, copy and paste the
token from the ES startup logs.

In your logs, you will see an error letting users know that PKCS12
settings are not allowed in FIPS

It should look like:
<img width="1767" alt="Screenshot 2024-09-11 at 1 57 22 PM"
src="https://github.com/user-attachments/assets/c32c03b6-98b9-476d-8624-8cc1a535e23f">

## Release note
When running in FIPS mode, Kibana will forbid usage of PKCS12
configuration options

---------

Co-authored-by: Jean-Louis Leysens <[email protected]>
(cherry picked from commit a002a1b)
  • Loading branch information
kc13greiner committed Nov 19, 2024
1 parent 4a9f70d commit 5638329
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import { CriticalError } from '@kbn/core-base-server-internal';

const mockGetFipsFn = jest.fn();
jest.mock('crypto', () => ({
randomBytes: jest.fn(),
Expand All @@ -21,89 +23,74 @@ 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);
});
});

describe('checkFipsConfig', () => {
let mockExit: jest.SpyInstance;

beforeAll(() => {
mockExit = jest.spyOn(process, 'exit').mockImplementation((exitCode) => {
throw new Error(`Fake Exit: ${exitCode}`);
});
});

afterAll(() => {
mockExit.mockRestore();
});

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();
let fipsException: undefined | CriticalError;
try {
checkFipsConfig(config, logger);
checkFipsConfig(securityConfig, {}, {}, logger);
} catch (e) {
expect(mockExit).toHaveBeenNthCalledWith(1, 78);
fipsException = e;
}

expect(loggingSystemMock.collect(logger).error).toMatchInlineSnapshot(`
Array [
Array [
"Configuration mismatch error. xpack.security.experimental.fipsMode.enabled is set to true and the configured Node.js environment has FIPS disabled",
],
]
`);
expect(fipsException).toBeInstanceOf(CriticalError);
expect(fipsException!.processExitCode).toBe(78);
expect(fipsException!.message).toEqual(
'Configuration mismatch error. xpack.security.experimental.fipsMode.enabled is set to true and the configured Node.js environment has FIPS disabled'
);
});

it('should log an error message if FIPS mode is misconfigured - xpack.security.experimental.fipsMode.enabled false, Nodejs FIPS mode true', async () => {
mockGetFipsFn.mockImplementationOnce(() => {
return 1;
});

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

let fipsException: undefined | CriticalError;
try {
checkFipsConfig(config, logger);
checkFipsConfig(securityConfig, {}, {}, logger);
} catch (e) {
expect(mockExit).toHaveBeenNthCalledWith(1, 78);
fipsException = e;
}

expect(loggingSystemMock.collect(logger).error).toMatchInlineSnapshot(`
Array [
Array [
"Configuration mismatch error. xpack.security.experimental.fipsMode.enabled is set to false and the configured Node.js environment has FIPS enabled",
],
]
`);
expect(fipsException).toBeInstanceOf(CriticalError);
expect(fipsException!.processExitCode).toBe(78);
expect(fipsException!.message).toEqual(
'Configuration mismatch error. xpack.security.experimental.fipsMode.enabled is set to false and the configured Node.js environment has FIPS enabled'
);
});

it('should log an info message if FIPS mode is properly configured - xpack.security.experimental.fipsMode.enabled true, Nodejs FIPS mode true', async () => {
mockGetFipsFn.mockImplementationOnce(() => {
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 +103,89 @@ describe('fips', () => {
]
`);
});

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

beforeEach(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();

let fipsException: undefined | CriticalError;
try {
checkFipsConfig(securityConfig, elasticsearchConfig, serverConfig, logger);
} catch (e) {
fipsException = e;
}

expect(fipsException).toBeInstanceOf(CriticalError);
expect(fipsException!.processExitCode).toBe(78);
expect(fipsException!.message).toEqual(
'Configuration mismatch error: elasticsearch.ssl.keystore.path, elasticsearch.ssl.truststore.path, server.ssl.keystore.path, server.ssl.truststore.path are set, PKCS12 configurations are not allowed while running in FIPS mode.'
);
});

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

serverConfig = {};

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

let fipsException: undefined | CriticalError;
try {
checkFipsConfig(securityConfig, elasticsearchConfig, serverConfig, logger);
} catch (e) {
fipsException = e;
}

expect(fipsException).toBeInstanceOf(CriticalError);
expect(fipsException!.processExitCode).toBe(78);
expect(fipsException!.message).toEqual(
'Configuration mismatch error: elasticsearch.ssl.keystore.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 { CriticalError } from '@kbn/core-base-server-internal';
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(
throw new CriticalError(
`Configuration mismatch error. xpack.security.experimental.fipsMode.enabled is set to ${isFipsConfigEnabled} and the configured Node.js environment has FIPS ${
isNodeRunningWithFipsEnabled ? 'enabled' : 'disabled'
}`
}`,
'invalidConfig',
78
);
} else if (definedPKCS12ConfigOptions.length > 0) {
throw new CriticalError(
`Configuration mismatch error: ${definedPKCS12ConfigOptions.join(', ')} ${
definedPKCS12ConfigOptions.length > 1 ? 'are' : 'is'
} set, PKCS12 configurations are not allowed while running in FIPS mode.`,
'invalidConfig',
78
);
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;
};
};
}

0 comments on commit 5638329

Please sign in to comment.