Skip to content

Commit

Permalink
Merge pull request #27 from amazeeio/postgres-names
Browse files Browse the repository at this point in the history
Escape db and user names for PostgreSQL statements
  • Loading branch information
shreddedbacon authored Nov 30, 2020
2 parents ca426a5 + 70f9bf8 commit 4ea032d
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 18 deletions.
Binary file added charts/dbaas-operator-0.1.6.tgz
Binary file not shown.
4 changes: 2 additions & 2 deletions charts/dbaas-operator/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ type: application

# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
version: 0.1.5
version: 0.1.6

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application.
appVersion: 0.1.2
appVersion: 0.1.3
2 changes: 1 addition & 1 deletion charts/dbaas-operator/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ replicaCount: 1

image:
repository: amazeeio/dbaas-operator
tag: v0.1.2
tag: v0.1.3
pullPolicy: IfNotPresent

kubeRbacProxy:
Expand Down
30 changes: 20 additions & 10 deletions charts/index.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
apiVersion: v1
entries:
dbaas-operator:
- apiVersion: v2
appVersion: 0.1.3
created: "2020-11-30T14:50:47.469201+11:00"
description: A Helm chart for dbaas-operator
digest: a3e809db9edeef3d3b5708cb45a58b8318c2864d5ffab684a4e239ed5e8dd118
name: dbaas-operator
type: application
urls:
- dbaas-operator-0.1.6.tgz
version: 0.1.6
- apiVersion: v2
appVersion: 0.1.2
created: "2020-09-25T17:21:59.527102+10:00"
created: "2020-11-30T14:50:47.468804+11:00"
description: A Helm chart for dbaas-operator
digest: da29683319a4964eccf17ef2aa45caeffda9100acdf22f76869185c7174be65c
name: dbaas-operator
Expand All @@ -13,7 +23,7 @@ entries:
version: 0.1.5
- apiVersion: v2
appVersion: 0.1.1
created: "2020-09-25T17:21:59.526626+10:00"
created: "2020-11-30T14:50:47.468382+11:00"
description: A Helm chart for dbaas-operator
digest: c3f995573c09f887b0fd4370fb07b4148eb1d61f5f5ce3baddf01dd953c724cf
name: dbaas-operator
Expand All @@ -23,7 +33,7 @@ entries:
version: 0.1.4
- apiVersion: v2
appVersion: 0.1.0
created: "2020-09-25T17:21:59.526213+10:00"
created: "2020-11-30T14:50:47.467977+11:00"
description: A Helm chart for dbaas-operator
digest: 67712098bd65fce262f59cfcb5566bcd9dee5c0f75afbeccb78bf4776a0b657d
name: dbaas-operator
Expand All @@ -33,7 +43,7 @@ entries:
version: 0.1.3
- apiVersion: v2
appVersion: 0.1.0
created: "2020-09-25T17:21:59.525795+10:00"
created: "2020-11-30T14:50:47.467553+11:00"
description: A Helm chart for dbaas-operator
digest: 16294c3b156e3d6192f9205358bce552952c75b8d28b06d37fbc4804e960cd8d
name: dbaas-operator
Expand All @@ -43,7 +53,7 @@ entries:
version: 0.1.2
- apiVersion: v2
appVersion: 0.1.0
created: "2020-09-25T17:21:59.525389+10:00"
created: "2020-11-30T14:50:47.466095+11:00"
description: A Helm chart for dbaas-operator
digest: f0f32afe174f62ea1ba792d6a564223cd0ed6ac821bdf7a09cf5c254f02f2fac
name: dbaas-operator
Expand All @@ -53,7 +63,7 @@ entries:
version: 0.1.1
- apiVersion: v2
appVersion: 0.1.0
created: "2020-09-25T17:21:59.525005+10:00"
created: "2020-11-30T14:50:47.465646+11:00"
description: A Helm chart for dbaas-operator
digest: 49e3bac9fd6a6eea448accd9483d9fbacd3a3b0b3e2e146c7304b558438c0df2
name: dbaas-operator
Expand All @@ -64,7 +74,7 @@ entries:
mariadbprovider:
- apiVersion: v2
appVersion: 0.1.0
created: "2020-09-25T17:21:59.527474+10:00"
created: "2020-11-30T14:50:47.469599+11:00"
description: A Helm chart for MariaDBProvider used by dbaas-operator
digest: a59bd057c64ca68b2f52a8b1a6dece0746ebc0ee0f1a6a17c807c8df5d6a0c3e
name: mariadbprovider
Expand All @@ -74,7 +84,7 @@ entries:
version: 0.1.2
- apiVersion: v2
appVersion: 0.1.0
created: "2020-09-25T17:21:59.52727+10:00"
created: "2020-11-30T14:50:47.469373+11:00"
description: A Helm chart for MariaDBProvider used by dbaas-operator
digest: bc03d2aa3c0d0aba80063294dbf467cb79b32a408cb34dec1841b531ebd2094c
name: mariadbprovider
Expand All @@ -85,12 +95,12 @@ entries:
postgresqlprovider:
- apiVersion: v2
appVersion: 0.1.0
created: "2020-09-25T17:21:59.527649+10:00"
created: "2020-11-30T14:50:47.469843+11:00"
description: A Helm chart for PostgreSQLProvider used by dbaas-operator
digest: 27b6b05d4dfa94020fe122afa69cfe2c4bba351b8b069a660a0403de262ba567
name: postgresqlprovider
type: application
urls:
- postgresqlprovider-0.1.0.tgz
version: 0.1.0
generated: "2020-09-25T17:21:59.524405+10:00"
generated: "2020-11-30T14:50:47.464019+11:00"
10 changes: 5 additions & 5 deletions controllers/postgres/postgresqlconsumer_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,14 +303,14 @@ func createDatabaseIfNotExist(provider postgresv1.PostgreSQLProviderSpec, consum
userName = strings.Split(consumer.Spec.Consumer.Username, "@")
}
// @TODO: check the equivalent of of create if not exists
createDB := fmt.Sprintf("CREATE DATABASE %s;", consumer.Spec.Consumer.Database)
createDB := fmt.Sprintf("CREATE DATABASE \"%s\";", consumer.Spec.Consumer.Database)
_, err = db.Exec(createDB)
if err != nil {
return err
}
// @TODO: check the equivalent of of create if not exists
var createUser string
createUser = fmt.Sprintf("CREATE USER %s WITH ENCRYPTED PASSWORD '%s';", userName[0], consumer.Spec.Consumer.Password)
createUser = fmt.Sprintf("CREATE USER \"%s\" WITH ENCRYPTED PASSWORD '%s';", userName[0], consumer.Spec.Consumer.Password)
_, err = db.Exec(createUser)
if err != nil {
// if user creation fails, drop the database that gets created
Expand All @@ -321,7 +321,7 @@ func createDatabaseIfNotExist(provider postgresv1.PostgreSQLProviderSpec, consum
return fmt.Errorf("Unable to create user %s, dropped database %s: %v", consumer.Spec.Consumer.Username, consumer.Spec.Consumer.Database, err)
}
var grantUser string
grantUser = fmt.Sprintf("GRANT ALL PRIVILEGES ON DATABASE %s TO %s;", consumer.Spec.Consumer.Database, userName[0])
grantUser = fmt.Sprintf("GRANT ALL PRIVILEGES ON DATABASE \"%s\" TO \"%s\";", consumer.Spec.Consumer.Database, userName[0])
_, err = db.Exec(grantUser)
if err != nil {
// if grants fails, drop the database and user that gets created
Expand Down Expand Up @@ -359,7 +359,7 @@ func dropDbAndUser(provider postgresv1.PostgreSQLProviderSpec, consumer postgres
}

func dropDatabase(db *sql.DB, database string) error {
dropDB := fmt.Sprintf("DROP DATABASE %s;", database)
dropDB := fmt.Sprintf("DROP DATABASE \"%s\";", database)
_, err := db.Exec(dropDB)
if err != nil {
return err
Expand All @@ -375,7 +375,7 @@ func dropUser(db *sql.DB, consumer postgresv1.PostgreSQLConsumer, provider postg
}

var dropUser string
dropUser = fmt.Sprintf("DROP USER %s;", userName[0])
dropUser = fmt.Sprintf("DROP USER \"%s\";", userName[0])
_, err := db.Exec(dropUser)
if err != nil {
return err
Expand Down

0 comments on commit 4ea032d

Please sign in to comment.