Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse service account user from LDAP str #547

Merged
merged 1 commit into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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])
}
}
Comment on lines +241 to +246
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK, a CN can only be a leaf in a LDAP/AD directory, so there is no need to iterate over each node. Furthermore, = may be a legal character if it has been escaped (see RFC4514, page 13). How about using a regex instead?

Finally, potentially out of scope for this issue, but the string may contain sequence character U+... but I'm not sure it would be interpreted as valid unicode char code.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@acolombier thanks for the clarification and link to the RFC! I'm not familiar with the specification so this is helpful. Just skimming the spec for now, but is = allowed to have spaces around it after a section? i.e. is CN = test valid?


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"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If my interpretation of the RFC4514 (page 11) is correct, this should result in minio-user

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