Skip to content

Commit 1a35843

Browse files
authored
[Fleet] Code in 'common' directories shouldn't import server code (#53938)
* [Fleet] Remove server code from common folder in fleet * [Fleet] Fix typescript issues after master merge * [EPM] Fix typescript issues after master merge
1 parent 0f0d7ca commit 1a35843

File tree

26 files changed

+205
-207
lines changed

26 files changed

+205
-207
lines changed

x-pack/legacy/plugins/epm/public/data.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
import { HttpHandler } from 'src/core/public';
7+
import { HttpHandler, HttpFetchOptions } from 'src/core/public';
88
import {
99
getCategoriesPath,
1010
getFilePath,
@@ -26,7 +26,8 @@ import {
2626
} from '../common/types';
2727
import { ReturnTypeList } from '../../ingest/common/types/std_return_format';
2828

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

3132
let _fetch: HttpHandler = defaultClient;
3233

x-pack/legacy/plugins/epm/server/plugin.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ import { routes } from './routes';
1616

1717
export { createSetupShim } from './shim';
1818

19-
export type EPMPluginInitializerContext = Pick<PluginInitializerContext, 'config'>;
19+
export interface EPMPluginInitializerContext {
20+
config: Pick<PluginInitializerContext['config'], 'create' | 'createIfExists'>;
21+
}
2022

2123
export interface EPMCoreSetup {
2224
elasticsearch: CoreSetup['elasticsearch'];

x-pack/legacy/plugins/fleet/common/types/domain_data.ts

Lines changed: 154 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,126 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66
import * as t from 'io-ts';
7+
import { AGENT_TYPE_EPHEMERAL, AGENT_TYPE_PERMANENT, AGENT_TYPE_TEMPORARY } from '../constants';
78
export { Policy, Datasource, Status, Output } from '../../../ingest/server/libs/types';
8-
import { RuntimeAgent, RuntimeAgentAction } from '../../server/repositories/agents/types';
9-
import { RuntimeAgentEvent } from '../../server/repositories/agent_events/types';
10-
export { EnrollmentApiKey } from '../../server/repositories/enrollment_api_keys/types';
9+
10+
const RuntimeAgentActionType = t.union([
11+
t.literal('POLICY_CHANGE'),
12+
t.literal('DATA_DUMP'),
13+
t.literal('RESUME'),
14+
t.literal('PAUSE'),
15+
]);
16+
17+
export type AgentActionType = t.TypeOf<typeof RuntimeAgentActionType>;
18+
19+
export const RuntimeAgentActionData = t.interface(
20+
{
21+
type: RuntimeAgentActionType,
22+
},
23+
'AgentActionData'
24+
);
25+
26+
export const RuntimeAgentAction = t.intersection([
27+
RuntimeAgentActionData,
28+
t.interface(
29+
{
30+
id: t.string,
31+
created_at: t.string,
32+
},
33+
'AgentAction'
34+
),
35+
t.partial({
36+
data: t.string,
37+
sent_at: t.string,
38+
}),
39+
]);
40+
41+
export const RuntimeAgentType = t.union([
42+
t.literal(AGENT_TYPE_PERMANENT),
43+
t.literal(AGENT_TYPE_EPHEMERAL),
44+
t.literal(AGENT_TYPE_TEMPORARY),
45+
]);
46+
47+
export type AgentType = t.TypeOf<typeof RuntimeAgentType>;
48+
49+
export const RuntimeAgentEventType = t.union([
50+
t.literal('STATE'),
51+
t.literal('ERROR'),
52+
t.literal('ACTION_RESULT'),
53+
t.literal('ACTION'),
54+
]);
55+
56+
export const RuntimeAgentEventSubtype = t.union([
57+
// State
58+
t.literal('RUNNING'),
59+
t.literal('STARTING'),
60+
t.literal('IN_PROGRESS'),
61+
t.literal('CONFIG'),
62+
t.literal('FAILED'),
63+
t.literal('STOPPED'),
64+
// Action results
65+
t.literal('DATA_DUMP'),
66+
// Actions
67+
t.literal('ACKNOWLEDGED'),
68+
t.literal('UNKNOWN'),
69+
]);
70+
71+
export const RuntimeAgentEvent = t.intersection(
72+
[
73+
t.interface({
74+
type: RuntimeAgentEventType,
75+
subtype: RuntimeAgentEventSubtype,
76+
timestamp: t.string,
77+
message: t.string,
78+
}),
79+
t.partial({
80+
payload: t.any,
81+
data: t.string,
82+
action_id: t.string,
83+
policy_id: t.string,
84+
stream_id: t.string,
85+
}),
86+
],
87+
'AgentEvent'
88+
);
89+
90+
export type AgentEvent = t.TypeOf<typeof RuntimeAgentEvent>;
91+
92+
const newAgentProperties = {
93+
type: RuntimeAgentType,
94+
active: t.boolean,
95+
};
96+
const newAgentOptionalProperties = t.partial({
97+
parent_id: t.string,
98+
version: t.string,
99+
enrolled_at: t.string,
100+
user_provided_metadata: t.dictionary(t.string, t.string),
101+
local_metadata: t.dictionary(t.string, t.string),
102+
shared_id: t.string,
103+
access_api_key_id: t.string,
104+
access_api_key: t.string,
105+
policy_id: t.string,
106+
});
107+
108+
export const RuntimeAgent = t.intersection([
109+
t.interface({
110+
...newAgentProperties,
111+
id: t.string,
112+
actions: t.array(RuntimeAgentAction),
113+
current_error_events: t.array(RuntimeAgentEvent),
114+
}),
115+
t.partial({
116+
last_updated: t.string,
117+
last_checkin: t.string,
118+
}),
119+
newAgentOptionalProperties,
120+
]);
121+
122+
export const NewRuntimeAgent = t.intersection([
123+
t.interface(newAgentProperties),
124+
newAgentOptionalProperties,
125+
]);
126+
export type NewAgent = t.TypeOf<typeof NewRuntimeAgent>;
11127

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

44159
export type PolicyUpdatedEvent =
45160
| {
@@ -56,3 +171,38 @@ export type PolicyUpdatedEvent =
56171
type: 'deleted';
57172
policyId: string;
58173
};
174+
175+
export const RuntimeEnrollmentRuleData = t.partial(
176+
{
177+
ip_ranges: t.array(t.string),
178+
window_duration: t.interface(
179+
{
180+
from: t.string,
181+
to: t.string,
182+
},
183+
'WindowDuration'
184+
),
185+
types: t.array(RuntimeAgentType),
186+
},
187+
'EnrollmentRuleData'
188+
);
189+
190+
export type EnrollmentRuleData = t.TypeOf<typeof RuntimeEnrollmentRuleData>;
191+
192+
export type EnrollmentRule = EnrollmentRuleData & {
193+
id: string;
194+
created_at: string;
195+
updated_at?: string;
196+
};
197+
export interface EnrollmentApiKey {
198+
id: string;
199+
api_key_id: string;
200+
api_key: string;
201+
name?: string;
202+
created_at: string;
203+
expire_at?: string;
204+
active: boolean;
205+
enrollment_rules: EnrollmentRule[];
206+
policy_id?: string;
207+
[k: string]: any; // allow to use it as saved object attributes type
208+
}

x-pack/legacy/plugins/fleet/public/components/layouts/no_data.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ interface LayoutProps {
1414
modalClosePath?: string;
1515
}
1616

17-
export const NoDataLayout: React.FC<LayoutProps> = withRouter<any>(
18-
({ actionSection, title, modalClosePath, children, history }) => {
17+
export const NoDataLayout: React.FC<LayoutProps> = withRouter<any, React.FC<LayoutProps>>(
18+
({ actionSection, title, modalClosePath, children }) => {
1919
return (
2020
<EuiFlexGroup justifyContent="spaceAround">
2121
<EuiFlexItem grow={false}>

x-pack/legacy/plugins/fleet/public/components/navigation/connected_link.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@ export function ConnectedLinkComponent({
3737
);
3838
}
3939

40-
export const ConnectedLink = withRouter<any>(ConnectedLinkComponent);
40+
export const ConnectedLink = withRouter<any, typeof ConnectedLinkComponent>(ConnectedLinkComponent);

x-pack/legacy/plugins/fleet/public/hooks/with_url_state.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,4 @@ export class WithURLStateComponent<URLState extends object> extends React.Compon
8585
});
8686
};
8787
}
88-
export const WithUrlState = withRouter(WithURLStateComponent);
88+
export const WithUrlState = withRouter<any, typeof WithURLStateComponent>(WithURLStateComponent);

x-pack/legacy/plugins/fleet/server/libs/__mocks__/api_keys.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
import { FrameworkUser, internalAuthData } from '../../adapters/framework/adapter_types';
88
import {
99
AccessApiKeyVerificationResponse,
10-
EnrollmentApiKey,
1110
EnrollmentApiKeyVerificationResponse,
12-
EnrollmentRule,
13-
EnrollmentRuleData,
1411
} from '../../repositories/enrollment_api_keys/types';
1512
import { ApiKeyLib as ApiKeyLibType } from '../api_keys';
13+
import {
14+
EnrollmentApiKey,
15+
EnrollmentRuleData,
16+
EnrollmentRule,
17+
} from '../../../common/types/domain_data';
1618

1719
type Interface<T> = {
1820
[P in keyof T]: T[P];

x-pack/legacy/plugins/fleet/server/libs/agent.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,12 @@
77
import Boom from 'boom';
88
import uuid from 'uuid/v4';
99
import { FrameworkUser } from '../adapters/framework/adapter_types';
10-
import {
11-
Agent,
12-
AgentAction,
13-
AgentActionType,
14-
AgentsRepository,
15-
AgentType,
16-
NewAgent,
17-
SortOptions,
18-
} from '../repositories/agents/types';
19-
import { AgentEvent } from '../repositories/agent_events/types';
10+
import { Agent, AgentAction, AgentsRepository, SortOptions } from '../repositories/agents/types';
2011
import { AgentPolicy } from '../repositories/policies/types';
2112
import { ApiKeyLib } from './api_keys';
2213
import { AgentStatusHelper } from './agent_status_helper';
2314
import { AgentEventLib } from './agent_event';
15+
import { AgentEvent, AgentType, NewAgent, AgentActionType } from '../../common/types/domain_data';
2416

2517
export class AgentLib {
2618
constructor(

x-pack/legacy/plugins/fleet/server/libs/agent_event.contract.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { SODatabaseAdapter } from '../adapters/saved_objects_database/default';
1717
import { FleetServerLib } from './types';
1818
import { compose } from './compose/memorized';
1919
import { Agent } from '../repositories/agents/types';
20-
import { AgentEvent } from '../repositories/agent_events/types';
20+
import { AgentEvent } from '../../common/types/domain_data';
2121

2222
jest.mock('./framework');
2323
jest.mock('./policy', () => ({

x-pack/legacy/plugins/fleet/server/libs/agent_event.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
import { FrameworkUser } from '../adapters/framework/adapter_types';
88
import { AgentEventsRepository } from '../repositories/agent_events/default';
9-
import { AgentEvent } from '../repositories/agent_events/types';
109
import { Agent } from '../repositories/agents/types';
10+
import { AgentEvent } from '../../common/types/domain_data';
1111

1212
/**
1313
* This is the server lib to manage everything related to policies and agents

0 commit comments

Comments
 (0)