feat(cli): Support creating/updating a Username-Password-Domain static credential (#1525)

Co-authored-by: Andrew Gaffney <andrew@gaffney.cc>
Co-authored-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
This commit is contained in:
authored and irenarindos committed 2025-09-02 15:12:38 -04:00
1 parent 0824dd7d16
commit 582efa2fd2
9 files changed
+784

No files matched your search

+44
View File
@@ -0,0 +1,44 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package credentials
import (
"fmt"
"strings"
)
// ParseUsernameDomain parses the username and domain from the given strings.
// It returns the username, domain, and an error if any.
// The username can be in the format of "username", "username@domain", or "domain\username".
func ParseUsernameDomain(username, domain string) (string, string, error) {
switch {
case username != "" && domain == "":
atCount := strings.Count(username, "@")
if atCount > 1 {
return "", "", fmt.Errorf("invalid format, more than one '@' found")
}
backslashCount := strings.Count(username, "\\")
if backslashCount > 1 {
return "", "", fmt.Errorf("invalid format, more than one '\\' found")
}
if atCount > 0 && backslashCount > 0 {
return "", "", fmt.Errorf("invalid format, both '@' and '\\' found")
}
// username@domain
if u, d, ok := strings.Cut(username, "@"); ok {
return u, d, nil
}
// domain\username
if d, u, ok := strings.Cut(username, "\\"); ok {
return u, d, nil
}
case username != "" && domain != "":
if strings.Contains(username, "@") || strings.Contains(username, "\\") {
return "", "", fmt.Errorf("username and domain cannot be provided together with a username in the format of username@domain or domain\\username")
}
}
return username, domain, nil
}