fix: correct display name when adding an instance (#4930)

* fix: handling of default values inside add instance

* fix: remove release from 2.16.x branch

* chore(lint): show all issues

* refactor: instance converter

Co-authored-by: Silvan <silvan.reusser@gmail.com>
This commit is contained in:
Stefan Benz 2023-01-03 09:16:36 +00:00 committed by GitHub
parent 9d78fb66dc
commit b1d7433eba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 114 additions and 91 deletions

View File

@ -1,6 +1,9 @@
issues: issues:
new: true new: true
fix: false # Set to 0 to disable.
max-issues-per-linter: 0
# Set to 0 to disable.
max-same-issues: 0
run: run:
concurrency: 2 concurrency: 2
@ -50,8 +53,6 @@ linters:
- gosimple - gosimple
# Vet examines Go source code and reports suspicious constructs, such as Printf calls whose arguments do not align with the format string [fast: false, auto-fix: false] # Vet examines Go source code and reports suspicious constructs, such as Printf calls whose arguments do not align with the format string [fast: false, auto-fix: false]
- govet - govet
# In addition to fixing imports, goimports also formats your code in the same style as gofmt. [fast: true, auto-fix: true]
- goimports
# Detects when assignments to existing variables are not used [fast: true, auto-fix: false] # Detects when assignments to existing variables are not used [fast: true, auto-fix: false]
- ineffassign - ineffassign
# Finds commonly misspelled English words in comments [fast: true, auto-fix: true] # Finds commonly misspelled English words in comments [fast: true, auto-fix: true]
@ -157,6 +158,9 @@ linters:
# Checks is file header matches to pattern [fast: true, auto-fix: false] # Checks is file header matches to pattern [fast: true, auto-fix: false]
# ignored because we don't write licenses as headers # ignored because we don't write licenses as headers
- goheader - goheader
# In addition to fixing imports, goimports also formats your code in the same style as gofmt. [fast: true, auto-fix: true]
# ignored in favor of gci
- goimports
#deprecated]: Golint differs from gofmt. Gofmt reformats Go source code, whereas golint prints out style mistakes [fast: false, auto-fix: false] #deprecated]: Golint differs from gofmt. Gofmt reformats Go source code, whereas golint prints out style mistakes [fast: false, auto-fix: false]
# ignored in favor of goimports # ignored in favor of goimports
- golint - golint
@ -301,3 +305,4 @@ linters-settings:
- standard # Standard section: captures all standard packages. - standard # Standard section: captures all standard packages.
- default # Default section: contains all imports that could not be matched to another section type. - default # Default section: contains all imports that could not be matched to another section type.
- prefix(github.com/zitadel/zitadel) # Custom section: groups all imports with the specified Prefix. - prefix(github.com/zitadel/zitadel) # Custom section: groups all imports with the specified Prefix.
custom-order: true

View File

@ -18,169 +18,187 @@ import (
) )
func CreateInstancePbToSetupInstance(req *system_pb.CreateInstanceRequest, defaultInstance command.InstanceSetup, externalDomain string) *command.InstanceSetup { func CreateInstancePbToSetupInstance(req *system_pb.CreateInstanceRequest, defaultInstance command.InstanceSetup, externalDomain string) *command.InstanceSetup {
instance := defaultInstance
if req.InstanceName != "" { if req.InstanceName != "" {
defaultInstance.InstanceName = req.InstanceName instance.InstanceName = req.InstanceName
defaultInstance.Org.Name = req.InstanceName instance.Org.Name = req.InstanceName
} }
if req.CustomDomain != "" { if req.CustomDomain != "" {
defaultInstance.CustomDomain = req.CustomDomain instance.CustomDomain = req.CustomDomain
} }
if req.FirstOrgName != "" { if req.FirstOrgName != "" {
defaultInstance.Org.Name = req.FirstOrgName instance.Org.Name = req.FirstOrgName
} }
if user := req.GetMachine(); user != nil { if user := req.GetMachine(); user != nil {
defaultMachine := command.AddMachine{} defaultMachine := instance.Org.Machine
if defaultInstance.Org.Machine != nil { if defaultMachine == nil {
defaultMachine = *defaultInstance.Org.Machine defaultMachine = new(command.AddMachine)
} }
defaultInstance.Org.Machine = createInstancePbToAddMachine(user, defaultMachine) instance.Org.Machine = createInstancePbToAddMachine(user, *defaultMachine)
defaultInstance.Org.Human = nil instance.Org.Human = nil
} } else if user := req.GetHuman(); user != nil {
if user := req.GetHuman(); user != nil { defaultHuman := instance.Org.Human
defaultHuman := command.AddHuman{} if instance.Org.Human != nil {
if defaultInstance.Org.Human != nil { defaultHuman = new(command.AddHuman)
defaultHuman = *defaultInstance.Org.Human
} }
defaultInstance.Org.Human = createInstancePbToAddHuman(user, defaultHuman, defaultInstance.DomainPolicy.UserLoginMustBeDomain, defaultInstance.Org.Name, externalDomain) instance.Org.Human = createInstancePbToAddHuman(user, *defaultHuman, instance.DomainPolicy.UserLoginMustBeDomain, instance.Org.Name, externalDomain)
defaultInstance.Org.Machine = nil instance.Org.Machine = nil
} }
if lang := language.Make(req.DefaultLanguage); lang != language.Und { if lang := language.Make(req.DefaultLanguage); !lang.IsRoot() {
defaultInstance.DefaultLanguage = lang instance.DefaultLanguage = lang
} }
return &defaultInstance return &instance
} }
func createInstancePbToAddHuman(user *system_pb.CreateInstanceRequest_Human, defaultHuman command.AddHuman, userLoginMustBeDomain bool, org, externalDomain string) *command.AddHuman { func createInstancePbToAddHuman(req *system_pb.CreateInstanceRequest_Human, defaultHuman command.AddHuman, userLoginMustBeDomain bool, org, externalDomain string) *command.AddHuman {
if user.Email != nil { user := defaultHuman
defaultHuman.Email.Address = user.Email.Email if req.Email != nil {
defaultHuman.Email.Verified = user.Email.IsEmailVerified user.Email.Address = req.Email.Email
user.Email.Verified = req.Email.IsEmailVerified
} }
if user.Profile != nil { if req.Profile != nil {
if user.Profile.FirstName != "" { if req.Profile.FirstName != "" {
defaultHuman.FirstName = user.Profile.FirstName user.FirstName = req.Profile.FirstName
} }
if user.Profile.LastName != "" { if req.Profile.LastName != "" {
defaultHuman.LastName = user.Profile.LastName user.LastName = req.Profile.LastName
} }
if user.Profile.PreferredLanguage != "" { if req.Profile.PreferredLanguage != "" {
lang, err := language.Parse(user.Profile.PreferredLanguage) lang, err := language.Parse(req.Profile.PreferredLanguage)
if err == nil { if err == nil {
defaultHuman.PreferredLanguage = lang user.PreferredLanguage = lang
} }
} }
} }
// check if default username is email style or else append @<orgname>.<custom-domain> // check if default username is email style or else append @<orgname>.<custom-domain>
// this way we have the same value as before changing `UserLoginMustBeDomain` to false // this way we have the same value as before changing `UserLoginMustBeDomain` to false
if !userLoginMustBeDomain && !strings.Contains(defaultHuman.Username, "@") { if !userLoginMustBeDomain && !strings.Contains(user.Username, "@") {
defaultHuman.Username = defaultHuman.Username + "@" + domain.NewIAMDomainName(org, externalDomain) user.Username = user.Username + "@" + domain.NewIAMDomainName(org, externalDomain)
} }
if user.UserName != "" { if req.UserName != "" {
defaultHuman.Username = user.UserName user.Username = req.UserName
} }
if user.Password != nil { if req.Password != nil {
defaultHuman.Password = user.Password.Password user.Password = req.Password.Password
defaultHuman.PasswordChangeRequired = user.Password.PasswordChangeRequired user.PasswordChangeRequired = req.Password.PasswordChangeRequired
} }
return &defaultHuman return &user
} }
func createInstancePbToAddMachine(user *system_pb.CreateInstanceRequest_Machine, defaultMachine command.AddMachine) *command.AddMachine { func createInstancePbToAddMachine(req *system_pb.CreateInstanceRequest_Machine, defaultMachine command.AddMachine) (machine *command.AddMachine) {
machine := command.Machine{} machine = new(command.AddMachine)
if defaultMachine.Machine != nil { if defaultMachine.Machine != nil {
machine = *defaultMachine.Machine machineCopy := *defaultMachine.Machine
} machine.Machine = &machineCopy
if user.UserName != "" { } else {
machine.Username = user.UserName machine.Machine = new(command.Machine)
}
if user.Name != "" {
machine.Name = user.Name
}
defaultMachine.Machine = &machine
if defaultMachine.Pat != nil || user.PersonalAccessToken != nil {
pat := command.AddPat{}
if defaultMachine.Pat != nil {
pat = *defaultMachine.Pat
}
// scopes are currently static and can not be overwritten
pat.Scopes = []string{oidc.ScopeOpenID, z_oidc.ScopeUserMetaData, z_oidc.ScopeResourceOwner}
if user.PersonalAccessToken.ExpirationDate != nil {
pat.ExpirationDate = user.PersonalAccessToken.ExpirationDate.AsTime()
}
defaultMachine.Pat = &pat
} }
if defaultMachine.MachineKey != nil || user.MachineKey != nil { if req.UserName != "" {
machine.Machine.Username = req.UserName
}
if req.Name != "" {
machine.Machine.Name = req.Name
}
if defaultMachine.Pat != nil || req.PersonalAccessToken != nil {
pat := command.AddPat{
// Scopes are currently static and can not be overwritten
Scopes: []string{oidc.ScopeOpenID, z_oidc.ScopeUserMetaData, z_oidc.ScopeResourceOwner},
}
if !defaultMachine.Pat.ExpirationDate.IsZero() {
pat.ExpirationDate = defaultMachine.Pat.ExpirationDate
} else if req.PersonalAccessToken.ExpirationDate.IsValid() {
pat.ExpirationDate = req.PersonalAccessToken.ExpirationDate.AsTime()
}
machine.Pat = &pat
}
if defaultMachine.MachineKey != nil || req.MachineKey != nil {
machineKey := command.AddMachineKey{} machineKey := command.AddMachineKey{}
if defaultMachine.MachineKey != nil { if defaultMachine.MachineKey != nil {
machineKey = *defaultMachine.MachineKey machineKey = *defaultMachine.MachineKey
} }
if user.MachineKey != nil { if req.MachineKey != nil {
if user.MachineKey.Type != 0 { if req.MachineKey.Type != 0 {
machineKey.Type = authn.KeyTypeToDomain(user.MachineKey.Type) machineKey.Type = authn.KeyTypeToDomain(req.MachineKey.Type)
} }
if user.MachineKey.ExpirationDate != nil { if req.MachineKey.ExpirationDate.IsValid() {
machineKey.ExpirationDate = user.MachineKey.ExpirationDate.AsTime() machineKey.ExpirationDate = req.MachineKey.ExpirationDate.AsTime()
} }
} }
defaultMachine.MachineKey = &machineKey machine.MachineKey = &machineKey
} }
return &defaultMachine
return machine
} }
func AddInstancePbToSetupInstance(req *system_pb.AddInstanceRequest, defaultInstance command.InstanceSetup, externalDomain string) *command.InstanceSetup { func AddInstancePbToSetupInstance(req *system_pb.AddInstanceRequest, defaultInstance command.InstanceSetup, externalDomain string) *command.InstanceSetup {
instance := defaultInstance
if req.InstanceName != "" { if req.InstanceName != "" {
defaultInstance.InstanceName = req.InstanceName instance.InstanceName = req.InstanceName
defaultInstance.Org.Name = req.InstanceName instance.Org.Name = req.InstanceName
} }
if req.CustomDomain != "" { if req.CustomDomain != "" {
defaultInstance.CustomDomain = req.CustomDomain instance.CustomDomain = req.CustomDomain
} }
if req.FirstOrgName != "" { if req.FirstOrgName != "" {
defaultInstance.Org.Name = req.FirstOrgName instance.Org.Name = req.FirstOrgName
}
if defaultInstance.Org.Human != nil {
// used to not overwrite the default human later
humanCopy := *defaultInstance.Org.Human
instance.Org.Human = &humanCopy
} else {
instance.Org.Human = new(command.AddHuman)
} }
if req.OwnerEmail.Email != "" { if req.OwnerEmail.Email != "" {
defaultInstance.Org.Human.Email.Address = req.OwnerEmail.Email instance.Org.Human.Email.Address = req.OwnerEmail.Email
defaultInstance.Org.Human.Email.Verified = req.OwnerEmail.IsEmailVerified instance.Org.Human.Email.Verified = req.OwnerEmail.IsEmailVerified
} }
if req.OwnerProfile != nil { if req.OwnerProfile != nil {
if req.OwnerProfile.FirstName != "" { if req.OwnerProfile.FirstName != "" {
defaultInstance.Org.Human.FirstName = req.OwnerProfile.FirstName instance.Org.Human.FirstName = req.OwnerProfile.FirstName
} }
if req.OwnerProfile.LastName != "" { if req.OwnerProfile.LastName != "" {
defaultInstance.Org.Human.LastName = req.OwnerProfile.LastName instance.Org.Human.LastName = req.OwnerProfile.LastName
} }
if req.OwnerProfile.PreferredLanguage != "" { if req.OwnerProfile.PreferredLanguage != "" {
lang, err := language.Parse(req.OwnerProfile.PreferredLanguage) lang, err := language.Parse(req.OwnerProfile.PreferredLanguage)
if err == nil { if err == nil {
defaultInstance.Org.Human.PreferredLanguage = lang instance.Org.Human.PreferredLanguage = lang
} }
} }
} }
if req.OwnerUserName != "" {
instance.Org.Human.Username = req.OwnerUserName
}
// check if default username is email style or else append @<orgname>.<custom-domain> // check if default username is email style or else append @<orgname>.<custom-domain>
// this way we have the same value as before changing `UserLoginMustBeDomain` to false // this way we have the same value as before changing `UserLoginMustBeDomain` to false
if !defaultInstance.DomainPolicy.UserLoginMustBeDomain && !strings.Contains(defaultInstance.Org.Human.Username, "@") { if !instance.DomainPolicy.UserLoginMustBeDomain && !strings.Contains(instance.Org.Human.Username, "@") {
defaultInstance.Org.Human.Username = defaultInstance.Org.Human.Username + "@" + domain.NewIAMDomainName(defaultInstance.Org.Name, externalDomain) instance.Org.Human.Username = instance.Org.Human.Username + "@" + domain.NewIAMDomainName(instance.Org.Name, externalDomain)
}
if req.OwnerUserName != "" {
defaultInstance.Org.Human.Username = req.OwnerUserName
} }
if req.OwnerPassword != nil { if req.OwnerPassword != nil {
defaultInstance.Org.Human.Password = req.OwnerPassword.Password instance.Org.Human.Password = req.OwnerPassword.Password
defaultInstance.Org.Human.PasswordChangeRequired = req.OwnerPassword.PasswordChangeRequired instance.Org.Human.PasswordChangeRequired = req.OwnerPassword.PasswordChangeRequired
} }
if lang := language.Make(req.DefaultLanguage); lang != language.Und { if lang := language.Make(req.DefaultLanguage); lang != language.Und {
defaultInstance.DefaultLanguage = lang instance.DefaultLanguage = lang
} }
return &defaultInstance return &instance
} }
func ListInstancesRequestToModel(req *system_pb.ListInstancesRequest) (*query.InstanceSearchQueries, error) { func ListInstancesRequestToModel(req *system_pb.ListInstancesRequest) (*query.InstanceSearchQueries, error) {
offset, limit, asc := object.ListQueryToModel(req.Query) offset, limit, asc := object.ListQueryToModel(req.Query)
queries, err := instance_grpc.InstanceQueriesToModel(req.Queries) queries, err := instance_grpc.InstanceQueriesToModel(req.Queries)