Skip to content

Commit

Permalink
Change the auth configuration endpoint request
Browse files Browse the repository at this point in the history
  • Loading branch information
lucianogorza committed Dec 14, 2023
1 parent e360757 commit f8bb25f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { Steps } from '../steps/steps';
import { InputForm } from '../../../../common/form';
import {
getGroups,
getMasterRemoteConfiguration,
getMasterConfiguration,
} from '../../services/register-agent-services';
import { useForm } from '../../../../common/form/hooks';
import { FormConfiguration } from '../../../../common/form/types';
Expand Down Expand Up @@ -99,24 +99,12 @@ export const RegisterAgent = compose(

const form = useForm(initialFields);

const getRemoteConfig = async () => {
const remoteConfig = await getMasterRemoteConfiguration();
if (remoteConfig) {
setHaveUdpProtocol(remoteConfig.isUdp);
}
};

const getAuthInfo = async () => {
try {
const result = await WzRequest.apiReq(
'GET',
'/agents/000/config/auth/auth',
{},
);
return result?.data?.data || {};
} catch (error) {
ErrorHandler.handleError(error);
const getMasterConfig = async () => {
const masterConfig = await getMasterConfiguration();
if (masterConfig?.remote) {
setHaveUdpProtocol(masterConfig.remote.isUdp);
}
return masterConfig;
};

const getWazuhVersion = async () => {
Expand All @@ -143,15 +131,14 @@ export const RegisterAgent = compose(
const fetchData = async () => {
try {
const wazuhVersion = await getWazuhVersion();
await getRemoteConfig();
const authInfo = await getAuthInfo();
const { auth: authConfig } = await getMasterConfig();
// get wazuh password configuration
let wazuhPassword = '';
const needsPassword = (authInfo.auth || {}).use_password === 'yes';
const needsPassword = authConfig?.auth?.use_password === 'yes';
if (needsPassword) {
wazuhPassword =
configuration['enrollment.password'] ||
authInfo['authd.pass'] ||
configuration?.['enrollment.password'] ||
authConfig?.['authd.pass'] ||
'';
}
const groups = await getGroups();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export type ServerAddressOptions = {
/**
* Get the cluster status
*/
const clusterStatusResponse = async (): Promise<boolean> => {
export const clusterStatusResponse = async (): Promise<boolean> => {
const clusterStatus = await WzRequest.apiReq('GET', '/cluster/status', {});
if (
clusterStatus.data.data.enabled === 'yes' &&
Expand All @@ -48,17 +48,17 @@ const clusterStatusResponse = async (): Promise<boolean> => {
/**
* Get the remote configuration from api
*/
export const getRemoteConfiguration = async (
async function getRemoteConfiguration(
nodeName: string,
): Promise<RemoteConfig> => {
clusterStatus: boolean,
): Promise<RemoteConfig> {
let config: RemoteConfig = {
name: nodeName,
isUdp: false,
haveSecureConnection: false,
};

try {
const clusterStatus = await clusterStatusResponse();
let result;
if (clusterStatus) {
result = await WzRequest.apiReq(
Expand Down Expand Up @@ -99,7 +99,20 @@ export const getRemoteConfiguration = async (
} catch (error) {
return config;
}
};
}
/**
* Get the manager/cluster auth configuration from Wazuh API
* @param node
* @returns
*/
async function getAuthConfiguration(node: string, clusterStatus: boolean) {
const authConfigUrl = clusterStatus
? `/cluster/${node}/configuration/auth/auth`
: '/manager/configuration/auth/auth';
const result = await WzRequest.apiReq('GET', authConfigUrl, {});
const auth = result?.data?.data?.affected_items?.[0];
return auth;
}

/**
* Get the remote protocol available from list of protocols
Expand All @@ -118,15 +131,19 @@ function getRemoteProtocol(protocols: Protocol[]) {
* @param nodeSelected
* @param defaultServerAddress
*/
export const getConnectionConfig = async (
async function getConnectionConfig(
nodeSelected: ServerAddressOptions,
defaultServerAddress?: string,
) => {
) {
const nodeName = nodeSelected?.label;
const nodeIp = nodeSelected?.value;
if (!defaultServerAddress) {
if (nodeSelected.nodetype !== 'custom') {
const remoteConfig = await getRemoteConfiguration(nodeName);
const clusterStatus = await clusterStatusResponse();
const remoteConfig = await getRemoteConfiguration(
nodeName,
clusterStatus,
);
return {
serverAddress: nodeIp,
udpProtocol: remoteConfig.isUdp,
Expand All @@ -146,7 +163,7 @@ export const getConnectionConfig = async (
connectionSecure: true,
};
}
};
}

type NodeItem = {
name: string;
Expand All @@ -165,14 +182,14 @@ type NodeResponse = {
/**
* Get the list of the cluster nodes and parse it into a list of options
*/
const getNodeIPs = async (): Promise<any> => {
export const getNodeIPs = async (): Promise<any> => {
return await WzRequest.apiReq('GET', '/cluster/nodes', {});
};

/**
* Get the list of the manager and parse it into a list of options
*/
const getManagerNode = async (): Promise<any> => {
export const getManagerNode = async (): Promise<any> => {
const managerNode = await WzRequest.apiReq('GET', '/manager/api/config', {});
return (
managerNode?.data?.data?.affected_items?.map(item => ({
Expand All @@ -187,7 +204,9 @@ const getManagerNode = async (): Promise<any> => {
* Parse the nodes list from the API response to a format that can be used by the EuiComboBox
* @param nodes
*/
const parseNodesInOptions = (nodes: NodeResponse): ServerAddressOptions[] => {
export const parseNodesInOptions = (
nodes: NodeResponse,
): ServerAddressOptions[] => {
return nodes.data.data.affected_items.map((item: NodeItem) => ({
label: item.name,
value: item.ip,
Expand All @@ -198,7 +217,9 @@ const parseNodesInOptions = (nodes: NodeResponse): ServerAddressOptions[] => {
/**
* Get the list of the cluster nodes from API and parse it into a list of options
*/
const fetchClusterNodesOptions = async (): Promise<ServerAddressOptions[]> => {
export const fetchClusterNodesOptions = async (): Promise<
ServerAddressOptions[]
> => {
const clusterStatus = await clusterStatusResponse();
if (clusterStatus) {
// Cluster mode
Expand All @@ -216,22 +237,33 @@ const fetchClusterNodesOptions = async (): Promise<ServerAddressOptions[]> => {
* Get the master node data from the list of cluster nodes
* @param nodeIps
*/
const getMasterNode = (
export const getMasterNode = (
nodeIps: ServerAddressOptions[],
): ServerAddressOptions[] => {
return nodeIps.filter(nodeIp => nodeIp.nodetype === 'master');
};

/**
* Get the remote configuration from manager
* Get the remote and the auth configuration from manager
* This function get the config from manager mode or cluster mode
*/
export const getMasterRemoteConfiguration = async () => {
export const getMasterConfiguration = async () => {
const nodes = await fetchClusterNodesOptions();
const masterNode = getMasterNode(nodes);
return await getRemoteConfiguration(masterNode[0].label);
const clusterStatus = await clusterStatusResponse();
const remote = await getRemoteConfiguration(
masterNode[0].label,
clusterStatus,
);
const auth = await getAuthConfiguration(masterNode[0].label, clusterStatus);
return {
remote,
auth,
};
};

export { getConnectionConfig, getRemoteConfiguration };

export const getGroups = async () => {
try {
const result = await WzRequest.apiReq('GET', '/groups', {});
Expand Down

0 comments on commit f8bb25f

Please sign in to comment.