Skip to content

Commit

Permalink
refactor: remove usages of experimental maps package (#7849)
Browse files Browse the repository at this point in the history
All 4 usages of the `maps.Keys` function from the
`golang.org/x/exp/maps` package can be refactored to a simpler
alternative. If we need it in the future, it is available in the
standard library since Go 1.23.
  • Loading branch information
Juneezee authored Dec 6, 2024
1 parent 87104b0 commit 13db2a2
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 176 deletions.
20 changes: 20 additions & 0 deletions cmd/admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"errors"
"fmt"

"github.com/jmhodges/clock"
Expand Down Expand Up @@ -94,3 +95,22 @@ func newAdmin(configFile string, dryRun bool) (*admin, error) {
log: logger,
}, nil
}

// findActiveInputMethodFlag returns a single key from setInputs with a value of `true`,
// if exactly one exists. Otherwise it returns an error.
func findActiveInputMethodFlag(setInputs map[string]bool) (string, error) {
var activeFlags []string
for flag, isSet := range setInputs {
if isSet {
activeFlags = append(activeFlags, flag)
}
}

if len(activeFlags) == 0 {
return "", errors.New("at least one input method flag must be specified")
} else if len(activeFlags) > 1 {
return "", fmt.Errorf("more than one input method flag specified: %v", activeFlags)
}

return activeFlags[0], nil
}
59 changes: 59 additions & 0 deletions cmd/admin/admin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package main

import (
"testing"

"github.com/letsencrypt/boulder/test"
)

func Test_findActiveInputMethodFlag(t *testing.T) {
tests := []struct {
name string
setInputs map[string]bool
expected string
wantErr bool
}{
{
name: "No active flags",
setInputs: map[string]bool{
"-private-key": false,
"-spki-file": false,
"-cert-file": false,
},
expected: "",
wantErr: true,
},
{
name: "Multiple active flags",
setInputs: map[string]bool{
"-private-key": true,
"-spki-file": true,
"-cert-file": false,
},
expected: "",
wantErr: true,
},
{
name: "Single active flag",
setInputs: map[string]bool{
"-private-key": true,
"-spki-file": false,
"-cert-file": false,
},
expected: "-private-key",
wantErr: false,
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
result, err := findActiveInputMethodFlag(tc.setInputs)
if tc.wantErr {
test.AssertError(t, err, "findActiveInputMethodFlag() should have errored")
} else {
test.AssertNotError(t, err, "findActiveInputMethodFlag() should not have errored")
test.AssertEquals(t, result, tc.expected)
}
})
}
}
12 changes: 4 additions & 8 deletions cmd/admin/cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"unicode"

"golang.org/x/crypto/ocsp"
"golang.org/x/exp/maps"

core "github.com/letsencrypt/boulder/core"
berrors "github.com/letsencrypt/boulder/errors"
Expand Down Expand Up @@ -109,16 +108,13 @@ func (s *subcommandRevokeCert) Run(ctx context.Context, a *admin) error {
"-reg-id": s.regID != 0,
"-cert-file": s.certFile != "",
}
maps.DeleteFunc(setInputs, func(_ string, v bool) bool { return !v })
if len(setInputs) == 0 {
return errors.New("at least one input method flag must be specified")
} else if len(setInputs) > 1 {
return fmt.Errorf("more than one input method flag specified: %v", maps.Keys(setInputs))
activeFlag, err := findActiveInputMethodFlag(setInputs)
if err != nil {
return err
}

var serials []string
var err error
switch maps.Keys(setInputs)[0] {
switch activeFlag {
case "-serial":
serials, err = []string{s.serial}, nil
case "-incident-table":
Expand Down
12 changes: 4 additions & 8 deletions cmd/admin/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"sync"
"sync/atomic"

"golang.org/x/exp/maps"
"google.golang.org/protobuf/types/known/timestamppb"

"github.com/letsencrypt/boulder/core"
Expand Down Expand Up @@ -69,16 +68,13 @@ func (s *subcommandBlockKey) Run(ctx context.Context, a *admin) error {
"-cert-file": s.certFile != "",
"-csr-file": s.csrFile != "",
}
maps.DeleteFunc(setInputs, func(_ string, v bool) bool { return !v })
if len(setInputs) == 0 {
return errors.New("at least one input method flag must be specified")
} else if len(setInputs) > 1 {
return fmt.Errorf("more than one input method flag specified: %v", maps.Keys(setInputs))
activeFlag, err := findActiveInputMethodFlag(setInputs)
if err != nil {
return err
}

var spkiHashes [][]byte
var err error
switch maps.Keys(setInputs)[0] {
switch activeFlag {
case "-private-key":
var spkiHash []byte
spkiHash, err = a.spkiHashFromPrivateKey(s.privKey)
Expand Down
12 changes: 4 additions & 8 deletions cmd/admin/unpause_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (

sapb "github.com/letsencrypt/boulder/sa/proto"
"github.com/letsencrypt/boulder/unpause"
"golang.org/x/exp/maps"
)

// subcommandUnpauseAccount encapsulates the "admin unpause-account" command.
Expand Down Expand Up @@ -44,16 +43,13 @@ func (u *subcommandUnpauseAccount) Run(ctx context.Context, a *admin) error {
"-account": u.accountID != 0,
"-batch-file": u.batchFile != "",
}
maps.DeleteFunc(setInputs, func(_ string, v bool) bool { return !v })
if len(setInputs) == 0 {
return errors.New("at least one input method flag must be specified")
} else if len(setInputs) > 1 {
return fmt.Errorf("more than one input method flag specified: %v", maps.Keys(setInputs))
activeFlag, err := findActiveInputMethodFlag(setInputs)
if err != nil {
return err
}

var regIDs []int64
var err error
switch maps.Keys(setInputs)[0] {
switch activeFlag {
case "-account":
regIDs = []int64{u.accountID}
case "-batch-file":
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ require (
go.opentelemetry.io/otel/sdk v1.30.0
go.opentelemetry.io/otel/trace v1.30.0
golang.org/x/crypto v0.27.0
golang.org/x/exp v0.0.0-20240112132812-db7319d0e0e3
golang.org/x/net v0.29.0
golang.org/x/sync v0.8.0
golang.org/x/term v0.24.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,6 @@ golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v
golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54=
golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A=
golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70=
golang.org/x/exp v0.0.0-20240112132812-db7319d0e0e3 h1:hNQpMuAJe5CtcUqCXaWga3FHu+kQvCqcsoVaQgSV60o=
golang.org/x/exp v0.0.0-20240112132812-db7319d0e0e3/go.mod h1:idGWGoKP1toJGkd5/ig9ZLuPcZBC3ewk7SzmH0uou08=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
Expand Down
27 changes: 0 additions & 27 deletions vendor/golang.org/x/exp/LICENSE

This file was deleted.

22 changes: 0 additions & 22 deletions vendor/golang.org/x/exp/PATENTS

This file was deleted.

94 changes: 0 additions & 94 deletions vendor/golang.org/x/exp/maps/maps.go

This file was deleted.

3 changes: 0 additions & 3 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -369,9 +369,6 @@ golang.org/x/crypto/cryptobyte/asn1
golang.org/x/crypto/ed25519
golang.org/x/crypto/ocsp
golang.org/x/crypto/pbkdf2
# golang.org/x/exp v0.0.0-20240112132812-db7319d0e0e3
## explicit; go 1.20
golang.org/x/exp/maps
# golang.org/x/mod v0.18.0
## explicit; go 1.18
golang.org/x/mod/semver
Expand Down
4 changes: 1 addition & 3 deletions wfe2/wfe.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"math/rand/v2"
"net"
"net/http"
"slices"
"strconv"
"strings"
"time"
Expand All @@ -22,7 +21,6 @@ import (
"github.com/prometheus/client_golang/prometheus"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel/trace"
"golang.org/x/exp/maps"
"google.golang.org/protobuf/types/known/emptypb"

"github.com/letsencrypt/boulder/core"
Expand Down Expand Up @@ -2273,7 +2271,7 @@ func (wfe *WebFrontEndImpl) validateCertificateProfileName(profile string) error
// No profile name is specified.
return nil
}
if !slices.Contains(maps.Keys(wfe.certProfiles), profile) {
if _, ok := wfe.certProfiles[profile]; !ok {
// The profile name is not in the list of configured profiles.
return errors.New("not a recognized profile name")
}
Expand Down

0 comments on commit 13db2a2

Please sign in to comment.