@@ -29,8 +29,6 @@ import (
29
29
"testing"
30
30
"time"
31
31
32
- "github.com/google/uuid"
33
- "github.com/sirupsen/logrus"
34
32
"github.com/stretchr/testify/assert"
35
33
"github.com/stretchr/testify/require"
36
34
"go.uber.org/zap/zapcore"
@@ -50,17 +48,18 @@ import (
50
48
51
49
"github.com/gravitational/teleport/api/client"
52
50
"github.com/gravitational/teleport/api/types"
53
- "github.com/gravitational/teleport/entitlements"
54
51
"github.com/gravitational/teleport/integration/helpers"
55
52
resourcesv1 "github.com/gravitational/teleport/integrations/operator/apis/resources/v1"
56
53
resourcesv2 "github.com/gravitational/teleport/integrations/operator/apis/resources/v2"
57
54
resourcesv3 "github.com/gravitational/teleport/integrations/operator/apis/resources/v3"
58
55
resourcesv5 "github.com/gravitational/teleport/integrations/operator/apis/resources/v5"
59
56
"github.com/gravitational/teleport/integrations/operator/controllers"
60
57
"github.com/gravitational/teleport/integrations/operator/controllers/resources"
58
+ "github.com/gravitational/teleport/lib/auth"
61
59
"github.com/gravitational/teleport/lib/defaults"
62
60
"github.com/gravitational/teleport/lib/modules"
63
61
"github.com/gravitational/teleport/lib/service/servicecfg"
62
+ "github.com/gravitational/teleport/lib/services"
64
63
"github.com/gravitational/teleport/lib/srv/db/common"
65
64
"github.com/gravitational/teleport/lib/srv/db/postgres"
66
65
"github.com/gravitational/teleport/lib/utils"
@@ -103,73 +102,138 @@ func ValidRandomResourceName(prefix string) string {
103
102
return prefix + string (b )
104
103
}
105
104
106
- func defaultTeleportServiceConfig (t * testing.T ) (* helpers.TeleInstance , string ) {
107
- modules .SetTestModules (t , & modules.TestModules {
108
- TestBuildType : modules .BuildEnterprise ,
109
- TestFeatures : modules.Features {
110
- Entitlements : map [entitlements.EntitlementKind ]modules.EntitlementInfo {
111
- entitlements .OIDC : {Enabled : true },
112
- entitlements .SAML : {Enabled : true },
113
- },
114
- },
105
+ func startPostgresTestServer (t * testing.T , authServer * auth.Server ) * postgres.TestServer {
106
+ postgresTestServer , err := postgres .NewTestServer (common.TestServerConfig {
107
+ AuthClient : authServer ,
115
108
})
109
+ require .NoError (t , err )
116
110
117
- teleportServer := helpers .NewInstance (t , helpers.InstanceConfig {
118
- ClusterName : "root.example.com" ,
119
- HostID : uuid .New ().String (),
120
- NodeName : helpers .Loopback ,
121
- Log : logrus .StandardLogger (),
111
+ go func () {
112
+ t .Logf ("Postgres Fake server running at %s port" , postgresTestServer .Port ())
113
+ assert .NoError (t , postgresTestServer .Serve ())
114
+ }()
115
+ t .Cleanup (func () {
116
+ postgresTestServer .Close ()
122
117
})
123
118
124
- rcConf := servicecfg .MakeDefaultConfig ()
125
- rcConf .DataDir = t .TempDir ()
126
- rcConf .Auth .Enabled = true
127
- rcConf .Proxy .Enabled = true
128
- rcConf .Proxy .DisableWebInterface = true
129
- rcConf .SSH .Enabled = true
130
- rcConf .Version = "v2"
131
-
132
- rcConf .Auth .StaticTokens , _ = types .NewStaticTokens (types.StaticTokensSpecV2 {
133
- StaticTokens : []types.ProvisionTokenV1 {{
134
- Roles : []types.SystemRole {types .RoleDatabase },
135
- Expires : time .Now ().Add (time .Hour ),
136
- Token : "token" ,
137
- }},
119
+ return postgresTestServer
120
+ }
121
+ func doCopyPastedTest (t * testing.T ) {
122
+ modules .SetInsecureTestMode (true )
123
+
124
+ ctx := context .Background ()
125
+
126
+ // Start Teleport Auth and Proxy services
127
+ authProcess , proxyProcess , provisionToken := helpers .MakeTestServers (t )
128
+ authServer := authProcess .GetAuthServer ()
129
+ proxyAddr , err := proxyProcess .ProxyWebAddr ()
130
+ require .NoError (t , err )
131
+
132
+ // Start Fake Postgres Database
133
+ postgresTestServer := startPostgresTestServer (t , authServer )
134
+
135
+ // Start Teleport Database Service
136
+ databaseResourceName := "mypsqldb"
137
+ databaseDBName := "dbname"
138
+ databaseDBUser := "dbuser"
139
+ helpers .MakeTestDatabaseServer (t , * proxyAddr , provisionToken , nil /* resource matchers */ , servicecfg.Database {
140
+ Name : databaseResourceName ,
141
+ Protocol : defaults .ProtocolPostgres ,
142
+ URI : net .JoinHostPort ("localhost" , postgresTestServer .Port ()),
138
143
})
144
+ // Wait for the Database Server to be registered
145
+ waitForDatabases (t , func (ctx context.Context , name string ) ([]types.DatabaseServer , error ) {
146
+ return authServer .GetDatabaseServers (ctx , name )
147
+ }, databaseResourceName )
139
148
140
- roleName := ValidRandomResourceName ("role-" )
141
- unrestricted := []string {"list" , "create" , "read" , "update" , "delete" }
142
- role , err := types .NewRole (roleName , types.RoleSpecV6 {
149
+ roleWithFullAccess , err := types .NewRole ("fullaccess" , types.RoleSpecV6 {
143
150
Allow : types.RoleConditions {
144
- // the operator has wildcard node labs to be able to see them
145
- // but has no login allowed, so it cannot SSH into them
146
- NodeLabels : types.Labels {"*" : []string {"*" }},
151
+ Namespaces : []string {"default" },
152
+ DatabaseLabels : types.Labels {types .Wildcard : []string {types .Wildcard }},
147
153
Rules : []types.Rule {
148
- types .NewRule (types .KindRole , unrestricted ),
149
- types .NewRule (types .KindUser , unrestricted ),
150
- types .NewRule (types .KindAuthConnector , unrestricted ),
151
- types .NewRule (types .KindLoginRule , unrestricted ),
152
- types .NewRule (types .KindToken , unrestricted ),
153
- types .NewRule (types .KindOktaImportRule , unrestricted ),
154
- types .NewRule (types .KindAccessList , unrestricted ),
155
- types .NewRule (types .KindNode , unrestricted ),
156
- types .NewRule (types .KindDatabase , unrestricted ),
157
- },
158
- Impersonate : & types.ImpersonateConditions {
159
- Users : []string {"Db" },
160
- Roles : []string {"Db" },
154
+ types .NewRule (types .KindConnectionDiagnostic , services .RW ()),
161
155
},
156
+ DatabaseUsers : []string {databaseDBUser },
157
+ DatabaseNames : []string {databaseDBName },
162
158
},
163
159
})
164
160
require .NoError (t , err )
161
+ roleWithFullAccess , err = authServer .UpsertRole (ctx , roleWithFullAccess )
162
+ require .NoError (t , err )
163
+ }
164
+
165
+ func defaultTeleportServiceConfig (t * testing.T ) (* helpers.TeleInstance , string ) {
166
+ // modules.SetTestModules(t, &modules.TestModules{
167
+ // TestBuildType: modules.BuildEnterprise,
168
+ // TestFeatures: modules.Features{
169
+ // Entitlements: map[entitlements.EntitlementKind]modules.EntitlementInfo{
170
+ // entitlements.OIDC: {Enabled: true},
171
+ // entitlements.SAML: {Enabled: true},
172
+ // },
173
+ // },
174
+ // })
175
+
176
+ // teleportServer := helpers.NewInstance(t, helpers.InstanceConfig{
177
+ // ClusterName: "root.example.com",
178
+ // HostID: uuid.New().String(),
179
+ // NodeName: helpers.Loopback,
180
+ // Log: logrus.StandardLogger(),
181
+ // })
182
+
183
+ // rcConf := servicecfg.MakeDefaultConfig()
184
+ // rcConf.DataDir = t.TempDir()
185
+ // rcConf.Auth.Enabled = true
186
+ // rcConf.Proxy.Enabled = true
187
+ // rcConf.Proxy.DisableWebInterface = true
188
+ // rcConf.SSH.Enabled = true
189
+ // rcConf.Version = "v2"
190
+
191
+ // rcConf.Auth.StaticTokens, _ = types.NewStaticTokens(types.StaticTokensSpecV2{
192
+ // StaticTokens: []types.ProvisionTokenV1{{
193
+ // Roles: []types.SystemRole{types.RoleDatabase},
194
+ // Expires: time.Now().Add(time.Hour),
195
+ // Token: "token",
196
+ // }},
197
+ // })
198
+
199
+ // roleName := ValidRandomResourceName("role-")
200
+ // unrestricted := []string{"list", "create", "read", "update", "delete"}
201
+ // role, err := types.NewRole(roleName, types.RoleSpecV6{
202
+ // Allow: types.RoleConditions{
203
+ // // the operator has wildcard node labs to be able to see them
204
+ // // but has no login allowed, so it cannot SSH into them
205
+ // NodeLabels: types.Labels{"*": []string{"*"}},
206
+ // Rules: []types.Rule{
207
+ // types.NewRule(types.KindRole, unrestricted),
208
+ // types.NewRule(types.KindUser, unrestricted),
209
+ // types.NewRule(types.KindAuthConnector, unrestricted),
210
+ // types.NewRule(types.KindLoginRule, unrestricted),
211
+ // types.NewRule(types.KindToken, unrestricted),
212
+ // types.NewRule(types.KindOktaImportRule, unrestricted),
213
+ // types.NewRule(types.KindAccessList, unrestricted),
214
+ // types.NewRule(types.KindNode, unrestricted),
215
+ // types.NewRule(types.KindDatabase, unrestricted),
216
+ // },
217
+ // Impersonate: &types.ImpersonateConditions{
218
+ // Users: []string{"Db"},
219
+ // Roles: []string{"Db"},
220
+ // },
221
+ // },
222
+ // })
223
+ // require.NoError(t, err)
165
224
166
- operatorName := ValidRandomResourceName ("operator-" )
167
- _ = teleportServer .AddUserWithRole (operatorName , role )
225
+ // operatorName := ValidRandomResourceName("operator-")
226
+ // _ = teleportServer.AddUserWithRole(operatorName, role)
168
227
169
- err = teleportServer .CreateEx (t , nil , rcConf )
228
+ // err = teleportServer.CreateEx(t, nil, rcConf)
229
+ // require.NoError(t, err)
230
+
231
+ authProcess , proxyProcess , provisionToken := helpers .MakeTestServers (t )
232
+ authServer := authProcess .GetAuthServer ()
233
+ proxyAddr , err := proxyProcess .ProxyWebAddr ()
170
234
require .NoError (t , err )
171
235
172
- return teleportServer , operatorName
236
+ return teleportServer , ValidRandomResourceName ( "operator-" )
173
237
}
174
238
175
239
func FastEventually (t * testing.T , condition func () bool ) {
@@ -280,7 +344,7 @@ func setupMockPostgresServer(t *testing.T, setup *TestSetup) {
280
344
URI : net .JoinHostPort ("localhost" , postgresTestServer .Port ()),
281
345
})
282
346
283
- waitForDatabases (t , setup , databaseResourceName )
347
+ waitForDatabases (t , setup . TeleportClient . GetDatabaseServers , databaseResourceName )
284
348
285
349
// server, err := clickhouse.NewTestServer(common.TestServerConfig{
286
350
// AuthClient: setup.TeleportClient,
@@ -296,11 +360,11 @@ func setupMockPostgresServer(t *testing.T, setup *TestSetup) {
296
360
// }
297
361
}
298
362
299
- func waitForDatabases (t * testing.T , setup * TestSetup , dbNames ... string ) {
363
+ func waitForDatabases (t * testing.T , GetDatabaseServers func ( ctx context. Context , name string ) ([]types. DatabaseServer , error ) , dbNames ... string ) {
300
364
ctx := context .Background ()
301
365
302
366
require .Eventually (t , func () bool {
303
- all , err := setup . TeleportClient . GetDatabaseServers (ctx , "default" )
367
+ all , err := GetDatabaseServers (ctx , "default" )
304
368
assert .NoError (t , err )
305
369
306
370
if len (dbNames ) > len (all ) {
@@ -311,7 +375,6 @@ func waitForDatabases(t *testing.T, setup *TestSetup, dbNames ...string) {
311
375
for _ , db := range dbNames {
312
376
for _ , a := range all {
313
377
if a .GetName () == db {
314
- require .FailNow (t , "Got db: %#v" , a )
315
378
registered ++
316
379
break
317
380
}
@@ -368,6 +431,8 @@ func StepByStep(setup *TestSetup) {
368
431
369
432
// SetupTestEnv creates a Kubernetes server, a teleport server and starts the operator
370
433
func SetupTestEnv (t * testing.T , opts ... TestOption ) * TestSetup {
434
+ doCopyPastedTest (t )
435
+
371
436
// Hack to get the path of this file in order to find the crd path no matter
372
437
// where this is called from.
373
438
_ , thisFileName , _ , _ := runtime .Caller (0 )
0 commit comments