Skip to content

Commit 2b75d18

Browse files
authored
Allow for Postgres system account user passwords to be updated
This introduces the "--set-system-account-password" flag to allow for one to update the password for a PostgreSQL system account user. The flag allows for an override as well as a safety mechanism for one to think about the action they are going to partake in. Issue: #2169
1 parent 0eeafe7 commit 2b75d18

File tree

5 files changed

+46
-37
lines changed

5 files changed

+46
-37
lines changed

cmd/pgo/cmd/update.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ func init() {
187187
UpdateUserCmd.Flags().BoolVar(&PasswordValidAlways, "valid-always", false, "Sets a password to never expire based on expiration time. Takes precedence over --valid-days")
188188
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.")
189189
UpdateUserCmd.Flags().StringVarP(&Selector, "selector", "s", "", "The selector to use for cluster filtering.")
190+
UpdateUserCmd.Flags().BoolVar(&ShowSystemAccounts, "set-system-account-password", false, "Allows for a system account password to be set.")
190191
}
191192

192193
// UpdateCmd represents the update command

cmd/pgo/cmd/user.go

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ var PasswordLength int
5555
var PasswordValidAlways bool
5656

5757
// ShowSystemAccounts enables the display of the PostgreSQL user accounts that
58-
// perform system functions, such as the "postgres" user
58+
// perform system functions, such as the "postgres" user, and for taking action
59+
// on these accounts
5960
var ShowSystemAccounts bool
6061

6162
func createUser(args []string, ns string) {
@@ -366,20 +367,21 @@ func showUser(args []string, ns string) {
366367
func updateUser(clusterNames []string, namespace string) {
367368
// set up the reuqest
368369
request := msgs.UpdateUserRequest{
369-
AllFlag: AllFlag,
370-
Clusters: clusterNames,
371-
Expired: Expired,
372-
ExpireUser: ExpireUser,
373-
ManagedUser: ManagedUser,
374-
Namespace: namespace,
375-
Password: Password,
376-
PasswordAgeDays: PasswordAgeDays,
377-
PasswordLength: PasswordLength,
378-
PasswordValidAlways: PasswordValidAlways,
379-
PasswordType: PasswordType,
380-
RotatePassword: RotatePassword,
381-
Selector: Selector,
382-
Username: strings.TrimSpace(Username),
370+
AllFlag: AllFlag,
371+
Clusters: clusterNames,
372+
Expired: Expired,
373+
ExpireUser: ExpireUser,
374+
ManagedUser: ManagedUser,
375+
Namespace: namespace,
376+
Password: Password,
377+
PasswordAgeDays: PasswordAgeDays,
378+
PasswordLength: PasswordLength,
379+
PasswordValidAlways: PasswordValidAlways,
380+
PasswordType: PasswordType,
381+
RotatePassword: RotatePassword,
382+
Selector: Selector,
383+
SetSystemAccountPassword: ShowSystemAccounts,
384+
Username: strings.TrimSpace(Username),
383385
}
384386

385387
// check to see if EnableLogin or DisableLogin is set. If so, set a value
@@ -391,8 +393,9 @@ func updateUser(clusterNames []string, namespace string) {
391393
}
392394

393395
// check to see if this is a system account if a user name is passed in
394-
if request.Username != "" && utiloperator.IsPostgreSQLUserSystemAccount(request.Username) {
395-
fmt.Println("Error:", request.Username, "is a system account and cannot be used")
396+
if request.Username != "" && utiloperator.IsPostgreSQLUserSystemAccount(request.Username) && !request.SetSystemAccountPassword {
397+
fmt.Println("Error:", request.Username, "is a system account and cannot be used. "+
398+
"You can override this with the \"--set-system-account-password\" flag.")
396399
os.Exit(1)
397400
}
398401

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

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,28 @@ pgo update user [flags]
3232
### Options
3333

3434
```
35-
--all all clusters.
36-
--disable-login Disables a PostgreSQL user from being able to log into the PostgreSQL cluster.
37-
--enable-login Enables a PostgreSQL user to be able to log into the PostgreSQL cluster.
38-
--expire-user Performs expiring a user if set to true.
39-
--expired int Updates passwords that will expire in X days using an autogenerated password.
40-
-h, --help help for user
41-
-o, --output string The output format. Supported types are: "json"
42-
--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.
43-
--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")
45-
--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.
46-
-s, --selector string The selector to use for cluster filtering.
47-
--username string Updates the postgres user on selective clusters.
48-
--valid-always Sets a password to never expire based on expiration time. Takes precedence over --valid-days
49-
--valid-days int Sets the number of days that a password is valid. Defaults to the server value.
35+
--all all clusters.
36+
--disable-login Disables a PostgreSQL user from being able to log into the PostgreSQL cluster.
37+
--enable-login Enables a PostgreSQL user to be able to log into the PostgreSQL cluster.
38+
--expire-user Performs expiring a user if set to true.
39+
--expired int Updates passwords that will expire in X days using an autogenerated password.
40+
-h, --help help for user
41+
-o, --output string The output format. Supported types are: "json"
42+
--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.
43+
--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")
45+
--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.
46+
-s, --selector string The selector to use for cluster filtering.
47+
--set-system-account-password Allows for a system account password to be set.
48+
--username string Updates the postgres user on selective clusters.
49+
--valid-always Sets a password to never expire based on expiration time. Takes precedence over --valid-days
50+
--valid-days int Sets the number of days that a password is valid. Defaults to the server value.
5051
```
5152

5253
### Options inherited from parent commands
5354

5455
```
55-
--apiserver-url string The URL for the PostgreSQL Operator apiserver that will process the request from the pgo client.
56+
--apiserver-url string The URL for the PostgreSQL Operator apiserver that will process the request from the pgo client. Note that the URL should **not** end in a '/'.
5657
--debug Enable additional output for debugging.
5758
--disable-tls Disable TLS authentication to the Postgres Operator.
5859
--exclude-os-trust Exclude CA certs from OS default trust store
@@ -66,4 +67,4 @@ pgo update user [flags]
6667

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

69-
###### Auto generated by spf13/cobra on 1-Oct-2020
70+
###### Auto generated by spf13/cobra on 14-Jan-2021

internal/apiserver/userservice/userimpl.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -593,9 +593,10 @@ func UpdateUser(request *msgs.UpdateUserRequest, pgouser string) msgs.UpdateUser
593593

594594
// if this involes updating a specific PostgreSQL account, and it is a system
595595
// account, return here
596-
if request.Username != "" && util.IsPostgreSQLUserSystemAccount(request.Username) {
596+
if request.Username != "" && util.IsPostgreSQLUserSystemAccount(request.Username) && !request.SetSystemAccountPassword {
597597
response.Status.Code = msgs.Error
598-
response.Status.Msg = fmt.Sprintf(errSystemAccountFormat, request.Username)
598+
response.Status.Msg = fmt.Sprintf(errSystemAccountFormat, request.Username) +
599+
" You can override this with the \"--set-system-account-password\" flag."
599600
return response
600601
}
601602

pkg/apiservermsgs/usermsgs.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,10 @@ type UpdateUserRequest struct {
129129
PasswordValidAlways bool
130130
RotatePassword bool
131131
Selector string
132-
Username string
132+
// SetSystemAccountPassword allows one to override the password for a
133+
// designated system account
134+
SetSystemAccountPassword bool
135+
Username string
133136
}
134137

135138
// UpdateUserResponse contains the response after an update user request

0 commit comments

Comments
 (0)