Skip to content

Commit ace77d9

Browse files
committed
feat(mysql): add optional authPlugin parameter for MySQL user creation
1 parent 33fb91c commit ace77d9

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

apis/mysql/v1alpha1/user_types.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ 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"`
52+
53+
// AuthPlugin defines the MySQL authentication plugin.
54+
// Supported values: "mysql_native_password", "caching_sha2_password", "AWSAuthenticationPlugin".
55+
// +kubebuilder:validation:Enum=mysql_native_password;caching_sha2_password;AWSAuthenticationPlugin
56+
// +optional
57+
AuthPlugin string `json:"authPlugin,omitempty,"`
5258
}
5359

5460
// ResourceOptions define the account specific resource limits.

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,15 @@ 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 authentication plugin.
77+
Supported values: "mysql_native_password", "caching_sha2_password", "AWSAuthenticationPlugin".
78+
enum:
79+
- mysql_native_password
80+
- caching_sha2_password
81+
- AWSAuthenticationPlugin
82+
type: string
7483
binlog:
7584
description: BinLog defines whether the create, delete, update
7685
operations of this user are propagated to replicas. Defaults

pkg/controller/mysql/user/reconciler.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,10 @@ func (c *external) Create(ctx context.Context, mg resource.Managed) (managed.Ext
259259
}
260260

261261
ro := resourceOptionsToClauses(cr.Spec.ForProvider.ResourceOptions)
262-
if err := c.executeCreateUserQuery(ctx, username, host, ro, pw); err != nil {
262+
263+
authplugin := cr.Spec.ForProvider.AuthPlugin
264+
265+
if err := c.executeCreateUserQuery(ctx, username, host, ro, pw, authplugin); err != nil {
263266
return managed.ExternalCreation{}, err
264267
}
265268

@@ -272,17 +275,30 @@ func (c *external) Create(ctx context.Context, mg resource.Managed) (managed.Ext
272275
}, nil
273276
}
274277

275-
func (c *external) executeCreateUserQuery(ctx context.Context, username string, host string, resourceOptionsClauses []string, pw string) error {
278+
func (c *external) executeCreateUserQuery(ctx context.Context, username string, host string, resourceOptionsClauses []string, pw string, authplugin string) error {
276279
resourceOptions := ""
277280
if len(resourceOptionsClauses) != 0 {
278281
resourceOptions = fmt.Sprintf(" WITH %s", strings.Join(resourceOptionsClauses, " "))
279282
}
280283

284+
var authStm string
285+
286+
if len(authplugin) > 0 {
287+
switch authplugin {
288+
case "mysql_native_password", "caching_sha2_password":
289+
authStm = fmt.Sprintf("WITH %s BY %s", authplugin, mysql.QuoteValue(pw))
290+
case "AWSAuthenticationPlugin":
291+
authStm = fmt.Sprintf("WITH %s AS %s", authplugin, mysql.QuoteValue("RDS"))
292+
}
293+
} else {
294+
authStm = fmt.Sprintf("BY %s", mysql.QuoteValue(pw))
295+
}
296+
281297
query := fmt.Sprintf(
282-
"CREATE USER %s@%s IDENTIFIED BY %s%s",
298+
"CREATE USER %s@%s IDENTIFIED %s%s",
283299
mysql.QuoteValue(username),
284300
mysql.QuoteValue(host),
285-
mysql.QuoteValue(pw),
301+
authStm,
286302
resourceOptions,
287303
)
288304

0 commit comments

Comments
 (0)