48 lines
1.8 KiB
Go
48 lines
1.8 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"runtime"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
const (
|
|
// defaultPasswordHashConcurrency is the default number of Argon2id
|
|
// derivations with the current parameters (64 MiB, 4 lanes each) that may
|
|
// run at once. The default is lowered to GOMAXPROCS, which Go derives from
|
|
// the container CPU limit, because more parallel derivations than CPUs
|
|
// only share the same CPUs while holding more memory.
|
|
defaultPasswordHashConcurrency = 2
|
|
// maxPasswordHashConcurrency keeps the hashing budget at 256 MiB, which
|
|
// fits under the 600 MiB GOMEMLIMIT of the 768 MiB backend container
|
|
// together with the rest of the process.
|
|
maxPasswordHashConcurrency = 4
|
|
)
|
|
|
|
// loadPasswordHash reads PASSWORD_HASH_CONCURRENCY. An unset value takes the
|
|
// smaller of the default and GOMAXPROCS; an empty or non-integer value is an
|
|
// error.
|
|
func (config *Config) loadPasswordHash(lookup LookupEnv) error {
|
|
config.PasswordHashConcurrency = min(defaultPasswordHashConcurrency, runtime.GOMAXPROCS(0))
|
|
value, ok := lookup("PASSWORD_HASH_CONCURRENCY")
|
|
if !ok {
|
|
return nil
|
|
}
|
|
number, err := strconv.Atoi(strings.TrimSpace(value))
|
|
if err != nil {
|
|
return fmt.Errorf("Config.loadPasswordHash - PASSWORD_HASH_CONCURRENCY must be an integer between 1 and %d", maxPasswordHashConcurrency)
|
|
}
|
|
config.PasswordHashConcurrency = number
|
|
return nil
|
|
}
|
|
|
|
// ValidatePasswordHash checks the password hashing budget: the number of
|
|
// derivations with the current parameters that may run at once.
|
|
func (config Config) ValidatePasswordHash() error {
|
|
if config.PasswordHashConcurrency < 1 || config.PasswordHashConcurrency > maxPasswordHashConcurrency {
|
|
return fmt.Errorf("Config.ValidatePasswordHash - PASSWORD_HASH_CONCURRENCY must be between 1 and %d", maxPasswordHashConcurrency)
|
|
}
|
|
return nil
|
|
}
|