2021-07-16 18:28:13 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-09-14 12:11:07 +00:00
|
|
|
"context"
|
2021-07-19 19:48:49 +00:00
|
|
|
"database/sql"
|
|
|
|
"encoding/json"
|
2021-07-16 18:28:13 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/ptr"
|
2021-07-19 19:48:49 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/service"
|
2021-07-16 18:28:13 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
var userRoleSpecList = []*fleet.User{
|
2021-09-14 12:11:07 +00:00
|
|
|
{
|
2021-07-16 18:28:13 +00:00
|
|
|
UpdateCreateTimestamps: fleet.UpdateCreateTimestamps{
|
|
|
|
CreateTimestamp: fleet.CreateTimestamp{CreatedAt: time.Now()},
|
|
|
|
UpdateTimestamp: fleet.UpdateTimestamp{UpdatedAt: time.Now()},
|
|
|
|
},
|
|
|
|
ID: 42,
|
|
|
|
Name: "Test Name admin1@example.com",
|
|
|
|
Email: "admin1@example.com",
|
|
|
|
GlobalRole: ptr.String(fleet.RoleAdmin),
|
|
|
|
},
|
2021-09-14 12:11:07 +00:00
|
|
|
{
|
2021-07-16 18:28:13 +00:00
|
|
|
UpdateCreateTimestamps: fleet.UpdateCreateTimestamps{
|
|
|
|
CreateTimestamp: fleet.CreateTimestamp{CreatedAt: time.Now()},
|
|
|
|
UpdateTimestamp: fleet.UpdateTimestamp{UpdatedAt: time.Now()},
|
|
|
|
},
|
|
|
|
ID: 23,
|
|
|
|
Name: "Test Name2 admin2@example.com",
|
|
|
|
Email: "admin2@example.com",
|
|
|
|
GlobalRole: nil,
|
|
|
|
Teams: []fleet.UserTeam{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestApplyUserRoles(t *testing.T) {
|
2021-09-15 19:27:53 +00:00
|
|
|
_, ds := runServerWithMockedDS(t)
|
2021-07-16 18:28:13 +00:00
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
ds.ListUsersFunc = func(ctx context.Context, opt fleet.UserListOptions) ([]*fleet.User, error) {
|
2021-07-16 18:28:13 +00:00
|
|
|
return userRoleSpecList, nil
|
|
|
|
}
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
ds.UserByEmailFunc = func(ctx context.Context, email string) (*fleet.User, error) {
|
2021-07-16 18:28:13 +00:00
|
|
|
if email == "admin1@example.com" {
|
|
|
|
return userRoleSpecList[0], nil
|
|
|
|
}
|
|
|
|
return userRoleSpecList[1], nil
|
|
|
|
}
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
ds.TeamByNameFunc = func(ctx context.Context, name string) (*fleet.Team, error) {
|
2021-07-16 18:28:13 +00:00
|
|
|
return &fleet.Team{
|
|
|
|
ID: 1,
|
|
|
|
CreatedAt: time.Now(),
|
|
|
|
Name: "team1",
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
ds.SaveUsersFunc = func(ctx context.Context, users []*fleet.User) error {
|
2021-07-16 18:28:13 +00:00
|
|
|
for _, u := range users {
|
|
|
|
switch u.Email {
|
|
|
|
case "admin1@example.com":
|
|
|
|
userRoleList[0] = u
|
|
|
|
case "admin2@example.com":
|
|
|
|
userRoleList[1] = u
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpFile, err := ioutil.TempFile(os.TempDir(), "*.yml")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.Remove(tmpFile.Name())
|
|
|
|
|
|
|
|
tmpFile.WriteString(`
|
|
|
|
---
|
|
|
|
apiVersion: v1
|
|
|
|
kind: user_roles
|
|
|
|
spec:
|
|
|
|
roles:
|
|
|
|
admin1@example.com:
|
|
|
|
global_role: admin
|
|
|
|
teams: null
|
|
|
|
admin2@example.com:
|
|
|
|
global_role: null
|
|
|
|
teams:
|
|
|
|
- role: maintainer
|
|
|
|
team: team1
|
|
|
|
`)
|
|
|
|
|
|
|
|
assert.Equal(t, "[+] applied user roles\n", runAppForTest(t, []string{"apply", "-f", tmpFile.Name()}))
|
|
|
|
require.Len(t, userRoleSpecList[1].Teams, 1)
|
|
|
|
assert.Equal(t, fleet.RoleMaintainer, userRoleSpecList[1].Teams[0].Role)
|
|
|
|
}
|
2021-07-19 19:48:49 +00:00
|
|
|
|
|
|
|
func TestApplyTeamSpecs(t *testing.T) {
|
2021-09-03 16:05:23 +00:00
|
|
|
license := &fleet.LicenseInfo{Tier: fleet.TierPremium, Expiration: time.Now().Add(24 * time.Hour)}
|
2021-09-15 19:27:53 +00:00
|
|
|
_, ds := runServerWithMockedDS(t, service.TestServerOpts{License: license})
|
2021-07-19 19:48:49 +00:00
|
|
|
|
|
|
|
teamsByName := map[string]*fleet.Team{
|
2021-09-15 19:27:53 +00:00
|
|
|
"team1": {
|
2021-07-19 19:48:49 +00:00
|
|
|
ID: 42,
|
|
|
|
Name: "team1",
|
|
|
|
Description: "team1 description",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
ds.TeamByNameFunc = func(ctx context.Context, name string) (*fleet.Team, error) {
|
2021-07-19 19:48:49 +00:00
|
|
|
team, ok := teamsByName[name]
|
|
|
|
if !ok {
|
|
|
|
return nil, sql.ErrNoRows
|
|
|
|
}
|
|
|
|
return team, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
i := 1
|
2021-09-14 12:11:07 +00:00
|
|
|
ds.NewTeamFunc = func(ctx context.Context, team *fleet.Team) (*fleet.Team, error) {
|
2021-07-19 19:48:49 +00:00
|
|
|
team.ID = uint(i)
|
|
|
|
i++
|
|
|
|
teamsByName[team.Name] = team
|
|
|
|
return team, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
agentOpts := json.RawMessage(`{"config":{"foo":"bar"},"overrides":{"platforms":{"darwin":{"foo":"override"}}}}`)
|
2021-09-14 12:11:07 +00:00
|
|
|
ds.AppConfigFunc = func(ctx context.Context) (*fleet.AppConfig, error) {
|
2021-07-19 19:48:49 +00:00
|
|
|
return &fleet.AppConfig{AgentOptions: &agentOpts}, nil
|
|
|
|
}
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
ds.SaveTeamFunc = func(ctx context.Context, team *fleet.Team) (*fleet.Team, error) {
|
2021-07-19 19:48:49 +00:00
|
|
|
teamsByName[team.Name] = team
|
|
|
|
return team, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
enrolledSecretsCalled := make(map[uint][]*fleet.EnrollSecret)
|
2021-09-14 12:11:07 +00:00
|
|
|
ds.ApplyEnrollSecretsFunc = func(ctx context.Context, teamID *uint, secrets []*fleet.EnrollSecret) error {
|
2021-07-19 19:48:49 +00:00
|
|
|
enrolledSecretsCalled[*teamID] = secrets
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-26 13:28:53 +00:00
|
|
|
tmpFile, err := ioutil.TempFile(t.TempDir(), "*.yml")
|
2021-07-19 19:48:49 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
tmpFile.WriteString(`
|
|
|
|
---
|
|
|
|
apiVersion: v1
|
|
|
|
kind: team
|
|
|
|
spec:
|
|
|
|
team:
|
|
|
|
name: team2
|
|
|
|
---
|
|
|
|
apiVersion: v1
|
|
|
|
kind: team
|
|
|
|
spec:
|
|
|
|
team:
|
|
|
|
agent_options:
|
|
|
|
config:
|
|
|
|
something: else
|
|
|
|
name: team1
|
|
|
|
secrets:
|
|
|
|
- secret: AAA
|
|
|
|
`)
|
|
|
|
|
|
|
|
newAgentOpts := json.RawMessage("{\"config\":{\"something\":\"else\"}}")
|
|
|
|
|
|
|
|
assert.Equal(t, "[+] applied 2 teams\n", runAppForTest(t, []string{"apply", "-f", tmpFile.Name()}))
|
|
|
|
assert.Equal(t, &agentOpts, teamsByName["team2"].AgentOptions)
|
|
|
|
assert.Equal(t, &newAgentOpts, teamsByName["team1"].AgentOptions)
|
|
|
|
assert.Equal(t, []*fleet.EnrollSecret{{Secret: "AAA"}}, enrolledSecretsCalled[uint(42)])
|
|
|
|
}
|
2021-08-11 17:56:11 +00:00
|
|
|
|
|
|
|
func writeTmpYml(t *testing.T, contents string) string {
|
|
|
|
tmpFile, err := ioutil.TempFile(t.TempDir(), "*.yml")
|
|
|
|
require.NoError(t, err)
|
|
|
|
_, err = tmpFile.WriteString(contents)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return tmpFile.Name()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestApplyAppConfig(t *testing.T) {
|
2021-09-15 19:27:53 +00:00
|
|
|
_, ds := runServerWithMockedDS(t)
|
2021-08-11 17:56:11 +00:00
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
ds.ListUsersFunc = func(ctx context.Context, opt fleet.UserListOptions) ([]*fleet.User, error) {
|
2021-08-11 17:56:11 +00:00
|
|
|
return userRoleSpecList, nil
|
|
|
|
}
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
ds.UserByEmailFunc = func(ctx context.Context, email string) (*fleet.User, error) {
|
2021-08-11 17:56:11 +00:00
|
|
|
if email == "admin1@example.com" {
|
|
|
|
return userRoleSpecList[0], nil
|
|
|
|
}
|
|
|
|
return userRoleSpecList[1], nil
|
|
|
|
}
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
ds.AppConfigFunc = func(ctx context.Context) (*fleet.AppConfig, error) {
|
2021-08-11 17:56:11 +00:00
|
|
|
return &fleet.AppConfig{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var savedAppConfig *fleet.AppConfig
|
2021-09-14 12:11:07 +00:00
|
|
|
ds.SaveAppConfigFunc = func(ctx context.Context, config *fleet.AppConfig) error {
|
2021-08-11 17:56:11 +00:00
|
|
|
savedAppConfig = config
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
name := writeTmpYml(t, `---
|
|
|
|
apiVersion: v1
|
|
|
|
kind: config
|
|
|
|
spec:
|
|
|
|
host_settings:
|
|
|
|
enable_host_users: false
|
2021-08-11 18:57:53 +00:00
|
|
|
enable_software_inventory: false
|
2021-08-11 17:56:11 +00:00
|
|
|
`)
|
|
|
|
|
|
|
|
assert.Equal(t, "[+] applied fleet config\n", runAppForTest(t, []string{"apply", "-f", name}))
|
|
|
|
require.NotNil(t, savedAppConfig)
|
2021-08-20 15:27:41 +00:00
|
|
|
assert.False(t, savedAppConfig.HostSettings.EnableHostUsers)
|
|
|
|
assert.False(t, savedAppConfig.HostSettings.EnableSoftwareInventory)
|
2021-08-11 17:56:11 +00:00
|
|
|
|
|
|
|
name = writeTmpYml(t, `---
|
|
|
|
apiVersion: v1
|
|
|
|
kind: config
|
|
|
|
spec:
|
|
|
|
host_settings:
|
|
|
|
enable_host_users: true
|
2021-08-11 18:57:53 +00:00
|
|
|
enable_software_inventory: true
|
2021-08-11 17:56:11 +00:00
|
|
|
`)
|
|
|
|
|
|
|
|
assert.Equal(t, "[+] applied fleet config\n", runAppForTest(t, []string{"apply", "-f", name}))
|
|
|
|
require.NotNil(t, savedAppConfig)
|
2021-08-20 15:27:41 +00:00
|
|
|
assert.True(t, savedAppConfig.HostSettings.EnableHostUsers)
|
|
|
|
assert.True(t, savedAppConfig.HostSettings.EnableSoftwareInventory)
|
2021-08-11 17:56:11 +00:00
|
|
|
}
|