mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-11 18:33:28 +00:00
refactor: rename package errors to zerrors (#7039)
* chore: rename package errors to zerrors * rename package errors to gerrors * fix error related linting issues * fix zitadel error assertion * fix gosimple linting issues * fix deprecated linting issues * resolve gci linting issues * fix import structure --------- Co-authored-by: Elio Bischof <elio@zitadel.com>
This commit is contained in:
@@ -7,7 +7,7 @@ import (
|
||||
"encoding/base64"
|
||||
"io"
|
||||
|
||||
"github.com/zitadel/zitadel/internal/errors"
|
||||
"github.com/zitadel/zitadel/internal/zerrors"
|
||||
)
|
||||
|
||||
var _ EncryptionAlgorithm = (*AESCrypto)(nil)
|
||||
@@ -73,7 +73,7 @@ func (a *AESCrypto) encryptionKey() string {
|
||||
func (a *AESCrypto) decryptionKey(keyID string) (string, error) {
|
||||
key, ok := a.keys[keyID]
|
||||
if !ok {
|
||||
return "", errors.ThrowNotFound(nil, "CRYPT-nkj1s", "unknown key id")
|
||||
return "", zerrors.ThrowNotFound(nil, "CRYPT-nkj1s", "unknown key id")
|
||||
}
|
||||
return key, nil
|
||||
}
|
||||
@@ -94,7 +94,7 @@ func EncryptAES(plainText []byte, key string) ([]byte, error) {
|
||||
|
||||
maxSize := 64 * 1024 * 1024
|
||||
if len(plainText) > maxSize {
|
||||
return nil, errors.ThrowPreconditionFailedf(nil, "CRYPT-AGg4t3", "data too large, max bytes: %v", maxSize)
|
||||
return nil, zerrors.ThrowPreconditionFailedf(nil, "CRYPT-AGg4t3", "data too large, max bytes: %v", maxSize)
|
||||
}
|
||||
cipherText := make([]byte, aes.BlockSize+len(plainText))
|
||||
iv := cipherText[:aes.BlockSize]
|
||||
@@ -130,7 +130,7 @@ func DecryptAES(text []byte, key string) ([]byte, error) {
|
||||
}
|
||||
|
||||
if len(cipherText) < aes.BlockSize {
|
||||
err = errors.ThrowPreconditionFailed(nil, "CRYPT-23kH1", "cipher text block too short")
|
||||
err = zerrors.ThrowPreconditionFailed(nil, "CRYPT-23kH1", "cipher text block too short")
|
||||
return nil, err
|
||||
}
|
||||
iv := cipherText[:aes.BlockSize]
|
||||
|
@@ -4,7 +4,7 @@ import (
|
||||
"crypto/rand"
|
||||
"time"
|
||||
|
||||
"github.com/zitadel/zitadel/internal/errors"
|
||||
"github.com/zitadel/zitadel/internal/zerrors"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -126,7 +126,7 @@ func VerifyCode(creationDate time.Time, expiry time.Duration, cryptoCode *Crypto
|
||||
|
||||
func VerifyCodeWithAlgorithm(creationDate time.Time, expiry time.Duration, cryptoCode *CryptoValue, verificationCode string, algorithm Crypto) error {
|
||||
if IsCodeExpired(creationDate, expiry) {
|
||||
return errors.ThrowPreconditionFailed(nil, "CODE-QvUQ4P", "Errors.User.Code.Expired")
|
||||
return zerrors.ThrowPreconditionFailed(nil, "CODE-QvUQ4P", "Errors.User.Code.Expired")
|
||||
}
|
||||
switch alg := algorithm.(type) {
|
||||
case EncryptionAlgorithm:
|
||||
@@ -134,7 +134,7 @@ func VerifyCodeWithAlgorithm(creationDate time.Time, expiry time.Duration, crypt
|
||||
case HashAlgorithm:
|
||||
return verifyHashedCode(cryptoCode, verificationCode, alg)
|
||||
}
|
||||
return errors.ThrowInvalidArgument(nil, "CODE-fW2gNa", "Errors.User.Code.GeneratorAlgNotSupported")
|
||||
return zerrors.ThrowInvalidArgument(nil, "CODE-fW2gNa", "Errors.User.Code.GeneratorAlgNotSupported")
|
||||
}
|
||||
|
||||
func GenerateRandomString(length uint, chars []rune) (string, error) {
|
||||
@@ -161,7 +161,7 @@ func GenerateRandomString(length uint, chars []rune) (string, error) {
|
||||
|
||||
func verifyEncryptedCode(cryptoCode *CryptoValue, verificationCode string, alg EncryptionAlgorithm) error {
|
||||
if cryptoCode == nil {
|
||||
return errors.ThrowInvalidArgument(nil, "CRYPT-aqrFV", "Errors.User.Code.CryptoCodeNil")
|
||||
return zerrors.ThrowInvalidArgument(nil, "CRYPT-aqrFV", "Errors.User.Code.CryptoCodeNil")
|
||||
}
|
||||
code, err := DecryptString(cryptoCode, alg)
|
||||
if err != nil {
|
||||
@@ -169,14 +169,14 @@ func verifyEncryptedCode(cryptoCode *CryptoValue, verificationCode string, alg E
|
||||
}
|
||||
|
||||
if code != verificationCode {
|
||||
return errors.ThrowInvalidArgument(nil, "CODE-woT0xc", "Errors.User.Code.Invalid")
|
||||
return zerrors.ThrowInvalidArgument(nil, "CODE-woT0xc", "Errors.User.Code.Invalid")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func verifyHashedCode(cryptoCode *CryptoValue, verificationCode string, alg HashAlgorithm) error {
|
||||
if cryptoCode == nil {
|
||||
return errors.ThrowInvalidArgument(nil, "CRYPT-2q3r", "cryptoCode must not be nil")
|
||||
return zerrors.ThrowInvalidArgument(nil, "CRYPT-2q3r", "cryptoCode must not be nil")
|
||||
}
|
||||
return CompareHash(cryptoCode, []byte(verificationCode), alg)
|
||||
}
|
||||
|
@@ -5,7 +5,7 @@ import (
|
||||
|
||||
"go.uber.org/mock/gomock"
|
||||
|
||||
"github.com/zitadel/zitadel/internal/errors"
|
||||
"github.com/zitadel/zitadel/internal/zerrors"
|
||||
)
|
||||
|
||||
func CreateMockEncryptionAlg(ctrl *gomock.Controller) EncryptionAlgorithm {
|
||||
@@ -26,7 +26,7 @@ func CreateMockEncryptionAlgWithCode(ctrl *gomock.Controller, code string) Encry
|
||||
ctrl,
|
||||
func(c []byte) ([]byte, error) {
|
||||
if len(c) != len(code) {
|
||||
return nil, errors.ThrowInvalidArgumentf(nil, "id", "invalid code length - expected %d, got %d", len(code), len(c))
|
||||
return nil, zerrors.ThrowInvalidArgumentf(nil, "id", "invalid code length - expected %d, got %d", len(code), len(c))
|
||||
}
|
||||
return []byte(code), nil
|
||||
},
|
||||
@@ -44,7 +44,7 @@ func createMockEncryptionAlgorithm(ctrl *gomock.Controller, encryptFunction func
|
||||
mCrypto.EXPECT().DecryptString(gomock.Any(), gomock.Any()).AnyTimes().DoAndReturn(
|
||||
func(code []byte, keyID string) (string, error) {
|
||||
if keyID != "id" {
|
||||
return "", errors.ThrowInternal(nil, "id", "invalid key id")
|
||||
return "", zerrors.ThrowInternal(nil, "id", "invalid key id")
|
||||
}
|
||||
return string(code), nil
|
||||
},
|
||||
@@ -52,7 +52,7 @@ func createMockEncryptionAlgorithm(ctrl *gomock.Controller, encryptFunction func
|
||||
mCrypto.EXPECT().Decrypt(gomock.Any(), gomock.Any()).AnyTimes().DoAndReturn(
|
||||
func(code []byte, keyID string) ([]byte, error) {
|
||||
if keyID != "id" {
|
||||
return nil, errors.ThrowInternal(nil, "id", "invalid key id")
|
||||
return nil, zerrors.ThrowInternal(nil, "id", "invalid key id")
|
||||
}
|
||||
return code, nil
|
||||
},
|
||||
@@ -71,7 +71,7 @@ func CreateMockHashAlg(ctrl *gomock.Controller) HashAlgorithm {
|
||||
mCrypto.EXPECT().CompareHash(gomock.Any(), gomock.Any()).AnyTimes().DoAndReturn(
|
||||
func(hashed, comparer []byte) error {
|
||||
if string(hashed) != string(comparer) {
|
||||
return errors.ThrowInternal(nil, "id", "invalid")
|
||||
return zerrors.ThrowInternal(nil, "id", "invalid")
|
||||
}
|
||||
return nil
|
||||
},
|
||||
|
@@ -5,7 +5,7 @@ import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/zitadel/zitadel/internal/errors"
|
||||
"github.com/zitadel/zitadel/internal/zerrors"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -66,13 +66,13 @@ func Crypt(value []byte, c Crypto) (*CryptoValue, error) {
|
||||
case HashAlgorithm:
|
||||
return Hash(value, alg)
|
||||
}
|
||||
return nil, errors.ThrowInternal(nil, "CRYPT-r4IaHZ", "algorithm not supported")
|
||||
return nil, zerrors.ThrowInternal(nil, "CRYPT-r4IaHZ", "algorithm not supported")
|
||||
}
|
||||
|
||||
func Encrypt(value []byte, alg EncryptionAlgorithm) (*CryptoValue, error) {
|
||||
encrypted, err := alg.Encrypt(value)
|
||||
if err != nil {
|
||||
return nil, errors.ThrowInternal(err, "CRYPT-qCD0JB", "error encrypting value")
|
||||
return nil, zerrors.ThrowInternal(err, "CRYPT-qCD0JB", "error encrypting value")
|
||||
}
|
||||
return &CryptoValue{
|
||||
CryptoType: TypeEncryption,
|
||||
@@ -98,20 +98,20 @@ func DecryptString(value *CryptoValue, alg EncryptionAlgorithm) (string, error)
|
||||
|
||||
func checkEncryptionAlgorithm(value *CryptoValue, alg EncryptionAlgorithm) error {
|
||||
if value.Algorithm != alg.Algorithm() {
|
||||
return errors.ThrowInvalidArgument(nil, "CRYPT-Nx7XlT", "value was encrypted with a different key")
|
||||
return zerrors.ThrowInvalidArgument(nil, "CRYPT-Nx7XlT", "value was encrypted with a different key")
|
||||
}
|
||||
for _, id := range alg.DecryptionKeyIDs() {
|
||||
if id == value.KeyID {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return errors.ThrowInvalidArgument(nil, "CRYPT-Kq12vn", "value was encrypted with a different key")
|
||||
return zerrors.ThrowInvalidArgument(nil, "CRYPT-Kq12vn", "value was encrypted with a different key")
|
||||
}
|
||||
|
||||
func Hash(value []byte, alg HashAlgorithm) (*CryptoValue, error) {
|
||||
hashed, err := alg.Hash(value)
|
||||
if err != nil {
|
||||
return nil, errors.ThrowInternal(err, "CRYPT-rBVaJU", "error hashing value")
|
||||
return nil, zerrors.ThrowInternal(err, "CRYPT-rBVaJU", "error hashing value")
|
||||
}
|
||||
return &CryptoValue{
|
||||
CryptoType: TypeHash,
|
||||
@@ -122,7 +122,7 @@ func Hash(value []byte, alg HashAlgorithm) (*CryptoValue, error) {
|
||||
|
||||
func CompareHash(value *CryptoValue, comparer []byte, alg HashAlgorithm) error {
|
||||
if value.Algorithm != alg.Algorithm() {
|
||||
return errors.ThrowInvalidArgument(nil, "CRYPT-HF32f", "value was hashed with a different algorithm")
|
||||
return zerrors.ThrowInvalidArgument(nil, "CRYPT-HF32f", "value was hashed with a different algorithm")
|
||||
}
|
||||
return alg.CompareHash(value.Crypted, comparer)
|
||||
}
|
||||
@@ -137,18 +137,18 @@ func FillHash(value []byte, alg HashAlgorithm) *CryptoValue {
|
||||
|
||||
func CheckToken(alg EncryptionAlgorithm, token string, content string) error {
|
||||
if token == "" {
|
||||
return errors.ThrowPermissionDenied(nil, "CRYPTO-Sfefs", "Errors.Intent.InvalidToken")
|
||||
return zerrors.ThrowPermissionDenied(nil, "CRYPTO-Sfefs", "Errors.Intent.InvalidToken")
|
||||
}
|
||||
data, err := base64.RawURLEncoding.DecodeString(token)
|
||||
if err != nil {
|
||||
return errors.ThrowPermissionDenied(err, "CRYPTO-Swg31", "Errors.Intent.InvalidToken")
|
||||
return zerrors.ThrowPermissionDenied(err, "CRYPTO-Swg31", "Errors.Intent.InvalidToken")
|
||||
}
|
||||
decryptedToken, err := alg.DecryptString(data, alg.EncryptionKeyID())
|
||||
if err != nil {
|
||||
return errors.ThrowPermissionDenied(err, "CRYPTO-Sf4gt", "Errors.Intent.InvalidToken")
|
||||
return zerrors.ThrowPermissionDenied(err, "CRYPTO-Sf4gt", "Errors.Intent.InvalidToken")
|
||||
}
|
||||
if decryptedToken != content {
|
||||
return errors.ThrowPermissionDenied(nil, "CRYPTO-CRYPTO", "Errors.Intent.InvalidToken")
|
||||
return zerrors.ThrowPermissionDenied(nil, "CRYPTO-CRYPTO", "Errors.Intent.InvalidToken")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@@ -7,7 +7,7 @@ import (
|
||||
|
||||
"github.com/zitadel/zitadel/internal/crypto"
|
||||
z_db "github.com/zitadel/zitadel/internal/database"
|
||||
caos_errs "github.com/zitadel/zitadel/internal/errors"
|
||||
"github.com/zitadel/zitadel/internal/zerrors"
|
||||
)
|
||||
|
||||
type database struct {
|
||||
@@ -41,18 +41,18 @@ func (d *database) ReadKeys() (crypto.Keys, error) {
|
||||
From(EncryptionKeysTable).
|
||||
ToSql()
|
||||
if err != nil {
|
||||
return nil, caos_errs.ThrowInternal(err, "", "unable to read keys")
|
||||
return nil, zerrors.ThrowInternal(err, "", "unable to read keys")
|
||||
}
|
||||
err = d.client.Query(func(rows *sql.Rows) error {
|
||||
for rows.Next() {
|
||||
var id, encryptionKey string
|
||||
err = rows.Scan(&id, &encryptionKey)
|
||||
if err != nil {
|
||||
return caos_errs.ThrowInternal(err, "", "unable to read keys")
|
||||
return zerrors.ThrowInternal(err, "", "unable to read keys")
|
||||
}
|
||||
key, err := d.decrypt(encryptionKey, d.masterKey)
|
||||
if err != nil {
|
||||
return caos_errs.ThrowInternal(err, "", "unable to decrypt key")
|
||||
return zerrors.ThrowInternal(err, "", "unable to decrypt key")
|
||||
}
|
||||
keys[id] = key
|
||||
}
|
||||
@@ -60,7 +60,7 @@ func (d *database) ReadKeys() (crypto.Keys, error) {
|
||||
}, stmt, args...)
|
||||
|
||||
if err != nil {
|
||||
return nil, caos_errs.ThrowInternal(err, "", "unable to read keys")
|
||||
return nil, zerrors.ThrowInternal(err, "", "unable to read keys")
|
||||
}
|
||||
|
||||
return keys, nil
|
||||
@@ -73,23 +73,23 @@ func (d *database) ReadKey(id string) (_ *crypto.Key, err error) {
|
||||
PlaceholderFormat(sq.Dollar).
|
||||
ToSql()
|
||||
if err != nil {
|
||||
return nil, caos_errs.ThrowInternal(err, "", "unable to read key")
|
||||
return nil, zerrors.ThrowInternal(err, "", "unable to read key")
|
||||
}
|
||||
var key string
|
||||
err = d.client.QueryRow(func(row *sql.Row) error {
|
||||
var encryptionKey string
|
||||
err = row.Scan(&encryptionKey)
|
||||
if err != nil {
|
||||
return caos_errs.ThrowInternal(err, "", "unable to read key")
|
||||
return zerrors.ThrowInternal(err, "", "unable to read key")
|
||||
}
|
||||
key, err = d.decrypt(encryptionKey, d.masterKey)
|
||||
if err != nil {
|
||||
return caos_errs.ThrowInternal(err, "", "unable to decrypt key")
|
||||
return zerrors.ThrowInternal(err, "", "unable to decrypt key")
|
||||
}
|
||||
return nil
|
||||
}, stmt, args...)
|
||||
if err != nil {
|
||||
return nil, caos_errs.ThrowInternal(err, "", "unable to read key")
|
||||
return nil, zerrors.ThrowInternal(err, "", "unable to read key")
|
||||
}
|
||||
|
||||
return &crypto.Key{
|
||||
@@ -104,33 +104,33 @@ func (d *database) CreateKeys(keys ...*crypto.Key) error {
|
||||
for _, key := range keys {
|
||||
encryptionKey, err := d.encrypt(key.Value, d.masterKey)
|
||||
if err != nil {
|
||||
return caos_errs.ThrowInternal(err, "", "unable to encrypt key")
|
||||
return zerrors.ThrowInternal(err, "", "unable to encrypt key")
|
||||
}
|
||||
insert = insert.Values(key.ID, encryptionKey)
|
||||
}
|
||||
stmt, args, err := insert.ToSql()
|
||||
if err != nil {
|
||||
return caos_errs.ThrowInternal(err, "", "unable to insert new keys")
|
||||
return zerrors.ThrowInternal(err, "", "unable to insert new keys")
|
||||
}
|
||||
tx, err := d.client.Begin()
|
||||
if err != nil {
|
||||
return caos_errs.ThrowInternal(err, "", "unable to insert new keys")
|
||||
return zerrors.ThrowInternal(err, "", "unable to insert new keys")
|
||||
}
|
||||
_, err = tx.Exec(stmt, args...)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return caos_errs.ThrowInternal(err, "", "unable to insert new keys")
|
||||
return zerrors.ThrowInternal(err, "", "unable to insert new keys")
|
||||
}
|
||||
err = tx.Commit()
|
||||
if err != nil {
|
||||
return caos_errs.ThrowInternal(err, "", "unable to insert new keys")
|
||||
return zerrors.ThrowInternal(err, "", "unable to insert new keys")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func checkMasterKeyLength(masterKey string) error {
|
||||
if length := len([]byte(masterKey)); length != 32 {
|
||||
return caos_errs.ThrowInternalf(nil, "", "masterkey must be 32 bytes, but is %d", length)
|
||||
return zerrors.ThrowInternalf(nil, "", "masterkey must be 32 bytes, but is %d", length)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@@ -13,7 +13,7 @@ import (
|
||||
|
||||
"github.com/zitadel/zitadel/internal/crypto"
|
||||
z_db "github.com/zitadel/zitadel/internal/database"
|
||||
caos_errs "github.com/zitadel/zitadel/internal/errors"
|
||||
"github.com/zitadel/zitadel/internal/zerrors"
|
||||
)
|
||||
|
||||
func Test_database_ReadKeys(t *testing.T) {
|
||||
@@ -62,7 +62,7 @@ func Test_database_ReadKeys(t *testing.T) {
|
||||
},
|
||||
},
|
||||
res{
|
||||
err: caos_errs.IsInternal,
|
||||
err: zerrors.IsInternal,
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -187,7 +187,7 @@ func Test_database_ReadKey(t *testing.T) {
|
||||
id: "id1",
|
||||
},
|
||||
res{
|
||||
err: caos_errs.IsInternal,
|
||||
err: zerrors.IsInternal,
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -212,7 +212,7 @@ func Test_database_ReadKey(t *testing.T) {
|
||||
id: "id1",
|
||||
},
|
||||
res{
|
||||
err: caos_errs.IsInternal,
|
||||
err: zerrors.IsInternal,
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -303,7 +303,7 @@ func Test_database_CreateKeys(t *testing.T) {
|
||||
},
|
||||
},
|
||||
res{
|
||||
err: caos_errs.IsInternal,
|
||||
err: zerrors.IsInternal,
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -422,7 +422,7 @@ func Test_checkMasterKeyLength(t *testing.T) {
|
||||
args{
|
||||
masterKey: "",
|
||||
},
|
||||
caos_errs.IsInternal,
|
||||
zerrors.IsInternal,
|
||||
},
|
||||
{
|
||||
"valid length",
|
||||
|
@@ -5,7 +5,7 @@ import (
|
||||
|
||||
"github.com/zitadel/logging"
|
||||
|
||||
"github.com/zitadel/zitadel/internal/errors"
|
||||
"github.com/zitadel/zitadel/internal/zerrors"
|
||||
)
|
||||
|
||||
type KeyConfig struct {
|
||||
@@ -41,7 +41,7 @@ func LoadKey(id string, keyStorage KeyStorage) (string, error) {
|
||||
|
||||
func LoadKeys(config *KeyConfig, keyStorage KeyStorage) (Keys, []string, error) {
|
||||
if config == nil {
|
||||
return nil, nil, errors.ThrowInvalidArgument(nil, "CRYPT-dJK8s", "config must not be nil")
|
||||
return nil, nil, zerrors.ThrowInvalidArgument(nil, "CRYPT-dJK8s", "config must not be nil")
|
||||
}
|
||||
readKeys, err := keyStorage.ReadKeys()
|
||||
if err != nil {
|
||||
@@ -52,7 +52,7 @@ func LoadKeys(config *KeyConfig, keyStorage KeyStorage) (Keys, []string, error)
|
||||
if config.EncryptionKeyID != "" {
|
||||
key, ok := readKeys[config.EncryptionKeyID]
|
||||
if !ok {
|
||||
return nil, nil, errors.ThrowInternalf(nil, "CRYPT-v2Kas", "encryption key %s not found", config.EncryptionKeyID)
|
||||
return nil, nil, zerrors.ThrowInternalf(nil, "CRYPT-v2Kas", "encryption key %s not found", config.EncryptionKeyID)
|
||||
}
|
||||
keys[config.EncryptionKeyID] = key
|
||||
ids = append(ids, config.EncryptionKeyID)
|
||||
|
@@ -13,7 +13,7 @@ import (
|
||||
"github.com/zitadel/passwap/scrypt"
|
||||
"github.com/zitadel/passwap/verifier"
|
||||
|
||||
"github.com/zitadel/zitadel/internal/errors"
|
||||
"github.com/zitadel/zitadel/internal/zerrors"
|
||||
)
|
||||
|
||||
type PasswordHasher struct {
|
||||
@@ -62,11 +62,11 @@ type PasswordHashConfig struct {
|
||||
func (c *PasswordHashConfig) PasswordHasher() (*PasswordHasher, error) {
|
||||
verifiers, vPrefixes, err := c.buildVerifiers()
|
||||
if err != nil {
|
||||
return nil, errors.ThrowInvalidArgument(err, "CRYPT-sahW9", "password hash config invalid")
|
||||
return nil, zerrors.ThrowInvalidArgument(err, "CRYPT-sahW9", "password hash config invalid")
|
||||
}
|
||||
hasher, hPrefixes, err := c.Hasher.buildHasher()
|
||||
if err != nil {
|
||||
return nil, errors.ThrowInvalidArgument(err, "CRYPT-Que4r", "password hash config invalid")
|
||||
return nil, zerrors.ThrowInvalidArgument(err, "CRYPT-Que4r", "password hash config invalid")
|
||||
}
|
||||
return &PasswordHasher{
|
||||
Swapper: passwap.NewSwapper(hasher, verifiers...),
|
||||
|
Reference in New Issue
Block a user