2016-11-04 20:44:38 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2021-07-30 15:45:49 +00:00
|
|
|
"context"
|
|
|
|
"runtime"
|
2016-11-04 20:44:38 +00:00
|
|
|
"testing"
|
|
|
|
|
2021-08-20 15:27:41 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/config"
|
|
|
|
|
2021-06-26 04:46:51 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/mock"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/test"
|
2016-11-04 20:44:38 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2017-03-22 19:40:01 +00:00
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-11-09 17:19:07 +00:00
|
|
|
func TestCreateAppConfig(t *testing.T) {
|
2020-05-29 16:12:39 +00:00
|
|
|
ds := new(mock.Store)
|
2022-11-15 14:08:05 +00:00
|
|
|
svc, ctx := newTestService(t, ds, nil, nil)
|
2020-05-29 16:12:39 +00:00
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
ds.AppConfigFunc = func(ctx context.Context) (*fleet.AppConfig, error) {
|
2021-06-06 22:07:29 +00:00
|
|
|
return &fleet.AppConfig{}, nil
|
2020-05-29 16:12:39 +00:00
|
|
|
}
|
|
|
|
|
2016-11-09 17:19:07 +00:00
|
|
|
var appConfigTests = []struct {
|
2021-08-20 15:27:41 +00:00
|
|
|
configPayload fleet.AppConfig
|
2016-11-04 20:44:38 +00:00
|
|
|
}{
|
|
|
|
{
|
2021-08-20 15:27:41 +00:00
|
|
|
configPayload: fleet.AppConfig{
|
|
|
|
OrgInfo: fleet.OrgInfo{
|
|
|
|
OrgLogoURL: "acme.co/images/logo.png",
|
|
|
|
OrgName: "Acme",
|
2016-11-04 20:44:38 +00:00
|
|
|
},
|
2021-08-20 15:27:41 +00:00
|
|
|
ServerSettings: fleet.ServerSettings{
|
|
|
|
ServerURL: "https://acme.co:8080/",
|
|
|
|
LiveQueryDisabled: true,
|
2016-11-04 20:44:38 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-11-09 17:19:07 +00:00
|
|
|
for _, tt := range appConfigTests {
|
2021-06-06 22:07:29 +00:00
|
|
|
var result *fleet.AppConfig
|
2021-09-14 12:11:07 +00:00
|
|
|
ds.NewAppConfigFunc = func(ctx context.Context, config *fleet.AppConfig) (*fleet.AppConfig, error) {
|
2020-05-29 16:12:39 +00:00
|
|
|
result = config
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
var gotSecrets []*fleet.EnrollSecret
|
2021-09-14 12:11:07 +00:00
|
|
|
ds.ApplyEnrollSecretsFunc = func(ctx context.Context, teamID *uint, secrets []*fleet.EnrollSecret) error {
|
2021-05-31 16:02:05 +00:00
|
|
|
gotSecrets = secrets
|
2020-05-29 16:12:39 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-15 14:08:05 +00:00
|
|
|
ctx = test.UserContext(ctx, test.UserAdmin)
|
2021-06-03 23:24:15 +00:00
|
|
|
_, err := svc.NewAppConfig(ctx, tt.configPayload)
|
2016-11-04 20:44:38 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
|
2016-11-09 17:19:07 +00:00
|
|
|
payload := tt.configPayload
|
2021-08-20 15:27:41 +00:00
|
|
|
assert.Equal(t, payload.OrgInfo.OrgLogoURL, result.OrgInfo.OrgLogoURL)
|
|
|
|
assert.Equal(t, payload.OrgInfo.OrgName, result.OrgInfo.OrgName)
|
|
|
|
assert.Equal(t, "https://acme.co:8080/", result.ServerSettings.ServerURL)
|
|
|
|
assert.Equal(t, payload.ServerSettings.LiveQueryDisabled, result.ServerSettings.LiveQueryDisabled)
|
2020-05-29 16:12:39 +00:00
|
|
|
|
|
|
|
// Ensure enroll secret was set
|
2021-05-31 16:02:05 +00:00
|
|
|
require.NotNil(t, gotSecrets)
|
|
|
|
require.Len(t, gotSecrets, 1)
|
|
|
|
assert.Len(t, gotSecrets[0].Secret, 32)
|
2016-11-04 20:44:38 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-19 22:49:53 +00:00
|
|
|
|
|
|
|
func TestEmptyEnrollSecret(t *testing.T) {
|
|
|
|
ds := new(mock.Store)
|
2022-11-15 14:08:05 +00:00
|
|
|
svc, ctx := newTestService(t, ds, nil, nil)
|
2021-01-19 22:49:53 +00:00
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
ds.ApplyEnrollSecretsFunc = func(ctx context.Context, teamID *uint, secrets []*fleet.EnrollSecret) error {
|
2021-01-19 22:49:53 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-09-14 12:11:07 +00:00
|
|
|
ds.AppConfigFunc = func(ctx context.Context) (*fleet.AppConfig, error) {
|
2021-06-06 22:07:29 +00:00
|
|
|
return &fleet.AppConfig{}, nil
|
2021-01-19 22:49:53 +00:00
|
|
|
}
|
|
|
|
|
2021-06-03 23:24:15 +00:00
|
|
|
err := svc.ApplyEnrollSecretSpec(
|
2022-11-15 14:08:05 +00:00
|
|
|
test.UserContext(ctx, test.UserAdmin),
|
2021-06-06 22:07:29 +00:00
|
|
|
&fleet.EnrollSecretSpec{
|
|
|
|
Secrets: []*fleet.EnrollSecret{{}},
|
2021-01-19 22:49:53 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
err = svc.ApplyEnrollSecretSpec(
|
2022-11-15 14:08:05 +00:00
|
|
|
test.UserContext(ctx, test.UserAdmin),
|
2021-06-06 22:07:29 +00:00
|
|
|
&fleet.EnrollSecretSpec{Secrets: []*fleet.EnrollSecret{{Secret: ""}}},
|
2021-01-19 22:49:53 +00:00
|
|
|
)
|
2021-05-31 16:02:05 +00:00
|
|
|
require.Error(t, err, "empty secret should be disallowed")
|
2021-01-19 22:49:53 +00:00
|
|
|
|
|
|
|
err = svc.ApplyEnrollSecretSpec(
|
2022-11-15 14:08:05 +00:00
|
|
|
test.UserContext(ctx, test.UserAdmin),
|
2021-06-06 22:07:29 +00:00
|
|
|
&fleet.EnrollSecretSpec{
|
|
|
|
Secrets: []*fleet.EnrollSecret{{Secret: "foo"}},
|
2021-01-19 22:49:53 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
2021-07-30 15:45:49 +00:00
|
|
|
|
2022-07-12 22:12:10 +00:00
|
|
|
func TestNewAppConfigWithGlobalEnrollConfig(t *testing.T) {
|
|
|
|
ds := new(mock.Store)
|
|
|
|
cfg := config.TestConfig()
|
|
|
|
cfg.Packaging.GlobalEnrollSecret = "xyz"
|
2022-11-15 14:08:05 +00:00
|
|
|
svc, ctx := newTestServiceWithConfig(t, ds, cfg, nil, nil)
|
2022-07-12 22:12:10 +00:00
|
|
|
|
|
|
|
ds.NewAppConfigFunc = func(ctx context.Context, config *fleet.AppConfig) (*fleet.AppConfig, error) {
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var gotSecrets []*fleet.EnrollSecret
|
|
|
|
ds.ApplyEnrollSecretsFunc = func(ctx context.Context, teamID *uint, secrets []*fleet.EnrollSecret) error {
|
|
|
|
gotSecrets = secrets
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-15 14:08:05 +00:00
|
|
|
ctx = test.UserContext(ctx, test.UserAdmin)
|
2022-07-12 22:12:10 +00:00
|
|
|
_, err := svc.NewAppConfig(ctx, fleet.AppConfig{ServerSettings: fleet.ServerSettings{ServerURL: "https://acme.co"}})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, gotSecrets)
|
|
|
|
require.Len(t, gotSecrets, 1)
|
|
|
|
require.Equal(t, gotSecrets[0].Secret, "xyz")
|
|
|
|
}
|
|
|
|
|
2021-07-30 15:45:49 +00:00
|
|
|
func TestService_LoggingConfig(t *testing.T) {
|
|
|
|
logFile := "/dev/null"
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
logFile = "NUL"
|
|
|
|
}
|
|
|
|
|
|
|
|
fileSystemConfig := fleet.FilesystemConfig{FilesystemConfig: config.FilesystemConfig{
|
|
|
|
StatusLogFile: logFile,
|
|
|
|
ResultLogFile: logFile,
|
|
|
|
EnableLogRotation: false,
|
|
|
|
EnableLogCompression: false,
|
|
|
|
}}
|
|
|
|
|
|
|
|
firehoseConfig := fleet.FirehoseConfig{
|
|
|
|
Region: testFirehosePluginConfig().Firehose.Region,
|
|
|
|
StatusStream: testFirehosePluginConfig().Firehose.StatusStream,
|
|
|
|
ResultStream: testFirehosePluginConfig().Firehose.ResultStream,
|
|
|
|
}
|
|
|
|
|
|
|
|
kinesisConfig := fleet.KinesisConfig{
|
|
|
|
Region: testKinesisPluginConfig().Kinesis.Region,
|
|
|
|
StatusStream: testKinesisPluginConfig().Kinesis.StatusStream,
|
|
|
|
ResultStream: testKinesisPluginConfig().Kinesis.ResultStream,
|
|
|
|
}
|
|
|
|
|
|
|
|
lambdaConfig := fleet.LambdaConfig{
|
|
|
|
Region: testLambdaPluginConfig().Lambda.Region,
|
|
|
|
StatusFunction: testLambdaPluginConfig().Lambda.StatusFunction,
|
|
|
|
ResultFunction: testLambdaPluginConfig().Lambda.ResultFunction,
|
|
|
|
}
|
|
|
|
|
|
|
|
pubsubConfig := fleet.PubSubConfig{
|
|
|
|
PubSubConfig: config.PubSubConfig{
|
|
|
|
Project: testPubSubPluginConfig().PubSub.Project,
|
|
|
|
StatusTopic: testPubSubPluginConfig().PubSub.StatusTopic,
|
|
|
|
ResultTopic: testPubSubPluginConfig().PubSub.ResultTopic,
|
|
|
|
AddAttributes: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
type fields struct {
|
|
|
|
config config.FleetConfig
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
ctx context.Context
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
want *fleet.Logging
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "test default test config (aka filesystem)",
|
|
|
|
fields: fields{config: config.TestConfig()},
|
2022-11-15 14:08:05 +00:00
|
|
|
args: args{ctx: test.UserContext(context.Background(), test.UserAdmin)},
|
2021-07-30 15:45:49 +00:00
|
|
|
want: &fleet.Logging{
|
|
|
|
Debug: true,
|
|
|
|
Json: false,
|
|
|
|
Result: fleet.LoggingPlugin{
|
|
|
|
Plugin: "filesystem",
|
|
|
|
Config: fileSystemConfig,
|
|
|
|
},
|
|
|
|
Status: fleet.LoggingPlugin{
|
|
|
|
Plugin: "filesystem",
|
|
|
|
Config: fileSystemConfig,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test firehose config",
|
|
|
|
fields: fields{config: testFirehosePluginConfig()},
|
2022-11-15 14:08:05 +00:00
|
|
|
args: args{ctx: test.UserContext(context.Background(), test.UserAdmin)},
|
2021-07-30 15:45:49 +00:00
|
|
|
want: &fleet.Logging{
|
|
|
|
Debug: true,
|
|
|
|
Json: false,
|
|
|
|
Result: fleet.LoggingPlugin{
|
|
|
|
Plugin: "firehose",
|
|
|
|
Config: firehoseConfig,
|
|
|
|
},
|
|
|
|
Status: fleet.LoggingPlugin{
|
|
|
|
Plugin: "firehose",
|
|
|
|
Config: firehoseConfig,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test kinesis config",
|
|
|
|
fields: fields{config: testKinesisPluginConfig()},
|
2022-11-15 14:08:05 +00:00
|
|
|
args: args{ctx: test.UserContext(context.Background(), test.UserAdmin)},
|
2021-07-30 15:45:49 +00:00
|
|
|
want: &fleet.Logging{
|
|
|
|
Debug: true,
|
|
|
|
Json: false,
|
|
|
|
Result: fleet.LoggingPlugin{
|
|
|
|
Plugin: "kinesis",
|
|
|
|
Config: kinesisConfig,
|
|
|
|
},
|
|
|
|
Status: fleet.LoggingPlugin{
|
|
|
|
Plugin: "kinesis",
|
|
|
|
Config: kinesisConfig,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test lambda config",
|
|
|
|
fields: fields{config: testLambdaPluginConfig()},
|
2022-11-15 14:08:05 +00:00
|
|
|
args: args{ctx: test.UserContext(context.Background(), test.UserAdmin)},
|
2021-07-30 15:45:49 +00:00
|
|
|
want: &fleet.Logging{
|
|
|
|
Debug: true,
|
|
|
|
Json: false,
|
|
|
|
Result: fleet.LoggingPlugin{
|
|
|
|
Plugin: "lambda",
|
|
|
|
Config: lambdaConfig,
|
|
|
|
},
|
|
|
|
Status: fleet.LoggingPlugin{
|
|
|
|
Plugin: "lambda",
|
|
|
|
Config: lambdaConfig,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test pubsub config",
|
|
|
|
fields: fields{config: testPubSubPluginConfig()},
|
2022-11-15 14:08:05 +00:00
|
|
|
args: args{ctx: test.UserContext(context.Background(), test.UserAdmin)},
|
2021-07-30 15:45:49 +00:00
|
|
|
want: &fleet.Logging{
|
|
|
|
Debug: true,
|
|
|
|
Json: false,
|
|
|
|
Result: fleet.LoggingPlugin{
|
|
|
|
Plugin: "pubsub",
|
|
|
|
Config: pubsubConfig,
|
|
|
|
},
|
|
|
|
Status: fleet.LoggingPlugin{
|
|
|
|
Plugin: "pubsub",
|
|
|
|
Config: pubsubConfig,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "test stdout config",
|
|
|
|
fields: fields{config: testStdoutPluginConfig()},
|
2022-11-15 14:08:05 +00:00
|
|
|
args: args{ctx: test.UserContext(context.Background(), test.UserAdmin)},
|
2021-07-30 15:45:49 +00:00
|
|
|
want: &fleet.Logging{
|
|
|
|
Debug: true,
|
|
|
|
Json: false,
|
|
|
|
Result: fleet.LoggingPlugin{
|
|
|
|
Plugin: "stdout",
|
|
|
|
Config: nil,
|
|
|
|
},
|
|
|
|
Status: fleet.LoggingPlugin{
|
|
|
|
Plugin: "stdout",
|
|
|
|
Config: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-03-11 15:51:12 +00:00
|
|
|
name: "test unrecognized config",
|
|
|
|
fields: fields{config: testUnrecognizedPluginConfig()},
|
2022-11-15 14:08:05 +00:00
|
|
|
args: args{ctx: test.UserContext(context.Background(), test.UserAdmin)},
|
2021-07-30 15:45:49 +00:00
|
|
|
wantErr: true,
|
2021-08-20 15:27:41 +00:00
|
|
|
want: nil,
|
2021-07-30 15:45:49 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
t.Parallel()
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
ds := new(mock.Store)
|
2022-11-15 14:08:05 +00:00
|
|
|
svc, _ := newTestServiceWithConfig(t, ds, tt.fields.config, nil, nil)
|
2021-07-30 15:45:49 +00:00
|
|
|
got, err := svc.LoggingConfig(tt.args.ctx)
|
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("LoggingConfig() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !assert.Equal(t, tt.want, got) {
|
|
|
|
t.Errorf("LoggingConfig() got = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-08-20 15:27:41 +00:00
|
|
|
|
|
|
|
func TestModifyAppConfigPatches(t *testing.T) {
|
|
|
|
ds := new(mock.Store)
|
2022-11-15 14:08:05 +00:00
|
|
|
svc, ctx := newTestService(t, ds, nil, nil)
|
2021-08-20 15:27:41 +00:00
|
|
|
|
2022-09-19 17:53:44 +00:00
|
|
|
storedConfig := &fleet.AppConfig{OrgInfo: fleet.OrgInfo{OrgName: "FleetTest"}, ServerSettings: fleet.ServerSettings{ServerURL: "https://example.org"}}
|
2021-08-20 15:27:41 +00:00
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
ds.AppConfigFunc = func(ctx context.Context) (*fleet.AppConfig, error) {
|
2021-08-20 15:27:41 +00:00
|
|
|
return storedConfig, nil
|
|
|
|
}
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
ds.SaveAppConfigFunc = func(ctx context.Context, info *fleet.AppConfig) error {
|
2021-08-20 15:27:41 +00:00
|
|
|
storedConfig = info
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
configJSON := []byte(`{"org_info": { "org_name": "Acme", "org_logo_url": "somelogo.jpg" }}`)
|
|
|
|
|
2022-11-15 14:08:05 +00:00
|
|
|
ctx = test.UserContext(ctx, test.UserAdmin)
|
2022-09-19 17:53:44 +00:00
|
|
|
_, err := svc.ModifyAppConfig(ctx, configJSON, fleet.ApplySpecOptions{})
|
2021-08-20 15:27:41 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, "Acme", storedConfig.OrgInfo.OrgName)
|
|
|
|
|
|
|
|
configJSON = []byte(`{"server_settings": { "server_url": "http://someurl" }}`)
|
|
|
|
|
2022-09-19 17:53:44 +00:00
|
|
|
_, err = svc.ModifyAppConfig(ctx, configJSON, fleet.ApplySpecOptions{})
|
2021-08-20 15:27:41 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, "Acme", storedConfig.OrgInfo.OrgName)
|
|
|
|
assert.Equal(t, "http://someurl", storedConfig.ServerSettings.ServerURL)
|
|
|
|
}
|