Skip to content

Commit

Permalink
add locking to basstls
Browse files Browse the repository at this point in the history
  • Loading branch information
vito committed Aug 27, 2022
1 parent e7eba72 commit 5236ea7
Showing 1 changed file with 36 additions and 3 deletions.
39 changes: 36 additions & 3 deletions pkg/basstls/basstls.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package basstls

import (
"fmt"
"os"
"path/filepath"
"time"

"github.com/adrg/xdg"
"github.com/gofrs/flock"
"github.com/square/certstrap/depot"
"github.com/square/certstrap/pkix"
)
Expand All @@ -14,12 +16,16 @@ const (
// Common name for the certificate authority.
CAName = "bass"

CACountry = "CA"
// Arbitrary values for the CA cert.
CACountry = "CA" // Canada (coincidence)
CAProvince = "Ontario"
CALocality = "Toronto"

// RSA key bits.
keySize = 2048

// File stored within the cert depot to synchronize cert generation.
lockFile = "certs.lock"
)

var (
Expand All @@ -33,8 +39,29 @@ func CACert(dir string) string {
return filepath.Join(dir, CAName+".crt")
}

func lockDepot(dir string) (*flock.Flock, error) {
lock := flock.New(filepath.Join(dir, lockFile))

if err := os.MkdirAll(dir, 0755); err != nil {
return nil, err
}

if err := lock.Lock(); err != nil {
return nil, err
}

return lock, nil
}

// Init initializes dir with a CA.
func Init(dir string) error {
lock, err := lockDepot(dir)
if err != nil {
return err
}

defer lock.Unlock()

d, err := depot.NewFileDepot(dir)
if err != nil {
return fmt.Errorf("init depot: %w", err)
Expand Down Expand Up @@ -79,16 +106,22 @@ func Init(dir string) error {
return fmt.Errorf("put ca: %w", err)
}

return nil
return lock.Unlock()
}

func Generate(dir, host string) (*pkix.Certificate, *pkix.Key, error) {
lock, err := lockDepot(dir)
if err != nil {
return nil, nil, err
}

defer lock.Unlock()

d, err := depot.NewFileDepot(dir)
if err != nil {
return nil, nil, fmt.Errorf("init depot: %w", err)
}

// TODO: file locking?
crt, err := depot.GetCertificate(d, host)
if err == nil {
// cert and key already exist
Expand Down

0 comments on commit 5236ea7

Please sign in to comment.