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] Remove fleetServerEnabled feature flag and use fleet system indices #92422

Merged
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
14 changes: 7 additions & 7 deletions x-pack/plugins/fleet/common/services/agent_status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* 2.0.
*/

import { AGENT_POLLING_THRESHOLD_MS, AGENT_SAVED_OBJECT_TYPE } from '../constants';
import { AGENT_POLLING_THRESHOLD_MS } from '../constants';
import type { Agent, AgentStatus } from '../types';

export function getAgentStatus(agent: Agent, now: number = Date.now()): AgentStatus {
Expand Down Expand Up @@ -42,35 +42,35 @@ export function getAgentStatus(agent: Agent, now: number = Date.now()): AgentSta
}

export function buildKueryForEnrollingAgents() {
return `not (${AGENT_SAVED_OBJECT_TYPE}.last_checkin:*)`;
return 'not (last_checkin:*)';
}

export function buildKueryForUnenrollingAgents() {
return `${AGENT_SAVED_OBJECT_TYPE}.unenrollment_started_at:*`;
return 'unenrollment_started_at:*';
}

export function buildKueryForOnlineAgents() {
return `not (${buildKueryForOfflineAgents()}) AND not (${buildKueryForErrorAgents()}) AND not (${buildKueryForUpdatingAgents()})`;
}

export function buildKueryForErrorAgents() {
return `${AGENT_SAVED_OBJECT_TYPE}.last_checkin_status:error or ${AGENT_SAVED_OBJECT_TYPE}.last_checkin_status:degraded`;
return 'last_checkin_status:error or .last_checkin_status:degraded';
}

export function buildKueryForOfflineAgents() {
return `${AGENT_SAVED_OBJECT_TYPE}.last_checkin < now-${
return `last_checkin < now-${
(4 * AGENT_POLLING_THRESHOLD_MS) / 1000
}s AND not (${buildKueryForErrorAgents()}) AND not ( ${buildKueryForUpdatingAgents()} )`;
}

export function buildKueryForUpgradingAgents() {
return `(${AGENT_SAVED_OBJECT_TYPE}.upgrade_started_at:*) and not (${AGENT_SAVED_OBJECT_TYPE}.upgraded_at:*)`;
return '(upgrade_started_at:*) and not (upgraded_at:*)';
}

export function buildKueryForUpdatingAgents() {
return `(${buildKueryForUpgradingAgents()}) or (${buildKueryForEnrollingAgents()}) or (${buildKueryForUnenrollingAgents()})`;
}

export function buildKueryForInactiveAgents() {
return `${AGENT_SAVED_OBJECT_TYPE}.active:false`;
return `active:false`;
}
1 change: 0 additions & 1 deletion x-pack/plugins/fleet/common/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export interface FleetConfigType {
registryUrl?: string;
registryProxyUrl?: string;
agents: {
fleetServerEnabled: boolean;
enabled: boolean;
tlsCheckDisabled: boolean;
pollingRequestTimeout: number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export const createConfigurationMock = (): FleetConfigType => {
registryProxyUrl: '',
agents: {
enabled: true,
fleetServerEnabled: false,
tlsCheckDisabled: true,
pollingRequestTimeout: 1000,
maxConcurrentConnections: 100,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ import { FormattedMessage } from '@kbn/i18n/react';

import { AgentPolicy } from '../../../../types';
import { SearchBar } from '../../../../components';
import { AGENTS_INDEX, AGENT_SAVED_OBJECT_TYPE } from '../../../../constants';
import { useConfig } from '../../../../hooks';
import { AGENTS_INDEX } from '../../../../constants';

const statusFilters = [
{
Expand Down Expand Up @@ -78,7 +77,6 @@ export const SearchAndFilterBar: React.FunctionComponent<{
showUpgradeable,
onShowUpgradeableChange,
}) => {
const config = useConfig();
// Policies state for filtering
const [isAgentPoliciesFilterOpen, setIsAgentPoliciesFilterOpen] = useState<boolean>(false);

Expand Down Expand Up @@ -112,13 +110,7 @@ export const SearchAndFilterBar: React.FunctionComponent<{
onSubmitSearch(newSearch);
}
}}
{...(config.agents.fleetServerEnabled
? {
indexPattern: AGENTS_INDEX,
}
: {
fieldPrefix: AGENT_SAVED_OBJECT_TYPE,
})}
indexPattern={AGENTS_INDEX}
/>
</EuiFlexItem>
<EuiFlexItem grow={2}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ import {
} from '@elastic/eui';
import { FormattedMessage, FormattedDate } from '@kbn/i18n/react';

import {
ENROLLMENT_API_KEYS_INDEX,
ENROLLMENT_API_KEYS_SAVED_OBJECT_TYPE,
} from '../../../constants';
import { ENROLLMENT_API_KEYS_INDEX } from '../../../constants';
import {
useBreadcrumbs,
usePagination,
Expand All @@ -33,7 +30,6 @@ import {
sendGetOneEnrollmentAPIKey,
useStartServices,
sendDeleteOneEnrollmentAPIKey,
useConfig,
} from '../../../hooks';
import { EnrollmentAPIKey } from '../../../types';
import { SearchBar } from '../../../components/search_bar';
Expand Down Expand Up @@ -160,7 +156,6 @@ const DeleteButton: React.FunctionComponent<{ apiKey: EnrollmentAPIKey; refresh:

export const EnrollmentTokenListPage: React.FunctionComponent<{}> = () => {
useBreadcrumbs('fleet_enrollment_tokens');
const config = useConfig();
const [flyoutOpen, setFlyoutOpen] = useState(false);
const [search, setSearch] = useState('');
const { pagination, setPagination, pageSizeOptions } = usePagination();
Expand Down Expand Up @@ -288,13 +283,7 @@ export const EnrollmentTokenListPage: React.FunctionComponent<{}> = () => {
});
setSearch(newSearch);
}}
{...(config.agents.fleetServerEnabled
? {
indexPattern: ENROLLMENT_API_KEYS_INDEX,
}
: {
fieldPrefix: ENROLLMENT_API_KEYS_SAVED_OBJECT_TYPE,
})}
indexPattern={ENROLLMENT_API_KEYS_INDEX}
/>
</EuiFlexItem>
<EuiFlexItem grow={false}>
Expand Down
3 changes: 1 addition & 2 deletions x-pack/plugins/fleet/server/collectors/agent_collectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ export const getAgentUsage = async (
esClient?: ElasticsearchClient
): Promise<AgentUsage> => {
// TODO: unsure if this case is possible at all.
const fleetServerMissing = config.agents.fleetServerEnabled && !(await isFleetServerSetup());
if (!soClient || !esClient || fleetServerMissing) {
if (!soClient || !esClient || !(await isFleetServerSetup())) {
return {
total: 0,
online: 0,
Expand Down
4 changes: 4 additions & 0 deletions x-pack/plugins/fleet/server/errors/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
AgentPolicyNameExistsError,
PackageUnsupportedMediaTypeError,
ConcurrentInstallOperationError,
AgentNotFoundError,
} from './index';

type IngestErrorHandler = (
Expand Down Expand Up @@ -78,6 +79,9 @@ const getHTTPResponseCode = (error: IngestManagerError): number => {
if (error instanceof ConcurrentInstallOperationError) {
return 409; // Conflict
}
if (error instanceof AgentNotFoundError) {
return 404;
}
return 400; // Bad Request
};

Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/fleet/server/errors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ export class IngestManagerError extends Error {
this.name = this.constructor.name; // for stack traces
}
}

export class RegistryError extends IngestManagerError {}
export class RegistryConnectionError extends RegistryError {}
export class RegistryResponseError extends RegistryError {}
export class PackageNotFoundError extends IngestManagerError {}
export class PackageOutdatedError extends IngestManagerError {}
export class AgentPolicyError extends IngestManagerError {}
export class AgentNotFoundError extends IngestManagerError {}
export class AgentPolicyNameExistsError extends AgentPolicyError {}
export class PackageUnsupportedMediaTypeError extends IngestManagerError {}
export class PackageInvalidArchiveError extends IngestManagerError {}
Expand Down
5 changes: 3 additions & 2 deletions x-pack/plugins/fleet/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,24 @@ export {
AgentPolicyServiceInterface,
} from './services';
export { FleetSetupContract, FleetSetupDeps, FleetStartContract, ExternalCallback } from './plugin';
export { AgentNotFoundError } from './errors';

export const config: PluginConfigDescriptor = {
exposeToBrowser: {
epm: true,
agents: true,
},
deprecations: ({ renameFromRoot }) => [
deprecations: ({ renameFromRoot, unused }) => [
renameFromRoot('xpack.ingestManager', 'xpack.fleet'),
renameFromRoot('xpack.fleet.fleet', 'xpack.fleet.agents'),
unused('agents.fleetServerEnabled'),
],
schema: schema.object({
enabled: schema.boolean({ defaultValue: true }),
registryUrl: schema.maybe(schema.uri({ scheme: ['http', 'https'] })),
registryProxyUrl: schema.maybe(schema.uri({ scheme: ['http', 'https'] })),
agents: schema.object({
enabled: schema.boolean({ defaultValue: true }),
fleetServerEnabled: schema.boolean({ defaultValue: false }),
tlsCheckDisabled: schema.boolean({ defaultValue: false }),
pollingRequestTimeout: schema.number({
defaultValue: AGENT_POLLING_REQUEST_TIMEOUT_MS,
Expand Down
5 changes: 1 addition & 4 deletions x-pack/plugins/fleet/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,10 +299,7 @@ export class FleetPlugin
licenseService.start(this.licensing$);
agentCheckinState.start();

if (appContextService.getConfig()?.agents?.fleetServerEnabled) {
// Break the promise chain, the error handling is done in startFleetServerSetup
startFleetServerSetup();
}
startFleetServerSetup();

return {
esIndexPatternService: new ESIndexPatternSavedObjectService(),
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/fleet/server/routes/agent/acks_handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const postAgentAcksHandlerBuilder = function (
try {
const soClient = ackService.getSavedObjectsClientContract(request);
const esClient = ackService.getElasticsearchClientContract();
const agent = await ackService.authenticateAgentWithAccessToken(soClient, esClient, request);
const agent = await ackService.authenticateAgentWithAccessToken(esClient, request);
const agentEvents = request.body.events as AgentEvent[];

// validate that all events are for the authorized agent obtained from the api key
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const postNewAgentActionHandlerBuilder = function (
const soClient = context.core.savedObjects.client;
const esClient = context.core.elasticsearch.client.asInternalUser;

const agent = await actionsService.getAgent(soClient, esClient, request.params.agentId);
const agent = await actionsService.getAgent(esClient, request.params.agentId);

const newAgentAction = request.body.action;

Expand Down
23 changes: 8 additions & 15 deletions x-pack/plugins/fleet/server/routes/agent/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const getAgentHandler: RequestHandler<
const esClient = context.core.elasticsearch.client.asCurrentUser;

try {
const agent = await AgentService.getAgent(soClient, esClient, request.params.agentId);
const agent = await AgentService.getAgent(esClient, request.params.agentId);

const body: GetOneAgentResponse = {
item: {
Expand Down Expand Up @@ -101,11 +101,10 @@ export const getAgentEventsHandler: RequestHandler<
export const deleteAgentHandler: RequestHandler<
TypeOf<typeof DeleteAgentRequestSchema.params>
> = async (context, request, response) => {
const soClient = context.core.savedObjects.client;
const esClient = context.core.elasticsearch.client.asCurrentUser;

try {
await AgentService.deleteAgent(soClient, esClient, request.params.agentId);
await AgentService.deleteAgent(esClient, request.params.agentId);

const body = {
action: 'deleted',
Expand All @@ -129,14 +128,13 @@ export const updateAgentHandler: RequestHandler<
undefined,
TypeOf<typeof UpdateAgentRequestSchema.body>
> = async (context, request, response) => {
const soClient = context.core.savedObjects.client;
const esClient = context.core.elasticsearch.client.asCurrentUser;

try {
await AgentService.updateAgent(soClient, esClient, request.params.agentId, {
await AgentService.updateAgent(esClient, request.params.agentId, {
user_provided_metadata: request.body.user_provided_metadata,
});
const agent = await AgentService.getAgent(soClient, esClient, request.params.agentId);
const agent = await AgentService.getAgent(esClient, request.params.agentId);

const body = {
item: {
Expand Down Expand Up @@ -165,7 +163,7 @@ export const postAgentCheckinHandler: RequestHandler<
try {
const soClient = appContextService.getInternalUserSOClient(request);
const esClient = appContextService.getInternalUserESClient();
const agent = await AgentService.authenticateAgentWithAccessToken(soClient, esClient, request);
const agent = await AgentService.authenticateAgentWithAccessToken(esClient, request);
const abortController = new AbortController();
request.events.aborted$.subscribe(() => {
abortController.abort();
Expand Down Expand Up @@ -209,11 +207,7 @@ export const postAgentEnrollHandler: RequestHandler<
const soClient = appContextService.getInternalUserSOClient(request);
const esClient = context.core.elasticsearch.client.asInternalUser;
const { apiKeyId } = APIKeyService.parseApiKeyFromHeaders(request.headers);
const enrollmentAPIKey = await APIKeyService.getEnrollmentAPIKeyById(
soClient,
esClient,
apiKeyId
);
const enrollmentAPIKey = await APIKeyService.getEnrollmentAPIKeyById(esClient, apiKeyId);

if (!enrollmentAPIKey || !enrollmentAPIKey.active) {
return response.unauthorized({
Expand Down Expand Up @@ -248,19 +242,18 @@ export const getAgentsHandler: RequestHandler<
undefined,
TypeOf<typeof GetAgentsRequestSchema.query>
> = async (context, request, response) => {
const soClient = context.core.savedObjects.client;
const esClient = context.core.elasticsearch.client.asCurrentUser;

try {
const { agents, total, page, perPage } = await AgentService.listAgents(soClient, esClient, {
const { agents, total, page, perPage } = await AgentService.listAgents(esClient, {
page: request.query.page,
perPage: request.query.perPage,
showInactive: request.query.showInactive,
showUpgradeable: request.query.showUpgradeable,
kuery: request.query.kuery,
});
const totalInactive = request.query.showInactive
? await AgentService.countInactiveAgents(soClient, esClient, {
? await AgentService.countInactiveAgents(esClient, {
kuery: request.query.kuery,
})
: 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const postAgentUpgradeHandler: RequestHandler<
},
});
}
const agent = await getAgent(soClient, esClient, request.params.agentId);
const agent = await getAgent(esClient, request.params.agentId);
if (agent.unenrollment_started_at || agent.unenrolled_at) {
return response.customError({
statusCode: 400,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export const getAgentPoliciesHandler: RequestHandler<
await bluebird.map(
items,
(agentPolicy: GetAgentPoliciesResponseItem) =>
listAgents(soClient, esClient, {
listAgents(esClient, {
showInactive: false,
perPage: 0,
page: 1,
Expand Down
22 changes: 6 additions & 16 deletions x-pack/plugins/fleet/server/routes/enrollment_api_key/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,14 @@ export const getEnrollmentApiKeysHandler: RequestHandler<
undefined,
TypeOf<typeof GetEnrollmentAPIKeysRequestSchema.query>
> = async (context, request, response) => {
const soClient = context.core.savedObjects.client;
const esClient = context.core.elasticsearch.client.asCurrentUser;

try {
const { items, total, page, perPage } = await APIKeyService.listEnrollmentApiKeys(
soClient,
esClient,
{
page: request.query.page,
perPage: request.query.perPage,
kuery: request.query.kuery,
}
);
const { items, total, page, perPage } = await APIKeyService.listEnrollmentApiKeys(esClient, {
page: request.query.page,
perPage: request.query.perPage,
kuery: request.query.kuery,
});
const body: GetEnrollmentAPIKeysResponse = { list: items, total, page, perPage };

return response.ok({ body });
Expand Down Expand Up @@ -93,14 +88,9 @@ export const deleteEnrollmentApiKeyHandler: RequestHandler<
export const getOneEnrollmentApiKeyHandler: RequestHandler<
TypeOf<typeof GetOneEnrollmentAPIKeyRequestSchema.params>
> = async (context, request, response) => {
const soClient = context.core.savedObjects.client;
const esClient = context.core.elasticsearch.client.asCurrentUser;
try {
const apiKey = await APIKeyService.getEnrollmentAPIKey(
soClient,
esClient,
request.params.keyId
);
const apiKey = await APIKeyService.getEnrollmentAPIKey(esClient, request.params.keyId);
const body: GetOneEnrollmentAPIKeyResponse = { item: apiKey };

return response.ok({ body });
Expand Down
Loading