Skip to content

Commit

Permalink
update AWS RDS db e2e tests
Browse files Browse the repository at this point in the history
* update GHA workflow env vars to be RDS specific
* add auto db user provisioning tests
* provision any db users needed in test code as setup, instead of
  relying on the cloud-terraform repo provisioning them
  • Loading branch information
GavinFrazar committed Mar 30, 2024
1 parent 4690eea commit 953e090
Show file tree
Hide file tree
Showing 7 changed files with 582 additions and 201 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/aws-e2e-tests-non-root.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ env:
KUBERNETES_SERVICE_ASSUME_ROLE: arn:aws:iam::307493967395:role/tf-eks-discovery-ci-cluster-kubernetes-service-access-role
KUBE_DISCOVERY_SERVICE_ASSUME_ROLE: arn:aws:iam::307493967395:role/tf-eks-discovery-ci-cluster-discovery-service-access-role
EKS_CLUSTER_NAME: gha-discovery-ci-eks-us-west-2-307493967395
DATABASE_USER: teleport-ci-e2e-test
DATABASE_SERVICE_ASSUME_ROLE: arn:aws:iam::307493967395:role/ci-database-e2e-tests-database-svc
DATABASE_DISCOVERY_SERVICE_ASSUME_ROLE: arn:aws:iam::307493967395:role/ci-database-e2e-tests-discovery-svc
RDS_ACCESS_ROLE: arn:aws:iam::307493967395:role/ci-database-e2e-tests-rds-access
RDS_DISCOVERY_ROLE: arn:aws:iam::307493967395:role/ci-database-e2e-tests-rds-discovery
RDS_POSTGRES_INSTANCE_NAME: ci-database-e2e-tests-rds-postgres-instance-us-west-2-307493967395
RDS_MYSQL_INSTANCE_NAME: ci-database-e2e-tests-rds-mysql-instance-us-west-2-307493967395
DISCOVERY_MATCHER_LABELS: "*=*"
Expand Down
4 changes: 2 additions & 2 deletions e2e/aws/eks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func awsEKSDiscoveryMatchedCluster(t *testing.T) {
// the permissions are correct.
kubeClient, _, err := kube.ProxyClient(kube.ProxyConfig{
T: teleport,
Username: username,
Username: hostUser,
KubeUsers: kubeUsers,
KubeGroups: kubeGroups,
KubeCluster: clusters[0].GetName(),
Expand Down Expand Up @@ -182,7 +182,7 @@ func awsEKSDiscoveryUnmatchedCluster(t *testing.T) {
// clusters.
func withFullKubeAccessUserRole(t *testing.T) testOptionsFunc {
// Create a new role with full access to all kube clusters.
return withUserRole(t, "kubemaster", types.RoleSpecV6{
return withUserRole(t, hostUser, "kubemaster", types.RoleSpecV6{
Allow: types.RoleConditions{
KubeGroups: kubeGroups,
KubeUsers: kubeUsers,
Expand Down
66 changes: 50 additions & 16 deletions e2e/aws/fixtures_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ import (
"github.com/gravitational/teleport/lib/utils"
)

// username is the name of the host user used for tests.
var username string
// hostUser is the name of the host user used for tests.
var hostUser string

func init() {
me, err := user.Current()
if err != nil {
panic(err)
}
username = me.Username
hostUser = me.Username
}

// mustGetEnv is a test helper that fetches an env variable or fails with an
Expand All @@ -67,6 +67,13 @@ func mustGetDiscoveryMatcherLabels(t *testing.T) types.Labels {
return out
}

func mustGetDBAdmin(t *testing.T, db types.Database) types.DatabaseAdminUser {
t.Helper()
adminUser := db.GetAdminUser()
require.NotEmpty(t, adminUser.Name, "unknown db auto user provisioning admin, should have been imported using the %q label", types.DatabaseAdminLabel)
return adminUser
}

// testOptionsFunc is a test option configuration func.
type testOptionsFunc func(*testOptions)

Expand All @@ -78,15 +85,16 @@ type testOptions struct {
// serviceConfigFuncs are a list of functions that configure the Teleport
// cluster before it starts.
serviceConfigFuncs []func(*servicecfg.Config)
// userRoles are roles that will be bootstrapped and added to the Teleport
// user under test.
userRoles []types.Role
// userRoles is a map from username to that user's roles that will be
// bootstrapped and added to the Teleport test cluster.
userRoles map[string][]types.Role
}

// createTeleportCluster sets up a Teleport cluster for tests.
func createTeleportCluster(t *testing.T, opts ...testOptionsFunc) *helpers.TeleInstance {
t.Helper()
var options testOptions
options.userRoles = make(map[string][]types.Role)
for _, opt := range opts {
opt(&options)
}
Expand All @@ -98,7 +106,9 @@ func createTeleportCluster(t *testing.T, opts ...testOptionsFunc) *helpers.TeleI
teleport := helpers.NewInstance(t, cfg)

// Create a new user with the role created above.
teleport.AddUserWithRole(username, options.userRoles...)
for name, roles := range options.userRoles {
teleport.AddUserWithRole(name, roles...)
}

tconf := newTeleportConfig(t)
for _, optFn := range options.serviceConfigFuncs {
Expand Down Expand Up @@ -149,13 +159,13 @@ func newTeleportConfig(t *testing.T) *servicecfg.Config {

// withUserRole creates a new role that will be bootstraped and then granted to
// the Teleport user under test.
func withUserRole(t *testing.T, name string, spec types.RoleSpecV6) testOptionsFunc {
func withUserRole(t *testing.T, user, name string, spec types.RoleSpecV6) testOptionsFunc {
t.Helper()
// Create a new role with full access to all databases.
role, err := types.NewRole(name, spec)
require.NoError(t, err)
return func(options *testOptions) {
options.userRoles = append(options.userRoles, role)
options.userRoles[user] = append(options.userRoles[user], role)
}
}

Expand Down Expand Up @@ -208,11 +218,35 @@ func withDatabaseService(t *testing.T, matchers ...services.ResourceMatcher) tes
func withFullDatabaseAccessUserRole(t *testing.T) testOptionsFunc {
t.Helper()
// Create a new role with full access to all databases.
return withUserRole(t, "db-access", types.RoleSpecV6{
Allow: types.RoleConditions{
DatabaseLabels: types.Labels{types.Wildcard: []string{types.Wildcard}},
DatabaseUsers: []string{types.Wildcard},
DatabaseNames: []string{types.Wildcard},
},
})
return withUserRole(t, hostUser, "db-access", allowDatabaseAccessRoleSpec)
}

var allowDatabaseAccessRoleSpec = types.RoleSpecV6{
Allow: types.RoleConditions{
DatabaseLabels: types.Labels{types.Wildcard: []string{types.Wildcard}},
DatabaseUsers: []string{types.Wildcard},
DatabaseNames: []string{types.Wildcard},
},
}

var autoDBUserKeepSpec = types.RoleSpecV6{
Allow: types.RoleConditions{
DatabaseLabels: types.Labels{types.Wildcard: []string{types.Wildcard}},
DatabaseUsers: []string{types.Wildcard},
DatabaseNames: []string{types.Wildcard},
},
Options: types.RoleOptions{
CreateDatabaseUserMode: types.CreateDatabaseUserMode_DB_USER_MODE_KEEP,
},
}

var autoDBUserDropSpec = types.RoleSpecV6{
Allow: types.RoleConditions{
DatabaseLabels: types.Labels{types.Wildcard: []string{types.Wildcard}},
DatabaseUsers: []string{types.Wildcard},
DatabaseNames: []string{types.Wildcard},
},
Options: types.RoleOptions{
CreateDatabaseUserMode: types.CreateDatabaseUserMode_DB_USER_MODE_BEST_EFFORT_DROP,
},
}
19 changes: 8 additions & 11 deletions e2e/aws/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,15 @@ const (
// discoveryMatcherLabelsEnv is the env variable that specifies the matcher
// labels to use in test discovery services.
discoveryMatcherLabelsEnv = "DISCOVERY_MATCHER_LABELS"
// dbSvcRoleARNEnv is the environment variable that specifies the IAM role
// that Teleport Database Service will assume to access databases.
// check modules/databases-ci/ from cloud-terraform repo for more details.
dbSvcRoleARNEnv = "DATABASE_SERVICE_ASSUME_ROLE"
// dbDiscoverySvcRoleARNEnv is the environment variable that specifies the
// rdsAccessRoleEnv is the environment variable that specifies the IAM role
// that Teleport Database Service will assume to access RDS databases.
// See modules/databases-ci/ from cloud-terraform repo for more details.
rdsAccessRoleEnv = "RDS_ACCESS_ROLE"
// rdsDiscoveryRoleEnv is the environment variable that specifies the
// IAM role that Teleport Discovery Service will assume to discover
// databases.
// check modules/databases-ci/ from cloud-terraform repo for more details.
dbDiscoverySvcRoleARNEnv = "DATABASE_DISCOVERY_SERVICE_ASSUME_ROLE"
// dbUserEnv is the database user configured in databases for access via
// Teleport.
dbUserEnv = "DATABASE_USER"
// RDS databases.
// See modules/databases-ci/ from cloud-terraform repo for more details.
rdsDiscoveryRoleEnv = "RDS_DISCOVERY_ROLE"
// rdsPostgresInstanceNameEnv is the environment variable that specifies the
// name of the RDS Postgres DB instance that will be created by the Teleport
// Discovery Service.
Expand Down
Loading

0 comments on commit 953e090

Please sign in to comment.