2021-07-08 15:50:43 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2021-07-16 13:15:15 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
2021-07-08 15:50:43 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/datastore/mysql"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/ptr"
|
2021-07-16 13:15:15 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/test"
|
2021-07-09 18:13:11 +00:00
|
|
|
"github.com/ghodss/yaml"
|
2021-07-08 15:50:43 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2021-07-19 19:48:49 +00:00
|
|
|
func TestDoubleUserCreationErrors(t *testing.T) {
|
|
|
|
ds := mysql.CreateMySQLDS(t)
|
|
|
|
defer ds.Close()
|
|
|
|
|
2021-07-16 18:28:13 +00:00
|
|
|
_, server := RunServerForTestsWithDS(t, ds)
|
2021-07-08 15:50:43 +00:00
|
|
|
token := getTestAdminToken(t, server)
|
|
|
|
|
|
|
|
params := fleet.UserPayload{
|
2021-07-13 19:33:04 +00:00
|
|
|
Name: ptr.String("user1"),
|
|
|
|
Email: ptr.String("email@asd.com"),
|
|
|
|
Password: ptr.String("pass"),
|
|
|
|
GlobalRole: ptr.String(fleet.RoleObserver),
|
2021-07-08 15:50:43 +00:00
|
|
|
}
|
|
|
|
j, err := json.Marshal(¶ms)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
requestBody := &nopCloser{bytes.NewBuffer(j)}
|
|
|
|
req, _ := http.NewRequest("POST", server.URL+"/api/v1/fleet/users/admin", requestBody)
|
|
|
|
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
|
|
|
|
client := &http.Client{}
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
|
|
|
|
|
|
|
requestBody = &nopCloser{bytes.NewBuffer(j)}
|
|
|
|
req, _ = http.NewRequest("POST", server.URL+"/api/v1/fleet/users/admin", requestBody)
|
|
|
|
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
|
|
|
|
resp, err = client.Do(req)
|
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Equal(t, http.StatusConflict, resp.StatusCode)
|
|
|
|
assertBodyContains(t, resp, `Error 1062: Duplicate entry 'email@asd.com'`)
|
|
|
|
}
|
|
|
|
|
2021-07-19 19:48:49 +00:00
|
|
|
func TestUserWithoutRoleErrors(t *testing.T) {
|
|
|
|
ds := mysql.CreateMySQLDS(t)
|
|
|
|
defer ds.Close()
|
|
|
|
|
2021-07-16 18:28:13 +00:00
|
|
|
_, server := RunServerForTestsWithDS(t, ds)
|
2021-07-13 19:33:04 +00:00
|
|
|
token := getTestAdminToken(t, server)
|
|
|
|
|
|
|
|
params := fleet.UserPayload{
|
|
|
|
Name: ptr.String("user1"),
|
|
|
|
Email: ptr.String("email@asd.com"),
|
|
|
|
Password: ptr.String("pass"),
|
|
|
|
}
|
|
|
|
j, err := json.Marshal(¶ms)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
requestBody := &nopCloser{bytes.NewBuffer(j)}
|
|
|
|
req, _ := http.NewRequest("POST", server.URL+"/api/v1/fleet/users/admin", requestBody)
|
|
|
|
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
|
|
|
|
client := &http.Client{}
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Equal(t, http.StatusUnprocessableEntity, resp.StatusCode)
|
|
|
|
assertErrorCodeAndMessage(t, resp, fleet.ErrNoRoleNeeded, "either global role or team role needs to be defined")
|
|
|
|
}
|
|
|
|
|
2021-07-19 19:48:49 +00:00
|
|
|
func TestUserWithWrongRoleErrors(t *testing.T) {
|
|
|
|
ds := mysql.CreateMySQLDS(t)
|
|
|
|
defer ds.Close()
|
|
|
|
|
2021-07-16 18:28:13 +00:00
|
|
|
_, server := RunServerForTestsWithDS(t, ds)
|
2021-07-13 19:33:04 +00:00
|
|
|
token := getTestAdminToken(t, server)
|
|
|
|
|
|
|
|
params := fleet.UserPayload{
|
|
|
|
Name: ptr.String("user1"),
|
|
|
|
Email: ptr.String("email@asd.com"),
|
|
|
|
Password: ptr.String("pass"),
|
|
|
|
GlobalRole: ptr.String("wrongrole"),
|
|
|
|
}
|
|
|
|
j, err := json.Marshal(¶ms)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
requestBody := &nopCloser{bytes.NewBuffer(j)}
|
|
|
|
req, _ := http.NewRequest("POST", server.URL+"/api/v1/fleet/users/admin", requestBody)
|
|
|
|
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
|
|
|
|
client := &http.Client{}
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
require.Nil(t, err)
|
|
|
|
assert.Equal(t, http.StatusUnprocessableEntity, resp.StatusCode)
|
|
|
|
assertErrorCodeAndMessage(t, resp, fleet.ErrNoRoleNeeded, "GlobalRole role can only be admin, observer, or maintainer.")
|
|
|
|
}
|
|
|
|
|
2021-07-19 19:48:49 +00:00
|
|
|
func TestUserCreationWrongTeamErrors(t *testing.T) {
|
|
|
|
ds := mysql.CreateMySQLDS(t)
|
|
|
|
defer ds.Close()
|
|
|
|
|
2021-07-16 18:28:13 +00:00
|
|
|
_, server := RunServerForTestsWithDS(t, ds)
|
2021-07-08 15:50:43 +00:00
|
|
|
token := getTestAdminToken(t, server)
|
|
|
|
|
|
|
|
teams := []fleet.UserTeam{
|
|
|
|
{
|
|
|
|
Team: fleet.Team{
|
|
|
|
ID: 9999,
|
|
|
|
},
|
2021-07-16 18:28:13 +00:00
|
|
|
Role: fleet.RoleObserver,
|
2021-07-08 15:50:43 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
params := fleet.UserPayload{
|
2021-07-16 18:28:13 +00:00
|
|
|
Name: ptr.String("user1"),
|
|
|
|
Email: ptr.String("email@asd.com"),
|
|
|
|
Password: ptr.String("pass"),
|
|
|
|
Teams: &teams,
|
2021-07-08 15:50:43 +00:00
|
|
|
}
|
2021-07-13 19:54:22 +00:00
|
|
|
method := "POST"
|
|
|
|
path := "/api/v1/fleet/users/admin"
|
|
|
|
expectedStatusCode := http.StatusUnprocessableEntity
|
|
|
|
|
|
|
|
resp := doReq(t, params, method, server, path, token, expectedStatusCode)
|
|
|
|
assertBodyContains(t, resp, `Error 1452: Cannot add or update a child row: a foreign key constraint fails`)
|
|
|
|
}
|
|
|
|
|
|
|
|
func doReq(
|
|
|
|
t *testing.T,
|
|
|
|
params interface{},
|
|
|
|
method string,
|
|
|
|
server *httptest.Server,
|
|
|
|
path string,
|
|
|
|
token string,
|
|
|
|
expectedStatusCode int,
|
|
|
|
) *http.Response {
|
2021-07-08 15:50:43 +00:00
|
|
|
j, err := json.Marshal(¶ms)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
requestBody := &nopCloser{bytes.NewBuffer(j)}
|
2021-07-13 19:54:22 +00:00
|
|
|
req, _ := http.NewRequest(method, server.URL+path, requestBody)
|
2021-07-08 15:50:43 +00:00
|
|
|
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
|
|
|
|
client := &http.Client{}
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
require.Nil(t, err)
|
2021-07-13 19:54:22 +00:00
|
|
|
assert.Equal(t, expectedStatusCode, resp.StatusCode)
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
func doJSONReq(
|
|
|
|
t *testing.T,
|
|
|
|
params interface{},
|
|
|
|
method string,
|
|
|
|
server *httptest.Server,
|
|
|
|
path string,
|
|
|
|
token string,
|
|
|
|
expectedStatusCode int,
|
|
|
|
v interface{},
|
|
|
|
) {
|
|
|
|
resp := doReq(t, params, method, server, path, token, expectedStatusCode)
|
|
|
|
err := json.NewDecoder(resp.Body).Decode(v)
|
|
|
|
require.Nil(t, err)
|
2021-07-08 15:50:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func assertBodyContains(t *testing.T, resp *http.Response, expectedError string) {
|
|
|
|
bodyBytes, err := ioutil.ReadAll(resp.Body)
|
|
|
|
require.Nil(t, err)
|
|
|
|
bodyString := string(bodyBytes)
|
|
|
|
assert.Contains(t, bodyString, expectedError)
|
|
|
|
}
|
|
|
|
|
2021-07-19 19:48:49 +00:00
|
|
|
func TestQueryCreationLogsActivity(t *testing.T) {
|
|
|
|
ds := mysql.CreateMySQLDS(t)
|
|
|
|
defer ds.Close()
|
|
|
|
|
2021-07-22 15:28:42 +00:00
|
|
|
users, server := RunServerForTestsWithDS(t, ds)
|
2021-07-13 19:54:22 +00:00
|
|
|
token := getTestAdminToken(t, server)
|
|
|
|
|
2021-07-22 15:28:42 +00:00
|
|
|
admin1 := users["admin1@example.com"]
|
|
|
|
admin1.GravatarURL = "http://iii.com"
|
|
|
|
err := ds.SaveUser(&admin1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-07-13 19:54:22 +00:00
|
|
|
params := fleet.QueryPayload{
|
|
|
|
Name: ptr.String("user1"),
|
|
|
|
Query: ptr.String("select * from time;"),
|
|
|
|
}
|
|
|
|
doReq(t, params, "POST", server, "/api/v1/fleet/queries", token, http.StatusOK)
|
|
|
|
type activitiesRespose struct {
|
|
|
|
Activities []map[string]interface{} `json:"activities"`
|
|
|
|
}
|
|
|
|
activities := activitiesRespose{}
|
|
|
|
doJSONReq(t, nil, "GET", server, "/api/v1/fleet/activities", token, http.StatusOK, &activities)
|
|
|
|
|
|
|
|
assert.Len(t, activities.Activities, 1)
|
|
|
|
assert.Equal(t, "Test Name admin1@example.com", activities.Activities[0]["actor_full_name"])
|
2021-07-22 15:28:42 +00:00
|
|
|
assert.Equal(t, "http://iii.com", activities.Activities[0]["actor_gravatar"])
|
2021-07-13 19:54:22 +00:00
|
|
|
assert.Equal(t, "created_saved_query", activities.Activities[0]["type"])
|
|
|
|
}
|
|
|
|
|
2021-07-13 19:33:04 +00:00
|
|
|
func getJSON(r *http.Response, target interface{}) error {
|
|
|
|
return json.NewDecoder(r.Body).Decode(target)
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertErrorCodeAndMessage(t *testing.T, resp *http.Response, code int, message string) {
|
|
|
|
err := &fleet.Error{}
|
|
|
|
require.Nil(t, getJSON(resp, err))
|
|
|
|
assert.Equal(t, code, err.Code)
|
|
|
|
assert.Equal(t, message, err.Message)
|
|
|
|
}
|
|
|
|
|
2021-07-19 19:48:49 +00:00
|
|
|
func TestAppConfigAdditionalQueriesCanBeRemoved(t *testing.T) {
|
|
|
|
ds := mysql.CreateMySQLDS(t)
|
|
|
|
defer ds.Close()
|
|
|
|
|
2021-07-16 18:28:13 +00:00
|
|
|
_, server := RunServerForTestsWithDS(t, ds)
|
2021-07-09 18:13:11 +00:00
|
|
|
token := getTestAdminToken(t, server)
|
|
|
|
|
|
|
|
spec := []byte(`
|
|
|
|
host_expiry_settings:
|
|
|
|
host_expiry_enabled: false
|
|
|
|
host_expiry_window: 0
|
|
|
|
host_settings:
|
|
|
|
additional_queries:
|
|
|
|
time: SELECT * FROM time
|
|
|
|
`)
|
|
|
|
applyConfig(t, spec, server, token)
|
|
|
|
|
|
|
|
spec = []byte(`
|
|
|
|
host_expiry_settings:
|
|
|
|
host_expiry_enabled: false
|
|
|
|
host_expiry_window: 0
|
|
|
|
host_settings:
|
|
|
|
`)
|
|
|
|
applyConfig(t, spec, server, token)
|
|
|
|
|
|
|
|
config := getConfig(t, server, token)
|
|
|
|
assert.Nil(t, config.HostSettings)
|
|
|
|
}
|
|
|
|
|
2021-07-30 15:45:49 +00:00
|
|
|
func TestAppConfigHasLogging(t *testing.T) {
|
|
|
|
ds := mysql.CreateMySQLDS(t)
|
|
|
|
defer ds.Close()
|
|
|
|
|
|
|
|
_, server := RunServerForTestsWithDS(t, ds)
|
|
|
|
token := getTestAdminToken(t, server)
|
|
|
|
|
|
|
|
config := getConfig(t, server, token)
|
|
|
|
require.NotNil(t, config.Logging)
|
|
|
|
}
|
|
|
|
|
2021-07-09 18:13:11 +00:00
|
|
|
func applyConfig(t *testing.T, spec []byte, server *httptest.Server, token string) {
|
|
|
|
var appConfigSpec fleet.AppConfigPayload
|
|
|
|
err := yaml.Unmarshal(spec, &appConfigSpec)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-07-13 19:54:22 +00:00
|
|
|
doReq(t, appConfigSpec, "PATCH", server, "/api/v1/fleet/config", token, http.StatusOK)
|
2021-07-09 18:13:11 +00:00
|
|
|
}
|
|
|
|
|
2021-07-30 15:45:49 +00:00
|
|
|
func getConfig(t *testing.T, server *httptest.Server, token string) *appConfigResponse {
|
|
|
|
var responseBody *appConfigResponse
|
2021-07-13 19:54:22 +00:00
|
|
|
doJSONReq(t, nil, "GET", server, "/api/v1/fleet/config", token, http.StatusOK, &responseBody)
|
2021-07-09 18:13:11 +00:00
|
|
|
return responseBody
|
|
|
|
}
|
|
|
|
|
2021-07-19 19:48:49 +00:00
|
|
|
func TestUserRolesSpec(t *testing.T) {
|
|
|
|
ds := mysql.CreateMySQLDS(t)
|
|
|
|
defer ds.Close()
|
|
|
|
|
2021-07-16 18:28:13 +00:00
|
|
|
_, server := RunServerForTestsWithDS(t, ds)
|
|
|
|
_, err := ds.NewTeam(&fleet.Team{
|
|
|
|
ID: 42,
|
|
|
|
Name: "team1",
|
|
|
|
Description: "desc team1",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
token := getTestAdminToken(t, server)
|
|
|
|
|
|
|
|
user, err := ds.UserByEmail("user1@example.com")
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Len(t, user.Teams, 0)
|
|
|
|
|
|
|
|
spec := []byte(`
|
|
|
|
roles:
|
|
|
|
user1@example.com:
|
|
|
|
global_role: null
|
|
|
|
teams:
|
|
|
|
- role: maintainer
|
|
|
|
team: team1
|
|
|
|
`)
|
|
|
|
|
|
|
|
var userRoleSpec applyUserRoleSpecsRequest
|
|
|
|
err = yaml.Unmarshal(spec, &userRoleSpec.Spec)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
doReq(t, userRoleSpec, "POST", server, "/api/v1/fleet/users/roles/spec", token, http.StatusOK)
|
|
|
|
|
|
|
|
user, err = ds.UserByEmail("user1@example.com")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, user.Teams, 1)
|
|
|
|
assert.Equal(t, fleet.RoleMaintainer, user.Teams[0].Role)
|
|
|
|
|
|
|
|
// But users are not deleted
|
|
|
|
users, err := ds.ListUsers(fleet.UserListOptions{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Len(t, users, 3)
|
|
|
|
}
|
|
|
|
|
2021-07-19 19:48:49 +00:00
|
|
|
func TestGlobalSchedule(t *testing.T) {
|
|
|
|
ds := mysql.CreateMySQLDS(t)
|
|
|
|
defer ds.Close()
|
|
|
|
|
2021-07-16 13:15:15 +00:00
|
|
|
test.AddAllHostsLabel(t, ds)
|
|
|
|
|
2021-07-16 18:28:13 +00:00
|
|
|
_, server := RunServerForTestsWithDS(t, ds)
|
2021-07-16 13:15:15 +00:00
|
|
|
token := getTestAdminToken(t, server)
|
|
|
|
|
|
|
|
gs := fleet.GlobalSchedulePayload{}
|
|
|
|
doJSONReq(t, nil, "GET", server, "/api/v1/fleet/global/schedule", token, http.StatusOK, &gs)
|
|
|
|
assert.Len(t, gs.GlobalSchedule, 0)
|
|
|
|
|
|
|
|
qr, err := ds.NewQuery(&fleet.Query{
|
|
|
|
Name: "TestQuery",
|
|
|
|
Description: "Some description",
|
|
|
|
Query: "select * from osquery;",
|
|
|
|
ObserverCanRun: true,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
gsParams := fleet.ScheduledQueryPayload{QueryID: ptr.Uint(qr.ID), Interval: ptr.Uint(42)}
|
|
|
|
type responseType struct {
|
|
|
|
Scheduled *fleet.ScheduledQuery `json:"scheduled,omitempty"`
|
|
|
|
Err error `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
r := responseType{}
|
|
|
|
doJSONReq(t, gsParams, "POST", server, "/api/v1/fleet/global/schedule", token, http.StatusOK, &r)
|
|
|
|
require.Nil(t, r.Err)
|
|
|
|
|
|
|
|
gs = fleet.GlobalSchedulePayload{}
|
|
|
|
doJSONReq(t, nil, "GET", server, "/api/v1/fleet/global/schedule", token, http.StatusOK, &gs)
|
|
|
|
require.Len(t, gs.GlobalSchedule, 1)
|
|
|
|
assert.Equal(t, uint(42), gs.GlobalSchedule[0].Interval)
|
|
|
|
assert.Equal(t, "TestQuery", gs.GlobalSchedule[0].Name)
|
|
|
|
id := gs.GlobalSchedule[0].ID
|
|
|
|
|
|
|
|
gs = fleet.GlobalSchedulePayload{}
|
|
|
|
gsParams = fleet.ScheduledQueryPayload{Interval: ptr.Uint(55)}
|
|
|
|
doJSONReq(
|
|
|
|
t, gsParams, "PATCH", server,
|
|
|
|
fmt.Sprintf("/api/v1/fleet/global/schedule/%d", id),
|
|
|
|
token, http.StatusOK, &gs,
|
|
|
|
)
|
|
|
|
|
|
|
|
gs = fleet.GlobalSchedulePayload{}
|
|
|
|
doJSONReq(t, nil, "GET", server, "/api/v1/fleet/global/schedule", token, http.StatusOK, &gs)
|
|
|
|
require.Len(t, gs.GlobalSchedule, 1)
|
|
|
|
assert.Equal(t, uint(55), gs.GlobalSchedule[0].Interval)
|
|
|
|
|
|
|
|
r = responseType{}
|
|
|
|
doJSONReq(
|
|
|
|
t, nil, "DELETE", server,
|
|
|
|
fmt.Sprintf("/api/v1/fleet/global/schedule/%d", id),
|
|
|
|
token, http.StatusOK, &r,
|
|
|
|
)
|
|
|
|
require.Nil(t, r.Err)
|
|
|
|
|
|
|
|
gs = fleet.GlobalSchedulePayload{}
|
|
|
|
doJSONReq(t, nil, "GET", server, "/api/v1/fleet/global/schedule", token, http.StatusOK, &gs)
|
|
|
|
require.Len(t, gs.GlobalSchedule, 0)
|
|
|
|
}
|
|
|
|
|
2021-07-19 19:48:49 +00:00
|
|
|
func TestTeamSpecs(t *testing.T) {
|
|
|
|
ds := mysql.CreateMySQLDS(t)
|
|
|
|
defer ds.Close()
|
|
|
|
|
|
|
|
_, server := RunServerForTestsWithDS(t, ds)
|
|
|
|
_, err := ds.NewTeam(&fleet.Team{
|
|
|
|
ID: 42,
|
|
|
|
Name: "team1",
|
|
|
|
Description: "desc team1",
|
2021-07-08 15:50:43 +00:00
|
|
|
})
|
2021-07-19 19:48:49 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
token := getTestAdminToken(t, server)
|
|
|
|
|
|
|
|
// updates a team
|
|
|
|
agentOpts := json.RawMessage(`{"config": {"foo": "bar"}, "overrides": {"platforms": {"darwin": {"foo": "override"}}}}`)
|
|
|
|
teamSpecs := applyTeamSpecsRequest{Specs: []*fleet.TeamSpec{{Name: "team1", AgentOptions: &agentOpts}}}
|
|
|
|
doReq(t, teamSpecs, "POST", server, "/api/v1/fleet/spec/teams", token, http.StatusOK)
|
|
|
|
|
|
|
|
team, err := ds.TeamByName("team1")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Len(t, team.Secrets, 0)
|
|
|
|
assert.Equal(t, &agentOpts, team.AgentOptions)
|
|
|
|
|
|
|
|
// creates a team with default agent options
|
|
|
|
user, err := ds.UserByEmail("admin1@example.com")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
teams, err := ds.ListTeams(fleet.TeamFilter{User: user}, fleet.ListOptions{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Len(t, teams, 1)
|
|
|
|
|
|
|
|
teamSpecs = applyTeamSpecsRequest{Specs: []*fleet.TeamSpec{{Name: "team2"}}}
|
|
|
|
doReq(t, teamSpecs, "POST", server, "/api/v1/fleet/spec/teams", token, http.StatusOK)
|
|
|
|
|
|
|
|
teams, err = ds.ListTeams(fleet.TeamFilter{User: user}, fleet.ListOptions{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Len(t, teams, 2)
|
|
|
|
|
|
|
|
team, err = ds.TeamByName("team2")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
defaultOpts := json.RawMessage("{\"config\": {\"options\": {\"logger_plugin\": \"tls\", \"pack_delimiter\": \"/\", \"logger_tls_period\": 10, \"distributed_plugin\": \"tls\", \"disable_distributed\": false, \"logger_tls_endpoint\": \"/api/v1/osquery/log\", \"distributed_interval\": 10, \"distributed_tls_max_attempts\": 3}, \"decorators\": {\"load\": [\"SELECT uuid AS host_uuid FROM system_info;\", \"SELECT hostname AS hostname FROM system_info;\"]}}, \"overrides\": {}}")
|
|
|
|
assert.Len(t, team.Secrets, 0)
|
|
|
|
assert.Equal(t, &defaultOpts, team.AgentOptions)
|
|
|
|
|
|
|
|
// updates secrets
|
|
|
|
teamSpecs = applyTeamSpecsRequest{Specs: []*fleet.TeamSpec{{Name: "team2", Secrets: []fleet.EnrollSecret{{Secret: "ABC"}}}}}
|
|
|
|
doReq(t, teamSpecs, "POST", server, "/api/v1/fleet/spec/teams", token, http.StatusOK)
|
|
|
|
|
|
|
|
team, err = ds.TeamByName("team2")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Len(t, team.Secrets, 1)
|
|
|
|
assert.Equal(t, "ABC", team.Secrets[0].Secret)
|
2021-07-08 15:50:43 +00:00
|
|
|
}
|
2021-07-21 17:03:10 +00:00
|
|
|
|
|
|
|
func TestTranslator(t *testing.T) {
|
|
|
|
ds := mysql.CreateMySQLDS(t)
|
|
|
|
defer ds.Close()
|
|
|
|
|
|
|
|
users, server := RunServerForTestsWithDS(t, ds)
|
|
|
|
token := getTestAdminToken(t, server)
|
|
|
|
|
|
|
|
payload := translatorResponse{}
|
|
|
|
params := translatorRequest{List: []fleet.TranslatePayload{
|
|
|
|
{
|
|
|
|
Type: fleet.TranslatorTypeUserEmail,
|
|
|
|
Payload: fleet.StringIdentifierToIDPayload{Identifier: "admin1@example.com"},
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
doJSONReq(t, ¶ms, "POST", server, "/api/v1/fleet/translate", token, http.StatusOK, &payload)
|
|
|
|
|
|
|
|
require.Nil(t, payload.Err)
|
|
|
|
assert.Len(t, payload.List, 1)
|
|
|
|
|
|
|
|
assert.Equal(t, users[payload.List[0].Payload.Identifier].ID, payload.List[0].Payload.ID)
|
|
|
|
}
|