change: update name of method to check and normalize Domain name

This commit is contained in:
Adrien Raffin-Caboisse 2022-03-06 20:46:17 +01:00
parent dcf3ea567c
commit 1114449601
No known key found for this signature in database
GPG Key ID: 7FB60532DEBEAD6A
5 changed files with 14 additions and 14 deletions

View File

@ -444,7 +444,7 @@ func expandGroup(
errInvalidGroup, errInvalidGroup,
) )
} }
grp, err := NormalizeNamespaceName(group, stripEmailDomain) grp, err := NormalizeName(group, stripEmailDomain)
if err != nil { if err != nil {
return []string{}, fmt.Errorf( return []string{}, fmt.Errorf(
"failed to normalize group %q, err: %w", "failed to normalize group %q, err: %w",

View File

@ -41,7 +41,7 @@ type Namespace struct {
// CreateNamespace creates a new Namespace. Returns error if could not be created // CreateNamespace creates a new Namespace. Returns error if could not be created
// or another namespace already exists. // or another namespace already exists.
func (h *Headscale) CreateNamespace(name string) (*Namespace, error) { func (h *Headscale) CreateNamespace(name string) (*Namespace, error) {
err := CheckNamespaceName(name) err := CheckName(name)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -104,7 +104,7 @@ func (h *Headscale) RenameNamespace(oldName, newName string) error {
if err != nil { if err != nil {
return err return err
} }
err = CheckNamespaceName(newName) err = CheckName(newName)
if err != nil { if err != nil {
return err return err
} }
@ -150,7 +150,7 @@ func (h *Headscale) ListNamespaces() ([]Namespace, error) {
// ListMachinesInNamespace gets all the nodes in a given namespace. // ListMachinesInNamespace gets all the nodes in a given namespace.
func (h *Headscale) ListMachinesInNamespace(name string) ([]Machine, error) { func (h *Headscale) ListMachinesInNamespace(name string) ([]Machine, error) {
err := CheckNamespaceName(name) err := CheckName(name)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -169,7 +169,7 @@ func (h *Headscale) ListMachinesInNamespace(name string) ([]Machine, error) {
// SetMachineNamespace assigns a Machine to a namespace. // SetMachineNamespace assigns a Machine to a namespace.
func (h *Headscale) SetMachineNamespace(machine *Machine, namespaceName string) error { func (h *Headscale) SetMachineNamespace(machine *Machine, namespaceName string) error {
err := CheckNamespaceName(namespaceName) err := CheckName(namespaceName)
if err != nil { if err != nil {
return err return err
} }
@ -237,9 +237,9 @@ func (n *Namespace) toProto() *v1.Namespace {
} }
} }
// NormalizeNamespaceName will replace forbidden chars in namespace // NormalizeName will replace forbidden chars in namespace
// it can also return an error if the namespace doesn't respect RFC 952 and 1123. // it can also return an error if the namespace doesn't respect RFC 952 and 1123.
func NormalizeNamespaceName(name string, stripEmailDomain bool) (string, error) { func NormalizeName(name string, stripEmailDomain bool) (string, error) {
name = strings.ToLower(name) name = strings.ToLower(name)
name = strings.ReplaceAll(name, "'", "") name = strings.ReplaceAll(name, "'", "")
atIdx := strings.Index(name, "@") atIdx := strings.Index(name, "@")
@ -263,7 +263,7 @@ func NormalizeNamespaceName(name string, stripEmailDomain bool) (string, error)
return name, nil return name, nil
} }
func CheckNamespaceName(name string) error { func CheckName(name string) error {
if len(name) > labelHostnameLength { if len(name) > labelHostnameLength {
return fmt.Errorf( return fmt.Errorf(
"Namespace must not be over 63 chars. %v doesn't comply with this rule: %w", "Namespace must not be over 63 chars. %v doesn't comply with this rule: %w",

View File

@ -233,7 +233,7 @@ func (s *Suite) TestGetMapResponseUserProfiles(c *check.C) {
c.Assert(found, check.Equals, true) c.Assert(found, check.Equals, true)
} }
func TestNormalizeNamespaceName(t *testing.T) { func TestNormalizeName(t *testing.T) {
type args struct { type args struct {
name string name string
stripEmailDomain bool stripEmailDomain bool
@ -310,7 +310,7 @@ func TestNormalizeNamespaceName(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
got, err := NormalizeNamespaceName(tt.args.name, tt.args.stripEmailDomain) got, err := NormalizeName(tt.args.name, tt.args.stripEmailDomain)
if (err != nil) != tt.wantErr { if (err != nil) != tt.wantErr {
t.Errorf( t.Errorf(
"NormalizeNamespaceName() error = %v, wantErr %v", "NormalizeNamespaceName() error = %v, wantErr %v",
@ -327,7 +327,7 @@ func TestNormalizeNamespaceName(t *testing.T) {
} }
} }
func TestCheckNamespaceName(t *testing.T) { func TestCheckName(t *testing.T) {
type args struct { type args struct {
name string name string
} }
@ -366,7 +366,7 @@ func TestCheckNamespaceName(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
if err := CheckNamespaceName(tt.args.name); (err != nil) != tt.wantErr { if err := CheckName(tt.args.name); (err != nil) != tt.wantErr {
t.Errorf("CheckNamespaceName() error = %v, wantErr %v", err, tt.wantErr) t.Errorf("CheckNamespaceName() error = %v, wantErr %v", err, tt.wantErr)
} }
}) })

View File

@ -253,7 +253,7 @@ func (h *Headscale) OIDCCallback(ctx *gin.Context) {
return return
} }
namespaceName, err := NormalizeNamespaceName( namespaceName, err := NormalizeName(
claims.Email, claims.Email,
h.cfg.OIDC.StripEmaildomain, h.cfg.OIDC.StripEmaildomain,
) )

View File

@ -83,7 +83,7 @@ func (h *Headscale) PollNetMapHandler(ctx *gin.Context) {
Str("machine", machine.Name). Str("machine", machine.Name).
Msg("Found machine in database") Msg("Found machine in database")
hname, err := NormalizeNamespaceName( hname, err := NormalizeName(
req.Hostinfo.Hostname, req.Hostinfo.Hostname,
h.cfg.OIDC.StripEmaildomain, h.cfg.OIDC.StripEmaildomain,
) )