Skip to content

Commit

Permalink
WIP. Strawman for doing setup in plugin#start
Browse files Browse the repository at this point in the history
Still get failures while accessing certain urls. Not sure if it's the first time or while some other logic.

Errors like 'No admin user configured', 'No default output', 'unable to authenticate user [fleet_enroll] for REST request [/_security/api_key]', etc
  • Loading branch information
John Schulz committed Apr 6, 2020
1 parent d67f222 commit 0ddd227
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 16 deletions.
2 changes: 1 addition & 1 deletion x-pack/plugins/endpoint/kibana.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.0",
"kibanaVersion": "kibana",
"configPath": ["xpack", "endpoint"],
"requiredPlugins": ["features", "embeddable", "data", "dataEnhanced"],
"requiredPlugins": ["features", "embeddable", "data", "dataEnhanced", "ingestManager"],
"server": true,
"ui": true
}
6 changes: 5 additions & 1 deletion x-pack/plugins/endpoint/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Plugin, CoreSetup, AppMountParameters, CoreStart } from 'kibana/public'
import { EmbeddableSetup } from 'src/plugins/embeddable/public';
import { DataPublicPluginStart } from 'src/plugins/data/public';
import { i18n } from '@kbn/i18n';
import { IngestManagerStart } from '../../ingest_manager/public';
import { ResolverEmbeddableFactory } from './embeddables/resolver';

export type EndpointPluginStart = void;
Expand All @@ -18,6 +19,7 @@ export interface EndpointPluginSetupDependencies {
}
export interface EndpointPluginStartDependencies {
data: DataPublicPluginStart;
ingestManager: IngestManagerStart;
}

/**
Expand Down Expand Up @@ -63,7 +65,9 @@ export class EndpointPlugin
);
}

public start() {}
public start(core: CoreStart, deps: EndpointPluginStartDependencies) {
console.log('endpoint#start', { core, deps }, deps.ingestManager.isInitialized);
}

public stop() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,19 @@ import { WithoutHeaderLayout } from '../../../layouts';
export const SetupPage: React.FunctionComponent<{
refresh: () => Promise<void>;
}> = ({ refresh }) => {
console.log('SetupPage');
const [isFormLoading, setIsFormLoading] = useState<boolean>(false);
const core = useCore();

const onSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
setIsFormLoading(true);
try {
await sendRequest({
method: 'post',
path: fleetSetupRouteService.postFleetSetupPath(),
});
// console.log('fleet setup page does POST /fleet/setup');
// await sendRequest({
// method: 'post',
// path: fleetSetupRouteService.postFleetSetupPath(),
// });
await refresh();
} catch (error) {
core.notifications.toasts.addDanger(error.message);
Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/ingest_manager/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import { PluginInitializerContext } from 'src/core/public';
import { IngestManagerPlugin } from './plugin';

export { IngestManagerSetup, IngestManagerStart } from './plugin';

export const plugin = (initializerContext: PluginInitializerContext) => {
return new IngestManagerPlugin(initializerContext);
};
24 changes: 22 additions & 2 deletions x-pack/plugins/ingest_manager/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import { IngestManagerConfigType } from '../common/types';
export { IngestManagerConfigType } from '../common/types';

export type IngestManagerSetup = void;
export type IngestManagerStart = void;
export interface IngestManagerStart {
isInitialized: boolean;
}

export interface IngestManagerSetupDeps {
licensing: LicensingPluginSetup;
Expand All @@ -42,6 +44,7 @@ export class IngestManagerPlugin
}

public setup(core: CoreSetup, deps: IngestManagerSetupDeps) {
console.log('plugin#setup', { core, deps });
const config = this.config;
// Register main Ingest Manager app
core.application.register({
Expand All @@ -55,13 +58,30 @@ export class IngestManagerPlugin
IngestManagerStartDeps,
IngestManagerStart
];
console.log('plugin#setup mount()');
const { renderApp } = await import('./applications/ingest_manager');
return renderApp(coreStart, params, deps, startDeps, config);
},
});
}

public start(core: CoreStart) {}
public async start(core: CoreStart): Promise<IngestManagerStart> {
const config = this.config;
core.http.post('/api/ingest_manager/setup');

let isInitialized = false;
if (config.fleet.enabled) {
const results = await core.http.get('/api/ingest_manager/fleet/setup');
console.log({ results });
if (!results.isInitialized) {
const retry = await core.http.post('/api/ingest_manager/fleet/setup');
console.log({ retry });
}
isInitialized = results.isInitialized;
}

return { isInitialized };
}

public stop() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ export const createFleetSetupHandler: RequestHandler = async (context, request,
try {
const soClient = context.core.savedObjects.client;
const callCluster = context.core.elasticsearch.adminClient.callAsCurrentUser;
await setupIngestManager(soClient, callCluster);
// console.log('createFleetSetupHandler setupIngest');
// await setupIngestManager(soClient, callCluster);
console.log('createFleetSetupHandler setupFleet');
await setupFleet(soClient, callCluster);

return response.ok({
Expand All @@ -52,6 +54,7 @@ export const ingestManagerSetupHandler: RequestHandler = async (context, request
const soClient = context.core.savedObjects.client;
const callCluster = context.core.elasticsearch.adminClient.callAsCurrentUser;
try {
console.log('ingestManagerSetupHandler setupIngest');
await setupIngestManager(soClient, callCluster);
return response.ok({
body: { isInitialized: true },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ export async function createAPIKey(
name: string,
roleDescriptors: any
) {
console.log('createAPIKey', { name, roleDescriptors });
const adminUser = await outputService.getAdminUser(soClient);
console.log({ adminUser });
if (!adminUser) {
throw new Error('No admin user configured');
}
Expand Down
21 changes: 14 additions & 7 deletions x-pack/plugins/ingest_manager/server/services/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,28 +84,35 @@ export async function setupFleet(
],
},
});
console.log('created fleet_enroll roll');
const password = generateRandomPassword();
// Create fleet enroll user
await callCluster('transport.request', {
const a = await callCluster('transport.request', {
method: 'PUT',
path: `/_security/user/${FLEET_ENROLL_USERNAME}`,
body: {
password,
roles: [FLEET_ENROLL_ROLE],
},
});

console.log('created fleet_enroll user', a);
// save fleet admin user
await outputService.updateOutput(soClient, await outputService.getDefaultOutputId(soClient), {
fleet_enroll_username: FLEET_ENROLL_USERNAME,
fleet_enroll_password: password,
});
const b = await outputService.updateOutput(
soClient,
await outputService.getDefaultOutputId(soClient),
{
fleet_enroll_username: FLEET_ENROLL_USERNAME,
fleet_enroll_password: password,
}
);
console.log('created fleet admin user', b);

// Generate default enrollment key
await generateEnrollmentAPIKey(soClient, {
const c = await generateEnrollmentAPIKey(soClient, {
name: 'Default',
configId: await agentConfigService.getDefaultAgentConfigId(soClient),
});
console.log('generatged default enrollment key', c);
}

function generateRandomPassword() {
Expand Down

0 comments on commit 0ddd227

Please sign in to comment.