From fcda6580ffb86fe9f0651b5639c21a4202d92a5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20M=C3=B6hlmann?= Date: Tue, 13 Aug 2024 15:52:43 +0300 Subject: [PATCH] fix(query): print log line on secret generator error (#8424) # Which Problems Are Solved Log some details when a secret generator is not found. This should help us debugging such issue. # How the Problems Are Solved When a secret generator by type query fails, we log the generator type and instance id for which the generator was requested. # Additional Changes - none # Additional Context - Related to https://github.com/zitadel/zitadel/issues/8379 - Also encountered in https://github.com/zitadel/zitadel/pull/8407 --- internal/domain/secret_generator.go | 1 + internal/domain/secretgeneratortype_enumer.go | 114 ++++++++++++++++++ internal/query/secret_generators.go | 5 +- 3 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 internal/domain/secretgeneratortype_enumer.go diff --git a/internal/domain/secret_generator.go b/internal/domain/secret_generator.go index 4a3300a1b..10afb774a 100644 --- a/internal/domain/secret_generator.go +++ b/internal/domain/secret_generator.go @@ -1,5 +1,6 @@ package domain +//go:generate enumer -type SecretGeneratorType -transform snake -trimprefix SecretGeneratorType type SecretGeneratorType int32 const ( diff --git a/internal/domain/secretgeneratortype_enumer.go b/internal/domain/secretgeneratortype_enumer.go new file mode 100644 index 000000000..92e0ead33 --- /dev/null +++ b/internal/domain/secretgeneratortype_enumer.go @@ -0,0 +1,114 @@ +// Code generated by "enumer -type SecretGeneratorType -transform snake -trimprefix SecretGeneratorType"; DO NOT EDIT. + +package domain + +import ( + "fmt" + "strings" +) + +const _SecretGeneratorTypeName = "unspecifiedinit_codeverify_email_codeverify_phone_codeverify_domainpassword_reset_codepasswordless_init_codeapp_secretotpsmsotp_emailsecret_generator_type_count" + +var _SecretGeneratorTypeIndex = [...]uint8{0, 11, 20, 37, 54, 67, 86, 108, 118, 124, 133, 160} + +const _SecretGeneratorTypeLowerName = "unspecifiedinit_codeverify_email_codeverify_phone_codeverify_domainpassword_reset_codepasswordless_init_codeapp_secretotpsmsotp_emailsecret_generator_type_count" + +func (i SecretGeneratorType) String() string { + if i < 0 || i >= SecretGeneratorType(len(_SecretGeneratorTypeIndex)-1) { + return fmt.Sprintf("SecretGeneratorType(%d)", i) + } + return _SecretGeneratorTypeName[_SecretGeneratorTypeIndex[i]:_SecretGeneratorTypeIndex[i+1]] +} + +// An "invalid array index" compiler error signifies that the constant values have changed. +// Re-run the stringer command to generate them again. +func _SecretGeneratorTypeNoOp() { + var x [1]struct{} + _ = x[SecretGeneratorTypeUnspecified-(0)] + _ = x[SecretGeneratorTypeInitCode-(1)] + _ = x[SecretGeneratorTypeVerifyEmailCode-(2)] + _ = x[SecretGeneratorTypeVerifyPhoneCode-(3)] + _ = x[SecretGeneratorTypeVerifyDomain-(4)] + _ = x[SecretGeneratorTypePasswordResetCode-(5)] + _ = x[SecretGeneratorTypePasswordlessInitCode-(6)] + _ = x[SecretGeneratorTypeAppSecret-(7)] + _ = x[SecretGeneratorTypeOTPSMS-(8)] + _ = x[SecretGeneratorTypeOTPEmail-(9)] + _ = x[secretGeneratorTypeCount-(10)] +} + +var _SecretGeneratorTypeValues = []SecretGeneratorType{SecretGeneratorTypeUnspecified, SecretGeneratorTypeInitCode, SecretGeneratorTypeVerifyEmailCode, SecretGeneratorTypeVerifyPhoneCode, SecretGeneratorTypeVerifyDomain, SecretGeneratorTypePasswordResetCode, SecretGeneratorTypePasswordlessInitCode, SecretGeneratorTypeAppSecret, SecretGeneratorTypeOTPSMS, SecretGeneratorTypeOTPEmail, secretGeneratorTypeCount} + +var _SecretGeneratorTypeNameToValueMap = map[string]SecretGeneratorType{ + _SecretGeneratorTypeName[0:11]: SecretGeneratorTypeUnspecified, + _SecretGeneratorTypeLowerName[0:11]: SecretGeneratorTypeUnspecified, + _SecretGeneratorTypeName[11:20]: SecretGeneratorTypeInitCode, + _SecretGeneratorTypeLowerName[11:20]: SecretGeneratorTypeInitCode, + _SecretGeneratorTypeName[20:37]: SecretGeneratorTypeVerifyEmailCode, + _SecretGeneratorTypeLowerName[20:37]: SecretGeneratorTypeVerifyEmailCode, + _SecretGeneratorTypeName[37:54]: SecretGeneratorTypeVerifyPhoneCode, + _SecretGeneratorTypeLowerName[37:54]: SecretGeneratorTypeVerifyPhoneCode, + _SecretGeneratorTypeName[54:67]: SecretGeneratorTypeVerifyDomain, + _SecretGeneratorTypeLowerName[54:67]: SecretGeneratorTypeVerifyDomain, + _SecretGeneratorTypeName[67:86]: SecretGeneratorTypePasswordResetCode, + _SecretGeneratorTypeLowerName[67:86]: SecretGeneratorTypePasswordResetCode, + _SecretGeneratorTypeName[86:108]: SecretGeneratorTypePasswordlessInitCode, + _SecretGeneratorTypeLowerName[86:108]: SecretGeneratorTypePasswordlessInitCode, + _SecretGeneratorTypeName[108:118]: SecretGeneratorTypeAppSecret, + _SecretGeneratorTypeLowerName[108:118]: SecretGeneratorTypeAppSecret, + _SecretGeneratorTypeName[118:124]: SecretGeneratorTypeOTPSMS, + _SecretGeneratorTypeLowerName[118:124]: SecretGeneratorTypeOTPSMS, + _SecretGeneratorTypeName[124:133]: SecretGeneratorTypeOTPEmail, + _SecretGeneratorTypeLowerName[124:133]: SecretGeneratorTypeOTPEmail, + _SecretGeneratorTypeName[133:160]: secretGeneratorTypeCount, + _SecretGeneratorTypeLowerName[133:160]: secretGeneratorTypeCount, +} + +var _SecretGeneratorTypeNames = []string{ + _SecretGeneratorTypeName[0:11], + _SecretGeneratorTypeName[11:20], + _SecretGeneratorTypeName[20:37], + _SecretGeneratorTypeName[37:54], + _SecretGeneratorTypeName[54:67], + _SecretGeneratorTypeName[67:86], + _SecretGeneratorTypeName[86:108], + _SecretGeneratorTypeName[108:118], + _SecretGeneratorTypeName[118:124], + _SecretGeneratorTypeName[124:133], + _SecretGeneratorTypeName[133:160], +} + +// SecretGeneratorTypeString retrieves an enum value from the enum constants string name. +// Throws an error if the param is not part of the enum. +func SecretGeneratorTypeString(s string) (SecretGeneratorType, error) { + if val, ok := _SecretGeneratorTypeNameToValueMap[s]; ok { + return val, nil + } + + if val, ok := _SecretGeneratorTypeNameToValueMap[strings.ToLower(s)]; ok { + return val, nil + } + return 0, fmt.Errorf("%s does not belong to SecretGeneratorType values", s) +} + +// SecretGeneratorTypeValues returns all values of the enum +func SecretGeneratorTypeValues() []SecretGeneratorType { + return _SecretGeneratorTypeValues +} + +// SecretGeneratorTypeStrings returns a slice of all String values of the enum +func SecretGeneratorTypeStrings() []string { + strs := make([]string, len(_SecretGeneratorTypeNames)) + copy(strs, _SecretGeneratorTypeNames) + return strs +} + +// IsASecretGeneratorType returns "true" if the value is listed in the enum definition. "false" otherwise +func (i SecretGeneratorType) IsASecretGeneratorType() bool { + for _, v := range _SecretGeneratorTypeValues { + if i == v { + return true + } + } + return false +} diff --git a/internal/query/secret_generators.go b/internal/query/secret_generators.go index ffd62bd26..8ee8694d2 100644 --- a/internal/query/secret_generators.go +++ b/internal/query/secret_generators.go @@ -7,6 +7,7 @@ import ( "time" sq "github.com/Masterminds/squirrel" + "github.com/zitadel/logging" "github.com/zitadel/zitadel/internal/api/authz" "github.com/zitadel/zitadel/internal/api/call" @@ -125,10 +126,11 @@ func (q *Queries) SecretGeneratorByType(ctx context.Context, generatorType domai ctx, span := tracing.NewSpan(ctx) defer func() { span.EndWithError(err) }() + instanceID := authz.GetInstance(ctx).InstanceID() stmt, scan := prepareSecretGeneratorQuery(ctx, q.client) query, args, err := stmt.Where(sq.Eq{ SecretGeneratorColumnGeneratorType.identifier(): generatorType, - SecretGeneratorColumnInstanceID.identifier(): authz.GetInstance(ctx).InstanceID(), + SecretGeneratorColumnInstanceID.identifier(): instanceID, }).ToSql() if err != nil { return nil, zerrors.ThrowInternal(err, "QUERY-3k99f", "Errors.Query.SQLStatment") @@ -138,6 +140,7 @@ func (q *Queries) SecretGeneratorByType(ctx context.Context, generatorType domai generator, err = scan(row) return err }, query, args...) + logging.OnError(err).WithField("type", generatorType).WithField("instance_id", instanceID).Error("secret generator by type") return generator, err }