Skip to content

Commit

Permalink
Parse service account user from LDAP str (#547)
Browse files Browse the repository at this point in the history
  • Loading branch information
pjsier committed Mar 11, 2024
1 parent 67ee876 commit ac81376
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
19 changes: 18 additions & 1 deletion minio/resource_minio_service_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand Down Expand Up @@ -177,7 +178,8 @@ func minioReadServiceAccount(ctx context.Context, d *schema.ResourceData, meta i

_ = d.Set("disable_user", output.AccountStatus == "off")

if err := d.Set("target_user", output.ParentUser); err != nil {
targetUser := parseUserFromParentUser(output.ParentUser)
if err := d.Set("target_user", targetUser); err != nil {
return NewResourceError("reading service account failed", d.Id(), err)
}

Expand Down Expand Up @@ -230,3 +232,18 @@ func processServiceAccountPolicy(policy string) []byte {
}
return []byte(policy)
}

// Handle LDAP responses in ParentUser struct
func parseUserFromParentUser(parentUser string) string {
user := parentUser

// Iterate through comma-separated chunks, will be ignored if not LDAP
for _, ldapSection := range strings.Split(parentUser, ",") {
splitSection := strings.Split(ldapSection, "=")
if len(splitSection) == 2 && strings.ToLower(strings.TrimSpace(splitSection[0])) == "cn" {
return strings.TrimSpace(splitSection[1])
}
}

return user
}
7 changes: 7 additions & 0 deletions minio/resource_minio_service_account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/minio/madmin-go/v3"
"gotest.tools/v3/assert"
)

func TestServiceAccount_basic(t *testing.T) {
Expand Down Expand Up @@ -147,6 +148,12 @@ func TestServiceAccount_Policy(t *testing.T) {
})
}

func TestParseUserFromParentUser(t *testing.T) {
assert.Equal(t, "minio-user", parseUserFromParentUser("minio-user"))
assert.Equal(t, "minio-user", parseUserFromParentUser("CN = minio-user, DC=example,DC=org"))
assert.Equal(t, "minio-user", parseUserFromParentUser("cn=minio-user, DC=example"))
}

func testAccMinioServiceAccountConfig(rName string) string {
return fmt.Sprintf(`
resource "minio_iam_service_account" "test" {
Expand Down

0 comments on commit ac81376

Please sign in to comment.