Skip to content

Commit b1cd0eb

Browse files
use new randutil functions without error return values
1 parent 4f5c524 commit b1cd0eb

33 files changed

+79
-324
lines changed

acme/api/order.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -321,11 +321,7 @@ func newAuthorization(ctx context.Context, az *acme.Authorization) error {
321321

322322
chTypes := challengeTypes(az)
323323

324-
var err error
325-
az.Token, err = randutil.Alphanumeric(32)
326-
if err != nil {
327-
return acme.WrapErrorISE(err, "error generating random alphanumeric ID")
328-
}
324+
az.Token = randutil.Alphanumeric(32)
329325

330326
db := acme.MustDatabaseFromContext(ctx)
331327
prov := acme.MustProvisionerFromContext(ctx)
@@ -378,7 +374,7 @@ func newAuthorization(ctx context.Context, az *acme.Authorization) error {
378374
}
379375
az.Challenges = append(az.Challenges, ch)
380376
}
381-
if err = db.CreateAuthorization(ctx, az); err != nil {
377+
if err := db.CreateAuthorization(ctx, az); err != nil {
382378
return acme.WrapErrorISE(err, "error creating authorization")
383379
}
384380
return nil

acme/db/nosql/account.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,7 @@ func (db *DB) GetAccountByKeyID(ctx context.Context, kid string) (*acme.Account,
8686

8787
// CreateAccount imlements the AcmeDB.CreateAccount interface.
8888
func (db *DB) CreateAccount(ctx context.Context, acc *acme.Account) error {
89-
var err error
90-
acc.ID, err = randID()
91-
if err != nil {
92-
return err
93-
}
89+
acc.ID = randID()
9490

9591
dba := &dbAccount{
9692
ID: acc.ID,

acme/db/nosql/authz.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,7 @@ func (db *DB) GetAuthorization(ctx context.Context, id string) (*acme.Authorizat
7878
// CreateAuthorization creates an entry in the database for the Authorization.
7979
// Implements the acme.DB.CreateAuthorization interface.
8080
func (db *DB) CreateAuthorization(ctx context.Context, az *acme.Authorization) error {
81-
var err error
82-
az.ID, err = randID()
83-
if err != nil {
84-
return err
85-
}
81+
az.ID = randID()
8682

8783
chIDs := make([]string, len(az.Challenges))
8884
for i, ch := range az.Challenges {

acme/db/nosql/certificate.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,7 @@ type dbSerial struct {
2828

2929
// CreateCertificate creates and stores an ACME certificate type.
3030
func (db *DB) CreateCertificate(ctx context.Context, cert *acme.Certificate) error {
31-
var err error
32-
cert.ID, err = randID()
33-
if err != nil {
34-
return err
35-
}
31+
cert.ID = randID()
3632

3733
leaf := pem.EncodeToMemory(&pem.Block{
3834
Type: "CERTIFICATE",
@@ -54,8 +50,7 @@ func (db *DB) CreateCertificate(ctx context.Context, cert *acme.Certificate) err
5450
Intermediates: intermediates,
5551
CreatedAt: time.Now().UTC(),
5652
}
57-
err = db.save(ctx, cert.ID, dbch, nil, "certificate", certTable)
58-
if err != nil {
53+
if err := db.save(ctx, cert.ID, dbch, nil, "certificate", certTable); err != nil {
5954
return err
6055
}
6156

acme/db/nosql/challenge.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,7 @@ func (db *DB) getDBChallenge(_ context.Context, id string) (*dbChallenge, error)
4848
// CreateChallenge creates a new ACME challenge data structure in the database.
4949
// Implements acme.DB.CreateChallenge interface.
5050
func (db *DB) CreateChallenge(ctx context.Context, ch *acme.Challenge) error {
51-
var err error
52-
ch.ID, err = randID()
53-
if err != nil {
54-
return errors.Wrap(err, "error generating random id for ACME challenge")
55-
}
51+
ch.ID = randID()
5652

5753
dbch := &dbChallenge{
5854
ID: ch.ID,

acme/db/nosql/eab.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package nosql
22

33
import (
44
"context"
5-
"crypto/rand"
65
"encoding/json"
6+
"go.step.sm/crypto/randutil"
77
"sync"
88
"time"
99

@@ -57,22 +57,12 @@ func (db *DB) CreateExternalAccountKey(ctx context.Context, provisionerID, refer
5757
externalAccountKeyMutex.Lock()
5858
defer externalAccountKeyMutex.Unlock()
5959

60-
keyID, err := randID()
61-
if err != nil {
62-
return nil, err
63-
}
64-
65-
random := make([]byte, 32)
66-
_, err = rand.Read(random)
67-
if err != nil {
68-
return nil, err
69-
}
70-
60+
keyID := randID()
7161
dbeak := &dbExternalAccountKey{
7262
ID: keyID,
7363
ProvisionerID: provisionerID,
7464
Reference: reference,
75-
HmacKey: random,
65+
HmacKey: randutil.Bytes(32),
7666
CreatedAt: clock.Now(),
7767
}
7868

acme/db/nosql/nonce.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,7 @@ type dbNonce struct {
2121
// CreateNonce creates, stores, and returns an ACME replay-nonce.
2222
// Implements the acme.DB interface.
2323
func (db *DB) CreateNonce(ctx context.Context) (acme.Nonce, error) {
24-
_id, err := randID()
25-
if err != nil {
26-
return "", err
27-
}
28-
29-
id := base64.RawURLEncoding.EncodeToString([]byte(_id))
24+
id := base64.RawURLEncoding.EncodeToString([]byte(randID()))
3025
n := &dbNonce{
3126
ID: id,
3227
CreatedAt: clock.Now(),

acme/db/nosql/nosql.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,8 @@ func (db *DB) save(_ context.Context, id string, nu, old interface{}, typ string
8686

8787
var idLen = 32
8888

89-
func randID() (val string, err error) {
90-
val, err = randutil.Alphanumeric(idLen)
91-
if err != nil {
92-
return "", errors.Wrap(err, "error generating random alphanumeric ID")
93-
}
94-
return val, nil
89+
func randID() string {
90+
return randutil.Alphanumeric(idLen)
9591
}
9692

9793
// Clock that returns time in UTC rounded to seconds.

acme/db/nosql/order.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,7 @@ func (db *DB) GetOrder(ctx context.Context, id string) (*acme.Order, error) {
7575

7676
// CreateOrder creates ACME Order resources and saves them to the DB.
7777
func (db *DB) CreateOrder(ctx context.Context, o *acme.Order) error {
78-
var err error
79-
o.ID, err = randID()
80-
if err != nil {
81-
return err
82-
}
78+
o.ID = randID()
8379

8480
now := clock.Now()
8581
dbo := &dbOrder{
@@ -98,11 +94,8 @@ func (db *DB) CreateOrder(ctx context.Context, o *acme.Order) error {
9894
return err
9995
}
10096

101-
_, err = db.updateAddOrderIDs(ctx, o.AccountID, false, o.ID)
102-
if err != nil {
103-
return err
104-
}
105-
return nil
97+
_, err := db.updateAddOrderIDs(ctx, o.AccountID, false, o.ID)
98+
return err
10699
}
107100

108101
// UpdateOrder saves an updated ACME Order to the database.

authority/admin/api/webhook.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,7 @@ func (war *webhookAdminResponder) CreateProvisionerWebhook(w http.ResponseWriter
9090
return
9191
}
9292

93-
id, err := randutil.UUIDv4()
94-
if err != nil {
95-
render.Error(w, r, admin.WrapErrorISE(err, "error generating webhook id"))
96-
return
97-
}
98-
newWebhook.Id = id
93+
newWebhook.Id = randutil.UUIDv4()
9994

10095
// verify the name is unique
10196
for _, wh := range prov.Webhooks {
@@ -106,12 +101,7 @@ func (war *webhookAdminResponder) CreateProvisionerWebhook(w http.ResponseWriter
106101
}
107102
}
108103

109-
secret, err := randutil.Bytes(64)
110-
if err != nil {
111-
render.Error(w, r, admin.WrapErrorISE(err, "error generating webhook secret"))
112-
return
113-
}
114-
newWebhook.Secret = base64.StdEncoding.EncodeToString(secret)
104+
newWebhook.Secret = base64.StdEncoding.EncodeToString(randutil.Bytes(64))
115105

116106
prov.Webhooks = append(prov.Webhooks, newWebhook)
117107

0 commit comments

Comments
 (0)