Skip to content

Commit 3e7a77e

Browse files
authored
Merge pull request #6360 from suranmiao/master
refactor: use slices.Contains to simplify code
2 parents 91beabf + 9d6a981 commit 3e7a77e

File tree

6 files changed

+14
-29
lines changed

6 files changed

+14
-29
lines changed

.changelog/6360.trivial.md

Whitespace-only changes.

go/common/crypto/signature/signers/file/file_signer.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"io"
99
"os"
1010
"path/filepath"
11+
"slices"
1112

1213
"github.com/oasisprotocol/curve25519-voi/primitives/ed25519"
1314
"github.com/oasisprotocol/curve25519-voi/primitives/ed25519/extra/ecvrf"
@@ -81,10 +82,8 @@ type Factory struct {
8182
// EnsureRole ensures that the SignerFactory is configured for the given
8283
// role.
8384
func (fac *Factory) EnsureRole(role signature.SignerRole) error {
84-
for _, v := range fac.roles {
85-
if v == role {
86-
return nil
87-
}
85+
if slices.Contains(fac.roles, role) {
86+
return nil
8887
}
8988
return signature.ErrRoleMismatch
9089
}

go/common/crypto/signature/signers/plugin/plugin.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"io"
77
"net/rpc"
88
"os/exec"
9+
"slices"
910
"sync"
1011

1112
hclog "github.com/hashicorp/go-hclog"
@@ -80,10 +81,8 @@ func NewFactory(config any, roles ...signature.SignerRole) (signature.SignerFact
8081
// It's not like all the cheap HSMs people seem to like using is going
8182
// to support ECVRF anytime soon, when they don't even fully support
8283
// RFC 8032.
83-
for _, role := range roles {
84-
if role == signature.SignerVRF {
85-
return nil, signature.ErrVRFNotSupported
86-
}
84+
if slices.Contains(roles, signature.SignerVRF) {
85+
return nil, signature.ErrVRFNotSupported
8786
}
8887

8988
// Why yes, the correct thing to do is to call `client.Kill`,

go/common/node/sgx.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package node
33
import (
44
"encoding/binary"
55
"fmt"
6+
"slices"
67
"time"
78

89
"github.com/oasisprotocol/curve25519-voi/primitives/x25519"
@@ -121,12 +122,7 @@ func (sc *SGXConstraints) ValidateBasic(cfg *TEEFeatures) error {
121122
// ContainsEnclave returns true iff the allowed enclave list in SGX constraints contain the given
122123
// enclave identity.
123124
func (sc *SGXConstraints) ContainsEnclave(eid sgx.EnclaveIdentity) bool {
124-
for _, e := range sc.Enclaves {
125-
if eid == e {
126-
return true
127-
}
128-
}
129-
return false
125+
return slices.Contains(sc.Enclaves, eid)
130126
}
131127

132128
const (

go/common/sgx/ias/avr.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"encoding/json"
1010
"fmt"
1111
"net/url"
12+
"slices"
1213
"time"
1314

1415
"github.com/oasisprotocol/oasis-core/go/common/sgx"
@@ -270,10 +271,8 @@ func (b *AVRBundle) Open(policy *QuotePolicy, trustRoots *x509.CertPool, ts time
270271
return nil, fmt.Errorf("quote open failure: %w", err)
271272
}
272273
// Validate EPID GID not blacklisted.
273-
for _, blocked := range policy.GIDBlacklist {
274-
if blocked == quote.Body.GID {
275-
return nil, fmt.Errorf("blacklisted quote GID")
276-
}
274+
if slices.Contains(policy.GIDBlacklist, quote.Body.GID) {
275+
return nil, fmt.Errorf("blacklisted quote GID")
277276
}
278277

279278
return avr, nil
@@ -324,13 +323,7 @@ func (a *AttestationVerificationReport) quoteStatusAllowed(policy *QuotePolicy)
324323

325324
// Search through the constraints to see if the AVR quote status is
326325
// explicitly allowed.
327-
for _, v := range policy.AllowedQuoteStatuses {
328-
if v == status {
329-
return true
330-
}
331-
}
332-
333-
return false
326+
return slices.Contains(policy.AllowedQuoteStatuses, status)
334327
}
335328

336329
func (a *AttestationVerificationReport) validate() error { // nolint: gocyclo

go/common/sgx/pcs/tcb.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,10 +271,8 @@ func (ti *TCBInfo) validate(teeType TeeType, ts time.Time, policy *QuotePolicy)
271271
}
272272

273273
// Validate FMSPC not blacklisted.
274-
for _, blocked := range policy.FMSPCBlacklist {
275-
if blocked == ti.FMSPC {
276-
return fmt.Errorf("pcs/tcb: blacklisted FMSPC")
277-
}
274+
if slices.Contains(policy.FMSPCBlacklist, ti.FMSPC) {
275+
return fmt.Errorf("pcs/tcb: blacklisted FMSPC")
278276
}
279277

280278
return nil

0 commit comments

Comments
 (0)