From 11fde62b8c4e61c3d037df985486d70ec12d9bc6 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Fri, 19 Jul 2024 09:04:04 +0200 Subject: [PATCH] remove custom contains funcs for slices.Contains (#2015) Signed-off-by: Kristoffer Dalby --- cmd/headscale/cli/nodes.go | 5 +++-- cmd/headscale/cli/utils.go | 11 ----------- hscontrol/oidc.go | 7 ++++--- hscontrol/util/string.go | 10 ---------- 4 files changed, 7 insertions(+), 26 deletions(-) diff --git a/cmd/headscale/cli/nodes.go b/cmd/headscale/cli/nodes.go index 58890cb002..4de7b969e1 100644 --- a/cmd/headscale/cli/nodes.go +++ b/cmd/headscale/cli/nodes.go @@ -4,6 +4,7 @@ import ( "fmt" "log" "net/netip" + "slices" "strconv" "strings" "time" @@ -617,14 +618,14 @@ func nodesToPtables( forcedTags = strings.TrimLeft(forcedTags, ",") var invalidTags string for _, tag := range node.GetInvalidTags() { - if !contains(node.GetForcedTags(), tag) { + if !slices.Contains(node.GetForcedTags(), tag) { invalidTags += "," + pterm.LightRed(tag) } } invalidTags = strings.TrimLeft(invalidTags, ",") var validTags string for _, tag := range node.GetValidTags() { - if !contains(node.GetForcedTags(), tag) { + if !slices.Contains(node.GetForcedTags(), tag) { validTags += "," + pterm.LightGreen(tag) } } diff --git a/cmd/headscale/cli/utils.go b/cmd/headscale/cli/utils.go index 8a91c5c614..e4fef8074a 100644 --- a/cmd/headscale/cli/utils.go +++ b/cmd/headscale/cli/utils.go @@ -6,7 +6,6 @@ import ( "encoding/json" "fmt" "os" - "reflect" "github.com/rs/zerolog/log" "google.golang.org/grpc" @@ -197,13 +196,3 @@ func (t tokenAuth) GetRequestMetadata( func (tokenAuth) RequireTransportSecurity() bool { return true } - -func contains[T string](ts []T, t T) bool { - for _, v := range ts { - if reflect.DeepEqual(v, t) { - return true - } - } - - return false -} diff --git a/hscontrol/oidc.go b/hscontrol/oidc.go index b728a6d009..bb836a06c2 100644 --- a/hscontrol/oidc.go +++ b/hscontrol/oidc.go @@ -10,6 +10,7 @@ import ( "fmt" "html/template" "net/http" + "slices" "strings" "time" @@ -365,7 +366,7 @@ func validateOIDCAllowedDomains( ) error { if len(allowedDomains) > 0 { if at := strings.LastIndex(claims.Email, "@"); at < 0 || - !util.IsStringInSlice(allowedDomains, claims.Email[at+1:]) { + !slices.Contains(allowedDomains, claims.Email[at+1:]) { log.Trace().Msg("authenticated principal does not match any allowed domain") writer.Header().Set("Content-Type", "text/plain; charset=utf-8") @@ -393,7 +394,7 @@ func validateOIDCAllowedGroups( ) error { if len(allowedGroups) > 0 { for _, group := range allowedGroups { - if util.IsStringInSlice(claims.Groups, group) { + if slices.Contains(claims.Groups, group) { return nil } } @@ -420,7 +421,7 @@ func validateOIDCAllowedUsers( claims *IDTokenClaims, ) error { if len(allowedUsers) > 0 && - !util.IsStringInSlice(allowedUsers, claims.Email) { + !slices.Contains(allowedUsers, claims.Email) { log.Trace().Msg("authenticated principal does not match any allowed user") writer.Header().Set("Content-Type", "text/plain; charset=utf-8") writer.WriteHeader(http.StatusBadRequest) diff --git a/hscontrol/util/string.go b/hscontrol/util/string.go index 6f018affdb..ce38b82e87 100644 --- a/hscontrol/util/string.go +++ b/hscontrol/util/string.go @@ -56,16 +56,6 @@ func GenerateRandomStringDNSSafe(size int) (string, error) { return str[:size], nil } -func IsStringInSlice(slice []string, str string) bool { - for _, s := range slice { - if s == str { - return true - } - } - - return false -} - func TailNodesToString(nodes []*tailcfg.Node) string { temp := make([]string, len(nodes))