Skip to content

Commit b69a92a

Browse files
authored
Add ability to set Postgres password type at cluster creation
Specifically, this allows one to use "scram-sha-256" formatted passwords when a PostgreSQL cluster is created. This adds a new attribute to the `pgclusters.crunchydata.com` CRD called "passwordType", which stores the default value of the password hashing mechanism that the Postgres cluster should use. This also adds the "--password-type" flag to "pgo create cluster", which accepts the same values as the user-oriented commands, i.e. "scram-sha-256" and "md5". If a password type is not provided when a new user is created, the value from the custom resource is used. Issue: [ch11049]
1 parent be3d4e5 commit b69a92a

File tree

15 files changed

+69
-10
lines changed

15 files changed

+69
-10
lines changed

cmd/pgo/cmd/cluster.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ func createCluster(args []string, ns string, createClusterCmd *cobra.Command) {
269269
r.PasswordSuperuser = PasswordSuperuser
270270
r.PasswordReplication = PasswordReplication
271271
r.Password = Password
272+
r.PasswordType = PasswordType
272273
r.SecretFrom = SecretFrom
273274
r.UserLabels = getLabels(UserLabels)
274275
r.Policies = PoliciesFlag

cmd/pgo/cmd/create.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,8 @@ func init() {
418418
createClusterCmd.Flags().StringVarP(&NodeLabel, "node-label", "", "", "The node label (key=value) to use in placing the primary database. If not set, any node is used.")
419419
createClusterCmd.Flags().StringVarP(&Password, "password", "", "", "The password to use for standard user account created during cluster initialization.")
420420
createClusterCmd.Flags().IntVarP(&PasswordLength, "password-length", "", 0, "If no password is supplied, sets the length of the automatically generated password. Defaults to the value set on the server.")
421+
createClusterCmd.Flags().StringVar(&PasswordType, "password-type", "", "The default Postgres password type to use for managed users. "+
422+
"Either \"scram-sha-256\" or \"md5\". Defaults to \"md5\".")
421423
createClusterCmd.Flags().StringVarP(&PasswordSuperuser, "password-superuser", "", "", "The password to use for the PostgreSQL superuser.")
422424
createClusterCmd.Flags().StringVarP(&PasswordReplication, "password-replication", "", "", "The password to use for the PostgreSQL replication user.")
423425
createClusterCmd.Flags().StringVar(&BackrestCPURequest, "pgbackrest-cpu", "", "Set the number of millicores to request for CPU "+
@@ -595,7 +597,7 @@ func init() {
595597
createUserCmd.Flags().StringVarP(&OutputFormat, "output", "o", "", `The output format. Supported types are: "json"`)
596598
createUserCmd.Flags().StringVarP(&Password, "password", "", "", "The password to use for creating a new user which overrides a generated password.")
597599
createUserCmd.Flags().IntVarP(&PasswordLength, "password-length", "", 0, "If no password is supplied, sets the length of the automatically generated password. Defaults to the value set on the server.")
598-
createUserCmd.Flags().StringVar(&PasswordType, "password-type", "md5", "The type of password hashing to use."+
600+
createUserCmd.Flags().StringVar(&PasswordType, "password-type", "", "The type of password hashing to use."+
599601
"Choices are: (md5, scram-sha-256).")
600602
createUserCmd.Flags().StringVarP(&Selector, "selector", "s", "", "The selector to use for cluster filtering.")
601603
createUserCmd.Flags().StringVarP(&Username, "username", "", "", "The username to use for creating a new user")

cmd/pgo/cmd/update.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ func init() {
206206
UpdateUserCmd.Flags().StringVarP(&OutputFormat, "output", "o", "", `The output format. Supported types are: "json"`)
207207
UpdateUserCmd.Flags().StringVarP(&Password, "password", "", "", "Specifies the user password when updating a user password or creating a new user. If --rotate-password is set as well, --password takes precedence.")
208208
UpdateUserCmd.Flags().IntVarP(&PasswordLength, "password-length", "", 0, "If no password is supplied, sets the length of the automatically generated password. Defaults to the value set on the server.")
209-
UpdateUserCmd.Flags().StringVar(&PasswordType, "password-type", "md5", "The type of password hashing to use."+
209+
UpdateUserCmd.Flags().StringVar(&PasswordType, "password-type", "", "The type of password hashing to use."+
210210
"Choices are: (md5, scram-sha-256). This only takes effect if the password is being changed.")
211211
UpdateUserCmd.Flags().BoolVar(&PasswordValidAlways, "valid-always", false, "Sets a password to never expire based on expiration time. Takes precedence over --valid-days")
212212
UpdateUserCmd.Flags().BoolVar(&RotatePassword, "rotate-password", false, "Rotates the user's password with an automatically generated password. The length of the password is determine by either --password-length or the value set on the server, in that order.")

docs/content/custom-resources/_index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -933,6 +933,7 @@ make changes, as described below.
933933
| limits | `create`, `update` | Specify the container resource limits that the PostgreSQL cluster should use. Follows the [Kubernetes definitions of resource limits](https://kubernetes.io/docs/concepts/configuration/manage-resources-containers/#resource-requests-and-limits-of-pod-and-container). |
934934
| name | `create` | The name of the PostgreSQL instance that is the primary. On creation, this should be set to be the same as `ClusterName`. |
935935
| nodeAffinity | `create` | Sets the [node affinity rules](/architecture/high-availability/#node-affinity) for the PostgreSQL cluster and associated PostgreSQL instances. Can be overridden on a per-instance (`pgreplicas.crunchydata.com`) basis. Please see the `Node Affinity Specification` section below. |
936+
| passwordType | `create`, `update` | If set, provides the Postgres password type that is used for creating Postgres users that are managed by PGO. Can be either `md5` or `scram-sha-256`. |
936937
| pgBadger | `create`,`update` | If `true`, deploys the `crunchy-pgbadger` sidecar for query analysis. |
937938
| pgbadgerport | `create` | If the `PGBadger` label is set, then this specifies the port that the pgBadger sidecar runs on (e.g. `10000`) |
938939
| pgBouncer | `create`, `update` | If specified, defines the attributes to use for the pgBouncer connection pooling deployment that can be used in conjunction with this PostgreSQL cluster. Please see the specification defined below. |

docs/content/pgo-client/reference/pgo_create_cluster.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ pgo create cluster [flags]
5555
--password-length int If no password is supplied, sets the length of the automatically generated password. Defaults to the value set on the server.
5656
--password-replication string The password to use for the PostgreSQL replication user.
5757
--password-superuser string The password to use for the PostgreSQL superuser.
58+
--password-type string The default Postgres password type to use for managed users. Either "scram-sha-256" or "md5". Defaults to "md5".
5859
--pgbackrest-cpu string Set the number of millicores to request for CPU for the pgBackRest repository.
5960
--pgbackrest-cpu-limit string Set the number of millicores to limit for CPU for the pgBackRest repository.
6061
--pgbackrest-custom-config string The name of a ConfigMap containing pgBackRest configuration files.
@@ -141,4 +142,4 @@ pgo create cluster [flags]
141142

142143
* [pgo create](/pgo-client/reference/pgo_create/) - Create a Postgres Operator resource
143144

144-
###### Auto generated by spf13/cobra on 11-Apr-2021
145+
###### Auto generated by spf13/cobra on 19-Apr-2021

docs/content/pgo-client/reference/pgo_create_user.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pgo create user [flags]
2727
-o, --output string The output format. Supported types are: "json"
2828
--password string The password to use for creating a new user which overrides a generated password.
2929
--password-length int If no password is supplied, sets the length of the automatically generated password. Defaults to the value set on the server.
30-
--password-type string The type of password hashing to use.Choices are: (md5, scram-sha-256). (default "md5")
30+
--password-type string The type of password hashing to use.Choices are: (md5, scram-sha-256).
3131
-s, --selector string The selector to use for cluster filtering.
3232
--username string The username to use for creating a new user
3333
--valid-days int Sets the number of days that a password is valid. Defaults to the server value.
@@ -50,4 +50,4 @@ pgo create user [flags]
5050

5151
* [pgo create](/pgo-client/reference/pgo_create/) - Create a Postgres Operator resource
5252

53-
###### Auto generated by spf13/cobra on 14-Jan-2021
53+
###### Auto generated by spf13/cobra on 19-Apr-2021

docs/content/pgo-client/reference/pgo_update_user.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pgo update user [flags]
4141
-o, --output string The output format. Supported types are: "json"
4242
--password string Specifies the user password when updating a user password or creating a new user. If --rotate-password is set as well, --password takes precedence.
4343
--password-length int If no password is supplied, sets the length of the automatically generated password. Defaults to the value set on the server.
44-
--password-type string The type of password hashing to use.Choices are: (md5, scram-sha-256). This only takes effect if the password is being changed. (default "md5")
44+
--password-type string The type of password hashing to use.Choices are: (md5, scram-sha-256). This only takes effect if the password is being changed.
4545
--rotate-password Rotates the user's password with an automatically generated password. The length of the password is determine by either --password-length or the value set on the server, in that order.
4646
-s, --selector string The selector to use for cluster filtering.
4747
--set-system-account-password Allows for a system account password to be set.
@@ -67,4 +67,4 @@ pgo update user [flags]
6767

6868
* [pgo update](/pgo-client/reference/pgo_update/) - Update a pgouser, pgorole, or cluster
6969

70-
###### Auto generated by spf13/cobra on 14-Jan-2021
70+
###### Auto generated by spf13/cobra on 19-Apr-2021

installers/ansible/roles/pgo-operator/files/pgo-configs/cluster-bootstrap-job.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@
7878
}, {
7979
"name": "PGHA_TLS_ONLY",
8080
"value": "{{.TLSOnly}}"
81+
}, {
82+
"name": "PGHA_PASSWORD_TYPE",
83+
"value": "{{.PasswordType}}"
8184
}, {
8285
"name": "PGHA_STANDBY",
8386
"value": "{{.Standby}}"

installers/ansible/roles/pgo-operator/files/pgo-configs/cluster-deployment.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@
117117
}, {
118118
"name": "PGHA_TLS_ONLY",
119119
"value": "{{.TLSOnly}}"
120+
}, {
121+
"name": "PGHA_PASSWORD_TYPE",
122+
"value": "{{.PasswordType}}"
120123
}, {
121124
"name": "PGHA_STANDBY",
122125
"value": "{{.Standby}}"

internal/apiserver/clusterservice/clusterimpl.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,15 @@ func CreateCluster(request *msgs.CreateClusterRequest, ns, pgouser string) msgs.
792792
}
793793
}
794794

795+
// determine if the the password type is valid
796+
if _, err := apiserver.GetPasswordType(request.PasswordType); err != nil {
797+
resp.Status.Code = msgs.Error
798+
resp.Status.Msg = err.Error()
799+
return resp
800+
} else if request.PasswordType == "scram" {
801+
request.PasswordType = "scram-sha-256"
802+
}
803+
795804
// if the pgBouncer flag is set, validate that replicas is set to a
796805
// nonnegative value and the service type.
797806
if request.PgbouncerFlag {
@@ -1400,6 +1409,9 @@ func getClusterParams(request *msgs.CreateClusterRequest, name string, ns string
14001409

14011410
log.Debugf("username set to [%s]", spec.User)
14021411

1412+
// set the password type
1413+
spec.PasswordType = request.PasswordType
1414+
14031415
// set the name of the database. The hierarchy is as such:
14041416
// 1. Use the name that the user provides in the request
14051417
// 2. Use the name that is in the pgo.yaml file

0 commit comments

Comments
 (0)