-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Security Solution] add package_policy_id to .fleet-policies inputs (#…
- Loading branch information
Showing
17 changed files
with
370 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
.../plugins/fleet/server/integration_tests/__snapshots__/cloud_preconfiguration.test.ts.snap
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
178 changes: 178 additions & 0 deletions
178
x-pack/plugins/fleet/server/integration_tests/upgrade_agent_policy_schema_version.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import uuid from 'uuid/v4'; | ||
|
||
import type { | ||
KibanaRequest, | ||
SavedObjectsClientContract, | ||
ElasticsearchClient, | ||
} from '@kbn/core/server'; | ||
import * as kbnTestServer from '@kbn/core/test_helpers/kbn_server'; | ||
import type { SearchTotalHits } from '@elastic/elasticsearch/lib/api/types'; | ||
|
||
import { AGENT_POLICY_SAVED_OBJECT_TYPE, FLEET_AGENT_POLICIES_SCHEMA_VERSION } from '../constants'; | ||
import { upgradeAgentPolicySchemaVersion } from '../services/setup/upgrade_agent_policy_schema_version'; | ||
import { AGENT_POLICY_INDEX } from '../../common'; | ||
import { agentPolicyService } from '../services'; | ||
|
||
import { useDockerRegistry, waitForFleetSetup } from './helpers'; | ||
|
||
const fakeRequest = { | ||
headers: {}, | ||
getBasePath: () => '', | ||
path: '/', | ||
route: { settings: {} }, | ||
url: { | ||
href: '/', | ||
}, | ||
raw: { | ||
req: { | ||
url: '/', | ||
}, | ||
}, | ||
} as unknown as KibanaRequest; | ||
|
||
describe('upgrade agent policy schema version', () => { | ||
let esServer: kbnTestServer.TestElasticsearchUtils; | ||
let kbnServer: kbnTestServer.TestKibanaUtils; | ||
|
||
const registryUrl = useDockerRegistry(); | ||
|
||
const startServers = async () => { | ||
const { startES } = kbnTestServer.createTestServers({ | ||
adjustTimeout: (t) => jest.setTimeout(t), | ||
settings: { | ||
es: { | ||
license: 'trial', | ||
}, | ||
kbn: {}, | ||
}, | ||
}); | ||
|
||
esServer = await startES(); | ||
const startKibana = async () => { | ||
const root = kbnTestServer.createRootWithCorePlugins( | ||
{ | ||
xpack: { | ||
fleet: { | ||
registryUrl, | ||
packages: [ | ||
{ | ||
name: 'fleet_server', | ||
version: 'latest', | ||
}, | ||
{ | ||
name: 'system', | ||
version: 'latest', | ||
}, | ||
], | ||
}, | ||
}, | ||
}, | ||
{ oss: false } | ||
); | ||
|
||
await root.preboot(); | ||
const coreSetup = await root.setup(); | ||
const coreStart = await root.start(); | ||
|
||
return { | ||
root, | ||
coreSetup, | ||
coreStart, | ||
stop: async () => await root.shutdown(), | ||
}; | ||
}; | ||
kbnServer = await startKibana(); | ||
|
||
await waitForFleetSetup(kbnServer.root); | ||
}; | ||
|
||
const stopServers = async () => { | ||
if (kbnServer) { | ||
await kbnServer.stop(); | ||
} | ||
|
||
if (esServer) { | ||
await esServer.stop(); | ||
} | ||
|
||
await new Promise((res) => setTimeout(res, 10000)); | ||
}; | ||
|
||
// Share the same servers for all the test to make test a lot faster (but test are not isolated anymore) | ||
beforeAll(async () => { | ||
await startServers(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await stopServers(); | ||
}); | ||
|
||
describe('with package installed with outdated schema version', () => { | ||
let soClient: SavedObjectsClientContract; | ||
let esClient: ElasticsearchClient; | ||
|
||
beforeAll(async () => { | ||
soClient = kbnServer.coreStart.savedObjects.getScopedClient(fakeRequest, { | ||
excludedWrappers: ['security'], | ||
}); | ||
esClient = kbnServer.coreStart.elasticsearch.client.asInternalUser; | ||
}); | ||
|
||
it('should correctly upgrade schema version', async () => { | ||
await esClient.indices.create({ index: AGENT_POLICY_INDEX }); | ||
let esRes = await esClient.search({ index: AGENT_POLICY_INDEX }); | ||
expect((esRes.hits.total as SearchTotalHits).value).toBe(0); | ||
|
||
await soClient.bulkCreate([ | ||
// up-to-date schema_version | ||
{ | ||
type: AGENT_POLICY_SAVED_OBJECT_TYPE, | ||
id: uuid(), | ||
attributes: { | ||
schema_version: FLEET_AGENT_POLICIES_SCHEMA_VERSION, | ||
revision: 1, | ||
}, | ||
}, | ||
// out-of-date schema_version | ||
{ | ||
type: AGENT_POLICY_SAVED_OBJECT_TYPE, | ||
id: uuid(), | ||
attributes: { | ||
schema_version: '0.0.1', | ||
revision: 1, | ||
}, | ||
}, | ||
// missing schema_version | ||
{ | ||
type: AGENT_POLICY_SAVED_OBJECT_TYPE, | ||
id: uuid(), | ||
attributes: { | ||
revision: 1, | ||
}, | ||
}, | ||
]); | ||
|
||
await upgradeAgentPolicySchemaVersion(soClient); | ||
|
||
const policies = await agentPolicyService.list(soClient, { | ||
kuery: `${AGENT_POLICY_SAVED_OBJECT_TYPE}.schema_version:${FLEET_AGENT_POLICIES_SCHEMA_VERSION}`, | ||
}); | ||
// all 3 should be up-to-date after upgrade | ||
expect(policies.total).toBe(3); | ||
|
||
esRes = await esClient.search({ | ||
index: AGENT_POLICY_INDEX, | ||
body: { query: { match: { revision_idx: 2 } } }, | ||
}); | ||
// since only 2 were updated, only 2 should be written | ||
expect((esRes.hits.total as SearchTotalHits).value).toBe(2); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.