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

Support Operator to Add/delete User for PostgreSQL database #1097

Merged
merged 32 commits into from
Jun 3, 2020
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
b18ee67
add user support for pgresql
buhongw7583c May 18, 2020
9995bf7
change the connection method
buhongw7583c May 20, 2020
fb66373
Merge branch 'UserManagerPgreSQL' of https://github.com/buhongw7583c/…
buhongw7583c May 20, 2020
7257fdb
Merge branch 'UserManagerPgreSQL' of https://github.com/buhongw7583c/…
buhongw7583c May 20, 2020
6594b5c
add user for postgreSQL
buhongw7583c May 21, 2020
3a1e65e
Merge branch 'master' of https://github.com/Azure/azure-service-opera…
buhongw7583c May 21, 2020
650d4ac
Merge branch 'master' into UserManagerPgreSQL
buhongw7583c May 22, 2020
89307c6
Merge branch 'master' of https://github.com/Azure/azure-service-opera…
buhongw7583c May 26, 2020
c24735d
change the DNS suffix to the correct one base on config environment
buhongw7583c May 26, 2020
61cbcf1
Merge branch 'UserManagerPgreSQL' of https://github.com/buhongw7583c/…
buhongw7583c May 26, 2020
838465b
correct the fullserveraddress when connect to mysql server
buhongw7583c May 26, 2020
4ed4488
Update per comments: modify the crd name to align with the convention…
buhongw7583c May 28, 2020
4488407
modify the TestPostgreSQLUserControllerNoResourceGroup in postgresqlu…
buhongw7583c May 28, 2020
475eba2
postgresql_combined_test change to make it work
buhongw7583c May 28, 2020
cb88bbe
Merge branch 'master' into UserManagerPgreSQL
buhongw7583c May 28, 2020
8f955bd
Merge branch 'master' of https://github.com/Azure/azure-service-opera…
buhongw7583c May 29, 2020
b100424
Handle the "no pg_hba.conf entry" in a graceful way. Add comments to …
buhongw7583c May 29, 2020
84d2cba
Merge branch 'UserManagerPgreSQL' of https://github.com/buhongw7583c/…
buhongw7583c May 29, 2020
b1f8456
add the postgresqluser RBAC files and modify the sample yaml file for…
buhongw7583c May 29, 2020
d5105f1
Merge branch 'master' of https://github.com/Azure/azure-service-opera…
buhongw7583c Jun 1, 2020
5e4f10a
delete the postgresql user when the IP address no access to server an…
buhongw7583c Jun 1, 2020
8a80e83
Merge branch 'master' into UserManagerPgreSQL
melonrush13 Jun 1, 2020
ecb716f
Update PROJECT
frodopwns Jun 1, 2020
2cae7f1
Merge branch 'master' of https://github.com/Azure/azure-service-opera…
buhongw7583c Jun 2, 2020
5a53399
modify based on comments: update cainjection, use the secret to retri…
buhongw7583c Jun 2, 2020
3fa4b4a
Merge branch 'UserManagerPgreSQL' of https://github.com/buhongw7583c/…
buhongw7583c Jun 2, 2020
3791abd
updates to go.mod and sum, also deepcopy gen update
frodopwns Jun 2, 2020
9a8d6bc
remove conn string formats, harden against sql injection
frodopwns Jun 2, 2020
4ea7ae7
remove unused fields from user spec
frodopwns Jun 2, 2020
77492c9
Merge pull request #1 from frodopwns/UserManagerPgreSQL
buhongw7583c Jun 3, 2020
cb2f85b
Merge branch 'master' into UserManagerPgreSQL
buhongw7583c Jun 3, 2020
a2a3fdf
Merge branch 'master' into UserManagerPgreSQL
buhongw7583c Jun 3, 2020
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
61 changes: 61 additions & 0 deletions api/v1alpha1/postgresqluser_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

package v1alpha1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

// PostgreSQLUserSpec defines the desired state of PostgreSqlUser
type PostgreSQLUserSpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
// Important: Run "make" to regenerate code after modifying this file
Server string `json:"server"`
DbName string `json:"dbName"`
ResourceGroup string `json:"resourceGroup,omitempty"`
Roles []string `json:"roles"`
// optional
AdminSecret string `json:"adminSecret,omitempty"`
AdminSecretKeyVault string `json:"adminSecretKeyVault,omitempty"`
Username string `json:"username,omitempty"`
KeyVaultToStoreSecrets string `json:"keyVaultToStoreSecrets,omitempty"`
KeyVaultSecretPrefix string `json:"keyVaultSecretPrefix,omitempty"`
KeyVaultSecretFormats []string `json:"keyVaultSecretFormats,omitempty"`
}

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status

// PostgreSQLUser is the Schema for the postgresqlusers API
// +kubebuilder:resource:shortName=psqlu,path=psqluser
// +kubebuilder:printcolumn:name="Provisioned",type="string",JSONPath=".status.provisioned"
// +kubebuilder:printcolumn:name="Message",type="string",JSONPath=".status.message"
type PostgreSQLUser struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec PostgreSQLUserSpec `json:"spec,omitempty"`
Status ASOStatus `json:"status,omitempty"`
}

// +kubebuilder:object:root=true

// PostgreSQLUserList contains a list of PostgreSQLUser
type PostgreSQLUserList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []PostgreSQLUser `json:"items"`
}

func init() {
SchemeBuilder.Register(&PostgreSQLUser{}, &PostgreSQLUserList{})
}

// IsSubmitted checks if sqluser is provisioning
func (s *PostgreSQLUser) IsSubmitted() bool {
return s.Status.Provisioning || s.Status.Provisioned
}
8 changes: 8 additions & 0 deletions config/crd/patches/cainjection_in_postgresqlusers.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# The following patch adds a directive for certmanager to inject CA into the CRD
# CRD conversion requires k8s 1.13 or later.
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
annotations:
certmanager.k8s.io/inject-ca-from: $(CERTIFICATE_NAMESPACE)/$(CERTIFICATE_NAME)
buhongw7583c marked this conversation as resolved.
Show resolved Hide resolved
name: psqlusers.azure.microsoft.com
33 changes: 33 additions & 0 deletions config/samples/azure_v1alpha1_postgresqluser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
apiVersion: azure.microsoft.com/v1alpha1
kind: PostgreSQLUser
metadata:
name: psqluser-sample2
spec:
server: postgresqlserver-sample
dbName: postgresqldatabase-sample
resourceGroup: resourcegroup-azure-operators
# The Azure Database for PostgreSQL server is created with the 3 default roles defined.
# azure_pg_admin
# azure_superuser
# your server admin user
roles:
- "azure_pg_admin"
# Specify a specific username for the user
username: hong
buhongw7583c marked this conversation as resolved.
Show resolved Hide resolved
# Specify adminSecret and adminSecretKeyVault if you want to
# read the PSQL server admin creds from a specific keyvault secret
adminSecret: default-postgresqlserver-sample
adminSecretKeyVault: asokeyvault

# Use the field below to optionally specify a different keyvault
# to store the secrets in
# keyVaultToStoreSecrets: asokeyvault

# Below are optional fields that allow customizing the secrets you need
# keyVaultSecretPrefix: sqlServer-sqlDatabase
# valid secret formats
# adonet, adonet-urlonly, jdbc, jdbc-urlonly, odbc, odbc-urlonly, server, database, username, password
#keyVaultSecretFormats:
# - "adonet"


44 changes: 44 additions & 0 deletions controllers/postgresqluser_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

package controllers

import (
ctrl "sigs.k8s.io/controller-runtime"

azurev1alpha1 "github.com/Azure/azure-service-operator/api/v1alpha1"
)

// PSqlServerPort is the default server port for psql server
const PSqlServerPort = 5432

// DriverName is driver name for db connection
const PDriverName = "postgres"

// SecretUsernameKey is the username key in secret
const PSecretUsernameKey = "username"

// SecretPasswordKey is the password key in secret
const PSecretPasswordKey = "password"

// PSQLUserFinalizerName is the name of the finalizer
const PSQLUserFinalizerName = "psqluser.finalizers.azure.com"

// PostgreSQLUserReconciler reconciles a PSQLUser object
type PostgreSQLUserReconciler struct {
Reconciler *AsyncReconciler
}

// +kubebuilder:rbac:groups=azure.microsoft.com,resources=PostgreSQLUsers,verbs=get;list;watch;create;update;patch;delete
buhongw7583c marked this conversation as resolved.
Show resolved Hide resolved
// +kubebuilder:rbac:groups=azure.microsoft.com,resources=PostgreSQLUsers/status,verbs=get;update;patch

func (r *PostgreSQLUserReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
return r.Reconciler.Reconcile(req, &azurev1alpha1.PostgreSQLUser{})
}

// SetupWithManager runs reconcile loop with manager
func (r *PostgreSQLUserReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&azurev1alpha1.PostgreSQLUser{}).
Complete(r)
}
21 changes: 21 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import (
pip "github.com/Azure/azure-service-operator/pkg/resourcemanager/pip"
psqldatabase "github.com/Azure/azure-service-operator/pkg/resourcemanager/psql/database"
psqlfirewallrule "github.com/Azure/azure-service-operator/pkg/resourcemanager/psql/firewallrule"
psqluser "github.com/Azure/azure-service-operator/pkg/resourcemanager/psql/psqluser"
psqlserver "github.com/Azure/azure-service-operator/pkg/resourcemanager/psql/server"
psqlvnetrule "github.com/Azure/azure-service-operator/pkg/resourcemanager/psql/vnetrule"
resourcemanagerrediscache "github.com/Azure/azure-service-operator/pkg/resourcemanager/rediscaches"
Expand Down Expand Up @@ -170,6 +171,10 @@ func main() {
psqlserverclient := psqlserver.NewPSQLServerClient(secretClient, mgr.GetScheme())
psqldatabaseclient := psqldatabase.NewPSQLDatabaseClient()
psqlfirewallruleclient := psqlfirewallrule.NewPSQLFirewallRuleClient()
psqlusermanager := psqluser.NewPostgreSqlUserManager(
secretClient,
scheme,
)
sqlUserManager := resourcemanagersqluser.NewAzureSqlUserManager(
secretClient,
scheme,
Expand Down Expand Up @@ -536,6 +541,22 @@ func main() {
os.Exit(1)
}

if err = (&controllers.PostgreSQLUserReconciler{
Reconciler: &controllers.AsyncReconciler{
Client: mgr.GetClient(),
AzureClient: psqlusermanager,
Telemetry: telemetry.InitializeTelemetryDefault(
"PSQLUser",
ctrl.Log.WithName("controllers").WithName("PSQLUser"),
),
Recorder: mgr.GetEventRecorderFor("PSQLUser-controller"),
buhongw7583c marked this conversation as resolved.
Show resolved Hide resolved
Scheme: scheme,
},
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "PSQLUser")
os.Exit(1)
}

if err = (&controllers.ApimServiceReconciler{
Reconciler: &controllers.AsyncReconciler{
Client: mgr.GetClient(),
Expand Down
9 changes: 5 additions & 4 deletions pkg/resourcemanager/psql/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ func NewPSQLDatabaseClient() *PSQLDatabaseClient {
return &PSQLDatabaseClient{}
}

func getPSQLDatabasesClient() (psql.DatabasesClient, error) {
//GetPSQLDatabasesClient retrieves the psqldabase
func GetPSQLDatabasesClient() (psql.DatabasesClient, error) {
databasesClient := psql.NewDatabasesClientWithBaseURI(config.BaseURI(), config.SubscriptionID())
a, err := iam.GetResourceManagementAuthorizer()
if err != nil {
Expand Down Expand Up @@ -64,7 +65,7 @@ func (p *PSQLDatabaseClient) CheckDatabaseNameAvailability(ctx context.Context,

func (p *PSQLDatabaseClient) CreateDatabaseIfValid(ctx context.Context, databasename string, servername string, resourcegroup string) (*http.Response, error) {

client, err := getPSQLDatabasesClient()
client, err := GetPSQLDatabasesClient()
if err != nil {
return &http.Response{
StatusCode: 500,
Expand Down Expand Up @@ -99,7 +100,7 @@ func (p *PSQLDatabaseClient) CreateDatabaseIfValid(ctx context.Context, database

func (p *PSQLDatabaseClient) DeleteDatabase(ctx context.Context, databasename string, servername string, resourcegroup string) (status string, err error) {

client, err := getPSQLDatabasesClient()
client, err := GetPSQLDatabasesClient()
if err != nil {
return "", err
}
Expand All @@ -116,7 +117,7 @@ func (p *PSQLDatabaseClient) DeleteDatabase(ctx context.Context, databasename st

func (p *PSQLDatabaseClient) GetDatabase(ctx context.Context, resourcegroup string, servername string, databasename string) (db psql.Database, err error) {

client, err := getPSQLDatabasesClient()
client, err := GetPSQLDatabasesClient()
if err != nil {
return psql.Database{}, err
}
Expand Down
Loading