style: заменить короткие имена переменных
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
@@ -68,12 +68,14 @@ func newKeygenCmdWithDeps(reg crypto.Registry) *cobra.Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !force {
|
if !force {
|
||||||
for _, s := range schemes {
|
for _, scheme := range schemes {
|
||||||
pubPath := outPrefix + "." + s.name + ".pub.pem"
|
pubPath := outPrefix + "." + scheme.name + ".pub.pem"
|
||||||
privPath := outPrefix + "." + s.name + ".priv.pem"
|
privPath := outPrefix + "." + scheme.name + ".priv.pem"
|
||||||
for _, p := range []string{pubPath, privPath} {
|
paths := make([]string, 0, 2)
|
||||||
if _, err := os.Stat(p); err == nil {
|
paths = append(paths, pubPath, privPath)
|
||||||
return fmt.Errorf("file already exists: %s (use --force to overwrite)", p)
|
for _, path := range paths {
|
||||||
|
if _, err := os.Stat(path); err == nil {
|
||||||
|
return fmt.Errorf("file already exists: %s (use --force to overwrite)", path)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -81,9 +83,9 @@ func newKeygenCmdWithDeps(reg crypto.Registry) *cobra.Command {
|
|||||||
|
|
||||||
km := keymanager.NewKeyManager(reg)
|
km := keymanager.NewKeyManager(reg)
|
||||||
|
|
||||||
for _, s := range schemes {
|
for _, scheme := range schemes {
|
||||||
pubPath := outPrefix + "." + s.name + ".pub.pem"
|
pubPath := outPrefix + "." + scheme.name + ".pub.pem"
|
||||||
privPath := outPrefix + "." + s.name + ".priv.pem"
|
privPath := outPrefix + "." + scheme.name + ".priv.pem"
|
||||||
|
|
||||||
pubFile, err := os.OpenFile(pubPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o644)
|
pubFile, err := os.OpenFile(pubPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -96,10 +98,10 @@ func newKeygenCmdWithDeps(reg crypto.Registry) *cobra.Command {
|
|||||||
return fmt.Errorf("create private key file %s: %w", privPath, err)
|
return fmt.Errorf("create private key file %s: %w", privPath, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := km.Generate(s.schemeID, pubFile, privFile, rand.Reader); err != nil {
|
if err := km.Generate(scheme.schemeID, pubFile, privFile, rand.Reader); err != nil {
|
||||||
_ = pubFile.Close()
|
_ = pubFile.Close()
|
||||||
_ = privFile.Close()
|
_ = privFile.Close()
|
||||||
return fmt.Errorf("generate %s keys: %w", s.name, err)
|
return fmt.Errorf("generate %s keys: %w", scheme.name, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := pubFile.Close(); err != nil {
|
if err := pubFile.Close(); err != nil {
|
||||||
|
|||||||
@@ -74,88 +74,88 @@ func RegisterFlags(cmd *cobra.Command) {
|
|||||||
// launch arguments (Cobra flags) → APP_* env variables → config file → defaults.
|
// launch arguments (Cobra flags) → APP_* env variables → config file → defaults.
|
||||||
func Load(cmd *cobra.Command) (*Config, error) {
|
func Load(cmd *cobra.Command) (*Config, error) {
|
||||||
// Use a fresh Viper instance so that successive calls do not leak state.
|
// Use a fresh Viper instance so that successive calls do not leak state.
|
||||||
v := viper.New()
|
viperInstance := viper.New()
|
||||||
|
|
||||||
// Defaults (lowest priority).
|
// Defaults (lowest priority).
|
||||||
v.SetDefault("pg.port", 5432)
|
viperInstance.SetDefault("pg.port", 5432)
|
||||||
v.SetDefault("pg.sslmode", "prefer")
|
viperInstance.SetDefault("pg.sslmode", "prefer")
|
||||||
v.SetDefault("pg.exclude_tables", []string{"e2e_one_time_keys_json"})
|
viperInstance.SetDefault("pg.exclude_tables", []string{"e2e_one_time_keys_json"})
|
||||||
v.SetDefault("backup.retention_days", 180)
|
viperInstance.SetDefault("backup.retention_days", 180)
|
||||||
v.SetDefault("backup.cron", "0 0 3 * * *")
|
viperInstance.SetDefault("backup.cron", "0 0 3 * * *")
|
||||||
v.SetDefault("pq_scheme", uint16(0x0006))
|
viperInstance.SetDefault("pq_scheme", uint16(0x0006))
|
||||||
v.SetDefault("classical_scheme", uint16(0x0007))
|
viperInstance.SetDefault("classical_scheme", uint16(0x0007))
|
||||||
v.SetDefault("healthz.port", 8080)
|
viperInstance.SetDefault("healthz.port", 8080)
|
||||||
v.SetDefault("log.level", "info")
|
viperInstance.SetDefault("log.level", "info")
|
||||||
v.SetDefault("shutdown_timeout", 30*time.Second)
|
viperInstance.SetDefault("shutdown_timeout", 30*time.Second)
|
||||||
|
|
||||||
// Bind parsed Cobra flags to Viper keys.
|
// Bind parsed Cobra flags to Viper keys.
|
||||||
if cmd != nil {
|
if cmd != nil {
|
||||||
_ = v.BindPFlag("pg.host", cmd.Flags().Lookup("pg-host"))
|
_ = viperInstance.BindPFlag("pg.host", cmd.Flags().Lookup("pg-host"))
|
||||||
_ = v.BindPFlag("pg.port", cmd.Flags().Lookup("pg-port"))
|
_ = viperInstance.BindPFlag("pg.port", cmd.Flags().Lookup("pg-port"))
|
||||||
_ = v.BindPFlag("pg.user", cmd.Flags().Lookup("pg-user"))
|
_ = viperInstance.BindPFlag("pg.user", cmd.Flags().Lookup("pg-user"))
|
||||||
_ = v.BindPFlag("pg.database", cmd.Flags().Lookup("pg-database"))
|
_ = viperInstance.BindPFlag("pg.database", cmd.Flags().Lookup("pg-database"))
|
||||||
_ = v.BindPFlag("pg.sslmode", cmd.Flags().Lookup("pg-sslmode"))
|
_ = viperInstance.BindPFlag("pg.sslmode", cmd.Flags().Lookup("pg-sslmode"))
|
||||||
_ = v.BindPFlag("pg.exclude_tables", cmd.Flags().Lookup("pg-exclude-tables"))
|
_ = viperInstance.BindPFlag("pg.exclude_tables", cmd.Flags().Lookup("pg-exclude-tables"))
|
||||||
_ = v.BindPFlag("backup.dir", cmd.Flags().Lookup("backup-dir"))
|
_ = viperInstance.BindPFlag("backup.dir", cmd.Flags().Lookup("backup-dir"))
|
||||||
_ = v.BindPFlag("backup.retention_days", cmd.Flags().Lookup("backup-retention-days"))
|
_ = viperInstance.BindPFlag("backup.retention_days", cmd.Flags().Lookup("backup-retention-days"))
|
||||||
_ = v.BindPFlag("backup.cron", cmd.Flags().Lookup("backup-cron"))
|
_ = viperInstance.BindPFlag("backup.cron", cmd.Flags().Lookup("backup-cron"))
|
||||||
_ = v.BindPFlag("pq_scheme", cmd.Flags().Lookup("pq-scheme"))
|
_ = viperInstance.BindPFlag("pq_scheme", cmd.Flags().Lookup("pq-scheme"))
|
||||||
_ = v.BindPFlag("classical_scheme", cmd.Flags().Lookup("classical-scheme"))
|
_ = viperInstance.BindPFlag("classical_scheme", cmd.Flags().Lookup("classical-scheme"))
|
||||||
_ = v.BindPFlag("pq_public_key_path", cmd.Flags().Lookup("pq-public-key-path"))
|
_ = viperInstance.BindPFlag("pq_public_key_path", cmd.Flags().Lookup("pq-public-key-path"))
|
||||||
_ = v.BindPFlag("classical_public_key_path", cmd.Flags().Lookup("classical-public-key-path"))
|
_ = viperInstance.BindPFlag("classical_public_key_path", cmd.Flags().Lookup("classical-public-key-path"))
|
||||||
_ = v.BindPFlag("healthz.port", cmd.Flags().Lookup("healthz-port"))
|
_ = viperInstance.BindPFlag("healthz.port", cmd.Flags().Lookup("healthz-port"))
|
||||||
_ = v.BindPFlag("log.level", cmd.Flags().Lookup("log-level"))
|
_ = viperInstance.BindPFlag("log.level", cmd.Flags().Lookup("log-level"))
|
||||||
_ = v.BindPFlag("shutdown_timeout", cmd.Flags().Lookup("shutdown-timeout"))
|
_ = viperInstance.BindPFlag("shutdown_timeout", cmd.Flags().Lookup("shutdown-timeout"))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Config file search with logging.
|
// Config file search with logging.
|
||||||
v.SetConfigName("config")
|
viperInstance.SetConfigName("config")
|
||||||
v.SetConfigType("yaml")
|
viperInstance.SetConfigType("yaml")
|
||||||
|
|
||||||
if envLoc := os.Getenv("APP_CONFIG_LOCATION"); envLoc != "" {
|
if envLoc := os.Getenv("APP_CONFIG_LOCATION"); envLoc != "" {
|
||||||
v.SetConfigFile(envLoc)
|
viperInstance.SetConfigFile(envLoc)
|
||||||
if err := v.ReadInConfig(); err == nil {
|
if err := viperInstance.ReadInConfig(); err == nil {
|
||||||
logf("Config file found: %s (from APP_CONFIG_LOCATION)", v.ConfigFileUsed())
|
logf("Config file found: %s (from APP_CONFIG_LOCATION)", viperInstance.ConfigFileUsed())
|
||||||
} else {
|
} else {
|
||||||
return nil, fmt.Errorf("config file specified in APP_CONFIG_LOCATION not found: %s", envLoc)
|
return nil, fmt.Errorf("config file specified in APP_CONFIG_LOCATION not found: %s", envLoc)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
v.AddConfigPath(".")
|
viperInstance.AddConfigPath(".")
|
||||||
|
|
||||||
homeDir, _ := os.UserHomeDir()
|
homeDir, _ := os.UserHomeDir()
|
||||||
appName := "synapse-backupper"
|
appName := "synapse-backupper"
|
||||||
userConfigPath := filepath.Join(homeDir, ".config", appName)
|
userConfigPath := filepath.Join(homeDir, ".config", appName)
|
||||||
v.AddConfigPath(userConfigPath)
|
viperInstance.AddConfigPath(userConfigPath)
|
||||||
|
|
||||||
exePath, _ := os.Executable()
|
exePath, _ := os.Executable()
|
||||||
exeDir := filepath.Dir(exePath)
|
exeDir := filepath.Dir(exePath)
|
||||||
v.AddConfigPath(exeDir)
|
viperInstance.AddConfigPath(exeDir)
|
||||||
|
|
||||||
etcPath := filepath.Join("/etc", appName)
|
etcPath := filepath.Join("/etc", appName)
|
||||||
v.AddConfigPath(etcPath)
|
viperInstance.AddConfigPath(etcPath)
|
||||||
|
|
||||||
if err := v.ReadInConfig(); err != nil {
|
if err := viperInstance.ReadInConfig(); err != nil {
|
||||||
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
|
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
|
||||||
logf("Configuration file not found. Using defaults and env/args.")
|
logf("Configuration file not found. Using defaults and env/args.")
|
||||||
} else {
|
} else {
|
||||||
return nil, fmt.Errorf("error reading config: %w", err)
|
return nil, fmt.Errorf("error reading config: %w", err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
logf("Config file found: %s", v.ConfigFileUsed())
|
logf("Config file found: %s", viperInstance.ConfigFileUsed())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Environment variables.
|
// Environment variables.
|
||||||
v.SetEnvPrefix("APP")
|
viperInstance.SetEnvPrefix("APP")
|
||||||
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_"))
|
viperInstance.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_"))
|
||||||
v.AutomaticEnv()
|
viperInstance.AutomaticEnv()
|
||||||
// pg.password is intentionally not exposed as a CLI flag (CWE-214), but
|
// pg.password is intentionally not exposed as a CLI flag (CWE-214), but
|
||||||
// must still be loadable from APP_PG_PASSWORD. Viper needs an explicit
|
// must still be loadable from APP_PG_PASSWORD. Viper needs an explicit
|
||||||
// BindEnv for a nested key that has no bound flag.
|
// BindEnv for a nested key that has no bound flag.
|
||||||
_ = v.BindEnv("pg.password")
|
_ = viperInstance.BindEnv("pg.password")
|
||||||
|
|
||||||
var cfg Config
|
var cfg Config
|
||||||
if err := v.Unmarshal(&cfg); err != nil {
|
if err := viperInstance.Unmarshal(&cfg); err != nil {
|
||||||
return nil, fmt.Errorf("config unmarshal failed: %w", err)
|
return nil, fmt.Errorf("config unmarshal failed: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -233,40 +233,40 @@ func writeHeader(
|
|||||||
wrapNonce, wrappedCek, firstPayloadNonce []byte,
|
wrapNonce, wrappedCek, firstPayloadNonce []byte,
|
||||||
) error {
|
) error {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
var b4 [4]byte
|
var scratchBytes [4]byte
|
||||||
|
|
||||||
binary.BigEndian.PutUint32(b4[:], magic)
|
binary.BigEndian.PutUint32(scratchBytes[:], magic)
|
||||||
buf.Write(b4[:]) // [0:4] magic
|
buf.Write(scratchBytes[:]) // [0:4] magic
|
||||||
|
|
||||||
binary.BigEndian.PutUint16(b4[:2], version)
|
binary.BigEndian.PutUint16(scratchBytes[:2], version)
|
||||||
buf.Write(b4[:2]) // [4:6] version
|
buf.Write(scratchBytes[:2]) // [4:6] version
|
||||||
|
|
||||||
binary.BigEndian.PutUint32(b4[:], flags)
|
binary.BigEndian.PutUint32(scratchBytes[:], flags)
|
||||||
buf.Write(b4[:]) // [6:10] flags
|
buf.Write(scratchBytes[:]) // [6:10] flags
|
||||||
|
|
||||||
// [10] nRecipients — composite v2 always carries exactly two slots.
|
// [10] nRecipients — composite v2 always carries exactly two slots.
|
||||||
buf.WriteByte(byte(maxRecipients))
|
buf.WriteByte(byte(maxRecipients))
|
||||||
|
|
||||||
// Slot 0 (PQ).
|
// Slot 0 (PQ).
|
||||||
binary.BigEndian.PutUint16(b4[:2], pqPub.SchemeID())
|
binary.BigEndian.PutUint16(scratchBytes[:2], pqPub.SchemeID())
|
||||||
buf.Write(b4[:2])
|
buf.Write(scratchBytes[:2])
|
||||||
if len(pqPub.KeyID()) != 8 {
|
if len(pqPub.KeyID()) != 8 {
|
||||||
return ErrMalformedHeader
|
return ErrMalformedHeader
|
||||||
}
|
}
|
||||||
buf.Write(pqPub.KeyID())
|
buf.Write(pqPub.KeyID())
|
||||||
binary.BigEndian.PutUint32(b4[:], uint32(len(pqCt)))
|
binary.BigEndian.PutUint32(scratchBytes[:], uint32(len(pqCt)))
|
||||||
buf.Write(b4[:])
|
buf.Write(scratchBytes[:])
|
||||||
buf.Write(pqCt)
|
buf.Write(pqCt)
|
||||||
|
|
||||||
// Slot 1 (classical).
|
// Slot 1 (classical).
|
||||||
binary.BigEndian.PutUint16(b4[:2], classicalPub.SchemeID())
|
binary.BigEndian.PutUint16(scratchBytes[:2], classicalPub.SchemeID())
|
||||||
buf.Write(b4[:2])
|
buf.Write(scratchBytes[:2])
|
||||||
if len(classicalPub.KeyID()) != 8 {
|
if len(classicalPub.KeyID()) != 8 {
|
||||||
return ErrMalformedHeader
|
return ErrMalformedHeader
|
||||||
}
|
}
|
||||||
buf.Write(classicalPub.KeyID())
|
buf.Write(classicalPub.KeyID())
|
||||||
binary.BigEndian.PutUint32(b4[:], uint32(len(classicalCt)))
|
binary.BigEndian.PutUint32(scratchBytes[:], uint32(len(classicalCt)))
|
||||||
buf.Write(b4[:])
|
buf.Write(scratchBytes[:])
|
||||||
buf.Write(classicalCt)
|
buf.Write(classicalCt)
|
||||||
|
|
||||||
// wrapNonce + wrappedCEK + firstPayloadNonce.
|
// wrapNonce + wrappedCEK + firstPayloadNonce.
|
||||||
|
|||||||
@@ -136,8 +136,8 @@ func (k *kemAdapter) Decapsulate(
|
|||||||
|
|
||||||
// Reject the all-zero public key (identity point), which yields an all-zero shared secret.
|
// Reject the all-zero public key (identity point), which yields an all-zero shared secret.
|
||||||
allZero := true
|
allZero := true
|
||||||
for _, b := range ciphertext {
|
for _, byteValue := range ciphertext {
|
||||||
if b != 0 {
|
if byteValue != 0 {
|
||||||
allZero = false
|
allZero = false
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user