Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add the option to iamAuthentication in aws #160

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ spec:
type: string
role:
type: string
iamAuthentication:
type: boolean
default: false
secretName:
type: string
secretTemplate:
Expand Down
14 changes: 8 additions & 6 deletions pkg/apis/db/v1alpha1/postgresuser_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,21 @@ type PostgresUserSpec struct {
// +optional
SecretTemplate map[string]string `json:"secretTemplate,omitempty"` // key-value, where key is secret field, value is go template
// +optional
Privileges string `json:"privileges"`
Privileges string `json:"privileges"`
IamAuthentication bool `json:"iamAuthentication"`
// +optional
Annotations map[string]string `json:"annotations,omitempty"`
}

// PostgresUserStatus defines the observed state of PostgresUser
// +k8s:openapi-gen=true
type PostgresUserStatus struct {
Succeeded bool `json:"succeeded"`
PostgresRole string `json:"postgresRole"`
PostgresLogin string `json:"postgresLogin"`
PostgresGroup string `json:"postgresGroup"`
DatabaseName string `json:"databaseName"`
Succeeded bool `json:"succeeded"`
PostgresRole string `json:"postgresRole"`
PostgresLogin string `json:"postgresLogin"`
PostgresGroup string `json:"postgresGroup"`
DatabaseName string `json:"databaseName"`
IamAuthentication bool `json:"iamAuthentication"`
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "operator-sdk generate k8s" to regenerate code after modifying this file
// Add custom validation using kubebuilder tags: https://book.kubebuilder.io/beyond_basics/generating_crd.html
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/postgresuser/postgresuser_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func (r *ReconcilePostgresUser) Reconcile(request reconcile.Request) (reconcile.
// Create user role
suffix := utils.GetRandomString(6)
role = fmt.Sprintf("%s-%s", instance.Spec.Role, suffix)
login, err = r.pg.CreateUserRole(role, password)
login, err = r.pg.CreateUserRole(role, password, instance.spec.IamAuthentication)
if err != nil {
return r.requeue(instance, errors.NewInternalError(err))
}
Expand Down
10 changes: 8 additions & 2 deletions pkg/postgres/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,17 @@ func (c *awspg) CreateDB(dbname, role string) error {
return c.pg.CreateDB(dbname, role)
}

func (c *awspg) CreateUserRole(role, password string) (string, error) {
returnedRole, err := c.pg.CreateUserRole(role, password)
func (c *awspg) CreateUserRole(role, password string, iamAuthentication *bool) (string, error) {
returnedRole, err := c.pg.CreateUserRole(role, password, iamAuthentication)
if err != nil {
return "", err
}
if iamAuthentication != nil && *iamAuthentication {
err = c.GrantRole("rds_iam", role)
if err != nil {
return "", err
}
}
// On AWS RDS the postgres user isn't really superuser so he doesn't have permissions
// to ALTER DEFAULT PRIVILEGES FOR ROLE unless he belongs to the role
err = c.GrantRole(role, c.user)
Expand Down
2 changes: 1 addition & 1 deletion pkg/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type PG interface {
CreateSchema(db, role, schema string, logger logr.Logger) error
CreateExtension(db, extension string, logger logr.Logger) error
CreateGroupRole(role string) error
CreateUserRole(role, password string) (string, error)
CreateUserRole(role, password string, iamAuthentication *bool) (string, error)
UpdatePassword(role, password string) error
GrantRole(role, grantee string) error
SetSchemaPrivileges(schemaPrivileges PostgresSchemaPrivileges, logger logr.Logger) error
Expand Down
2 changes: 1 addition & 1 deletion pkg/postgres/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (c *pg) CreateGroupRole(role string) error {
return nil
}

func (c *pg) CreateUserRole(role, password string) (string, error) {
func (c *pg) CreateUserRole(role, password string, iamAuthentication *bool) (string, error) {
_, err := c.db.Exec(fmt.Sprintf(CREATE_USER_ROLE, role, password))
if err != nil {
return "", err
Expand Down
Loading