Skip to content

Commit

Permalink
remove unnecessary checks on slices
Browse files Browse the repository at this point in the history
  • Loading branch information
tsujamin committed Sep 23, 2022
1 parent 09863b5 commit c52e3aa
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 31 deletions.
11 changes: 5 additions & 6 deletions cmd/headscale/cli/preauthkeys.go
Expand Up @@ -108,15 +108,14 @@ var listPreAuthKeys = &cobra.Command{
reusable = fmt.Sprintf("%v", key.GetReusable())
}

var aclTags string
aclTags := ""

if len(key.AclTags) > 0 {
for _, tag := range key.AclTags {
aclTags += "," + tag
}
aclTags = strings.TrimLeft(aclTags, ",")
for _, tag := range key.AclTags {
aclTags += "," + tag
}

aclTags = strings.TrimLeft(aclTags, ",")

tableData = append(tableData, []string{
key.GetId(),
key.GetKey(),
Expand Down
18 changes: 7 additions & 11 deletions grpcv1.go
@@ -1,4 +1,4 @@
//nolint
// nolint
package headscale

import (
Expand Down Expand Up @@ -106,15 +106,12 @@ func (api headscaleV1APIServer) CreatePreAuthKey(
expiration = request.GetExpiration().AsTime()
}

if len(request.AclTags) > 0 {
for _, tag := range request.AclTags {
err := validateTag(tag)

if err != nil {
return &v1.CreatePreAuthKeyResponse{
PreAuthKey: nil,
}, status.Error(codes.InvalidArgument, err.Error())
}
for _, tag := range request.AclTags {
err := validateTag(tag)
if err != nil {
return &v1.CreatePreAuthKeyResponse{
PreAuthKey: nil,
}, status.Error(codes.InvalidArgument, err.Error())
}
}

Expand Down Expand Up @@ -154,7 +151,6 @@ func (api headscaleV1APIServer) ListPreAuthKeys(
request *v1.ListPreAuthKeysRequest,
) (*v1.ListPreAuthKeysResponse, error) {
preAuthKeys, err := api.h.ListPreAuthKeys(request.GetNamespace())

if err != nil {
return nil, err
}
Expand Down
6 changes: 2 additions & 4 deletions preauth_keys.go
Expand Up @@ -234,10 +234,8 @@ func (key *PreAuthKey) toProto() *v1.PreAuthKey {
protoKey.CreatedAt = timestamppb.New(*key.CreatedAt)
}

if len(key.ACLTags) > 0 {
for idx := range key.ACLTags {
protoKey.AclTags[idx] = key.ACLTags[idx].Tag
}
for idx := range key.ACLTags {
protoKey.AclTags[idx] = key.ACLTags[idx].Tag
}

return &protoKey
Expand Down
20 changes: 10 additions & 10 deletions protocol_common.go
Expand Up @@ -358,18 +358,18 @@ func (h *Headscale) handleAuthKeyCommon(
if len(aclTags) > 0 {
// This conditional preserves the existing behaviour, although SaaS would reset the tags on auth-key login
err = h.SetTags(machine, aclTags)
}

if err != nil {
log.Error().
Caller().
Bool("noise", machineKey.IsZero()).
Str("machine", machine.Hostname).
Strs("aclTags", aclTags).
Err(err).
Msg("Failed to set tags after refreshing machine")
if err != nil {
log.Error().
Caller().
Bool("noise", machineKey.IsZero()).
Str("machine", machine.Hostname).
Strs("aclTags", aclTags).
Err(err).
Msg("Failed to set tags after refreshing machine")

return
return
}
}
} else {
now := time.Now().UTC()
Expand Down

0 comments on commit c52e3aa

Please sign in to comment.