Skip to content

Commit

Permalink
fix(cli): create client only when needed (#2372)
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez authored Dec 3, 2024
1 parent aacfa2b commit eb04104
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 15 deletions.
4 changes: 4 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,10 @@ issues:
text: 'cyclomatic complexity \d+ of func `(renewForDomains|renewForCSR)` is high'
linters:
- gocyclo
- path: cmd/cmd_renew.go
text: "Function 'renewForDomains' has too many statements"
linters:
- funlen
- path: providers/dns/cpanel/cpanel.go
text: 'cyclomatic complexity 13 of func `\(\*DNSProvider\)\.CleanUp` is high'
linters:
Expand Down
27 changes: 21 additions & 6 deletions cmd/cmd_renew.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@ func createRenew() *cli.Command {
}

func renew(ctx *cli.Context) error {
account, client := setup(ctx, NewAccountsStorage(ctx))
setupChallenges(ctx, client)
account, keyType := setupAccount(ctx, NewAccountsStorage(ctx))

if account.Registration == nil {
log.Fatalf("Account %s is not registered. Use 'run' to register a new account.\n", account.Email)
Expand All @@ -138,14 +137,14 @@ func renew(ctx *cli.Context) error {

// CSR
if ctx.IsSet(flgCSR) {
return renewForCSR(ctx, client, certsStorage, bundle, meta)
return renewForCSR(ctx, account, keyType, certsStorage, bundle, meta)
}

// Domains
return renewForDomains(ctx, client, certsStorage, bundle, meta)
return renewForDomains(ctx, account, keyType, certsStorage, bundle, meta)
}

func renewForDomains(ctx *cli.Context, client *lego.Client, certsStorage *CertificatesStorage, bundle bool, meta map[string]string) error {
func renewForDomains(ctx *cli.Context, account *Account, keyType certcrypto.KeyType, certsStorage *CertificatesStorage, bundle bool, meta map[string]string) error {
domains := ctx.StringSlice(flgDomains)
domain := domains[0]

Expand All @@ -162,7 +161,11 @@ func renewForDomains(ctx *cli.Context, client *lego.Client, certsStorage *Certif
var ariRenewalTime *time.Time
var replacesCertID string

var client *lego.Client

if !ctx.Bool(flgARIDisable) {
client = setupClient(ctx, account, keyType)

ariRenewalTime = getARIRenewalTime(ctx, cert, domain, client)
if ariRenewalTime != nil {
now := time.Now().UTC()
Expand All @@ -189,6 +192,10 @@ func renewForDomains(ctx *cli.Context, client *lego.Client, certsStorage *Certif
return nil
}

if client == nil {
client = setupClient(ctx, account, keyType)
}

// This is just meant to be informal for the user.
timeLeft := cert.NotAfter.Sub(time.Now().UTC())
log.Infof("[%s] acme: Trying renewal with %d hours remaining", domain, int(timeLeft.Hours()))
Expand Down Expand Up @@ -250,7 +257,7 @@ func renewForDomains(ctx *cli.Context, client *lego.Client, certsStorage *Certif
return launchHook(ctx.String(flgRenewHook), meta)
}

func renewForCSR(ctx *cli.Context, client *lego.Client, certsStorage *CertificatesStorage, bundle bool, meta map[string]string) error {
func renewForCSR(ctx *cli.Context, account *Account, keyType certcrypto.KeyType, certsStorage *CertificatesStorage, bundle bool, meta map[string]string) error {
csr, err := readCSRFile(ctx.String(flgCSR))
if err != nil {
log.Fatal(err)
Expand All @@ -274,7 +281,11 @@ func renewForCSR(ctx *cli.Context, client *lego.Client, certsStorage *Certificat
var ariRenewalTime *time.Time
var replacesCertID string

var client *lego.Client

if !ctx.Bool(flgARIDisable) {
client = setupClient(ctx, account, keyType)

ariRenewalTime = getARIRenewalTime(ctx, cert, domain, client)
if ariRenewalTime != nil {
now := time.Now().UTC()
Expand All @@ -296,6 +307,10 @@ func renewForCSR(ctx *cli.Context, client *lego.Client, certsStorage *Certificat
return nil
}

if client == nil {
client = setupClient(ctx, account, keyType)
}

// This is just meant to be informal for the user.
timeLeft := cert.NotAfter.Sub(time.Now().UTC())
log.Infof("[%s] acme: Trying renewal with %d hours remaining", domain, int(timeLeft.Hours()))
Expand Down
8 changes: 5 additions & 3 deletions cmd/cmd_revoke.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ func createRevoke() *cli.Command {
}

func revoke(ctx *cli.Context) error {
acc, client := setup(ctx, NewAccountsStorage(ctx))
account, keyType := setupAccount(ctx, NewAccountsStorage(ctx))

if acc.Registration == nil {
log.Fatalf("Account %s is not registered. Use 'run' to register a new account.\n", acc.Email)
if account.Registration == nil {
log.Fatalf("Account %s is not registered. Use 'run' to register a new account.\n", account.Email)
}

client := newClient(ctx, account, keyType)

certsStorage := NewCertificatesStorage(ctx)
certsStorage.CreateRootFolder()

Expand Down
5 changes: 3 additions & 2 deletions cmd/cmd_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,9 @@ backups of this folder is ideal.
func run(ctx *cli.Context) error {
accountsStorage := NewAccountsStorage(ctx)

account, client := setup(ctx, accountsStorage)
setupChallenges(ctx, client)
account, keyType := setupAccount(ctx, accountsStorage)

client := setupClient(ctx, account, keyType)

if account.Registration == nil {
reg, err := register(ctx, client)
Expand Down
15 changes: 11 additions & 4 deletions cmd/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,16 @@ import (

const filePerm os.FileMode = 0o600

func setup(ctx *cli.Context, accountsStorage *AccountsStorage) (*Account, *lego.Client) {
// setupClient creates a new client with challenge settings.
func setupClient(ctx *cli.Context, account *Account, keyType certcrypto.KeyType) *lego.Client {
client := newClient(ctx, account, keyType)

setupChallenges(ctx, client)

return client
}

func setupAccount(ctx *cli.Context, accountsStorage *AccountsStorage) (*Account, certcrypto.KeyType) {
keyType := getKeyType(ctx)
privateKey := accountsStorage.GetPrivateKey(keyType)

Expand All @@ -29,9 +38,7 @@ func setup(ctx *cli.Context, accountsStorage *AccountsStorage) (*Account, *lego.
account = &Account{Email: accountsStorage.GetUserID(), key: privateKey}
}

client := newClient(ctx, account, keyType)

return account, client
return account, keyType
}

func newClient(ctx *cli.Context, acc registration.User, keyType certcrypto.KeyType) *lego.Client {
Expand Down

0 comments on commit eb04104

Please sign in to comment.