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:
1 parent
0824dd7d16
commit
582efa2fd2
9 files changed
+784
No files matched your search
@@ -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
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
package credentials
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// TestParseUsernameDomain tests the ParseUsernameDomain function with various cases.
|
||||
func TestParseUsernameDomain(t *testing.T) {
|
||||
t.Parallel()
|
||||
tests := []struct {
|
||||
name string
|
||||
username string
|
||||
domain string
|
||||
expectedUsername string
|
||||
expectedDomain string
|
||||
expectError bool
|
||||
expectErrorStr string
|
||||
}{
|
||||
{
|
||||
name: "Empty username and domain",
|
||||
username: "",
|
||||
domain: "",
|
||||
expectedUsername: "",
|
||||
expectedDomain: "",
|
||||
expectError: false,
|
||||
},
|
||||
{
|
||||
name: "Username without domain",
|
||||
username: "user",
|
||||
domain: "",
|
||||
expectedUsername: "user",
|
||||
expectedDomain: "",
|
||||
expectError: false,
|
||||
},
|
||||
{
|
||||
name: "Username and domain",
|
||||
username: "user",
|
||||
domain: "domain",
|
||||
expectedUsername: "user",
|
||||
expectedDomain: "domain",
|
||||
expectError: false,
|
||||
},
|
||||
{
|
||||
name: "Empty username with domain",
|
||||
username: "",
|
||||
domain: "domain",
|
||||
expectedUsername: "",
|
||||
expectedDomain: "domain",
|
||||
expectError: false,
|
||||
},
|
||||
{
|
||||
name: "Username with @domain",
|
||||
username: "user@domain",
|
||||
domain: "",
|
||||
expectedUsername: "user",
|
||||
expectedDomain: "domain",
|
||||
expectError: false,
|
||||
},
|
||||
{
|
||||
name: "Domain\\username format",
|
||||
username: "domain\\user",
|
||||
domain: "",
|
||||
expectedUsername: "user",
|
||||
expectedDomain: "domain",
|
||||
expectError: false,
|
||||
},
|
||||
{
|
||||
name: "Conflicting with username@domain and domain",
|
||||
username: "user@domain",
|
||||
domain: "domain2",
|
||||
expectedUsername: "",
|
||||
expectedDomain: "",
|
||||
expectError: true,
|
||||
expectErrorStr: "username and domain cannot be provided together with a username in the format of username@domain or domain\\username",
|
||||
},
|
||||
{
|
||||
name: "Conflicting with domain\\username and domain",
|
||||
username: "domain\\user",
|
||||
domain: "domain2",
|
||||
expectedUsername: "",
|
||||
expectedDomain: "",
|
||||
expectError: true,
|
||||
expectErrorStr: "username and domain cannot be provided together with a username in the format of username@domain or domain\\username",
|
||||
},
|
||||
{
|
||||
name: "Multiple '@'",
|
||||
username: "user@domain@domain",
|
||||
domain: "",
|
||||
expectedUsername: "",
|
||||
expectedDomain: "",
|
||||
expectError: true,
|
||||
expectErrorStr: "invalid format, more than one '@' found",
|
||||
},
|
||||
{
|
||||
name: "Multiple '\\'",
|
||||
username: "domain\\domain\\user",
|
||||
domain: "",
|
||||
expectedUsername: "",
|
||||
expectedDomain: "",
|
||||
expectError: true,
|
||||
expectErrorStr: "invalid format, more than one '\\' found",
|
||||
},
|
||||
{
|
||||
name: "Mixing '@' and '\\'",
|
||||
username: "domain\\user@domain.com",
|
||||
domain: "",
|
||||
expectedUsername: "",
|
||||
expectedDomain: "",
|
||||
expectError: true,
|
||||
expectErrorStr: "invalid format, both '@' and '\\' found",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
username, domain, err := ParseUsernameDomain(test.username, test.domain)
|
||||
if test.expectError {
|
||||
require.ErrorContains(t, err, test.expectErrorStr)
|
||||
return
|
||||
}
|
||||
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, test.expectedUsername, username)
|
||||
require.Equal(t, test.expectedDomain, domain)
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user