Skip to content

Commit

Permalink
[8.x] [ftr] use getopts to fetch server args (#198227) (#198356)
Browse files Browse the repository at this point in the history
# Backport

This will backport the following commits from `main` to `8.x`:
- [[ftr] use getopts to fetch server args
(#198227)](#198227)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Dzmitry
Lemechko","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-10-30T13:47:03Z","message":"[ftr]
use getopts to fetch server args (#198227)\n\n## Summary\r\n\r\nThis PR
simplifies the code to read server arguments by using
`getopts`\r\nmodule as @jbudz
suggested.","sha":"79e64b85dc59708b0d90728eb13b69512e74506f","branchLabelMapping":{"^v9.0.0$":"main","^v8.17.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","v9.0.0","FTR","backport:version","v8.17.0"],"title":"[ftr]
use getopts to fetch server
args","number":198227,"url":"https://github.com/elastic/kibana/pull/198227","mergeCommit":{"message":"[ftr]
use getopts to fetch server args (#198227)\n\n## Summary\r\n\r\nThis PR
simplifies the code to read server arguments by using
`getopts`\r\nmodule as @jbudz
suggested.","sha":"79e64b85dc59708b0d90728eb13b69512e74506f"}},"sourceBranch":"main","suggestedTargetBranches":["8.x"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/198227","number":198227,"mergeCommit":{"message":"[ftr]
use getopts to fetch server args (#198227)\n\n## Summary\r\n\r\nThis PR
simplifies the code to read server arguments by using
`getopts`\r\nmodule as @jbudz
suggested.","sha":"79e64b85dc59708b0d90728eb13b69512e74506f"}},{"branch":"8.x","label":"v8.17.0","branchLabelMappingKey":"^v8.17.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Dzmitry Lemechko <[email protected]>
  • Loading branch information
kibanamachine and dmlemeshko authored Oct 30, 2024
1 parent f0b49cc commit 00b51bd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import getopts from 'getopts';
import { ServerlessProjectType, SERVERLESS_ROLES_ROOT_PATH } from '@kbn/es';
import { type Config } from '@kbn/test';
import { isServerlessProjectType, readRolesDescriptorsFromResource } from '@kbn/es/src/utils';
Expand Down Expand Up @@ -34,30 +35,23 @@ const getDefaultServerlessRole = (projectType: string) => {
}
};

const isRoleManagementExplicitlyEnabled = (args: string[]): boolean => {
const roleManagementArg = args.find((arg) =>
arg.startsWith('--xpack.security.roleManagementEnabled=')
);

// Return true if the value is explicitly set to 'true', otherwise false
return roleManagementArg?.split('=')[1] === 'true' || false;
};

export class ServerlessAuthProvider implements AuthProvider {
private readonly projectType: string;
private readonly roleManagementEnabled: boolean;
private readonly rolesDefinitionPath: string;

constructor(config: Config) {
const kbnServerArgs = config.get('kbnTestServer.serverArgs') as string[];
this.projectType = kbnServerArgs.reduce((acc, arg) => {
const match = arg.match(/--serverless[=\s](\w+)/);
return acc + (match ? match[1] : '');
}, '') as ServerlessProjectType;
const options = getopts(config.get('kbnTestServer.serverArgs'), {
boolean: ['xpack.security.roleManagementEnabled'],
default: {
'xpack.security.roleManagementEnabled': false,
},
});
this.projectType = options.serverless as ServerlessProjectType;

// Indicates whether role management was explicitly enabled using
// the `--xpack.security.roleManagementEnabled=true` flag.
this.roleManagementEnabled = isRoleManagementExplicitlyEnabled(kbnServerArgs);
this.roleManagementEnabled = options['xpack.security.roleManagementEnabled'];

if (!isServerlessProjectType(this.projectType)) {
throw new Error(`Unsupported serverless projectType: ${this.projectType}`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Url from 'url';
import { resolve } from 'path';
import type { ToolingLog } from '@kbn/tooling-log';
import getPort from 'get-port';
import getopts from 'getopts';
import { REPO_ROOT } from '@kbn/repo-info';
import type { ArtifactLicense, ServerlessProjectType } from '@kbn/es';
import { isServerlessProjectType, extractAndArchiveLogs } from '@kbn/es/src/utils';
Expand Down Expand Up @@ -196,12 +197,8 @@ function getESServerlessOptions(
(config.get('kbnTestServer.serverArgs') as string[])) ||
[];

const projectType = kbnServerArgs
.filter((arg) => arg.startsWith('--serverless'))
.reduce((acc, arg) => {
const match = arg.match(/--serverless[=\s](\w+)/);
return acc + (match ? match[1] : '');
}, '') as ServerlessProjectType;
const options = getopts(kbnServerArgs);
const projectType = options.serverless as ServerlessProjectType;

if (!isServerlessProjectType(projectType)) {
throw new Error(`Unsupported serverless projectType: ${projectType}`);
Expand Down

0 comments on commit 00b51bd

Please sign in to comment.