Skip to content

Commit

Permalink
Restore internalSavedObjectsClient in app context
Browse files Browse the repository at this point in the history
  • Loading branch information
John Schulz committed Feb 21, 2020
1 parent 9871743 commit fbc4821
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
7 changes: 6 additions & 1 deletion x-pack/plugins/ingest_manager/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
PluginInitializerContext,
SavedObjectsLegacyService,
} from 'kibana/server';

import { SavedObjectsClient } from '../../../../src/core/server';
import { LicensingPluginSetup } from '../../licensing/server';
import { EncryptedSavedObjectsPluginStart } from '../../encrypted_saved_objects/server';
import { SecurityPluginSetup } from '../../security/server';
Expand Down Expand Up @@ -55,6 +55,7 @@ export interface IngestManagerAppContext {
encryptedSavedObjects: EncryptedSavedObjectsPluginStart;
security?: SecurityPluginSetup;
config$?: Observable<IngestManagerConfigType>;
internalSavedObjectsClient: SavedObjectsClient;
}

export class IngestManagerPlugin implements Plugin {
Expand Down Expand Up @@ -133,10 +134,14 @@ export class IngestManagerPlugin implements Plugin {
encryptedSavedObjects: EncryptedSavedObjectsPluginStart;
}
) {
const internalSavedObjectsClient = new SavedObjectsClient(
core.savedObjects.createInternalRepository()
);
appContextService.start({
encryptedSavedObjects: plugins.encryptedSavedObjects,
security: this.security,
config$: this.config$,
internalSavedObjectsClient,
});
}

Expand Down
7 changes: 4 additions & 3 deletions x-pack/plugins/ingest_manager/server/routes/agent/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
} from '../../types';
import * as AgentService from '../../services/agents';
import * as APIKeyService from '../../services/api_keys';
import { appContextService } from '../../services/app_context';

export const getAgentHandler: RequestHandler<TypeOf<
typeof GetOneAgentRequestSchema.params
Expand Down Expand Up @@ -162,7 +163,7 @@ export const postAgentCheckinHandler: RequestHandler<
TypeOf<typeof PostAgentCheckinRequestSchema.body>
> = async (context, request, response) => {
try {
const soClient = context.core.savedObjects.client;
const soClient = appContextService.getInternalSavedObjectsClient();
const callCluster = context.core.elasticsearch.adminClient.callAsCurrentUser;
const res = await APIKeyService.verifyAccessApiKey({ headers: request.headers, callCluster });
if (!res.valid) {
Expand Down Expand Up @@ -211,7 +212,7 @@ export const postAgentAcksHandler: RequestHandler<
TypeOf<typeof PostAgentAcksRequestSchema.body>
> = async (context, request, response) => {
try {
const soClient = context.core.savedObjects.client;
const soClient = appContextService.getInternalSavedObjectsClient();
const callCluster = context.core.elasticsearch.adminClient.callAsCurrentUser;
const res = await APIKeyService.verifyAccessApiKey({ headers: request.headers, callCluster });
if (!res.valid) {
Expand Down Expand Up @@ -253,7 +254,7 @@ export const postAgentEnrollHandler: RequestHandler<
TypeOf<typeof PostAgentEnrollRequestSchema.body>
> = async (context, request, response) => {
try {
const soClient = context.core.savedObjects.client;
const soClient = appContextService.getInternalSavedObjectsClient();
const callCluster = context.core.elasticsearch.adminClient.callAsCurrentUser;
const res = await APIKeyService.verifyEnrollmentAPIKey({
soClient,
Expand Down
10 changes: 10 additions & 0 deletions x-pack/plugins/ingest_manager/server/services/app_context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
import { BehaviorSubject, Observable } from 'rxjs';
import { first } from 'rxjs/operators';
import { SavedObjectsClientContract } from 'kibana/server';
import { EncryptedSavedObjectsPluginStart } from '../../../encrypted_saved_objects/server';
import { SecurityPluginSetup } from '../../../security/server';
import { IngestManagerConfigType } from '../../common';
Expand All @@ -15,10 +16,12 @@ class AppContextService {
private security: SecurityPluginSetup | undefined;
private config$?: Observable<IngestManagerConfigType>;
private configSubject$?: BehaviorSubject<IngestManagerConfigType>;
private internalSavedObjectsClient: SavedObjectsClientContract | undefined;

public async start(appContext: IngestManagerAppContext) {
this.encryptedSavedObjects = appContext.encryptedSavedObjects;
this.security = appContext.security;
this.internalSavedObjectsClient = appContext.internalSavedObjectsClient;

if (appContext.config$) {
this.config$ = appContext.config$;
Expand All @@ -45,6 +48,13 @@ class AppContextService {
public getConfig$() {
return this.config$;
}

public getInternalSavedObjectsClient() {
if (!this.internalSavedObjectsClient) {
throw new Error('No internal savedObjectsClient');
}
return this.internalSavedObjectsClient;
}
}

export const appContextService = new AppContextService();

0 comments on commit fbc4821

Please sign in to comment.