fleet/server/service/integrationtest/suite.go
Victor Lyuboslavsky 0180cc8086
Add SCEP endpoint for host identity. (#30589)
Fixes #30458 

Contributor docs PR: https://github.com/fleetdm/fleet/pull/30651

# Checklist for submitter

- We will add changes file later.
- [x] Input data is properly validated, `SELECT *` is avoided, SQL
injection is prevented (using placeholders for values in statements)
- [x] If database migrations are included, checked table schema to
confirm autoupdate
- For database migrations:
- [x] Checked schema for all modified table for columns that will
auto-update timestamps during migration.
- [x] Confirmed that updating the timestamps is acceptable, and will not
cause unwanted side effects.
- [x] Ensured the correct collation is explicitly set for character
columns (`COLLATE utf8mb4_unicode_ci`).
- [x] Added/updated automated tests
- Did not do manual QA since the SCEP client I have doesn't support ECC.
Will rely on next subtasks for manual QA.

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **New Features**
* Introduced Host Identity SCEP (Simple Certificate Enrollment Protocol)
support, enabling secure host identity certificate enrollment and
management.
* Added new API endpoints for Host Identity SCEP, including certificate
issuance and retrieval.
* Implemented MySQL-backed storage and management for host identity SCEP
certificates and serials.
* Added new database tables for storing host identity SCEP certificates
and serial numbers.
* Provided utilities for encoding certificates and keys, and handling
ECDSA public keys.

* **Bug Fixes**
  * None.

* **Tests**
* Added comprehensive integration and unit tests for Host Identity SCEP
functionality, including certificate issuance, validation, and error
scenarios.

* **Chores**
* Updated test utilities to support unique test names and new SCEP
storage options.
* Extended mock datastore and interfaces for new host identity
certificate methods.

* **Documentation**
* Added comments and documentation for new SCEP-related interfaces,
methods, and database schema changes.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2025-07-11 11:44:07 -03:00

86 lines
2.7 KiB
Go

package integrationtest
import (
"context"
"net/http/httptest"
"testing"
"github.com/fleetdm/fleet/v4/server/config"
"github.com/fleetdm/fleet/v4/server/datastore/mysql"
"github.com/fleetdm/fleet/v4/server/datastore/mysql/common_mysql/testing_utils"
"github.com/fleetdm/fleet/v4/server/datastore/redis/redistest"
"github.com/fleetdm/fleet/v4/server/fleet"
"github.com/fleetdm/fleet/v4/server/service"
"github.com/fleetdm/fleet/v4/server/test"
"github.com/go-kit/log"
"github.com/stretchr/testify/require"
)
type BaseSuite struct {
Logger log.Logger
FleetCfg config.FleetConfig
Server *httptest.Server
DS *mysql.Datastore
Users map[string]fleet.User
Token string
cachedAdminToken string
}
func (s *BaseSuite) GetTestAdminToken(t *testing.T) string {
// because the login endpoint is rate-limited, use the cached admin token
// if available (if for some reason a test needs to logout the admin user,
// then set cachedAdminToken = "" so that a new token is retrieved).
if s.cachedAdminToken == "" {
s.cachedAdminToken = s.GetTestToken(t, service.TestAdminUserEmail, test.GoodPassword)
}
return s.cachedAdminToken
}
func (s *BaseSuite) GetTestToken(t *testing.T, email string, password string) string {
return service.GetToken(t, email, password, s.Server.URL)
}
func SetUpServerURL(t *testing.T, ds *mysql.Datastore, server *httptest.Server) {
appConf, err := ds.AppConfig(t.Context())
require.NoError(t, err)
appConf.ServerSettings.ServerURL = server.URL
err = ds.SaveAppConfig(t.Context(), appConf)
require.NoError(t, err)
}
func SetUpMySQLAndService(t *testing.T, uniqueTestName string, opts ...*service.TestServerOpts) (
*mysql.Datastore,
config.FleetConfig,
fleet.Service, context.Context,
) {
ds := mysql.CreateMySQLDSWithOptions(t, &testing_utils.DatastoreTestOptions{
UniqueTestName: uniqueTestName,
})
test.AddAllHostsLabel(t, ds)
// Set up the required fields on AppConfig
appConf, err := ds.AppConfig(testContext())
require.NoError(t, err)
appConf.OrgInfo.OrgName = "FleetTest"
appConf.ServerSettings.ServerURL = "https://example.org"
err = ds.SaveAppConfig(testContext(), appConf)
require.NoError(t, err)
fleetCfg := config.TestConfig()
fleetSvc, ctx := service.NewTestService(t, ds, fleetCfg, opts...)
return ds, fleetCfg, fleetSvc, ctx
}
func SetUpMySQLAndRedisAndService(t *testing.T, uniqueTestName string, opts ...*service.TestServerOpts) (*mysql.Datastore, fleet.RedisPool,
config.FleetConfig,
fleet.Service, context.Context,
) {
redisPool := redistest.SetupRedis(t, uniqueTestName, false, false, false)
ds, fleetCfg, fleetSvc, ctx := SetUpMySQLAndService(t, uniqueTestName, opts...)
return ds, redisPool, fleetCfg, fleetSvc, ctx
}
func testContext() context.Context {
return context.Background()
}