Skip to content

Commit 8004d67

Browse files
committed
fix crossplane-contrib#106 add ability to set auth_plugin for mysql users
Signed-off-by: cten <[email protected]>
1 parent 3d42ddb commit 8004d67

File tree

3 files changed

+39
-9
lines changed

3 files changed

+39
-9
lines changed

apis/mysql/v1alpha1/user_types.go

+5
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ type UserParameters struct {
4949
// BinLog defines whether the create, delete, update operations of this user are propagated to replicas. Defaults to true
5050
// +optional
5151
BinLog *bool `json:"binlog,omitempty" default:"true"`
52+
53+
// AuthPlugin defines the MySQL auth plugin (ie. AWSAuthenticationPlugin for AWS IAM authentication when using AWS RDS )
54+
// See https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.IAMDBAuth.DBAccounts.html
55+
// +optional
56+
AuthPlugin string `json:"authPlugin,omitempty" default:"mysql_native_password"`
5257
}
5358

5459
// ResourceOptions define the account specific resource limits.

package/crds/mysql.sql.crossplane.io_users.yaml

+5
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ spec:
7171
description: UserParameters define the desired state of a MySQL user
7272
instance.
7373
properties:
74+
authPlugin:
75+
description: |-
76+
AuthPlugin defines the MySQL auth plugin (ie. AWSAuthenticationPlugin for AWS IAM authentication when using AWS RDS )
77+
See https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.IAMDBAuth.DBAccounts.html
78+
type: string
7479
binlog:
7580
description: BinLog defines whether the create, delete, update
7681
operations of this user are propagated to replicas. Defaults

pkg/controller/mysql/user/reconciler.go

+29-9
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ const (
5454
errUpdateUser = "cannot update user"
5555
errGetPasswordSecretFailed = "cannot get password secret"
5656
errCompareResourceOptions = "cannot compare desired and observed resource options"
57+
errAuthPluginNotSupported = "auth plugin not supported"
5758

5859
maxConcurrency = 5
5960
)
@@ -238,20 +239,39 @@ func (c *external) Create(ctx context.Context, mg resource.Managed) (managed.Ext
238239
cr.SetConditions(xpv1.Creating())
239240

240241
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+
var authplugin string
246+
247+
if cr.Spec.ForProvider.AuthPlugin != "" {
248+
authplugin = cr.Spec.ForProvider.AuthPlugin
244249
}
250+
var pw string
245251

246-
if pw == "" {
247-
pw, err = password.Generate()
252+
switch authplugin {
253+
case "":
254+
var err error
255+
pw, _, err = c.getPassword(ctx, cr)
248256
if err != nil {
249257
return managed.ExternalCreation{}, err
250258
}
259+
260+
if pw == "" {
261+
pw, err = password.Generate()
262+
if err != nil {
263+
return managed.ExternalCreation{}, err
264+
}
265+
}
266+
auth = fmt.Sprintf("BY %s", mysql.QuoteValue(pw))
267+
case "AWSAuthenticationPlugin":
268+
auth = fmt.Sprintf("WITH %s AS %s", authplugin, mysql.QuoteValue("RDS"))
269+
default:
270+
return managed.ExternalCreation{}, errors.New(errAuthPluginNotSupported)
251271
}
252272

253273
ro := resourceOptionsToClauses(cr.Spec.ForProvider.ResourceOptions)
254-
if err := c.executeCreateUserQuery(ctx, username, host, ro, pw); err != nil {
274+
if err := c.executeCreateUserQuery(ctx, username, host, ro, auth); err != nil {
255275
return managed.ExternalCreation{}, err
256276
}
257277

@@ -264,17 +284,17 @@ func (c *external) Create(ctx context.Context, mg resource.Managed) (managed.Ext
264284
}, nil
265285
}
266286

267-
func (c *external) executeCreateUserQuery(ctx context.Context, username string, host string, resourceOptionsClauses []string, pw string) error {
287+
func (c *external) executeCreateUserQuery(ctx context.Context, username string, host string, resourceOptionsClauses []string, auth string) error {
268288
resourceOptions := ""
269289
if len(resourceOptionsClauses) != 0 {
270290
resourceOptions = fmt.Sprintf(" WITH %s", strings.Join(resourceOptionsClauses, " "))
271291
}
272292

273293
query := fmt.Sprintf(
274-
"CREATE USER %s@%s IDENTIFIED BY %s%s",
294+
"CREATE USER %s@%s IDENTIFIED %s%s",
275295
mysql.QuoteValue(username),
276296
mysql.QuoteValue(host),
277-
mysql.QuoteValue(pw),
297+
auth,
278298
resourceOptions,
279299
)
280300

0 commit comments

Comments
 (0)