Skip to content

Commit

Permalink
SiemClient API uses getter function instead of direct property access
Browse files Browse the repository at this point in the history
  • Loading branch information
rylnd committed Apr 20, 2020
1 parent 8314d1c commit f92d564
Show file tree
Hide file tree
Showing 13 changed files with 16 additions and 14 deletions.
4 changes: 2 additions & 2 deletions x-pack/plugins/siem/server/client/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { createMockConfig } from '../lib/detection_engine/routes/__mocks__';
import { SiemClient } from './client';

describe('SiemClient', () => {
describe('#signalsIndex', () => {
describe('#getSignalsIndex', () => {
it('returns the index scoped to the specified spaceId', () => {
const mockConfig = {
...createMockConfig(),
Expand All @@ -18,7 +18,7 @@ describe('SiemClient', () => {
const spaceId = 'fooSpace';
const client = new SiemClient(spaceId, mockConfig);

expect(client.signalsIndex).toEqual('mockSignalsIndex-fooSpace');
expect(client.getSignalsIndex()).toEqual('mockSignalsIndex-fooSpace');
});
});
});
4 changes: 3 additions & 1 deletion x-pack/plugins/siem/server/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
import { ConfigType } from '../config';

export class SiemClient {
public readonly signalsIndex: string;
private readonly signalsIndex: string;

constructor(private spaceId: string, private config: ConfigType) {
const configuredSignalsIndex = this.config.signalsIndex;

this.signalsIndex = `${configuredSignalsIndex}-${this.spaceId}`;
}

public getSignalsIndex = (): string => this.signalsIndex;
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const createIndexRoute = (router: IRouter) => {
return siemResponse.error({ statusCode: 404 });
}

const index = siemClient.signalsIndex;
const index = siemClient.getSignalsIndex();
const indexExists = await getIndexExists(callCluster, index);
if (indexExists) {
return siemResponse.error({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const deleteIndexRoute = (router: IRouter) => {
}

const callCluster = clusterClient.callAsCurrentUser;
const index = siemClient.signalsIndex;
const index = siemClient.getSignalsIndex();
const indexExists = await getIndexExists(callCluster, index);

if (!indexExists) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const readIndexRoute = (router: IRouter) => {
return siemResponse.error({ statusCode: 404 });
}

const index = siemClient.signalsIndex;
const index = siemClient.getSignalsIndex();
const indexExists = await getIndexExists(clusterClient.callAsCurrentUser, index);

if (indexExists) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const readPrivilegesRoute = (
return siemResponse.error({ statusCode: 404 });
}

const index = siemClient.signalsIndex;
const index = siemClient.getSignalsIndex();
const clusterPrivileges = await readPrivileges(clusterClient.callAsCurrentUser, index);
const privileges = merge(clusterPrivileges, {
is_authenticated: security?.authc.isAuthenticated(request) ?? false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export const createRulesBulkRoute = (router: IRouter) => {
try {
validateLicenseForRuleType({ license: context.licensing.license, ruleType: type });

const finalIndex = outputIndex ?? siemClient.signalsIndex;
const finalIndex = outputIndex ?? siemClient.getSignalsIndex();
const indexExists = await getIndexExists(clusterClient.callAsCurrentUser, finalIndex);
if (!indexExists) {
return createBulkErrorObject({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export const createRulesRoute = (router: IRouter): void => {
return siemResponse.error({ statusCode: 404 });
}

const finalIndex = outputIndex ?? siemClient.signalsIndex;
const finalIndex = outputIndex ?? siemClient.getSignalsIndex();
const indexExists = await getIndexExists(clusterClient.callAsCurrentUser, finalIndex);
if (!indexExists) {
return siemResponse.error({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export const importRulesRoute = (router: IRouter, config: ConfigType) => {
ruleType: type,
});

const signalsIndex = siemClient.signalsIndex;
const signalsIndex = siemClient.getSignalsIndex();
const indexExists = await getIndexExists(
clusterClient.callAsCurrentUser,
signalsIndex
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export const updateRulesBulkRoute = (router: IRouter) => {
version,
exceptions_list,
} = payloadRule;
const finalIndex = outputIndex ?? siemClient.signalsIndex;
const finalIndex = outputIndex ?? siemClient.getSignalsIndex();
const idOrRuleIdOrUnknown = id ?? ruleId ?? '(unknown id)';
try {
validateLicenseForRuleType({ license: context.licensing.license, ruleType: type });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export const updateRulesRoute = (router: IRouter) => {
return siemResponse.error({ statusCode: 404 });
}

const finalIndex = outputIndex ?? siemClient.signalsIndex;
const finalIndex = outputIndex ?? siemClient.getSignalsIndex();
const rule = await updateRules({
alertsClient,
actionsClient,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const setSignalsStatusRoute = (router: IRouter) => {
}
try {
const result = await clusterClient.callAsCurrentUser('updateByQuery', {
index: siemClient.signalsIndex,
index: siemClient.getSignalsIndex(),
body: {
script: {
source: `ctx._source.signal.status = '${status}'`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const querySignalsRoute = (router: IRouter) => {

try {
const result = await clusterClient.callAsCurrentUser('search', {
index: siemClient.signalsIndex,
index: siemClient.getSignalsIndex(),
body: { query, aggs, _source, track_total_hits, size },
ignoreUnavailable: true,
});
Expand Down

0 comments on commit f92d564

Please sign in to comment.