mirror of
https://github.com/fleetdm/fleet
synced 2026-05-23 17:08:53 +00:00
Added code to trim whitespace and trim trailing slash from input server url (#1442)
This commit is contained in:
parent
55a1e3ec63
commit
71e66e6d16
3 changed files with 28 additions and 3 deletions
|
|
@ -3,6 +3,7 @@ package service
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/kolide/kolide/server/contexts/viewer"
|
||||
"github.com/kolide/kolide/server/kolide"
|
||||
|
|
@ -95,6 +96,10 @@ func (svc service) ModifyAppConfig(ctx context.Context, p kolide.AppConfigPayloa
|
|||
return config, nil
|
||||
}
|
||||
|
||||
func cleanupURL(url string) string {
|
||||
return strings.TrimRight(strings.Trim(url, " \t\n"), "/")
|
||||
}
|
||||
|
||||
func appConfigFromAppConfigPayload(p kolide.AppConfigPayload, config kolide.AppConfig) *kolide.AppConfig {
|
||||
if p.OrgInfo != nil && p.OrgInfo.OrgLogoURL != nil {
|
||||
config.OrgLogoURL = *p.OrgInfo.OrgLogoURL
|
||||
|
|
@ -103,7 +108,7 @@ func appConfigFromAppConfigPayload(p kolide.AppConfigPayload, config kolide.AppC
|
|||
config.OrgName = *p.OrgInfo.OrgName
|
||||
}
|
||||
if p.ServerSettings != nil && p.ServerSettings.KolideServerURL != nil {
|
||||
config.KolideServerURL = *p.ServerSettings.KolideServerURL
|
||||
config.KolideServerURL = cleanupURL(*p.ServerSettings.KolideServerURL)
|
||||
}
|
||||
if p.ServerSettings != nil && p.ServerSettings.EnrollSecret != nil {
|
||||
config.EnrollSecret = *p.ServerSettings.EnrollSecret
|
||||
|
|
|
|||
|
|
@ -11,6 +11,26 @@ import (
|
|||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestCleanupURL(t *testing.T) {
|
||||
tests := []struct {
|
||||
in string
|
||||
expected string
|
||||
name string
|
||||
}{
|
||||
{" http://foo.bar.com ", "http://foo.bar.com", "leading and trailing whitespace"},
|
||||
{"\n http://foo.com \t", "http://foo.com", "whitespace"},
|
||||
{"http://foo.com", "http://foo.com", "noop"},
|
||||
{"http://foo.com/", "http://foo.com", "trailing slash"},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(tt *testing.T) {
|
||||
actual := cleanupURL(test.in)
|
||||
assert.Equal(tt, test.expected, actual)
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestCreateAppConfig(t *testing.T) {
|
||||
ds, err := inmem.New(config.TestConfig())
|
||||
require.Nil(t, err)
|
||||
|
|
@ -42,6 +62,6 @@ func TestCreateAppConfig(t *testing.T) {
|
|||
assert.NotEmpty(t, result.ID)
|
||||
assert.Equal(t, *payload.OrgInfo.OrgLogoURL, result.OrgLogoURL)
|
||||
assert.Equal(t, *payload.OrgInfo.OrgName, result.OrgName)
|
||||
assert.Equal(t, *payload.ServerSettings.KolideServerURL, result.KolideServerURL)
|
||||
assert.Equal(t, "https://acme.co:8080", result.KolideServerURL)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ func (mw validationMiddleware) NewAppConfig(ctx context.Context, payload kolide.
|
|||
if payload.ServerSettings == nil {
|
||||
invalid.Append("kolide_server_url", "missing required argument")
|
||||
} else {
|
||||
serverURLString = *payload.ServerSettings.KolideServerURL
|
||||
serverURLString = cleanupURL(*payload.ServerSettings.KolideServerURL)
|
||||
}
|
||||
if err := validateKolideServerURL(serverURLString); err != nil {
|
||||
invalid.Append("kolide_server_url", err.Error())
|
||||
|
|
|
|||
Loading…
Reference in a new issue