WIP: WFE: block accounts based on configurable yaml
diff --git a/cmd/boulder-wfe2/main.go b/cmd/boulder-wfe2/main.go
index 86948e8..81e96c9 100644
--- a/cmd/boulder-wfe2/main.go
+++ b/cmd/boulder-wfe2/main.go
@@ -21,12 +21,14 @@
 	"github.com/letsencrypt/boulder/grpc/noncebalancer"
 	noncebalancerv1 "github.com/letsencrypt/boulder/grpc/noncebalancerv1"
 	"github.com/letsencrypt/boulder/issuance"
+	blog "github.com/letsencrypt/boulder/log"
 	"github.com/letsencrypt/boulder/nonce"
 	rapb "github.com/letsencrypt/boulder/ra/proto"
 	"github.com/letsencrypt/boulder/ratelimits"
 	bredis "github.com/letsencrypt/boulder/redis"
 	sapb "github.com/letsencrypt/boulder/sa/proto"
 	emailpb "github.com/letsencrypt/boulder/salesforce/email/proto"
+	"github.com/letsencrypt/boulder/strictyaml"
 	"github.com/letsencrypt/boulder/unpause"
 	"github.com/letsencrypt/boulder/web"
 	"github.com/letsencrypt/boulder/wfe2"
@@ -210,6 +212,11 @@
 		// repeats. We don't want to issue certs for names that look like they
 		// result from this process.
 		BlockedOnDemandLabels []string `validate:"omitempty"`
+
+		// BlockedAccountsFile is the path to a YAML file listing regIDs which are
+		// blocked from requesting issuance of new certificates. If empty, no
+		// accounts are blocked.
+		BlockedAccountsFile string `validate:"omitempty"`
 	}
 
 	Syslog        cmd.SyslogConfig
@@ -224,6 +231,36 @@
 	TTL  config.Duration
 }
 
+type blockedAccountsPolicy struct {
+	BlockedAccountIDs []int64 `yaml:"BlockedAccountIDs"`
+}
+
+// loadBlockedAccountsFile parses the YAML file at the given path and returns
+// the set of blocked account IDs it contains.
+// TODO: Update the schema etc to handle multiple lists each with their own
+// message string, so we don't have to hardcode the message in the WFE.
+func loadBlockedAccountsFile(f string, logger blog.Logger) (map[int64]bool, error) {
+	configBytes, err := os.ReadFile(f)
+	if err != nil {
+		return nil, err
+	}
+
+	var policy blockedAccountsPolicy
+	err = strictyaml.Unmarshal(configBytes, &policy)
+	if err != nil {
+		return nil, err
+	}
+
+	blocked := make(map[int64]bool, 0)
+	for _, id := range policy.BlockedAccountIDs {
+		if id <= 0 {
+			return nil, fmt.Errorf("malformed BlockedAccountIDs entry, not a positive integer: %d", id)
+		}
+		blocked[id] = true
+	}
+	return blocked, nil
+}
+
 // loadChain takes a list of filenames containing pem-formatted certificates,
 // and returns a chain representing all of those certificates in order. It
 // ensures that the resulting chain is valid. The final file is expected to be
@@ -380,6 +417,12 @@
 		overridesRefresherShutdown = txnBuilder.NewRefresher(30 * time.Minute)
 	}
 
+	var blockedAccounts map[int64]bool
+	if c.WFE.BlockedAccountsFile != "" {
+		blockedAccounts, err = loadBlockedAccountsFile(c.WFE.BlockedAccountsFile, logger)
+		cmd.FailOnError(err, "Couldn't load blocked accounts file")
+	}
+
 	var accountGetter wfe2.AccountGetter
 	if c.WFE.AccountCache != nil {
 		accountGetter = wfe2.NewAccountCache(sac,
@@ -414,6 +457,7 @@
 		c.WFE.Unpause.JWTLifetime.Duration,
 		c.WFE.Unpause.URL,
 		c.WFE.BlockedOnDemandLabels,
+		blockedAccounts,
 		c.WFE.DirectoryCAAIdentity,
 	)
 	cmd.FailOnError(err, "Unable to create WFE")
diff --git a/wfe2/wfe.go b/wfe2/wfe.go
index 03b7740..31dc5eb 100644
--- a/wfe2/wfe.go
+++ b/wfe2/wfe.go
@@ -179,6 +179,11 @@
 	// given request counts as a renewal or not.
 	blockedOnDemandLabels []string `validate:"omitempty"`
 
+	// blockedAccounts is the set of account (registration) IDs which have been
+	// administratively blocked from requesting issuance of new certificates,
+	// loaded once at process startup from a YAML file.
+	blockedAccounts map[int64]bool
+
 	// certProfiles is a map of acceptable certificate profile names to
 	// descriptions (perhaps including URLs) of those profiles. NewOrder
 	// Requests with a profile name not present in this map will be rejected.
@@ -210,6 +215,7 @@
 	unpauseJWTLifetime time.Duration,
 	unpauseURL string,
 	blockedOnDemandLabels []string,
+	blockedAccounts map[int64]bool,
 	caaIdentity string,
 ) (WebFrontEndImpl, error) {
 	if len(issuerCertificates) == 0 {
@@ -262,6 +268,7 @@
 		unpauseJWTLifetime:    unpauseJWTLifetime,
 		unpauseURL:            unpauseURL,
 		blockedOnDemandLabels: blockedLabels,
+		blockedAccounts:       blockedAccounts,
 		DirectoryCAAIdentity:  normalizedCAAIdentity,
 	}
 
@@ -2325,6 +2332,12 @@
 	return pausedValues, nil
 }
 
+// checkAccountPaused returns true if the given account ID is in the WFE's
+// administratively-configured set of blocked accounts.
+func (wfe *WebFrontEndImpl) checkAccountPaused(regID int64) bool {
+	return wfe.blockedAccounts[regID]
+}
+
 // NewOrder is used by clients to create a new order object and a set of
 // authorizations to fulfill for issuance.
 func (wfe *WebFrontEndImpl) NewOrder(
@@ -2391,6 +2404,11 @@
 	}
 
 	if features.Get().CheckIdentifiersPaused {
+		if wfe.checkAccountPaused(acct.ID) {
+			wfe.sendError(response, logEvent, probs.Unauthorized("Your account is prevented from requesting certificates due to abusive request patterns."), nil)
+			return
+		}
+
 		pausedValues, err := wfe.checkIdentifiersPaused(ctx, idents, acct.ID)
 		if err != nil {
 			wfe.sendError(response, logEvent, probs.ServerInternal("Failure while checking pause status of identifiers"), err)
diff --git a/wfe2/wfe_test.go b/wfe2/wfe_test.go
index ab0e4b9..89ee13f 100644
--- a/wfe2/wfe_test.go
+++ b/wfe2/wfe_test.go
@@ -443,6 +443,7 @@
 		unpauseLifetime,
 		unpauseURL,
 		[]string{"asdf"},
+		nil,
 		"letsencrypt.org",
 	)
 	test.AssertNotError(t, err, "Unable to create WFE")