From 0b8f6878feb9618f1b5dab6969a8e5b00aeb647a Mon Sep 17 00:00:00 2001 From: Sheen Capadngan Date: Mon, 1 Jul 2024 18:12:16 +0800 Subject: [PATCH 1/2] misc: added check for ldap group --- .../ldap-config/ldap-config-service.ts | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/backend/src/ee/services/ldap-config/ldap-config-service.ts b/backend/src/ee/services/ldap-config/ldap-config-service.ts index df4b2bba37..22b64c16e6 100644 --- a/backend/src/ee/services/ldap-config/ldap-config-service.ts +++ b/backend/src/ee/services/ldap-config/ldap-config-service.ts @@ -53,7 +53,7 @@ import { TTestLdapConnectionDTO, TUpdateLdapCfgDTO } from "./ldap-config-types"; -import { testLDAPConfig } from "./ldap-fns"; +import { searchGroups, testLDAPConfig } from "./ldap-fns"; import { TLdapGroupMapDALFactory } from "./ldap-group-map-dal"; type TLdapConfigServiceFactoryDep = { @@ -286,7 +286,7 @@ export const ldapConfigServiceFactory = ({ return ldapConfig; }; - const getLdapCfg = async (filter: { orgId: string; isActive?: boolean }) => { + const getLdapCfg = async (filter: { orgId: string; isActive?: boolean; id?: string }) => { const ldapConfig = await ldapConfigDAL.findOne(filter); if (!ldapConfig) throw new BadRequestError({ message: "Failed to find organization LDAP data" }); @@ -716,11 +716,25 @@ export const ldapConfigServiceFactory = ({ message: "Failed to create LDAP group map due to plan restriction. Upgrade plan to create LDAP group map." }); - const ldapConfig = await ldapConfigDAL.findOne({ - id: ldapConfigId, - orgId + const ldapConfig = await getLdapCfg({ + orgId, + id: ldapConfigId }); - if (!ldapConfig) throw new BadRequestError({ message: "Failed to find organization LDAP data" }); + + if (!ldapConfig.groupSearchBase) { + throw new BadRequestError({ + message: "Configure a group search base in your LDAP configuration in order to proceed." + }); + } + + const groupSearchFilter = `(cn=${ldapGroupCN})`; + const groups = await searchGroups(ldapConfig, groupSearchFilter, ldapConfig.groupSearchBase); + + if (!groups.some((g) => g.cn === ldapGroupCN)) { + throw new BadRequestError({ + message: "Failed to find LDAP Group CN" + }); + } const group = await groupDAL.findOne({ slug: groupSlug, orgId }); if (!group) throw new BadRequestError({ message: "Failed to find group" }); From 030d4fe15226fca9b39be2d7327d0b027221c722 Mon Sep 17 00:00:00 2001 From: Sheen Capadngan Date: Mon, 1 Jul 2024 21:10:27 +0800 Subject: [PATCH 2/2] misc: added handling of empty groups and default value --- .../OrgAuthTab/LDAPGroupMapModal.tsx | 253 ++++++++++-------- 1 file changed, 143 insertions(+), 110 deletions(-) diff --git a/frontend/src/views/Settings/OrgSettingsPage/components/OrgAuthTab/LDAPGroupMapModal.tsx b/frontend/src/views/Settings/OrgSettingsPage/components/OrgAuthTab/LDAPGroupMapModal.tsx index 2578dae4ec..8377c0e4d1 100644 --- a/frontend/src/views/Settings/OrgSettingsPage/components/OrgAuthTab/LDAPGroupMapModal.tsx +++ b/frontend/src/views/Settings/OrgSettingsPage/components/OrgAuthTab/LDAPGroupMapModal.tsx @@ -1,4 +1,6 @@ +import { useEffect } from "react"; import { Controller, useForm } from "react-hook-form"; +import { useRouter } from "next/router"; import { faUsers, faXmark } from "@fortawesome/free-solid-svg-icons"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { zodResolver } from "@hookform/resolvers/zod"; @@ -66,8 +68,9 @@ export const LDAPGroupMapModal = ({ popUp, handlePopUpOpen, handlePopUpToggle }: const { mutateAsync: createLDAPGroupMapping, isLoading: createIsLoading } = useCreateLDAPGroupMapping(); const { mutateAsync: deleteLDAPGroupMapping } = useDeleteLDAPGroupMapping(); + const router = useRouter(); - const { control, handleSubmit, reset } = useForm({ + const { control, handleSubmit, reset, setValue } = useForm({ resolver: zodResolver(schema), defaultValues: { ldapGroupCN: "", @@ -130,6 +133,12 @@ export const LDAPGroupMapModal = ({ popUp, handlePopUpOpen, handlePopUpToggle }: } }; + useEffect(() => { + if (groups && groups.length > 0) { + setValue("groupSlug", groups[0].slug); + } + }, [groups, popUp.ldapGroupMap.isOpen]); + return ( -

New Group Mapping

-
-
- ( - - - - )} - /> - ( - -
- - -
-
- )} - /> -
-
-

Group Mappings

- - - - - - - - - - {isLoading && } - {!isLoading && - groupMaps?.map(({ id, ldapGroupCN, group }) => { - return ( - - - - - - ); - })} - -
LDAP Group CNInfisical Group -
{ldapGroupCN}{group.name} - { - handlePopUpOpen("deleteLdapGroupMap", { - ldapGroupMapId: id, - ldapGroupCN - }); - }} - size="lg" - colorSchema="danger" - variant="plain" - ariaLabel="update" + + + )} + /> + ( + +
+
- {groupMaps?.length === 0 && ( - - )} -
- handlePopUpToggle("deleteLdapGroupMap", isOpen)} - deleteKey="confirm" - onDeleteApproved={() => { - const deleteLdapGroupMapData = popUp?.deleteLdapGroupMap?.data as { - ldapGroupMapId: string; - ldapGroupCN: string; - }; - return onDeleteGroupMapSubmit({ - ldapConfigId: ldapConfig?.id ?? "", - ldapGroupMapId: deleteLdapGroupMapData.ldapGroupMapId, - ldapGroupCN: deleteLdapGroupMapData.ldapGroupCN - }); - }} - /> + {(groups || []).map(({ name, id, slug }) => ( + + {name} + + ))} + + + + + )} + /> + + +

Group Mappings

+ + + + + + + + + + {isLoading && } + {!isLoading && + groupMaps?.map(({ id, ldapGroupCN, group }) => { + return ( + + + + + + ); + })} + +
LDAP Group CNInfisical Group +
{ldapGroupCN}{group.name} + { + handlePopUpOpen("deleteLdapGroupMap", { + ldapGroupMapId: id, + ldapGroupCN + }); + }} + size="lg" + colorSchema="danger" + variant="plain" + ariaLabel="update" + > + + +
+ {groupMaps?.length === 0 && ( + + )} +
+ handlePopUpToggle("deleteLdapGroupMap", isOpen)} + deleteKey="confirm" + onDeleteApproved={() => { + const deleteLdapGroupMapData = popUp?.deleteLdapGroupMap?.data as { + ldapGroupMapId: string; + ldapGroupCN: string; + }; + return onDeleteGroupMapSubmit({ + ldapConfigId: ldapConfig?.id ?? "", + ldapGroupMapId: deleteLdapGroupMapData.ldapGroupMapId, + ldapGroupCN: deleteLdapGroupMapData.ldapGroupCN + }); + }} + /> + + )} + {groups && groups.length === 0 && ( +
+
+ You do not have any Infisical groups in your organization. Create one in order to + proceed. +
+ +
+ )}
);