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

[Security solution][Endpoint] API error when edit/create TA by policy and license under platinum #113363

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 @@ -25,6 +25,15 @@ export class TrustedAppPolicyNotExistsError extends Error {
);
}
}
export class TrustedAppPolicyPermissionsError extends Error {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggestion: extend EndpointError instead as the base class. When you do that, you should not need to set the type below, as the base class already takes care of it (but instead of type it uses name)

public readonly type = 'TrustedApps/PolicyPermissionError';

constructor() {
super(
'Your Kibana license has been downgraded. As such, individual policy configuration is no longer supported.'
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggestion: use the same text as the one that Isolation is using: 'Your license level does not allow for this action' (here: https://github.com/elastic/kibana/blob/master/x-pack/plugins/security_solution/server/endpoint/routes/actions/isolation.ts#L79)

Also,

  • Maybe we should have a generic Endpoint error for license related errors - like a EndpointLicenseError in server/endpoint/errors.ts?
  • If you decide to stay with this one - since this seems to be very specific for only license violation, maybe rename the Error to TrustedAppPolicyLicenseError?

Either way - this comment can be done in a subsequent PR.

);
}
}

export class TrustedAppVersionConflictError extends Error {
constructor(id: string, public sourceError: Error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import { KibanaResponseFactory } from 'kibana/server';
import { Subject } from 'rxjs';

import { xpackMocks } from '../../../fixtures';
import { loggingSystemMock, httpServerMock } from '../../../../../../../src/core/server/mocks';
Expand All @@ -20,6 +21,9 @@ import {
OperatingSystem,
TrustedApp,
} from '../../../../common/endpoint/types';
import { LicenseService } from '../../../../common/license';
import { ILicense } from '../../../../../licensing/common/types';
import { licenseMock } from '../../../../../licensing/common/licensing.mock';
import { parseExperimentalConfigValue } from '../../../../common/experimental_features';
import { EndpointAppContextService } from '../../endpoint_app_context_services';
import { createConditionEntry, createEntryMatch } from './mapping';
Expand All @@ -36,6 +40,7 @@ import {
TrustedAppNotFoundError,
TrustedAppVersionConflictError,
TrustedAppPolicyNotExistsError,
TrustedAppPolicyPermissionsError,
} from './errors';
import { updateExceptionListItemImplementationMock } from './test_utils';
import { Logger } from '@kbn/logging';
Expand Down Expand Up @@ -95,13 +100,19 @@ const TRUSTED_APP: TrustedApp = {
],
};

const Platinum = licenseMock.createLicense({ license: { type: 'platinum', mode: 'platinum' } });
const Gold = licenseMock.createLicense({ license: { type: 'gold', mode: 'gold' } });

const packagePolicyClient =
createPackagePolicyServiceMock() as jest.Mocked<PackagePolicyServiceInterface>;

describe('handlers', () => {
beforeEach(() => {
packagePolicyClient.getByIDs.mockReset();
});
const licenseEmitter: Subject<ILicense> = new Subject();
const licenseService = new LicenseService();
licenseService.start(licenseEmitter);

const createAppContextMock = () => {
const context = {
Expand All @@ -112,6 +123,7 @@ describe('handlers', () => {
};

context.service.getPackagePolicyService = () => packagePolicyClient;
context.service.getLicenseService = () => licenseService;

// Ensure that `logFactory.get()` always returns the same instance for the same given prefix
const instances = new Map<string, ReturnType<typeof context.logFactory.get>>();
Expand Down Expand Up @@ -151,6 +163,7 @@ describe('handlers', () => {
beforeEach(() => {
appContextMock = createAppContextMock();
exceptionsListClient = listMock.getExceptionListClient() as jest.Mocked<ExceptionListClient>;
licenseEmitter.next(Platinum);
});

describe('getTrustedAppsDeleteRouteHandler', () => {
Expand Down Expand Up @@ -261,6 +274,27 @@ describe('handlers', () => {
body: { message: error.message, attributes: { type: error.type } },
});
});

it('should return error when license under platinum and by policy', async () => {
licenseEmitter.next(Gold);
const mockResponse = httpServerMock.createResponseFactory();
packagePolicyClient.getByIDs.mockReset();
packagePolicyClient.getByIDs.mockResolvedValueOnce(getPackagePoliciesResponse());

const trustedAppByPolicy = getTrustedAppByPolicy();
await createTrustedAppHandler(
createHandlerContextMock(),
httpServerMock.createKibanaRequest({ body: trustedAppByPolicy }),
mockResponse
);

const error = new TrustedAppPolicyPermissionsError();

expect(appContextMock.logFactory.get('trusted_apps').error).toHaveBeenCalledWith(error);
expect(mockResponse.badRequest).toHaveBeenCalledWith({
body: { message: error.message, attributes: { type: error.type } },
});
});
});

describe('getTrustedAppsListRouteHandler', () => {
Expand Down Expand Up @@ -591,5 +625,22 @@ describe('handlers', () => {
])
);
});

it('should return error when license under platinum and by policy', async () => {
licenseEmitter.next(Gold);
packagePolicyClient.getByIDs.mockReset();
packagePolicyClient.getByIDs.mockResolvedValueOnce(getPackagePoliciesResponse());

const trustedAppByPolicy = getTrustedAppByPolicy();
await updateHandler(
createHandlerContextMock(),
httpServerMock.createKibanaRequest({ body: trustedAppByPolicy }),
mockResponse
);

expect(appContextMock.logFactory.get('trusted_apps').error).toHaveBeenCalledWith(
new TrustedAppPolicyPermissionsError()
);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
TrustedAppNotFoundError,
TrustedAppVersionConflictError,
TrustedAppPolicyNotExistsError,
TrustedAppPolicyPermissionsError,
} from './errors';
import { PackagePolicyServiceInterface } from '../../../../../fleet/server';

Expand Down Expand Up @@ -87,6 +88,11 @@ const errorHandler = <E extends Error>(
return res.badRequest({ body: { message: error.message, attributes: { type: error.type } } });
}

if (error instanceof TrustedAppPolicyPermissionsError) {
logger.error(error);
return res.badRequest({ body: { message: error.message, attributes: { type: error.type } } });
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggestion: just send the error:

Suggested change
return res.badRequest({ body: { message: error.message, attributes: { type: error.type } } });
return res.badRequest({ body: error });

}

if (error instanceof TrustedAppVersionConflictError) {
logger.error(error);
return res.conflict({ body: error });
Expand Down Expand Up @@ -177,7 +183,8 @@ export const getTrustedAppsCreateRouteHandler = (
exceptionListClientFromContext(context),
context.core.savedObjects.client,
packagePolicyClientFromEndpointContext(endpointAppContext),
body
body,
endpointAppContext.service.getLicenseService().isAtLeast('platinum')
),
});
} catch (error) {
Expand Down Expand Up @@ -206,7 +213,8 @@ export const getTrustedAppsUpdateRouteHandler = (
context.core.savedObjects.client,
packagePolicyClientFromEndpointContext(endpointAppContext),
req.params.id,
body
body,
endpointAppContext.service.getLicenseService().isAtLeast('platinum')
),
});
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
TrustedAppNotFoundError,
TrustedAppVersionConflictError,
TrustedAppPolicyNotExistsError,
TrustedAppPolicyPermissionsError,
} from './errors';
import { toUpdateTrustedApp } from '../../../../common/endpoint/service/trusted_apps/to_update_trusted_app';
import { updateExceptionListItemImplementationMock } from './test_utils';
Expand Down Expand Up @@ -135,7 +136,8 @@ describe('service', () => {
'1234234659af249ddf3e40864e9fb241'
),
],
}
},
true
);

expect(result).toEqual({ data: TRUSTED_APP });
Expand Down Expand Up @@ -163,7 +165,8 @@ describe('service', () => {
'1234234659af249ddf3e40864e9fb241'
),
],
}
},
true
);

expect(result).toEqual({ data: TRUSTED_APP });
Expand All @@ -175,28 +178,67 @@ describe('service', () => {
packagePolicyClient.getByIDs.mockReset();
packagePolicyClient.getByIDs.mockResolvedValueOnce(getPackagePoliciesResponse());
await expect(
createTrustedApp(exceptionsListClient, savedObjectClient, packagePolicyClient, {
name: 'linux trusted app 1',
description: 'Linux trusted app 1',
effectScope: {
type: 'policy',
policies: [
'e5cbb9cf-98aa-4303-a04b-6a1165915079',
'9da95be9-9bee-4761-a8c4-28d6d9bd8c71',
createTrustedApp(
exceptionsListClient,
savedObjectClient,
packagePolicyClient,
{
name: 'linux trusted app 1',
description: 'Linux trusted app 1',
effectScope: {
type: 'policy',
policies: [
'e5cbb9cf-98aa-4303-a04b-6a1165915079',
'9da95be9-9bee-4761-a8c4-28d6d9bd8c71',
],
},
os: OperatingSystem.LINUX,
entries: [
createConditionEntry(ConditionEntryField.PATH, 'wildcard', '/bin/malware'),
createConditionEntry(
ConditionEntryField.HASH,
'wildcard',
'1234234659af249ddf3e40864e9fb241'
),
],
},
os: OperatingSystem.LINUX,
entries: [
createConditionEntry(ConditionEntryField.PATH, 'wildcard', '/bin/malware'),
createConditionEntry(
ConditionEntryField.HASH,
'wildcard',
'1234234659af249ddf3e40864e9fb241'
),
],
})
true
)
).rejects.toBeInstanceOf(TrustedAppPolicyNotExistsError);
});

it('should throw wwhen license under platinum and by policy', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

extra w in wwhen 😁

packagePolicyClient.getByIDs.mockReset();
packagePolicyClient.getByIDs.mockResolvedValueOnce(getPackagePoliciesResponse());
await expect(
createTrustedApp(
exceptionsListClient,
savedObjectClient,
packagePolicyClient,
{
name: 'linux trusted app 1',
description: 'Linux trusted app 1',
effectScope: {
type: 'policy',
policies: [
'e5cbb9cf-98aa-4303-a04b-6a1165915079',
'9da95be9-9bee-4761-a8c4-28d6d9bd8c71',
],
},
os: OperatingSystem.LINUX,
entries: [
createConditionEntry(ConditionEntryField.PATH, 'wildcard', '/bin/malware'),
createConditionEntry(
ConditionEntryField.HASH,
'wildcard',
'1234234659af249ddf3e40864e9fb241'
),
],
},
false
)
).rejects.toBeInstanceOf(TrustedAppPolicyPermissionsError);
});
});

describe('getTrustedAppsList', () => {
Expand Down Expand Up @@ -321,7 +363,8 @@ describe('service', () => {
savedObjectClient,
packagePolicyClient,
TRUSTED_APP.id,
trustedAppForUpdate
trustedAppForUpdate,
true
)
).resolves.toEqual({
data: {
Expand Down Expand Up @@ -357,7 +400,8 @@ describe('service', () => {
savedObjectClient,
packagePolicyClient,
TRUSTED_APP.id,
toUpdateTrustedApp(TRUSTED_APP)
toUpdateTrustedApp(TRUSTED_APP),
true
)
).rejects.toBeInstanceOf(TrustedAppNotFoundError);
});
Expand All @@ -374,7 +418,8 @@ describe('service', () => {
savedObjectClient,
packagePolicyClient,
TRUSTED_APP.id,
toUpdateTrustedApp(TRUSTED_APP)
toUpdateTrustedApp(TRUSTED_APP),
true
)
).rejects.toBeInstanceOf(TrustedAppVersionConflictError);
});
Expand All @@ -393,12 +438,13 @@ describe('service', () => {
savedObjectClient,
packagePolicyClient,
TRUSTED_APP.id,
toUpdateTrustedApp(TRUSTED_APP)
toUpdateTrustedApp(TRUSTED_APP),
true
)
).rejects.toBeInstanceOf(TrustedAppNotFoundError);
});

it("should throw wrong policy error if some policy doesn't exists", async () => {
it("should throw wrong policy error if some policy doesn't exists during update", async () => {
packagePolicyClient.getByIDs.mockReset();
packagePolicyClient.getByIDs.mockResolvedValueOnce(getPackagePoliciesResponse());
const trustedAppByPolicy = getTrustedAppByPolicy();
Expand All @@ -408,10 +454,27 @@ describe('service', () => {
savedObjectClient,
packagePolicyClient,
trustedAppByPolicy.id,
toUpdateTrustedApp(trustedAppByPolicy as MaybeImmutable<TrustedApp>)
toUpdateTrustedApp(trustedAppByPolicy as MaybeImmutable<TrustedApp>),
Copy link
Contributor

Choose a reason for hiding this comment

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

unrelated code?

true
)
).rejects.toBeInstanceOf(TrustedAppPolicyNotExistsError);
});

it('should throw when license under platinum and by policy', async () => {
packagePolicyClient.getByIDs.mockReset();
packagePolicyClient.getByIDs.mockResolvedValueOnce(getPackagePoliciesResponse());
const trustedAppByPolicy = getTrustedAppByPolicy();
await expect(
updateTrustedApp(
exceptionsListClient,
savedObjectClient,
packagePolicyClient,
trustedAppByPolicy.id,
toUpdateTrustedApp(trustedAppByPolicy as MaybeImmutable<TrustedApp>),
false
)
).rejects.toBeInstanceOf(TrustedAppPolicyPermissionsError);
});
});

describe('getTrustedApp', () => {
Expand Down
Loading