package service import ( "bytes" "compress/flate" "encoding/base64" "encoding/xml" "fmt" "net/http" "net/http/httptest" "net/url" "strings" "testing" "github.com/fleetdm/fleet/v4/server/datastore/redis/redistest" "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") pool := redistest.SetupRedis(s.T(), "zz", false, false, false) users, server := RunServerForTestsWithDS(s.T(), s.ds, TestServerOpts{Pool: pool}) 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() // start a test server to serve as the SAML identity provider srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, samltestIdPMetadata) })) defer srv.Close() // enable SSO spec := []byte(fmt.Sprintf(` sso_settings: enable_sso: true metadata_url: %s entity_id: https://samltest.id/saml/idp idp_name: SAMLtestIdP `, srv.URL)) s.applyConfig(spec) // double-check the settings var resGet ssoSettingsResponse s.DoJSON("GET", "/api/v1/fleet/sso", nil, http.StatusOK, &resGet) require.True(t, resGet.Settings.SSOEnabled) // initiate an SSO auth var resIni initiateSSOResponse s.DoJSON("POST", "/api/v1/fleet/sso", map[string]string{}, http.StatusOK, &resIni) 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) assert.Equal(t, "https://samltest.id/saml/idp", authReq.Issuer.Url) assert.Equal(t, "Fleet", authReq.ProviderName) assert.True(t, strings.HasPrefix(authReq.ID, "id"), authReq.ID) } 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 } const ( samltestIdPMetadata = ` samltest.id SAMLtest IdP A free and basic IdP for testing SAML deployments https://samltest.id/saml/logo.png MIIDETCCAfmgAwIBAgIUZRpDhkNKl5eWtJqk0Bu1BgTTargwDQYJKoZIhvcNAQEL BQAwFjEUMBIGA1UEAwwLc2FtbHRlc3QuaWQwHhcNMTgwODI0MjExNDEwWhcNMzgw ODI0MjExNDEwWjAWMRQwEgYDVQQDDAtzYW1sdGVzdC5pZDCCASIwDQYJKoZIhvcN AQEBBQADggEPADCCAQoCggEBAJrh9/PcDsiv3UeL8Iv9rf4WfLPxuOm9W6aCntEA 8l6c1LQ1Zyrz+Xa/40ZgP29ENf3oKKbPCzDcc6zooHMji2fBmgXp6Li3fQUzu7yd +nIC2teejijVtrNLjn1WUTwmqjLtuzrKC/ePoZyIRjpoUxyEMJopAd4dJmAcCq/K k2eYX9GYRlqvIjLFoGNgy2R4dWwAKwljyh6pdnPUgyO/WjRDrqUBRFrLQJorR2kD c4seZUbmpZZfp4MjmWMDgyGM1ZnR0XvNLtYeWAyt0KkSvFoOMjZUeVK/4xR74F8e 8ToPqLmZEg9ZUx+4z2KjVK00LpdRkH9Uxhh03RQ0FabHW6UCAwEAAaNXMFUwHQYD VR0OBBYEFJDbe6uSmYQScxpVJhmt7PsCG4IeMDQGA1UdEQQtMCuCC3NhbWx0ZXN0 LmlkhhxodHRwczovL3NhbWx0ZXN0LmlkL3NhbWwvaWRwMA0GCSqGSIb3DQEBCwUA A4IBAQBNcF3zkw/g51q26uxgyuy4gQwnSr01Mhvix3Dj/Gak4tc4XwvxUdLQq+jC cxr2Pie96klWhY/v/JiHDU2FJo9/VWxmc/YOk83whvNd7mWaNMUsX3xGv6AlZtCO L3JhCpHjiN+kBcMgS5jrtGgV1Lz3/1zpGxykdvS0B4sP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