Skip to content

Commit

Permalink
[ftr] use getopts to fetch server args
Browse files Browse the repository at this point in the history
  • Loading branch information
dmlemeshko committed Oct 29, 2024
1 parent 81856bc commit 60ec075
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 60ec075

Please sign in to comment.