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] Code in 'common' directories shouldn't import server code #53938

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
5 changes: 3 additions & 2 deletions x-pack/legacy/plugins/epm/public/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { HttpHandler } from 'src/core/public';
import { HttpHandler, HttpFetchOptions } from 'src/core/public';
import {
getCategoriesPath,
getFilePath,
Expand All @@ -26,7 +26,8 @@ import {
} from '../common/types';
import { ReturnTypeList } from '../../ingest/common/types/std_return_format';

const defaultClient: HttpHandler = (path, options?) => fetch(path, options).then(res => res.json());
const defaultClient: HttpHandler = (path: string, options?: HttpFetchOptions) =>
fetch(path, options).then(res => res.json());

let _fetch: HttpHandler = defaultClient;

Expand Down
4 changes: 3 additions & 1 deletion x-pack/legacy/plugins/epm/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import { routes } from './routes';

export { createSetupShim } from './shim';

export type EPMPluginInitializerContext = Pick<PluginInitializerContext, 'config'>;
export interface EPMPluginInitializerContext {
config: Pick<PluginInitializerContext['config'], 'create' | 'createIfExists'>;
}

export interface EPMCoreSetup {
elasticsearch: CoreSetup['elasticsearch'];
Expand Down
158 changes: 154 additions & 4 deletions x-pack/legacy/plugins/fleet/common/types/domain_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,126 @@
* you may not use this file except in compliance with the Elastic License.
*/
import * as t from 'io-ts';
import { AGENT_TYPE_EPHEMERAL, AGENT_TYPE_PERMANENT, AGENT_TYPE_TEMPORARY } from '../constants';
export { Policy, Datasource, Status, Output } from '../../../ingest/server/libs/types';
Copy link
Contributor

Choose a reason for hiding this comment

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

Interesting that it doesn't complain about this. If it passes, I'm ok with leaving it for later, but I think we'll eventually need to move the shared Ingest types to ingest/common/types or something similar so the client & server can both use them.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes it's weird that eslint is not complaining, I will keep it like this for later

import { RuntimeAgent, RuntimeAgentAction } from '../../server/repositories/agents/types';
import { RuntimeAgentEvent } from '../../server/repositories/agent_events/types';
export { EnrollmentApiKey } from '../../server/repositories/enrollment_api_keys/types';

const RuntimeAgentActionType = t.union([
t.literal('POLICY_CHANGE'),
t.literal('DATA_DUMP'),
t.literal('RESUME'),
t.literal('PAUSE'),
]);

export type AgentActionType = t.TypeOf<typeof RuntimeAgentActionType>;

export const RuntimeAgentActionData = t.interface(
{
type: RuntimeAgentActionType,
},
'AgentActionData'
);

export const RuntimeAgentAction = t.intersection([
RuntimeAgentActionData,
t.interface(
{
id: t.string,
created_at: t.string,
},
'AgentAction'
),
t.partial({
data: t.string,
sent_at: t.string,
}),
]);

export const RuntimeAgentType = t.union([
t.literal(AGENT_TYPE_PERMANENT),
t.literal(AGENT_TYPE_EPHEMERAL),
t.literal(AGENT_TYPE_TEMPORARY),
]);

export type AgentType = t.TypeOf<typeof RuntimeAgentType>;

export const RuntimeAgentEventType = t.union([
t.literal('STATE'),
t.literal('ERROR'),
t.literal('ACTION_RESULT'),
t.literal('ACTION'),
]);

export const RuntimeAgentEventSubtype = t.union([
// State
t.literal('RUNNING'),
t.literal('STARTING'),
t.literal('IN_PROGRESS'),
t.literal('CONFIG'),
t.literal('FAILED'),
t.literal('STOPPED'),
// Action results
t.literal('DATA_DUMP'),
// Actions
t.literal('ACKNOWLEDGED'),
t.literal('UNKNOWN'),
]);

export const RuntimeAgentEvent = t.intersection(
[
t.interface({
type: RuntimeAgentEventType,
subtype: RuntimeAgentEventSubtype,
timestamp: t.string,
message: t.string,
}),
t.partial({
payload: t.any,
data: t.string,
action_id: t.string,
policy_id: t.string,
stream_id: t.string,
}),
],
'AgentEvent'
);

export type AgentEvent = t.TypeOf<typeof RuntimeAgentEvent>;

const newAgentProperties = {
type: RuntimeAgentType,
active: t.boolean,
};
const newAgentOptionalProperties = t.partial({
parent_id: t.string,
version: t.string,
enrolled_at: t.string,
user_provided_metadata: t.dictionary(t.string, t.string),
local_metadata: t.dictionary(t.string, t.string),
shared_id: t.string,
access_api_key_id: t.string,
access_api_key: t.string,
policy_id: t.string,
});

export const RuntimeAgent = t.intersection([
t.interface({
...newAgentProperties,
id: t.string,
actions: t.array(RuntimeAgentAction),
current_error_events: t.array(RuntimeAgentEvent),
}),
t.partial({
last_updated: t.string,
last_checkin: t.string,
}),
newAgentOptionalProperties,
]);

export const NewRuntimeAgent = t.intersection([
t.interface(newAgentProperties),
newAgentOptionalProperties,
]);
export type NewAgent = t.TypeOf<typeof NewRuntimeAgent>;

// Here we create the runtime check for a generic, unknown beat config type.
// We can also pass in optional params to create spacific runtime checks that
Expand Down Expand Up @@ -39,7 +155,6 @@ export type Agent = t.TypeOf<typeof RuntimeAgent> & {
status: AgentStatus;
};
export type AgentAction = t.TypeOf<typeof RuntimeAgentAction>;
export type AgentEvent = t.TypeOf<typeof RuntimeAgentEvent>;

export type PolicyUpdatedEvent =
| {
Expand All @@ -56,3 +171,38 @@ export type PolicyUpdatedEvent =
type: 'deleted';
policyId: string;
};

export const RuntimeEnrollmentRuleData = t.partial(
{
ip_ranges: t.array(t.string),
window_duration: t.interface(
{
from: t.string,
to: t.string,
},
'WindowDuration'
),
types: t.array(RuntimeAgentType),
},
'EnrollmentRuleData'
);

export type EnrollmentRuleData = t.TypeOf<typeof RuntimeEnrollmentRuleData>;

export type EnrollmentRule = EnrollmentRuleData & {
id: string;
created_at: string;
updated_at?: string;
};
export interface EnrollmentApiKey {
id: string;
api_key_id: string;
api_key: string;
name?: string;
created_at: string;
expire_at?: string;
active: boolean;
enrollment_rules: EnrollmentRule[];
policy_id?: string;
[k: string]: any; // allow to use it as saved object attributes type
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ interface LayoutProps {
modalClosePath?: string;
}

export const NoDataLayout: React.FC<LayoutProps> = withRouter<any>(
({ actionSection, title, modalClosePath, children, history }) => {
export const NoDataLayout: React.FC<LayoutProps> = withRouter<any, React.FC<LayoutProps>>(
({ actionSection, title, modalClosePath, children }) => {
return (
<EuiFlexGroup justifyContent="spaceAround">
<EuiFlexItem grow={false}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ export function ConnectedLinkComponent({
);
}

export const ConnectedLink = withRouter<any>(ConnectedLinkComponent);
export const ConnectedLink = withRouter<any, typeof ConnectedLinkComponent>(ConnectedLinkComponent);
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,4 @@ export class WithURLStateComponent<URLState extends object> extends React.Compon
});
};
}
export const WithUrlState = withRouter(WithURLStateComponent);
export const WithUrlState = withRouter<any, typeof WithURLStateComponent>(WithURLStateComponent);
8 changes: 5 additions & 3 deletions x-pack/legacy/plugins/fleet/server/libs/__mocks__/api_keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
import { FrameworkUser, internalAuthData } from '../../adapters/framework/adapter_types';
import {
AccessApiKeyVerificationResponse,
EnrollmentApiKey,
EnrollmentApiKeyVerificationResponse,
EnrollmentRule,
EnrollmentRuleData,
} from '../../repositories/enrollment_api_keys/types';
import { ApiKeyLib as ApiKeyLibType } from '../api_keys';
import {
EnrollmentApiKey,
EnrollmentRuleData,
EnrollmentRule,
} from '../../../common/types/domain_data';

type Interface<T> = {
[P in keyof T]: T[P];
Expand Down
12 changes: 2 additions & 10 deletions x-pack/legacy/plugins/fleet/server/libs/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,12 @@
import Boom from 'boom';
import uuid from 'uuid/v4';
import { FrameworkUser } from '../adapters/framework/adapter_types';
import {
Agent,
AgentAction,
AgentActionType,
AgentsRepository,
AgentType,
NewAgent,
SortOptions,
} from '../repositories/agents/types';
import { AgentEvent } from '../repositories/agent_events/types';
import { Agent, AgentAction, AgentsRepository, SortOptions } from '../repositories/agents/types';
import { AgentPolicy } from '../repositories/policies/types';
import { ApiKeyLib } from './api_keys';
import { AgentStatusHelper } from './agent_status_helper';
import { AgentEventLib } from './agent_event';
import { AgentEvent, AgentType, NewAgent, AgentActionType } from '../../common/types/domain_data';

export class AgentLib {
constructor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { SODatabaseAdapter } from '../adapters/saved_objects_database/default';
import { FleetServerLib } from './types';
import { compose } from './compose/memorized';
import { Agent } from '../repositories/agents/types';
import { AgentEvent } from '../repositories/agent_events/types';
import { AgentEvent } from '../../common/types/domain_data';

jest.mock('./framework');
jest.mock('./policy', () => ({
Expand Down
2 changes: 1 addition & 1 deletion x-pack/legacy/plugins/fleet/server/libs/agent_event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

import { FrameworkUser } from '../adapters/framework/adapter_types';
import { AgentEventsRepository } from '../repositories/agent_events/default';
import { AgentEvent } from '../repositories/agent_events/types';
import { Agent } from '../repositories/agents/types';
import { AgentEvent } from '../../common/types/domain_data';

/**
* This is the server lib to manage everything related to policies and agents
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from '../../common/constants';
import { Agent } from '../repositories/agents/types';
import { AgentStatusHelper } from './agent_status_helper';
import { AgentEvent } from '../repositories/agent_events/types';
import { AgentEvent } from '../../common/types/domain_data';

describe('AgentStatusHelper', () => {
describe('getAgentStatus', () => {
Expand Down
8 changes: 5 additions & 3 deletions x-pack/legacy/plugins/fleet/server/libs/api_keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@ import Boom from 'boom';
import uuid from 'uuid/v4';
import {
EnrollmentApiKeyVerificationResponse,
EnrollmentApiKey,
EnrollmentRuleData,
EnrollmentApiKeysRepository,
AccessApiKeyVerificationResponse,
EnrollmentRule,
} from '../repositories/enrollment_api_keys/types';
import { FrameworkLib } from './framework';
import { FrameworkUser, internalAuthData } from '../adapters/framework/adapter_types';
import { ElasticsearchAdapter } from '../adapters/elasticsearch/adapter_types';
import { DEFAULT_POLICY_ID } from '../../common/constants';
import {
EnrollmentApiKey,
EnrollmentRuleData,
EnrollmentRule,
} from '../../common/types/domain_data';

export class ApiKeyLib {
constructor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { SODatabaseAdapter as SODatabaseAdapterType } from '../../adapters/saved
import { SODatabaseAdapter } from '../../adapters/saved_objects_database/default';
import { MemorizeSODatabaseAdapter } from '../../adapters/saved_objects_database/memorize_adapter';
import { FrameworkUser, internalAuthData } from '../../adapters/framework/adapter_types';
import { AgentEvent } from '../agent_events/types';
import { AgentEvent } from '../../../common/types/domain_data';

describe('AgentsEventsRepository', () => {
let repository: AgentEventsRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import { SODatabaseAdapter } from '../../adapters/saved_objects_database/adapter
import { FrameworkUser } from '../../adapters/framework/adapter_types';
import {
AgentEventsRepository as AgentEventsRepositoryType,
AgentEvent,
AgentEventSOAttributes,
} from './types';
import { AgentEvent } from '../../../common/types/domain_data';

const SO_TYPE = 'agent_events';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,47 +7,11 @@
import * as t from 'io-ts';
import { FrameworkUser } from '../../adapters/framework/adapter_types';

export const RuntimeAgentEventType = t.union([
t.literal('STATE'),
t.literal('ERROR'),
t.literal('ACTION_RESULT'),
t.literal('ACTION'),
]);

export const RuntimeAgentEventSubtype = t.union([
// State
t.literal('RUNNING'),
t.literal('STARTING'),
t.literal('IN_PROGRESS'),
t.literal('CONFIG'),
t.literal('FAILED'),
t.literal('STOPPED'),
// Action results
t.literal('DATA_DUMP'),
// Actions
t.literal('ACKNOWLEDGED'),
t.literal('UNKNOWN'),
]);

export const RuntimeAgentEvent = t.intersection(
[
t.interface({
type: RuntimeAgentEventType,
subtype: RuntimeAgentEventSubtype,
timestamp: t.string,
message: t.string,
}),
t.partial({
payload: t.any,
data: t.string,
action_id: t.string,
policy_id: t.string,
stream_id: t.string,
}),
],
'AgentEvent'
);
export type AgentEvent = t.TypeOf<typeof RuntimeAgentEvent>;
import {
RuntimeAgentEventType,
RuntimeAgentEventSubtype,
AgentEvent,
} from '../../../common/types/domain_data';

export const RuntimeAgentEventSOAttributes = t.intersection(
[
Expand Down
Loading