Skip to content

Commit

Permalink
remove custom contains funcs for slices.Contains (#2015)
Browse files Browse the repository at this point in the history
Signed-off-by: Kristoffer Dalby <[email protected]>
  • Loading branch information
kradalby authored Jul 19, 2024
1 parent 9e523d4 commit 11fde62
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 26 deletions.
5 changes: 3 additions & 2 deletions cmd/headscale/cli/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log"
"net/netip"
"slices"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -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)
}
}
Expand Down
11 changes: 0 additions & 11 deletions cmd/headscale/cli/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/json"
"fmt"
"os"
"reflect"

"github.com/rs/zerolog/log"
"google.golang.org/grpc"
Expand Down Expand Up @@ -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
}
7 changes: 4 additions & 3 deletions hscontrol/oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"html/template"
"net/http"
"slices"
"strings"
"time"

Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
}
}
Expand All @@ -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)
Expand Down
10 changes: 0 additions & 10 deletions hscontrol/util/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down

0 comments on commit 11fde62

Please sign in to comment.