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

[Fleet] Connect fleet to policy change update #53201

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 7 additions & 5 deletions x-pack/legacy/plugins/fleet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,13 @@ export function fleet(kibana: any) {
config: () => config,
configPrefix: CONFIG_PREFIX,
init(server: any) {
server.newPlatform.setup.plugins.encryptedSavedObjects.registerType({
type: 'enrollment_api_keys',
attributesToEncrypt: new Set(['api_key']),
attributesToExcludeFromAAD: new Set(['enrollment_rules']),
});
// TODO https://github.com/elastic/kibana/issues/53199
// server.newPlatform.setup.plugins.encryptedSavedObjects.registerType({
Copy link
Contributor

Choose a reason for hiding this comment

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

will commenting this out affect the current functionality of policy api keys?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's going to stop encrypting api keys, it's still going to work but it's not secure and production ready

// type: 'enrollment_api_keys',
// // attributesToEncrypt: new Set(['api_key']),
// attributesToEncrypt: new Set([]),
// attributesToExcludeFromAAD: new Set(['enrollment_rules']),
// });
server.plugins.xpack_main.registerFeature({
id: 'fleet',
name: 'Fleet',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export const AgentEnrollmentFlyout: React.FC<RouterProps> = ({ onClose, policies
{apiKeyListVisible && (
<>
<EuiSpacer size="m" />
<EnrollmentApiKeysTable />
<EnrollmentApiKeysTable onChange={() => enrollmentApiKeys.refresh()} />
</>
)}
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import { useLibs, usePagination } from '../../../../hooks';
import { useEnrollmentApiKeys, useEnrollmentApiKey } from './hooks';
import { ConfirmDeleteModal } from './confirm_delete_modal';
import { CreateApiKeyForm } from './create_api_key_form';

export { useEnrollmentApiKeys, useEnrollmentApiKey } from './hooks';

export const EnrollmentApiKeysTable: React.FC = () => {
export const EnrollmentApiKeysTable: React.FC<{
onChange: () => void;
}> = ({ onChange }) => {
const { enrollmentApiKeys } = useLibs();
const [confirmDeleteApiKeyId, setConfirmDeleteApiKeyId] = useState<string | null>(null);
const { pagination } = usePagination();
Expand Down Expand Up @@ -81,7 +82,12 @@ export const EnrollmentApiKeysTable: React.FC = () => {
columns={columns}
/>
<EuiSpacer size={'s'} />
<CreateApiKeyButton onChange={() => refresh()} />
<CreateApiKeyButton
onChange={() => {
refresh();
onChange();
}}
/>
</>
);
};
Expand Down
24 changes: 0 additions & 24 deletions x-pack/legacy/plugins/fleet/server/kibana.index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,8 @@

import { compose } from './libs/compose/kibana';
import { initRestApi } from './routes/init_api';
import { FrameworkUser } from './adapters/framework/adapter_types';
import { PolicyUpdatedEvent } from '../common/types/domain_data';

export const initServerWithKibana = (hapiServer: any) => {
const libs = compose(hapiServer);
initRestApi(hapiServer, libs);
// expose methods
libs.framework.expose('policyUpdated', async function handlePolicyUpdate(
event: PolicyUpdatedEvent,
user: FrameworkUser = {
kind: 'internal',
}
) {
if (event.type === 'created') {
await libs.apiKeys.generateEnrollmentApiKey(user, {
policyId: event.policyId,
});
}

if (event.type === 'updated') {
await libs.agentsPolicy.updateAgentsForPolicyId(user, event.policyId);
}

if (event.type === 'deleted') {
await libs.agents.unenrollForPolicy(user, event.policyId);
await libs.apiKeys.deleteEnrollmentApiKeyForPolicyId(user, event.policyId);
}
});
};
7 changes: 6 additions & 1 deletion x-pack/legacy/plugins/fleet/server/libs/compose/kibana.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { InstallLib } from '../install';
import { ElasticsearchAdapter } from '../../adapters/elasticsearch/default';
import { AgentPolicyLib } from '../agent_policy';
import { AgentEventLib } from '../agent_event';
import { makePolicyUpdateHandler } from '../policy_update';

export function compose(server: any): FleetServerLib {
const frameworkAdapter = new FrameworkAdapter(server);
Expand Down Expand Up @@ -59,7 +60,7 @@ export function compose(server: any): FleetServerLib {

const install = new InstallLib(framework);

return {
const libs = {
agents,
agentsPolicy,
agentEvents,
Expand All @@ -69,4 +70,8 @@ export function compose(server: any): FleetServerLib {
install,
framework,
};
const policyUpdateHandler = makePolicyUpdateHandler(libs);
server.plugins.ingest.policy.registerPolicyUpdateHandler(policyUpdateHandler);

return libs;
}
33 changes: 33 additions & 0 deletions x-pack/legacy/plugins/fleet/server/libs/policy_update.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { FrameworkUser } from '../adapters/framework/adapter_types';
import { FleetServerLib } from './types';

export function makePolicyUpdateHandler(libs: FleetServerLib) {
return async function policyUpdateHandler(user: FrameworkUser, action: string, policyId: string) {
const internalUser = libs.framework.getInternalUser();

if (action === 'created') {
if (policyId === 'default') {
// TODO wait for #53111 to be fixed
return;
}
await libs.apiKeys.generateEnrollmentApiKey(internalUser, {
policyId,
});
}

if (action === 'updated') {
await libs.agentsPolicy.updateAgentsForPolicyId(internalUser, policyId);
}

if (action === 'deleted') {
await libs.agents.unenrollForPolicy(internalUser, policyId);
await libs.apiKeys.deleteEnrollmentApiKeyForPolicyId(internalUser, policyId);
}
};
}
Loading