2021-11-23 13:25:43 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"compress/flate"
|
2022-08-15 17:42:33 +00:00
|
|
|
"context"
|
2021-11-23 13:25:43 +00:00
|
|
|
"encoding/base64"
|
2022-09-19 17:53:44 +00:00
|
|
|
"encoding/json"
|
2021-11-23 13:25:43 +00:00
|
|
|
"encoding/xml"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2023-01-20 15:43:22 +00:00
|
|
|
"sort"
|
2021-11-23 13:25:43 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/fleetdm/fleet/v4/server/datastore/redis/redistest"
|
2022-08-15 17:42:33 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/ptr"
|
2021-11-23 13:25:43 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/sso"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
)
|
|
|
|
|
|
|
|
type integrationSSOTestSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
withServer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *integrationSSOTestSuite) SetupSuite() {
|
|
|
|
s.withDS.SetupSuite("integrationSSOTestSuite")
|
|
|
|
|
2022-01-19 15:52:14 +00:00
|
|
|
pool := redistest.SetupRedis(s.T(), "zz", false, false, false)
|
2022-05-10 15:29:17 +00:00
|
|
|
users, server := RunServerForTestsWithDS(s.T(), s.ds, &TestServerOpts{Pool: pool})
|
2021-11-23 13:25:43 +00:00
|
|
|
s.server = server
|
|
|
|
s.users = users
|
|
|
|
s.token = s.getTestAdminToken()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIntegrationsSSO(t *testing.T) {
|
|
|
|
testingSuite := new(integrationSSOTestSuite)
|
|
|
|
testingSuite.s = &testingSuite.Suite
|
|
|
|
suite.Run(t, testingSuite)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *integrationSSOTestSuite) TestGetSSOSettings() {
|
|
|
|
t := s.T()
|
|
|
|
|
2022-08-15 17:42:33 +00:00
|
|
|
acResp := appConfigResponse{}
|
2022-09-19 17:53:44 +00:00
|
|
|
s.DoJSON("PATCH", "/api/latest/fleet/config", json.RawMessage(`{
|
|
|
|
"sso_settings": {
|
|
|
|
"enable_sso": true,
|
|
|
|
"entity_id": "https://localhost:8080",
|
|
|
|
"issuer_uri": "http://localhost:8080/simplesaml/saml2/idp/SSOService.php",
|
|
|
|
"idp_name": "SimpleSAML",
|
|
|
|
"metadata_url": "http://localhost:9080/simplesaml/saml2/idp/metadata.php",
|
|
|
|
"enable_jit_provisioning": false
|
|
|
|
}
|
|
|
|
}`), http.StatusOK, &acResp)
|
2022-08-15 17:42:33 +00:00
|
|
|
require.NotNil(t, acResp)
|
2021-11-23 13:25:43 +00:00
|
|
|
|
|
|
|
// double-check the settings
|
|
|
|
var resGet ssoSettingsResponse
|
2022-04-20 16:46:45 +00:00
|
|
|
s.DoJSON("GET", "/api/v1/fleet/sso", nil, http.StatusOK, &resGet)
|
2021-11-23 13:25:43 +00:00
|
|
|
require.True(t, resGet.Settings.SSOEnabled)
|
|
|
|
|
|
|
|
// initiate an SSO auth
|
|
|
|
var resIni initiateSSOResponse
|
2022-04-20 16:46:45 +00:00
|
|
|
s.DoJSON("POST", "/api/v1/fleet/sso", map[string]string{}, http.StatusOK, &resIni)
|
2021-11-23 13:25:43 +00:00
|
|
|
require.NotEmpty(t, resIni.URL)
|
|
|
|
|
|
|
|
parsed, err := url.Parse(resIni.URL)
|
|
|
|
require.NoError(t, err)
|
|
|
|
q := parsed.Query()
|
|
|
|
encoded := q.Get("SAMLRequest")
|
|
|
|
assert.NotEmpty(t, encoded)
|
|
|
|
authReq := inflate(t, encoded)
|
2022-08-15 17:42:33 +00:00
|
|
|
assert.Equal(t, "https://localhost:8080", authReq.Issuer.Url)
|
2021-11-23 13:25:43 +00:00
|
|
|
assert.Equal(t, "Fleet", authReq.ProviderName)
|
|
|
|
assert.True(t, strings.HasPrefix(authReq.ID, "id"), authReq.ID)
|
|
|
|
}
|
|
|
|
|
2022-08-15 17:42:33 +00:00
|
|
|
func (s *integrationSSOTestSuite) TestSSOLogin() {
|
|
|
|
t := s.T()
|
|
|
|
|
|
|
|
acResp := appConfigResponse{}
|
2022-09-19 17:53:44 +00:00
|
|
|
s.DoJSON("PATCH", "/api/latest/fleet/config", json.RawMessage(`{
|
|
|
|
"sso_settings": {
|
|
|
|
"enable_sso": true,
|
|
|
|
"entity_id": "https://localhost:8080",
|
|
|
|
"issuer_uri": "http://localhost:8080/simplesaml/saml2/idp/SSOService.php",
|
|
|
|
"idp_name": "SimpleSAML",
|
|
|
|
"metadata_url": "http://localhost:9080/simplesaml/saml2/idp/metadata.php"
|
|
|
|
}
|
|
|
|
}`), http.StatusOK, &acResp)
|
2022-08-15 17:42:33 +00:00
|
|
|
require.NotNil(t, acResp)
|
|
|
|
|
2023-01-20 15:43:22 +00:00
|
|
|
// Register current number of activities.
|
|
|
|
activitiesResp := listActivitiesResponse{}
|
|
|
|
s.DoJSON("GET", "/api/latest/fleet/activities", nil, http.StatusOK, &activitiesResp)
|
|
|
|
require.NoError(t, activitiesResp.Err)
|
|
|
|
oldActivitiesCount := len(activitiesResp.Activities)
|
|
|
|
|
2022-08-15 17:42:33 +00:00
|
|
|
// users can't login if they don't have an account on free plans
|
|
|
|
_, body := s.LoginSSOUser("sso_user", "user123#")
|
|
|
|
require.Contains(t, body, "/login?status=account_invalid")
|
|
|
|
|
2023-01-20 15:43:22 +00:00
|
|
|
newActivitiesCount := 1
|
|
|
|
checkNewFailedLoginActivity := func() {
|
|
|
|
activitiesResp = listActivitiesResponse{}
|
|
|
|
s.DoJSON("GET", "/api/latest/fleet/activities", nil, http.StatusOK, &activitiesResp)
|
|
|
|
require.NoError(t, activitiesResp.Err)
|
|
|
|
require.Len(t, activitiesResp.Activities, oldActivitiesCount+newActivitiesCount)
|
|
|
|
sort.Slice(activitiesResp.Activities, func(i, j int) bool {
|
|
|
|
return activitiesResp.Activities[i].ID < activitiesResp.Activities[j].ID
|
|
|
|
})
|
|
|
|
activity := activitiesResp.Activities[len(activitiesResp.Activities)-1]
|
|
|
|
require.Equal(t, activity.Type, fleet.ActivityTypeUserFailedLogin{}.ActivityName())
|
|
|
|
require.NotNil(t, activity.Details)
|
|
|
|
actDetails := fleet.ActivityTypeUserFailedLogin{}
|
|
|
|
err := json.Unmarshal(*activity.Details, &actDetails)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "sso_user@example.com", actDetails.Email)
|
|
|
|
|
|
|
|
newActivitiesCount++
|
|
|
|
}
|
|
|
|
|
|
|
|
// A new activity item for the failed SSO login is created.
|
|
|
|
checkNewFailedLoginActivity()
|
|
|
|
|
2022-08-15 17:42:33 +00:00
|
|
|
// users can't login if they don't have an account on free plans
|
|
|
|
// even if JIT provisioning is enabled
|
|
|
|
ac, err := s.ds.AppConfig(context.Background())
|
|
|
|
ac.SSOSettings.EnableJITProvisioning = true
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = s.ds.SaveAppConfig(context.Background(), ac)
|
|
|
|
require.NoError(t, err)
|
|
|
|
_, body = s.LoginSSOUser("sso_user", "user123#")
|
|
|
|
require.Contains(t, body, "/login?status=account_invalid")
|
|
|
|
|
2023-01-20 15:43:22 +00:00
|
|
|
// A new activity item for the failed SSO login is created.
|
|
|
|
checkNewFailedLoginActivity()
|
|
|
|
|
2022-08-15 17:42:33 +00:00
|
|
|
// an user created by an admin without SSOEnabled can't log-in
|
|
|
|
params := fleet.UserPayload{
|
|
|
|
Name: ptr.String("SSO User 1"),
|
|
|
|
Email: ptr.String("sso_user@example.com"),
|
|
|
|
GlobalRole: ptr.String(fleet.RoleObserver),
|
|
|
|
SSOEnabled: ptr.Bool(false),
|
|
|
|
}
|
|
|
|
s.Do("POST", "/api/latest/fleet/users/admin", ¶ms, http.StatusUnprocessableEntity)
|
|
|
|
_, body = s.LoginSSOUser("sso_user", "user123#")
|
|
|
|
require.Contains(t, body, "/login?status=account_invalid")
|
|
|
|
|
2023-01-20 15:43:22 +00:00
|
|
|
// A new activity item for the failed SSO login is created.
|
|
|
|
checkNewFailedLoginActivity()
|
|
|
|
|
2023-03-14 20:17:08 +00:00
|
|
|
// A user created by an admin with SSOEnabled is able to log-in
|
2022-08-15 17:42:33 +00:00
|
|
|
params = fleet.UserPayload{
|
|
|
|
Name: ptr.String("SSO User 2"),
|
|
|
|
Email: ptr.String("sso_user2@example.com"),
|
|
|
|
GlobalRole: ptr.String(fleet.RoleObserver),
|
|
|
|
SSOEnabled: ptr.Bool(true),
|
|
|
|
}
|
|
|
|
s.Do("POST", "/api/latest/fleet/users/admin", ¶ms, http.StatusOK)
|
|
|
|
auth, body := s.LoginSSOUser("sso_user2", "user123#")
|
|
|
|
assert.Equal(t, "sso_user2@example.com", auth.UserID())
|
|
|
|
assert.Equal(t, "SSO User 2", auth.UserDisplayName())
|
|
|
|
require.Contains(t, body, "Redirecting to Fleet at ...")
|
2022-12-22 23:06:33 +00:00
|
|
|
|
|
|
|
// a new activity item is created
|
2023-01-20 15:43:22 +00:00
|
|
|
activitiesResp = listActivitiesResponse{}
|
2022-12-22 23:06:33 +00:00
|
|
|
s.DoJSON("GET", "/api/latest/fleet/activities", nil, http.StatusOK, &activitiesResp)
|
|
|
|
require.NoError(t, activitiesResp.Err)
|
|
|
|
require.NotEmpty(t, activitiesResp.Activities)
|
|
|
|
require.Condition(t, func() bool {
|
|
|
|
for _, a := range activitiesResp.Activities {
|
2022-12-23 16:05:16 +00:00
|
|
|
if (a.Type == fleet.ActivityTypeUserLoggedIn{}.ActivityName()) && *a.ActorEmail == auth.UserID() {
|
2022-12-22 23:06:33 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
2022-08-15 17:42:33 +00:00
|
|
|
}
|
|
|
|
|
2021-11-23 13:25:43 +00:00
|
|
|
func inflate(t *testing.T, s string) *sso.AuthnRequest {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
decoded, err := base64.StdEncoding.DecodeString(s)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
r := flate.NewReader(bytes.NewReader(decoded))
|
|
|
|
defer r.Close()
|
|
|
|
|
|
|
|
var req sso.AuthnRequest
|
|
|
|
require.NoError(t, xml.NewDecoder(r).Decode(&req))
|
|
|
|
return &req
|
|
|
|
}
|