@@ -54,6 +54,7 @@ const (
54
54
errUpdateUser = "cannot update user"
55
55
errGetPasswordSecretFailed = "cannot get password secret"
56
56
errCompareResourceOptions = "cannot compare desired and observed resource options"
57
+ errAuthPluginNotSupported = "auth plugin not supported"
57
58
58
59
maxConcurrency = 5
59
60
)
@@ -238,21 +239,41 @@ func (c *external) Create(ctx context.Context, mg resource.Managed) (managed.Ext
238
239
cr .SetConditions (xpv1 .Creating ())
239
240
240
241
username , host := mysql .SplitUserHost (meta .GetExternalName (cr ))
241
- pw , _ , err := c .getPassword (ctx , cr )
242
- if err != nil {
243
- return managed.ExternalCreation {}, err
242
+
243
+ var auth string
244
+
245
+ ro := resourceOptionsToClauses (cr .Spec .ForProvider .ResourceOptions )
246
+ binlog := cr .Spec .ForProvider .BinLog
247
+
248
+ var authplugin string
249
+ if cr .Spec .ForProvider .AuthPlugin != "" {
250
+ authplugin = cr .Spec .ForProvider .AuthPlugin
251
+ } else {
252
+ authplugin = "mysql_native_password"
244
253
}
254
+ var pw string
245
255
246
- if pw == "" {
247
- pw , err = password .Generate ()
256
+ if authplugin == "mysql_native_password" {
257
+ var err error
258
+ pw , _ , err = c .getPassword (ctx , cr )
248
259
if err != nil {
249
260
return managed.ExternalCreation {}, err
250
261
}
262
+
263
+ if pw == "" {
264
+ pw , err = password .Generate ()
265
+ if err != nil {
266
+ return managed.ExternalCreation {}, err
267
+ }
268
+ }
269
+ auth = fmt .Sprintf ("%s BY %s" , authplugin , mysql .QuoteValue (pw ))
270
+ } else if authplugin == "AWSAuthenticationPlugin" {
271
+ auth = fmt .Sprintf ("%s AS %s" , authplugin , mysql .QuoteValue ("RDS" ))
272
+ } else {
273
+ return managed.ExternalCreation {}, errors .New (errAuthPluginNotSupported )
251
274
}
252
275
253
- ro := resourceOptionsToClauses (cr .Spec .ForProvider .ResourceOptions )
254
- binlog := cr .Spec .ForProvider .BinLog
255
- if err := c .executeCreateUserQuery (ctx , username , host , ro , pw , binlog ); err != nil {
276
+ if err := c .executeCreateUserQuery (ctx , username , host , ro , auth , binlog ); err != nil {
256
277
return managed.ExternalCreation {}, err
257
278
}
258
279
@@ -265,19 +286,20 @@ func (c *external) Create(ctx context.Context, mg resource.Managed) (managed.Ext
265
286
}, nil
266
287
}
267
288
268
- func (c * external ) executeCreateUserQuery (ctx context.Context , username string , host string , resourceOptionsClauses []string , pw string , binlog * bool ) error {
289
+ func (c * external ) executeCreateUserQuery (ctx context.Context , username string , host string , resourceOptionsClauses []string , auth string , binlog * bool ) error {
269
290
resourceOptions := ""
270
291
if len (resourceOptionsClauses ) != 0 {
271
292
resourceOptions = fmt .Sprintf (" WITH %s" , strings .Join (resourceOptionsClauses , " " ))
272
293
}
273
294
274
295
query := fmt .Sprintf (
275
- "CREATE USER %s@%s IDENTIFIED BY %s%s" ,
296
+ "CREATE USER %s@%s IDENTIFIED WITH %s%s" ,
276
297
mysql .QuoteValue (username ),
277
298
mysql .QuoteValue (host ),
278
- mysql . QuoteValue ( pw ) ,
299
+ auth ,
279
300
resourceOptions ,
280
301
)
302
+ fmt .Println (query )
281
303
282
304
if err := mysql .ExecWithBinlogAndFlush (ctx , c .db , mysql.ExecQuery {Query : query , ErrorValue : errCreateUser }, mysql.ExecOptions {Binlog : binlog }); err != nil {
283
305
return err
0 commit comments