From 57f221d95525dd1c09ff1bfd3f66e97ac974c7d5 Mon Sep 17 00:00:00 2001 From: Pablo RUTH Date: Tue, 20 Jun 2023 17:08:46 +0200 Subject: [PATCH] Make OU parsing case insensitive in ldap_group --- ldap/resource_ldap_group.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/ldap/resource_ldap_group.go b/ldap/resource_ldap_group.go index f9b7d35..0c235f1 100644 --- a/ldap/resource_ldap_group.go +++ b/ldap/resource_ldap_group.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "regexp" - "strings" "github.com/Ouest-France/goldap" "github.com/go-ldap/ldap/v3" @@ -139,9 +138,14 @@ func resourceLDAPGroupRead(ctx context.Context, d *schema.ResourceData, m interf return diag.FromErr(err) } - // Remove the `CN=,` from the DN to get the OU - ou := strings.ReplaceAll(dn, fmt.Sprintf("CN=%s,", attributes["name"][0]), "") - if err := d.Set("ou", ou); err != nil { + // Remove the `CN=` from the DN to get the OU + // using the regex `^cn=.*?,(.*)$` in case insensitive mode + reg := regexp.MustCompile(`(?i)^cn=.*?,(.*)$`) + match := reg.FindStringSubmatch(dn) + if len(match) != 2 { + return diag.Errorf("Failed parsing OU from DN (must match regex `^cn=.*?,(.*)$`): %s", dn) + } + if err := d.Set("ou", match[1]); err != nil { return diag.FromErr(err) }