-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
[Synthetics] add synthetics-private-location command #189531
Merged
dominiqueclarke
merged 30 commits into
elastic:main
from
dominiqueclarke:chore/synthetics-private-locations-automation
Aug 9, 2024
Merged
Changes from 9 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
8070ae3
add synthetics-private-location command
dominiqueclarke 0a41e97
create synthetics private location
dominiqueclarke d6e008e
add kibana api client
dominiqueclarke 7bee085
dynamically fetch version
dominiqueclarke 206792e
update constants
dominiqueclarke ec87de6
add README
dominiqueclarke 70e1138
Delete unintended file
dominiqueclarke db39747
update CODEOWNERS
dominiqueclarke 8fe4e46
Merge branch 'chore/synthetics-private-locations-automation' of githu…
dominiqueclarke e75e728
Update packages/kbn-synthetics-private-location/src/lib/generate_flee…
dominiqueclarke f349279
[CI] Auto-commit changed files from 'node scripts/lint_ts_projects --…
kibanamachine 13b156b
[CI] Auto-commit changed files from 'node scripts/generate codeowners'
kibanamachine b02bcd6
Update .github/CODEOWNERS
dominiqueclarke a65d819
[CI] Auto-commit changed files from 'node scripts/generate codeowners'
kibanamachine 2550202
Merge branch 'main' into chore/synthetics-private-locations-automation
dominiqueclarke 11caa5b
Merge branch 'main' of https://github.com/elastic/kibana into chore/s…
dominiqueclarke 5682e31
spawn Sync
dominiqueclarke ebef5fc
Merge branch 'chore/synthetics-private-locations-automation' of githu…
dominiqueclarke 58fcd1d
Merge branch 'main' into chore/synthetics-private-locations-automation
dominiqueclarke cdb5ba6
Merge branch 'main' into chore/synthetics-private-locations-automation
dominiqueclarke 893b588
Update .github/CODEOWNERS
shahzad31 39e0982
Update packages/kbn-synthetics-private-location/kibana.jsonc
shahzad31 11c420f
move directory
dominiqueclarke 79207ca
remove docker compose
dominiqueclarke 60c9ee2
Merge branch 'chore/synthetics-private-locations-automation' of githu…
dominiqueclarke dab1c7f
Update .github/CODEOWNERS
dominiqueclarke 718c8a6
Update x-pack/packages/kbn-synthetics-private-location/tsconfig.json
dominiqueclarke 319e3b4
[CI] Auto-commit changed files from 'node scripts/lint_packages --fix'
kibanamachine a07f193
Update x-pack/packages/kbn-synthetics-private-location/jest.config.js
dominiqueclarke 6caa260
Update x-pack/packages/kbn-synthetics-private-location/jest.config.js
dominiqueclarke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Validating CODEOWNERS rules …
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# @kbn/synthetics-private-location | ||
|
||
Quickily start Fleet, enroll Elastic Agent, and create a private location. | ||
|
||
## Usage | ||
|
||
``` | ||
node x-pack/scripts/synthetics_private_location.js | ||
``` | ||
|
||
For available options, run `--help`. | ||
|
||
## Prerequistes | ||
|
||
This script requires `docker` and the following `kibama.yml` configuration. | ||
|
||
``` | ||
# Create an agent policy for Fleet Server. | ||
xpack.fleet.agentPolicies: | ||
- name: Fleet Server policy | ||
id: fleet-server-policy | ||
is_default_fleet_server: true | ||
# is_managed: true # Useful to mimic cloud environment | ||
description: Fleet server policy | ||
namespace: default | ||
package_policies: | ||
- name: Fleet Server | ||
package: | ||
name: fleet_server | ||
inputs: | ||
- type: fleet-server | ||
keep_enabled: true | ||
vars: | ||
- name: host | ||
value: 0.0.0.0 | ||
frozen: true | ||
- name: port | ||
value: 8220 | ||
frozen: true | ||
|
||
# Set a default Fleet Server host. | ||
xpack.fleet.fleetServerHosts: | ||
- id: default-fleet-server | ||
name: Default Fleet server | ||
is_default: true | ||
host_urls: ['https://host.docker.internal:8220'] # For running a Fleet Server Docker container | ||
|
||
# Set a default Elasticsearch output. | ||
xpack.fleet.outputs: | ||
- id: es-default-output | ||
name: Default output | ||
type: elasticsearch | ||
is_default: true | ||
is_default_monitoring: true | ||
hosts: ['http://host.docker.internal:9200'] # For enrolling dockerized agents | ||
``` |
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,13 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
export type { CliOptions } from './src/types'; | ||
export { run } from './src/run'; | ||
export { cli } from './src/cli'; | ||
// export { cleanup } from './src/cleanup'; | ||
export { DEFAULTS } from './src/constants'; |
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,13 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
module.exports = { | ||
preset: '@kbn/test', | ||
rootDir: '../..', | ||
roots: ['<rootDir>/packages/kbn-synthetics-private-location'], | ||
}; |
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,5 @@ | ||
{ | ||
"type": "shared-common", | ||
"id": "@kbn/synthetics-private-location", | ||
"owner": "@elastic/obs-ux-management" | ||
shahzad31 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
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,6 @@ | ||
{ | ||
"name": "@kbn/synthetics-private-location", | ||
"private": true, | ||
"version": "1.0.0", | ||
"license": "SSPL-1.0 OR Elastic License 2.0" | ||
} |
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,18 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { ToolingLog } from '@kbn/tooling-log'; | ||
import { parseCliOptions } from './lib/parse_cli_options'; | ||
import { CliOptions } from './types'; | ||
import { run } from './run'; | ||
|
||
export async function cli(cliOptions?: CliOptions) { | ||
const options = cliOptions ?? parseCliOptions(); | ||
const logger = new ToolingLog({ level: 'info', writeTo: process.stdout }); | ||
return run(options, logger); | ||
} |
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,18 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { v4 as uuidv4 } from 'uuid'; | ||
|
||
export const DEFAULTS = { | ||
LOCATION_NAME: `Default location ${uuidv4()}`, | ||
AGENT_POLICY_NAME: `Synthetics agent policy ${uuidv4()}`, | ||
ELASTICSEARCH_HOST: 'http://localhost:9200', | ||
KIBANA_URL: 'http://localhost:5601', | ||
KIBANA_USERNAME: 'elastic', | ||
KIBANA_PASSWORD: 'changeme', | ||
}; |
41 changes: 41 additions & 0 deletions
41
packages/kbn-synthetics-private-location/src/lib/create_agent_policy.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,41 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { isError } from 'lodash'; | ||
import { ToolingLog } from '@kbn/tooling-log'; | ||
import { CliOptions } from '../types'; | ||
import type { KibanaAPIClient } from './kibana_api_client'; | ||
|
||
export async function createElasticAgentPolicy( | ||
{ agentPolicyName }: CliOptions, | ||
logger: ToolingLog, | ||
kibanaApiClient: KibanaAPIClient | ||
) { | ||
try { | ||
const response = await kibanaApiClient.sendRequest({ | ||
method: 'post', | ||
url: 'api/fleet/agent_policies', | ||
data: { | ||
name: agentPolicyName, | ||
description: '', | ||
namespace: 'default', | ||
monitoring_enabled: ['logs', 'metrics'], | ||
inactivity_timeout: 1209600, | ||
is_protected: false, | ||
}, | ||
}); | ||
|
||
logger.info(`Generated elastic agent policy`); | ||
return response.data; | ||
} catch (error) { | ||
if (isError(error)) { | ||
logger.error(`Error generating elastic agent policy: ${error.message} ${error.stack}`); | ||
} | ||
throw error; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
packages/kbn-synthetics-private-location/src/lib/create_private_location.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,37 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
import { isError } from 'lodash'; | ||
import { ToolingLog } from '@kbn/tooling-log'; | ||
import { CliOptions } from '../types'; | ||
import { KibanaAPIClient } from './kibana_api_client'; | ||
|
||
export async function createPrivateLocation( | ||
{ kibanaUrl, kibanaPassword, kibanaUsername, locationName }: CliOptions, | ||
logger: ToolingLog, | ||
kibanaApiClient: KibanaAPIClient, | ||
agentPolicyId: string | ||
) { | ||
try { | ||
const response = await kibanaApiClient.sendRequest({ | ||
method: 'post', | ||
url: 'api/synthetics/private_locations', | ||
data: { | ||
label: locationName, | ||
agentPolicyId, | ||
}, | ||
}); | ||
|
||
logger.info(`Synthetics private location created successfully`); | ||
return response.data; | ||
} catch (error) { | ||
if (isError(error)) { | ||
logger.error(`Error creating synthetics private location: ${error.message} ${error.stack}`); | ||
} | ||
throw error; | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
packages/kbn-synthetics-private-location/src/lib/enroll_agent.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,88 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import execa from 'execa'; | ||
import { spawn } from 'child_process'; | ||
import * as path from 'path'; | ||
import { CliOptions } from '../types'; | ||
import { KibanaAPIClient } from './kibana_api_client'; | ||
|
||
export async function enrollAgent( | ||
{ kibanaUrl, elasticsearchHost }: CliOptions, | ||
enrollmentToken: string, | ||
kibanaApiClient: KibanaAPIClient | ||
) { | ||
const formattedKibanaURL = new URL(kibanaUrl); | ||
const formattedElasticsearchHost = new URL(elasticsearchHost); | ||
if (formattedKibanaURL.hostname === 'localhost') { | ||
formattedKibanaURL.hostname = 'host.docker.internal'; | ||
} | ||
if (formattedElasticsearchHost.hostname === 'localhost') { | ||
formattedElasticsearchHost.hostname = 'host.docker.internal'; | ||
} | ||
const version = `${await kibanaApiClient.getKibanaVersion()}-SNAPSHOT`; | ||
await new Promise((res, rej) => { | ||
try { | ||
const fleetProcess = spawn( | ||
'docker', | ||
[ | ||
'run', | ||
'-e', | ||
'FLEET_SERVER_ENABLE=1', | ||
'-e', | ||
`FLEET_SERVER_ELASTICSEARCH_HOST=${formattedElasticsearchHost.origin}`, | ||
'-e', | ||
'FLEET_SERVER_POLICY_ID=fleet-server-policy', | ||
'-e', | ||
'FLEET_INSECURE=1', | ||
'-e', | ||
`KIBANA_HOST=${formattedKibanaURL.origin}`, | ||
'-e', | ||
'KIBANA_USERNAME=elastic', | ||
'-e', | ||
'KIBANA_PASSWORD=changeme', | ||
'-e', | ||
'KIBANA_FLEET_SETUP=1', | ||
'-p', | ||
'8220:8220', | ||
'--rm', | ||
`docker.elastic.co/beats/elastic-agent:${version}`, | ||
], | ||
{ | ||
shell: true, | ||
cwd: path.join(__dirname, '../'), | ||
timeout: 120000, | ||
} | ||
); | ||
setTimeout(res, 10_000); | ||
fleetProcess.on('error', rej); | ||
} catch (error) { | ||
rej(error); | ||
} | ||
}); | ||
|
||
execa( | ||
'docker', | ||
[ | ||
'run', | ||
'-e', | ||
'FLEET_URL=https://host.docker.internal:8220', | ||
'-e', | ||
'FLEET_ENROLL=1', | ||
'-e', | ||
`FLEET_ENROLLMENT_TOKEN=${enrollmentToken}`, | ||
'-e', | ||
'FLEET_INSECURE=1', | ||
'--rm', | ||
`docker.elastic.co/beats/elastic-agent-complete:${version}`, | ||
], | ||
{ | ||
stdio: 'inherit', | ||
} | ||
); | ||
} |
34 changes: 34 additions & 0 deletions
34
packages/kbn-synthetics-private-location/src/lib/fetch_agent_policy_enrollment_token.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,34 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { isError } from 'lodash'; | ||
import { ToolingLog } from '@kbn/tooling-log'; | ||
import { KibanaAPIClient } from './kibana_api_client'; | ||
import { CliOptions } from '../types'; | ||
|
||
export async function fetchAgentPolicyEnrollmentToken( | ||
{ kibanaUrl, kibanaPassword, kibanaUsername }: CliOptions, | ||
logger: ToolingLog, | ||
kibanaApiClient: KibanaAPIClient, | ||
agentPolicyId: string | ||
) { | ||
try { | ||
const response = await kibanaApiClient.sendRequest({ | ||
method: 'get', | ||
url: `api/fleet/enrollment_api_keys?kuery=policy_id:${agentPolicyId}`, | ||
}); | ||
|
||
logger.info(`Fetching agent policy enrollment token`); | ||
return response.data; | ||
} catch (error) { | ||
if (isError(error)) { | ||
logger.error(`Error fetching agent enrollment token: ${error.message} ${error.stack}`); | ||
} | ||
throw error; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
packages/kbn-synthetics-private-location/src/lib/generate_fleet_service_token.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,34 @@ | ||
/* | ||
* 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 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { isError } from 'lodash'; | ||
import { ToolingLog } from '@kbn/tooling-log'; | ||
import { CliOptions } from '../types'; | ||
import { KibanaAPIClient } from './kibana_api_client'; | ||
|
||
export async function generateFleetServiceToken( | ||
{ kibanaUrl, kibanaPassword, kibanaUsername }: CliOptions, | ||
logger: ToolingLog, | ||
kibanaApiClient: KibanaAPIClient | ||
) { | ||
try { | ||
// Send the saved objects to Kibana using the _import API | ||
dominiqueclarke marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const response = await kibanaApiClient.sendRequest({ | ||
method: 'post', | ||
url: 'api/fleet/service_tokens', | ||
}); | ||
|
||
logger.info(`Generated fleet server service token saved`); | ||
return response.data; | ||
} catch (error) { | ||
if (isError(error)) { | ||
logger.error(`Error generating fleet server service token: ${error.message} ${error.stack}`); | ||
} | ||
throw error; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't implemented a clean up yet.