mirror of
https://github.com/netbirdio/netbird.git
synced 2026-09-24 15:49:06 +02:00
Add config name to ConfigInput
This commit is contained in:
@@ -104,7 +104,11 @@ type ConfigInput struct {
|
||||
|
||||
LocalMetricsEnabled *bool
|
||||
LocalMetricsAddress *string
|
||||
Owner *ipcauth.Identity
|
||||
|
||||
// Name is the profile's display name, independent of the on-disk
|
||||
// filename. Empty leaves the stored name unchanged.
|
||||
Name string
|
||||
Owner *ipcauth.Identity
|
||||
}
|
||||
|
||||
// Config Configuration type
|
||||
@@ -323,8 +327,12 @@ func createNewConfig(input ConfigInput) (*Config, error) {
|
||||
}
|
||||
|
||||
func (config *Config) apply(input ConfigInput) (updated bool, err error) {
|
||||
if config.Name != "" {
|
||||
sanitized, err := sanitizeDisplayName(config.Name)
|
||||
name := config.Name
|
||||
if input.Name != "" {
|
||||
name = input.Name
|
||||
}
|
||||
if name != "" {
|
||||
sanitized, err := sanitizeDisplayName(name)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("invalid profile name: %w", err)
|
||||
}
|
||||
|
||||
@@ -413,11 +413,10 @@ func (s *ServiceManager) AddProfile(displayName string, callerId *ipcauth.Identi
|
||||
}
|
||||
|
||||
profPath := filepath.Join(configDir, id.String()+".json")
|
||||
cfg, err := createNewConfig(ConfigInput{ConfigPath: profPath, Owner: callerId})
|
||||
cfg, err := createNewConfig(ConfigInput{ConfigPath: profPath, Owner: callerId, Name: displayName})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create new config: %w", err)
|
||||
}
|
||||
cfg.Name = displayName
|
||||
|
||||
if err := util.WriteJsonWithRestrictedPermission(context.Background(), profPath, cfg); err != nil {
|
||||
return nil, fmt.Errorf("failed to write profile config: %w", err)
|
||||
|
||||
@@ -191,6 +191,21 @@ func TestAddProfile_AllowsDuplicateWithFlag(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestAddProfile_PersistsName(t *testing.T) {
|
||||
// The returned Profile carries the name regardless, so this reads the
|
||||
// file back: the name has to reach the config apply pass, which is what
|
||||
// the owner log line and every later reader see.
|
||||
withTestSM(t, func(sm *ServiceManager, userID ipcauth.Identity) {
|
||||
prof, err := sm.AddProfile("My Work Account", &userID)
|
||||
require.NoError(t, err)
|
||||
|
||||
cfg, err := ReadConfig(prof.Path)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "My Work Account", cfg.Name, "stored config should carry the display name")
|
||||
require.Len(t, cfg.Owners, 1, "profile should record its creator as owner")
|
||||
})
|
||||
}
|
||||
|
||||
func TestAddProfile_RejectsInvalidNames(t *testing.T) {
|
||||
withTestSM(t, func(sm *ServiceManager, userID ipcauth.Identity) {
|
||||
cases := []string{
|
||||
|
||||
Reference in New Issue
Block a user