feat: add random string to generated domain (#3634)

This commit is contained in:
Fabi 2022-05-16 11:26:24 +02:00 committed by GitHub
parent 4fcf03c9c8
commit 48fbf1a28e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 11 deletions

View File

@ -325,9 +325,11 @@ func (c *Commands) SetUpInstance(ctx context.Context, setup *InstanceSetup) (str
AddOIDCAppCommand(console, nil), AddOIDCAppCommand(console, nil),
SetIAMConsoleID(instanceAgg, &console.ClientID, &setup.zitadel.consoleAppID), SetIAMConsoleID(instanceAgg, &console.ClientID, &setup.zitadel.consoleAppID),
) )
validations = append(validations, addGenerateddDomain, err := c.addGeneratedInstanceDomain(ctx, instanceAgg, setup.InstanceName)
c.addGeneratedInstanceDomain(ctx, instanceAgg, setup.InstanceName)..., if err != nil {
) return "", nil, err
}
validations = append(validations, addGenerateddDomain...)
if setup.CustomDomain != "" { if setup.CustomDomain != "" {
validations = append(validations, validations = append(validations,
c.addInstanceDomain(instanceAgg, setup.CustomDomain, false), c.addInstanceDomain(instanceAgg, setup.CustomDomain, false),

View File

@ -68,12 +68,15 @@ func (c *Commands) RemoveInstanceDomain(ctx context.Context, instanceDomain stri
}, nil }, nil
} }
func (c *Commands) addGeneratedInstanceDomain(ctx context.Context, a *instance.Aggregate, instanceName string) []preparation.Validation { func (c *Commands) addGeneratedInstanceDomain(ctx context.Context, a *instance.Aggregate, instanceName string) ([]preparation.Validation, error) {
domain := domain.NewGeneratedInstanceDomain(instanceName, authz.GetInstance(ctx).RequestedDomain()) domain, err := domain.NewGeneratedInstanceDomain(instanceName, authz.GetInstance(ctx).RequestedDomain())
if err != nil {
return nil, err
}
return []preparation.Validation{ return []preparation.Validation{
c.addInstanceDomain(a, domain, true), c.addInstanceDomain(a, domain, true),
setPrimaryInstanceDomain(a, domain), setPrimaryInstanceDomain(a, domain),
} }, nil
} }
func (c *Commands) addInstanceDomain(a *instance.Aggregate, instanceDomain string, generated bool) preparation.Validation { func (c *Commands) addInstanceDomain(a *instance.Aggregate, instanceDomain string, generated bool) preparation.Validation {

View File

@ -102,7 +102,7 @@ func newGenerator(config GeneratorConfig) generator {
} }
func NewCode(g Generator) (*CryptoValue, string, error) { func NewCode(g Generator) (*CryptoValue, string, error) {
code, err := generateRandomString(g.Length(), g.Runes()) code, err := GenerateRandomString(g.Length(), g.Runes())
if err != nil { if err != nil {
return nil, "", err return nil, "", err
} }
@ -133,7 +133,7 @@ func VerifyCode(creationDate time.Time, expiry time.Duration, cryptoCode *Crypto
return errors.ThrowInvalidArgument(nil, "CODE-fW2gNa", "Errors.User.Code.GeneratorAlgNotSupported") return errors.ThrowInvalidArgument(nil, "CODE-fW2gNa", "Errors.User.Code.GeneratorAlgNotSupported")
} }
func generateRandomString(length uint, chars []rune) (string, error) { func GenerateRandomString(length uint, chars []rune) (string, error) {
if length == 0 { if length == 0 {
return "", nil return "", nil
} }

View File

@ -2,6 +2,12 @@ package domain
import ( import (
"strings" "strings"
"github.com/zitadel/zitadel/internal/crypto"
)
var (
domainRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
) )
type InstanceDomainState int32 type InstanceDomainState int32
@ -22,8 +28,11 @@ func (f InstanceDomainState) Exists() bool {
return f == InstanceDomainStateActive return f == InstanceDomainStateActive
} }
func NewGeneratedInstanceDomain(instanceName, iamDomain string) string { func NewGeneratedInstanceDomain(instanceName, iamDomain string) (string, error) {
//TODO: Add random number/string to be unique randomString, err := crypto.GenerateRandomString(6, domainRunes)
if err != nil {
return "", err
}
instanceName = strings.TrimSpace(instanceName) instanceName = strings.TrimSpace(instanceName)
return strings.ToLower(strings.ReplaceAll(instanceName, " ", "-") + "." + iamDomain) return strings.ToLower(strings.ReplaceAll(instanceName, " ", "-") + "-" + randomString + "." + iamDomain), nil
} }