mirror of
https://github.com/zitadel/zitadel.git
synced 2025-12-06 17:22:28 +00:00
feat: Hosted login translation API (#10011)
# Which Problems Are Solved This PR implements https://github.com/zitadel/zitadel/issues/9850 # How the Problems Are Solved - New protobuf definition - Implementation of retrieval of system translations - Implementation of retrieval and persistence of organization and instance level translations # Additional Context - Closes #9850 # TODO - [x] Integration tests for Get and Set hosted login translation endpoints - [x] DB migration test - [x] Command function tests - [x] Command util functions tests - [x] Query function test - [x] Query util functions tests
This commit is contained in:
256
internal/query/hosted_login_translation.go
Normal file
256
internal/query/hosted_login_translation.go
Normal file
@@ -0,0 +1,256 @@
|
||||
package query
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/md5"
|
||||
"database/sql"
|
||||
_ "embed"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"dario.cat/mergo"
|
||||
sq "github.com/Masterminds/squirrel"
|
||||
"github.com/zitadel/logging"
|
||||
"golang.org/x/text/language"
|
||||
"google.golang.org/protobuf/types/known/structpb"
|
||||
|
||||
"github.com/zitadel/zitadel/internal/api/authz"
|
||||
"github.com/zitadel/zitadel/internal/query/projection"
|
||||
"github.com/zitadel/zitadel/internal/repository/instance"
|
||||
"github.com/zitadel/zitadel/internal/telemetry/tracing"
|
||||
"github.com/zitadel/zitadel/internal/v2/org"
|
||||
"github.com/zitadel/zitadel/internal/zerrors"
|
||||
"github.com/zitadel/zitadel/pkg/grpc/settings/v2"
|
||||
)
|
||||
|
||||
var (
|
||||
//go:embed v2-default.json
|
||||
defaultLoginTranslations []byte
|
||||
|
||||
defaultSystemTranslations map[language.Tag]map[string]any
|
||||
|
||||
hostedLoginTranslationTable = table{
|
||||
name: projection.HostedLoginTranslationTable,
|
||||
instanceIDCol: projection.HostedLoginTranslationInstanceIDCol,
|
||||
}
|
||||
|
||||
hostedLoginTranslationColInstanceID = Column{
|
||||
name: projection.HostedLoginTranslationInstanceIDCol,
|
||||
table: hostedLoginTranslationTable,
|
||||
}
|
||||
hostedLoginTranslationColResourceOwner = Column{
|
||||
name: projection.HostedLoginTranslationAggregateIDCol,
|
||||
table: hostedLoginTranslationTable,
|
||||
}
|
||||
hostedLoginTranslationColResourceOwnerType = Column{
|
||||
name: projection.HostedLoginTranslationAggregateTypeCol,
|
||||
table: hostedLoginTranslationTable,
|
||||
}
|
||||
hostedLoginTranslationColLocale = Column{
|
||||
name: projection.HostedLoginTranslationLocaleCol,
|
||||
table: hostedLoginTranslationTable,
|
||||
}
|
||||
hostedLoginTranslationColFile = Column{
|
||||
name: projection.HostedLoginTranslationFileCol,
|
||||
table: hostedLoginTranslationTable,
|
||||
}
|
||||
hostedLoginTranslationColEtag = Column{
|
||||
name: projection.HostedLoginTranslationEtagCol,
|
||||
table: hostedLoginTranslationTable,
|
||||
}
|
||||
)
|
||||
|
||||
func init() {
|
||||
err := json.Unmarshal(defaultLoginTranslations, &defaultSystemTranslations)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
type HostedLoginTranslations struct {
|
||||
SearchResponse
|
||||
HostedLoginTranslations []*HostedLoginTranslation
|
||||
}
|
||||
|
||||
type HostedLoginTranslation struct {
|
||||
AggregateID string
|
||||
Sequence uint64
|
||||
CreationDate time.Time
|
||||
ChangeDate time.Time
|
||||
|
||||
Locale string
|
||||
File map[string]any
|
||||
LevelType string
|
||||
LevelID string
|
||||
Etag string
|
||||
}
|
||||
|
||||
func (q *Queries) GetHostedLoginTranslation(ctx context.Context, req *settings.GetHostedLoginTranslationRequest) (res *settings.GetHostedLoginTranslationResponse, err error) {
|
||||
ctx, span := tracing.NewSpan(ctx)
|
||||
defer func() { span.EndWithError(err) }()
|
||||
|
||||
inst := authz.GetInstance(ctx)
|
||||
defaultInstLang := inst.DefaultLanguage()
|
||||
|
||||
lang, err := language.BCP47.Parse(req.GetLocale())
|
||||
if err != nil || lang.IsRoot() {
|
||||
return nil, zerrors.ThrowInvalidArgument(nil, "QUERY-rZLAGi", "Errors.Arguments.Locale.Invalid")
|
||||
}
|
||||
parentLang := lang.Parent()
|
||||
if parentLang.IsRoot() {
|
||||
parentLang = lang
|
||||
}
|
||||
|
||||
sysTranslation, systemEtag, err := getSystemTranslation(parentLang, defaultInstLang)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var levelID, resourceOwner string
|
||||
switch t := req.GetLevel().(type) {
|
||||
case *settings.GetHostedLoginTranslationRequest_System:
|
||||
return getTranslationOutputMessage(sysTranslation, systemEtag)
|
||||
case *settings.GetHostedLoginTranslationRequest_Instance:
|
||||
levelID = authz.GetInstance(ctx).InstanceID()
|
||||
resourceOwner = instance.AggregateType
|
||||
case *settings.GetHostedLoginTranslationRequest_OrganizationId:
|
||||
levelID = t.OrganizationId
|
||||
resourceOwner = org.AggregateType
|
||||
default:
|
||||
return nil, zerrors.ThrowInvalidArgument(nil, "QUERY-YB6Sri", "Errors.Arguments.Level.Invalid")
|
||||
}
|
||||
|
||||
stmt, scan := prepareHostedLoginTranslationQuery()
|
||||
|
||||
langORBaseLang := sq.Or{
|
||||
sq.Eq{hostedLoginTranslationColLocale.identifier(): lang.String()},
|
||||
sq.Eq{hostedLoginTranslationColLocale.identifier(): parentLang.String()},
|
||||
}
|
||||
eq := sq.Eq{
|
||||
hostedLoginTranslationColInstanceID.identifier(): inst.InstanceID(),
|
||||
hostedLoginTranslationColResourceOwner.identifier(): levelID,
|
||||
hostedLoginTranslationColResourceOwnerType.identifier(): resourceOwner,
|
||||
}
|
||||
|
||||
query, args, err := stmt.Where(eq).Where(langORBaseLang).ToSql()
|
||||
if err != nil {
|
||||
logging.WithError(err).Error("unable to generate sql statement")
|
||||
return nil, zerrors.ThrowInternal(err, "QUERY-ZgCMux", "Errors.Query.SQLStatement")
|
||||
}
|
||||
|
||||
var trs []*HostedLoginTranslation
|
||||
err = q.client.QueryContext(ctx, func(rows *sql.Rows) error {
|
||||
trs, err = scan(rows)
|
||||
return err
|
||||
}, query, args...)
|
||||
if err != nil {
|
||||
logging.WithError(err).Error("failed to query translations")
|
||||
return nil, zerrors.ThrowInternal(err, "QUERY-6k1zjx", "Errors.Internal")
|
||||
}
|
||||
|
||||
requestedTranslation, parentTranslation := &HostedLoginTranslation{}, &HostedLoginTranslation{}
|
||||
for _, tr := range trs {
|
||||
if tr == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
if tr.LevelType == resourceOwner {
|
||||
requestedTranslation = tr
|
||||
} else {
|
||||
parentTranslation = tr
|
||||
}
|
||||
}
|
||||
|
||||
if !req.GetIgnoreInheritance() {
|
||||
|
||||
// There is no record for the requested level, set the upper level etag
|
||||
if requestedTranslation.Etag == "" {
|
||||
requestedTranslation.Etag = parentTranslation.Etag
|
||||
}
|
||||
|
||||
// Case where Level == ORGANIZATION -> Check if we have an instance level translation
|
||||
// If so, merge it with the translations we have
|
||||
if parentTranslation != nil && parentTranslation.LevelType == instance.AggregateType {
|
||||
if err := mergo.Merge(&requestedTranslation.File, parentTranslation.File); err != nil {
|
||||
return nil, zerrors.ThrowInternal(err, "QUERY-pdgEJd", "Errors.Query.MergeTranslations")
|
||||
}
|
||||
}
|
||||
|
||||
// The DB query returned no results, we have to set the system translation etag
|
||||
if requestedTranslation.Etag == "" {
|
||||
requestedTranslation.Etag = systemEtag
|
||||
}
|
||||
|
||||
// Merge the system translations
|
||||
if err := mergo.Merge(&requestedTranslation.File, sysTranslation); err != nil {
|
||||
return nil, zerrors.ThrowInternal(err, "QUERY-HdprNF", "Errors.Query.MergeTranslations")
|
||||
}
|
||||
}
|
||||
|
||||
return getTranslationOutputMessage(requestedTranslation.File, requestedTranslation.Etag)
|
||||
}
|
||||
|
||||
func getSystemTranslation(lang, instanceDefaultLang language.Tag) (map[string]any, string, error) {
|
||||
translation, ok := defaultSystemTranslations[lang]
|
||||
if !ok {
|
||||
translation, ok = defaultSystemTranslations[instanceDefaultLang]
|
||||
if !ok {
|
||||
return nil, "", zerrors.ThrowNotFoundf(nil, "QUERY-6gb5QR", "Errors.Query.HostedLoginTranslationNotFound-%s", lang)
|
||||
}
|
||||
}
|
||||
|
||||
hash := md5.Sum(fmt.Append(nil, translation))
|
||||
|
||||
return translation, hex.EncodeToString(hash[:]), nil
|
||||
}
|
||||
|
||||
func prepareHostedLoginTranslationQuery() (sq.SelectBuilder, func(*sql.Rows) ([]*HostedLoginTranslation, error)) {
|
||||
return sq.Select(
|
||||
hostedLoginTranslationColFile.identifier(),
|
||||
hostedLoginTranslationColResourceOwnerType.identifier(),
|
||||
hostedLoginTranslationColEtag.identifier(),
|
||||
).From(hostedLoginTranslationTable.identifier()).
|
||||
Limit(2).
|
||||
PlaceholderFormat(sq.Dollar),
|
||||
func(r *sql.Rows) ([]*HostedLoginTranslation, error) {
|
||||
translations := make([]*HostedLoginTranslation, 0, 2)
|
||||
for r.Next() {
|
||||
var rawTranslation json.RawMessage
|
||||
translation := &HostedLoginTranslation{}
|
||||
err := r.Scan(
|
||||
&rawTranslation,
|
||||
&translation.LevelType,
|
||||
&translation.Etag,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(rawTranslation, &translation.File); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
translations = append(translations, translation)
|
||||
}
|
||||
|
||||
if err := r.Close(); err != nil {
|
||||
return nil, zerrors.ThrowInternal(err, "QUERY-oc7r7i", "Errors.Query.CloseRows")
|
||||
}
|
||||
|
||||
return translations, nil
|
||||
}
|
||||
}
|
||||
|
||||
func getTranslationOutputMessage(translation map[string]any, etag string) (*settings.GetHostedLoginTranslationResponse, error) {
|
||||
protoTranslation, err := structpb.NewStruct(translation)
|
||||
if err != nil {
|
||||
return nil, zerrors.ThrowInternal(err, "QUERY-70ppPp", "Errors.Protobuf.ConvertToStruct")
|
||||
}
|
||||
|
||||
return &settings.GetHostedLoginTranslationResponse{
|
||||
Translations: protoTranslation,
|
||||
Etag: etag,
|
||||
}, nil
|
||||
}
|
||||
337
internal/query/hosted_login_translation_test.go
Normal file
337
internal/query/hosted_login_translation_test.go
Normal file
@@ -0,0 +1,337 @@
|
||||
package query
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"database/sql"
|
||||
"database/sql/driver"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"maps"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"golang.org/x/text/language"
|
||||
"google.golang.org/protobuf/runtime/protoimpl"
|
||||
"google.golang.org/protobuf/types/known/structpb"
|
||||
|
||||
"github.com/zitadel/zitadel/internal/api/authz"
|
||||
"github.com/zitadel/zitadel/internal/database"
|
||||
"github.com/zitadel/zitadel/internal/database/mock"
|
||||
"github.com/zitadel/zitadel/internal/zerrors"
|
||||
"github.com/zitadel/zitadel/pkg/grpc/settings/v2"
|
||||
)
|
||||
|
||||
func TestGetSystemTranslation(t *testing.T) {
|
||||
okTranslation := defaultLoginTranslations
|
||||
|
||||
parsedOKTranslation := map[string]map[string]any{}
|
||||
require.Nil(t, json.Unmarshal(okTranslation, &parsedOKTranslation))
|
||||
|
||||
hashOK := md5.Sum(fmt.Append(nil, parsedOKTranslation["de"]))
|
||||
|
||||
tt := []struct {
|
||||
testName string
|
||||
|
||||
inputLanguage language.Tag
|
||||
inputInstanceLanguage language.Tag
|
||||
systemTranslationToSet []byte
|
||||
|
||||
expectedLanguage map[string]any
|
||||
expectedEtag string
|
||||
expectedError error
|
||||
}{
|
||||
{
|
||||
testName: "when neither input language nor system default language have translation should return not found error",
|
||||
systemTranslationToSet: okTranslation,
|
||||
inputLanguage: language.MustParse("ro"),
|
||||
inputInstanceLanguage: language.MustParse("fr"),
|
||||
|
||||
expectedError: zerrors.ThrowNotFoundf(nil, "QUERY-6gb5QR", "Errors.Query.HostedLoginTranslationNotFound-%s", "ro"),
|
||||
},
|
||||
{
|
||||
testName: "when input language has no translation should fallback onto instance default",
|
||||
systemTranslationToSet: okTranslation,
|
||||
inputLanguage: language.MustParse("ro"),
|
||||
inputInstanceLanguage: language.MustParse("de"),
|
||||
|
||||
expectedLanguage: parsedOKTranslation["de"],
|
||||
expectedEtag: hex.EncodeToString(hashOK[:]),
|
||||
},
|
||||
{
|
||||
testName: "when input language has translation should return it",
|
||||
systemTranslationToSet: okTranslation,
|
||||
inputLanguage: language.MustParse("de"),
|
||||
inputInstanceLanguage: language.MustParse("en"),
|
||||
|
||||
expectedLanguage: parsedOKTranslation["de"],
|
||||
expectedEtag: hex.EncodeToString(hashOK[:]),
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tt {
|
||||
t.Run(tc.testName, func(t *testing.T) {
|
||||
// Given
|
||||
defaultLoginTranslations = tc.systemTranslationToSet
|
||||
|
||||
// When
|
||||
translation, etag, err := getSystemTranslation(tc.inputLanguage, tc.inputInstanceLanguage)
|
||||
|
||||
// Verify
|
||||
require.Equal(t, tc.expectedError, err)
|
||||
assert.Equal(t, tc.expectedLanguage, translation)
|
||||
assert.Equal(t, tc.expectedEtag, etag)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetTranslationOutput(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
validMap := map[string]any{"loginHeader": "A login header"}
|
||||
protoMap, err := structpb.NewStruct(validMap)
|
||||
require.NoError(t, err)
|
||||
|
||||
hash := md5.Sum(fmt.Append(nil, validMap))
|
||||
encodedHash := hex.EncodeToString(hash[:])
|
||||
|
||||
tt := []struct {
|
||||
testName string
|
||||
inputTranslation map[string]any
|
||||
expectedError error
|
||||
expectedResponse *settings.GetHostedLoginTranslationResponse
|
||||
}{
|
||||
{
|
||||
testName: "when unparsable map should return internal error",
|
||||
inputTranslation: map[string]any{"\xc5z": "something"},
|
||||
expectedError: zerrors.ThrowInternal(protoimpl.X.NewError("invalid UTF-8 in string: %q", "\xc5z"), "QUERY-70ppPp", "Errors.Protobuf.ConvertToStruct"),
|
||||
},
|
||||
{
|
||||
testName: "when input translation is valid should return expected response message",
|
||||
inputTranslation: validMap,
|
||||
expectedResponse: &settings.GetHostedLoginTranslationResponse{
|
||||
Translations: protoMap,
|
||||
Etag: hex.EncodeToString(hash[:]),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tt {
|
||||
t.Run(tc.testName, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
// When
|
||||
res, err := getTranslationOutputMessage(tc.inputTranslation, encodedHash)
|
||||
|
||||
// Verify
|
||||
require.Equal(t, tc.expectedError, err)
|
||||
assert.Equal(t, tc.expectedResponse, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetHostedLoginTranslation(t *testing.T) {
|
||||
query := `SELECT projections.hosted_login_translations.file, projections.hosted_login_translations.aggregate_type, projections.hosted_login_translations.etag
|
||||
FROM projections.hosted_login_translations
|
||||
WHERE projections.hosted_login_translations.aggregate_id = $1
|
||||
AND projections.hosted_login_translations.aggregate_type = $2
|
||||
AND projections.hosted_login_translations.instance_id = $3
|
||||
AND (projections.hosted_login_translations.locale = $4 OR projections.hosted_login_translations.locale = $5)
|
||||
LIMIT 2`
|
||||
okTranslation := defaultLoginTranslations
|
||||
|
||||
parsedOKTranslation := map[string]map[string]any{}
|
||||
require.NoError(t, json.Unmarshal(okTranslation, &parsedOKTranslation))
|
||||
|
||||
protoDefaultTranslation, err := structpb.NewStruct(parsedOKTranslation["en"])
|
||||
require.Nil(t, err)
|
||||
|
||||
defaultWithDBTranslations := maps.Clone(parsedOKTranslation["en"])
|
||||
defaultWithDBTranslations["test"] = "translation"
|
||||
defaultWithDBTranslations["test2"] = "translation2"
|
||||
protoDefaultWithDBTranslation, err := structpb.NewStruct(defaultWithDBTranslations)
|
||||
require.NoError(t, err)
|
||||
|
||||
nilProtoDefaultMap, err := structpb.NewStruct(nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
hashDefaultTranslations := md5.Sum(fmt.Append(nil, parsedOKTranslation["en"]))
|
||||
|
||||
tt := []struct {
|
||||
testName string
|
||||
|
||||
defaultInstanceLanguage language.Tag
|
||||
sqlExpectations []mock.Expectation
|
||||
|
||||
inputRequest *settings.GetHostedLoginTranslationRequest
|
||||
|
||||
expectedError error
|
||||
expectedResult *settings.GetHostedLoginTranslationResponse
|
||||
}{
|
||||
{
|
||||
testName: "when input language is invalid should return invalid argument error",
|
||||
|
||||
inputRequest: &settings.GetHostedLoginTranslationRequest{},
|
||||
|
||||
expectedError: zerrors.ThrowInvalidArgument(nil, "QUERY-rZLAGi", "Errors.Arguments.Locale.Invalid"),
|
||||
},
|
||||
{
|
||||
testName: "when input language is root should return invalid argument error",
|
||||
|
||||
defaultInstanceLanguage: language.English,
|
||||
inputRequest: &settings.GetHostedLoginTranslationRequest{
|
||||
Locale: "root",
|
||||
},
|
||||
|
||||
expectedError: zerrors.ThrowInvalidArgument(nil, "QUERY-rZLAGi", "Errors.Arguments.Locale.Invalid"),
|
||||
},
|
||||
{
|
||||
testName: "when no system translation is available should return not found error",
|
||||
|
||||
defaultInstanceLanguage: language.Romanian,
|
||||
inputRequest: &settings.GetHostedLoginTranslationRequest{
|
||||
Locale: "ro-RO",
|
||||
},
|
||||
|
||||
expectedError: zerrors.ThrowNotFoundf(nil, "QUERY-6gb5QR", "Errors.Query.HostedLoginTranslationNotFound-%s", "ro"),
|
||||
},
|
||||
{
|
||||
testName: "when requesting system translation should return it",
|
||||
|
||||
defaultInstanceLanguage: language.English,
|
||||
inputRequest: &settings.GetHostedLoginTranslationRequest{
|
||||
Locale: "en-US",
|
||||
Level: &settings.GetHostedLoginTranslationRequest_System{},
|
||||
},
|
||||
|
||||
expectedResult: &settings.GetHostedLoginTranslationResponse{
|
||||
Translations: protoDefaultTranslation,
|
||||
Etag: hex.EncodeToString(hashDefaultTranslations[:]),
|
||||
},
|
||||
},
|
||||
{
|
||||
testName: "when querying DB fails should return internal error",
|
||||
|
||||
defaultInstanceLanguage: language.English,
|
||||
sqlExpectations: []mock.Expectation{
|
||||
mock.ExpectQuery(
|
||||
query,
|
||||
mock.WithQueryArgs("123", "org", "instance-id", "en-US", "en"),
|
||||
mock.WithQueryErr(sql.ErrConnDone),
|
||||
),
|
||||
},
|
||||
inputRequest: &settings.GetHostedLoginTranslationRequest{
|
||||
Locale: "en-US",
|
||||
Level: &settings.GetHostedLoginTranslationRequest_OrganizationId{
|
||||
OrganizationId: "123",
|
||||
},
|
||||
},
|
||||
|
||||
expectedError: zerrors.ThrowInternal(sql.ErrConnDone, "QUERY-6k1zjx", "Errors.Internal"),
|
||||
},
|
||||
{
|
||||
testName: "when querying DB returns no result should return system translations",
|
||||
|
||||
defaultInstanceLanguage: language.English,
|
||||
sqlExpectations: []mock.Expectation{
|
||||
mock.ExpectQuery(
|
||||
query,
|
||||
mock.WithQueryArgs("123", "org", "instance-id", "en-US", "en"),
|
||||
mock.WithQueryResult(
|
||||
[]string{"file", "aggregate_type", "etag"},
|
||||
[][]driver.Value{},
|
||||
),
|
||||
),
|
||||
},
|
||||
inputRequest: &settings.GetHostedLoginTranslationRequest{
|
||||
Locale: "en-US",
|
||||
Level: &settings.GetHostedLoginTranslationRequest_OrganizationId{
|
||||
OrganizationId: "123",
|
||||
},
|
||||
},
|
||||
|
||||
expectedResult: &settings.GetHostedLoginTranslationResponse{
|
||||
Translations: protoDefaultTranslation,
|
||||
Etag: hex.EncodeToString(hashDefaultTranslations[:]),
|
||||
},
|
||||
},
|
||||
{
|
||||
testName: "when querying DB returns no result and inheritance disabled should return empty result",
|
||||
|
||||
defaultInstanceLanguage: language.English,
|
||||
sqlExpectations: []mock.Expectation{
|
||||
mock.ExpectQuery(
|
||||
query,
|
||||
mock.WithQueryArgs("123", "org", "instance-id", "en-US", "en"),
|
||||
mock.WithQueryResult(
|
||||
[]string{"file", "aggregate_type", "etag"},
|
||||
[][]driver.Value{},
|
||||
),
|
||||
),
|
||||
},
|
||||
inputRequest: &settings.GetHostedLoginTranslationRequest{
|
||||
Locale: "en-US",
|
||||
Level: &settings.GetHostedLoginTranslationRequest_OrganizationId{
|
||||
OrganizationId: "123",
|
||||
},
|
||||
IgnoreInheritance: true,
|
||||
},
|
||||
|
||||
expectedResult: &settings.GetHostedLoginTranslationResponse{
|
||||
Etag: "",
|
||||
Translations: nilProtoDefaultMap,
|
||||
},
|
||||
},
|
||||
{
|
||||
testName: "when querying DB returns records should return merged result",
|
||||
|
||||
defaultInstanceLanguage: language.English,
|
||||
sqlExpectations: []mock.Expectation{
|
||||
mock.ExpectQuery(
|
||||
query,
|
||||
mock.WithQueryArgs("123", "org", "instance-id", "en-US", "en"),
|
||||
mock.WithQueryResult(
|
||||
[]string{"file", "aggregate_type", "etag"},
|
||||
[][]driver.Value{
|
||||
{[]byte(`{"test": "translation"}`), "org", "etag-org"},
|
||||
{[]byte(`{"test2": "translation2"}`), "instance", "etag-instance"},
|
||||
},
|
||||
),
|
||||
),
|
||||
},
|
||||
inputRequest: &settings.GetHostedLoginTranslationRequest{
|
||||
Locale: "en-US",
|
||||
Level: &settings.GetHostedLoginTranslationRequest_OrganizationId{
|
||||
OrganizationId: "123",
|
||||
},
|
||||
},
|
||||
|
||||
expectedResult: &settings.GetHostedLoginTranslationResponse{
|
||||
Etag: "etag-org",
|
||||
Translations: protoDefaultWithDBTranslation,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tt {
|
||||
t.Run(tc.testName, func(t *testing.T) {
|
||||
// Given
|
||||
db := &database.DB{DB: mock.NewSQLMock(t, tc.sqlExpectations...).DB}
|
||||
querier := Queries{client: db}
|
||||
|
||||
ctx := authz.NewMockContext("instance-id", "org-id", "user-id", authz.WithMockDefaultLanguage(tc.defaultInstanceLanguage))
|
||||
|
||||
// When
|
||||
res, err := querier.GetHostedLoginTranslation(ctx, tc.inputRequest)
|
||||
|
||||
// Verify
|
||||
require.Equal(t, tc.expectedError, err)
|
||||
|
||||
if tc.expectedError == nil {
|
||||
assert.Equal(t, tc.expectedResult.GetEtag(), res.GetEtag())
|
||||
assert.Equal(t, tc.expectedResult.GetTranslations().GetFields(), res.GetTranslations().GetFields())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
144
internal/query/projection/hosted_login_translation.go
Normal file
144
internal/query/projection/hosted_login_translation.go
Normal file
@@ -0,0 +1,144 @@
|
||||
package projection
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
|
||||
"github.com/zitadel/zitadel/internal/eventstore"
|
||||
old_handler "github.com/zitadel/zitadel/internal/eventstore/handler"
|
||||
"github.com/zitadel/zitadel/internal/eventstore/handler/v2"
|
||||
"github.com/zitadel/zitadel/internal/repository/instance"
|
||||
"github.com/zitadel/zitadel/internal/repository/org"
|
||||
"github.com/zitadel/zitadel/internal/zerrors"
|
||||
)
|
||||
|
||||
const (
|
||||
HostedLoginTranslationTable = "projections.hosted_login_translations"
|
||||
|
||||
HostedLoginTranslationInstanceIDCol = "instance_id"
|
||||
HostedLoginTranslationCreationDateCol = "creation_date"
|
||||
HostedLoginTranslationChangeDateCol = "change_date"
|
||||
HostedLoginTranslationAggregateIDCol = "aggregate_id"
|
||||
HostedLoginTranslationAggregateTypeCol = "aggregate_type"
|
||||
HostedLoginTranslationSequenceCol = "sequence"
|
||||
HostedLoginTranslationLocaleCol = "locale"
|
||||
HostedLoginTranslationFileCol = "file"
|
||||
HostedLoginTranslationEtagCol = "etag"
|
||||
)
|
||||
|
||||
type hostedLoginTranslationProjection struct{}
|
||||
|
||||
func newHostedLoginTranslationProjection(ctx context.Context, config handler.Config) *handler.Handler {
|
||||
return handler.NewHandler(ctx, &config, new(hostedLoginTranslationProjection))
|
||||
}
|
||||
|
||||
// Init implements [handler.initializer]
|
||||
func (p *hostedLoginTranslationProjection) Init() *old_handler.Check {
|
||||
return handler.NewTableCheck(
|
||||
handler.NewTable([]*handler.InitColumn{
|
||||
handler.NewColumn(HostedLoginTranslationInstanceIDCol, handler.ColumnTypeText),
|
||||
handler.NewColumn(HostedLoginTranslationCreationDateCol, handler.ColumnTypeTimestamp),
|
||||
handler.NewColumn(HostedLoginTranslationChangeDateCol, handler.ColumnTypeTimestamp),
|
||||
handler.NewColumn(HostedLoginTranslationAggregateIDCol, handler.ColumnTypeText),
|
||||
handler.NewColumn(HostedLoginTranslationAggregateTypeCol, handler.ColumnTypeText),
|
||||
handler.NewColumn(HostedLoginTranslationSequenceCol, handler.ColumnTypeInt64),
|
||||
handler.NewColumn(HostedLoginTranslationLocaleCol, handler.ColumnTypeText),
|
||||
handler.NewColumn(HostedLoginTranslationFileCol, handler.ColumnTypeJSONB),
|
||||
handler.NewColumn(HostedLoginTranslationEtagCol, handler.ColumnTypeText),
|
||||
},
|
||||
handler.NewPrimaryKey(
|
||||
HostedLoginTranslationInstanceIDCol,
|
||||
HostedLoginTranslationAggregateIDCol,
|
||||
HostedLoginTranslationAggregateTypeCol,
|
||||
HostedLoginTranslationLocaleCol,
|
||||
),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
func (hltp *hostedLoginTranslationProjection) Name() string {
|
||||
return HostedLoginTranslationTable
|
||||
}
|
||||
|
||||
func (hltp *hostedLoginTranslationProjection) Reducers() []handler.AggregateReducer {
|
||||
return []handler.AggregateReducer{
|
||||
{
|
||||
Aggregate: org.AggregateType,
|
||||
EventReducers: []handler.EventReducer{
|
||||
{
|
||||
Event: org.HostedLoginTranslationSet,
|
||||
Reduce: hltp.reduceSet,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Aggregate: instance.AggregateType,
|
||||
EventReducers: []handler.EventReducer{
|
||||
{
|
||||
Event: instance.HostedLoginTranslationSet,
|
||||
Reduce: hltp.reduceSet,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (hltp *hostedLoginTranslationProjection) reduceSet(e eventstore.Event) (*handler.Statement, error) {
|
||||
|
||||
switch e := e.(type) {
|
||||
case *org.HostedLoginTranslationSetEvent:
|
||||
orgEvent := *e
|
||||
return handler.NewUpsertStatement(
|
||||
&orgEvent,
|
||||
[]handler.Column{
|
||||
handler.NewCol(HostedLoginTranslationInstanceIDCol, nil),
|
||||
handler.NewCol(HostedLoginTranslationAggregateIDCol, nil),
|
||||
handler.NewCol(HostedLoginTranslationAggregateTypeCol, nil),
|
||||
handler.NewCol(HostedLoginTranslationLocaleCol, nil),
|
||||
},
|
||||
[]handler.Column{
|
||||
handler.NewCol(HostedLoginTranslationInstanceIDCol, orgEvent.Aggregate().InstanceID),
|
||||
handler.NewCol(HostedLoginTranslationAggregateIDCol, orgEvent.Aggregate().ID),
|
||||
handler.NewCol(HostedLoginTranslationAggregateTypeCol, orgEvent.Aggregate().Type),
|
||||
handler.NewCol(HostedLoginTranslationCreationDateCol, handler.OnlySetValueOnInsert(HostedLoginTranslationTable, orgEvent.CreationDate())),
|
||||
handler.NewCol(HostedLoginTranslationChangeDateCol, orgEvent.CreationDate()),
|
||||
handler.NewCol(HostedLoginTranslationSequenceCol, orgEvent.Sequence()),
|
||||
handler.NewCol(HostedLoginTranslationLocaleCol, orgEvent.Language),
|
||||
handler.NewCol(HostedLoginTranslationFileCol, orgEvent.Translation),
|
||||
handler.NewCol(HostedLoginTranslationEtagCol, hltp.computeEtag(orgEvent.Translation)),
|
||||
},
|
||||
), nil
|
||||
case *instance.HostedLoginTranslationSetEvent:
|
||||
instanceEvent := *e
|
||||
return handler.NewUpsertStatement(
|
||||
&instanceEvent,
|
||||
[]handler.Column{
|
||||
handler.NewCol(HostedLoginTranslationInstanceIDCol, nil),
|
||||
handler.NewCol(HostedLoginTranslationAggregateIDCol, nil),
|
||||
handler.NewCol(HostedLoginTranslationAggregateTypeCol, nil),
|
||||
handler.NewCol(HostedLoginTranslationLocaleCol, nil),
|
||||
},
|
||||
[]handler.Column{
|
||||
handler.NewCol(HostedLoginTranslationInstanceIDCol, instanceEvent.Aggregate().InstanceID),
|
||||
handler.NewCol(HostedLoginTranslationAggregateIDCol, instanceEvent.Aggregate().ID),
|
||||
handler.NewCol(HostedLoginTranslationAggregateTypeCol, instanceEvent.Aggregate().Type),
|
||||
handler.NewCol(HostedLoginTranslationCreationDateCol, handler.OnlySetValueOnInsert(HostedLoginTranslationTable, instanceEvent.CreationDate())),
|
||||
handler.NewCol(HostedLoginTranslationChangeDateCol, instanceEvent.CreationDate()),
|
||||
handler.NewCol(HostedLoginTranslationSequenceCol, instanceEvent.Sequence()),
|
||||
handler.NewCol(HostedLoginTranslationLocaleCol, instanceEvent.Language),
|
||||
handler.NewCol(HostedLoginTranslationFileCol, instanceEvent.Translation),
|
||||
handler.NewCol(HostedLoginTranslationEtagCol, hltp.computeEtag(instanceEvent.Translation)),
|
||||
},
|
||||
), nil
|
||||
default:
|
||||
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-AZshaa", "reduce.wrong.event.type %v", []eventstore.EventType{org.HostedLoginTranslationSet})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (hltp *hostedLoginTranslationProjection) computeEtag(translation map[string]any) string {
|
||||
hash := md5.Sum(fmt.Append(nil, translation))
|
||||
return hex.EncodeToString(hash[:])
|
||||
}
|
||||
@@ -86,6 +86,7 @@ var (
|
||||
UserSchemaProjection *handler.Handler
|
||||
WebKeyProjection *handler.Handler
|
||||
DebugEventsProjection *handler.Handler
|
||||
HostedLoginTranslationProjection *handler.Handler
|
||||
|
||||
ProjectGrantFields *handler.FieldHandler
|
||||
OrgDomainVerifiedFields *handler.FieldHandler
|
||||
@@ -179,6 +180,7 @@ func Create(ctx context.Context, sqlClient *database.DB, es handler.EventStore,
|
||||
UserSchemaProjection = newUserSchemaProjection(ctx, applyCustomConfig(projectionConfig, config.Customizations["user_schemas"]))
|
||||
WebKeyProjection = newWebKeyProjection(ctx, applyCustomConfig(projectionConfig, config.Customizations["web_keys"]))
|
||||
DebugEventsProjection = newDebugEventsProjection(ctx, applyCustomConfig(projectionConfig, config.Customizations["debug_events"]))
|
||||
HostedLoginTranslationProjection = newHostedLoginTranslationProjection(ctx, applyCustomConfig(projectionConfig, config.Customizations["hosted_login_translation"]))
|
||||
|
||||
ProjectGrantFields = newFillProjectGrantFields(applyCustomConfig(projectionConfig, config.Customizations[fieldsProjectGrant]))
|
||||
OrgDomainVerifiedFields = newFillOrgDomainVerifiedFields(applyCustomConfig(projectionConfig, config.Customizations[fieldsOrgDomainVerified]))
|
||||
@@ -357,5 +359,6 @@ func newProjectionsList() {
|
||||
UserSchemaProjection,
|
||||
WebKeyProjection,
|
||||
DebugEventsProjection,
|
||||
HostedLoginTranslationProjection,
|
||||
}
|
||||
}
|
||||
|
||||
1557
internal/query/v2-default.json
Normal file
1557
internal/query/v2-default.json
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user