Skip to content

Commit b917790

Browse files
author
jmccormick2001
committed
add --expired flag to pgo show user, updated docs
1 parent 62ed12a commit b917790

File tree

7 files changed

+78
-22
lines changed

7 files changed

+78
-22
lines changed

apiserver/userservice/userimpl.go

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ func User(request *msgs.UserRequest) msgs.UserResponse {
138138
if len(results) > 0 {
139139
log.Debug("expired passwords....")
140140
for _, v := range results {
141-
resp.Results = append(resp.Results, "RoleName "+v.Rolname+" Role Valid Until "+v.Rolvaliduntil)
142141
log.Debug("RoleName " + v.Rolname + " Role Valid Until " + v.Rolvaliduntil)
143142
if request.UpdatePasswords {
144143
newPassword := util.GeneratePassword(defaultPasswordLength)
@@ -152,9 +151,6 @@ func User(request *msgs.UserRequest) msgs.UserResponse {
152151
//log.Debug("new password for %s is %s new expiration is %s\n", v.Rolname, newPassword, newExpireDate)
153152
}
154153
}
155-
} else {
156-
log.Debug("no expired passwords....")
157-
resp.Results = append(resp.Results, "no users were found with expired passwords")
158154
}
159155
}
160156

@@ -627,7 +623,7 @@ func isManaged(secretName string) (bool, error) {
627623
}
628624

629625
// ShowUser ...
630-
func ShowUser(name, selector string) msgs.ShowUserResponse {
626+
func ShowUser(name, selector, expired string) msgs.ShowUserResponse {
631627
var err error
632628

633629
response := msgs.ShowUserResponse{}
@@ -652,11 +648,54 @@ func ShowUser(name, selector string) msgs.ShowUserResponse {
652648
return response
653649
}
654650

651+
var expiredInt int
652+
if expired != "" {
653+
expiredInt, err = strconv.Atoi(expired)
654+
if err != nil {
655+
response.Status.Code = msgs.Error
656+
response.Status.Msg = "--expired is not a valid integer"
657+
return response
658+
}
659+
if expiredInt < 1 {
660+
response.Status.Code = msgs.Error
661+
response.Status.Msg = "--expired is requited to be greater than 0"
662+
return response
663+
}
664+
}
665+
655666
log.Debug("clusters found len is %d\n", len(clusterList.Items))
656667

657668
for _, c := range clusterList.Items {
658669
detail := msgs.ShowUserDetail{}
659670
detail.Cluster = c
671+
detail.ExpiredMsgs = make([]string, 0)
672+
673+
if expired != "" {
674+
selector := util.LABEL_PG_CLUSTER + "=" + c.Spec.Name + "," + util.LABEL_PRIMARY + "=true"
675+
deployments, err := kubeapi.GetDeployments(apiserver.Clientset, selector, apiserver.Namespace)
676+
if err != nil {
677+
response.Status.Code = msgs.Error
678+
response.Status.Msg = err.Error()
679+
return response
680+
}
681+
682+
for _, d := range deployments.Items {
683+
info := getPostgresUserInfo(apiserver.Namespace, d.ObjectMeta.Name)
684+
if expired != "" {
685+
results := callDB(info, d.ObjectMeta.Name, expired)
686+
if len(results) > 0 {
687+
log.Debug("expired passwords....")
688+
for _, v := range results {
689+
detail.ExpiredMsgs = append(detail.ExpiredMsgs, "RoleName "+v.Rolname+" Role Valid Until "+v.Rolvaliduntil)
690+
log.Debug("RoleName " + v.Rolname + " Role Valid Until " + v.Rolvaliduntil)
691+
692+
}
693+
}
694+
}
695+
696+
}
697+
}
698+
660699
detail.Secrets, err = apiserver.GetSecrets(&c)
661700
if err != nil {
662701
response.Status.Code = msgs.Error

apiserver/userservice/userservice.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func ShowUserHandler(w http.ResponseWriter, r *http.Request) {
161161
resp.Status = msgs.Status{Code: msgs.Error, Msg: apiserver.VERSION_MISMATCH_ERROR}
162162
resp.Results = make([]msgs.ShowUserDetail, 0)
163163
} else {
164-
resp = ShowUser(clustername, selector)
164+
resp = ShowUser(clustername, selector, expired)
165165
}
166166
json.NewEncoder(w).Encode(resp)
167167

apiservermsgs/usermsgs.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,9 @@ type ShowUserSecret struct {
7575

7676
// ShowUsersDetail ...
7777
type ShowUserDetail struct {
78-
Cluster crv1.Pgcluster
79-
Secrets []ShowUserSecret
78+
Cluster crv1.Pgcluster
79+
Secrets []ShowUserSecret
80+
ExpiredMsgs []string
8081
}
8182

8283
// ShowUsersResponse ...

hugo/content/getting-started/_index.adoc

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,21 @@ are available on user management below.
434434
pgo show user mycluster
435435
....
436436

437+
438+
===== Viewing Users With Passwords Set to Expire
439+
440+
To see user passwords that have expired past a certain number
441+
of days in the *mycluster* cluster:
442+
....
443+
pgo show user --expired=7 --selector=name=mycluster
444+
....
445+
446+
[width="100%",cols="5,^1,^1, 13",options="header"]
447+
|=========================================================
448+
|Name |Shorthand |Input |Usage
449+
|`--expired` |N/A |String |
450+
|=========================================================
451+
437452
===== PostgreSQL Version
438453

439454
Filter the results based on the PostgeSQL version of the cluster with the `--ccp-image-tag` flag:
@@ -1241,7 +1256,8 @@ Updates the password for a user on selective clusters.
12411256
Grants the user access to a database.
12421257
12431258
|`--expired` |N/A |String |
1244-
Shows passwords that will expire in X days.
1259+
Specifies number of days to check for expiring passwords when
1260+
using --update-passwords flag to update passwords.
12451261
12461262
|`--managed` |N/A |N/A |
12471263
Creates a user with secrets that can be managed by the Operator.
@@ -1323,13 +1339,6 @@ If pgpool is part of your cluster, changing a managed user password
13231339
will cause pgpool to be reconfigured to pick up the password change.
13241340
13251341
1326-
===== Viewing Expired Passwords
1327-
1328-
To see user passwords that have expired past a certain number
1329-
of days in the *mycluster* cluster:
1330-
....
1331-
pgo user --expired=7 --selector=name=mycluster
1332-
....
13331342
13341343
===== Updating Expired Passwords
13351344

pgo/api/user.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ import (
2424
"net/http"
2525
)
2626

27-
func ShowUser(httpclient *http.Client, arg, selector string, SessionCredentials *msgs.BasicAuthCredentials) (msgs.ShowUserResponse, error) {
27+
func ShowUser(httpclient *http.Client, arg, selector, expired string, SessionCredentials *msgs.BasicAuthCredentials) (msgs.ShowUserResponse, error) {
2828

2929
var response msgs.ShowUserResponse
3030

31-
url := SessionCredentials.APIServerURL + "/users/" + arg + "?selector=" + selector + "&version=" + msgs.PGO_VERSION
31+
url := SessionCredentials.APIServerURL + "/users/" + arg + "?selector=" + selector + "&version=" + msgs.PGO_VERSION + "&expired=" + expired
3232

3333
log.Debug("show users called [" + url + "]")
3434

pgo/cmd/show.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ func init() {
104104
ShowScheduleCmd.Flags().StringVarP(&ScheduleName, "schedule-name", "", "", "The name of the schedule to show.")
105105
ShowScheduleCmd.Flags().BoolVarP(&NoPrompt, "no-prompt", "n", false, "No command line confirmation.")
106106
ShowUserCmd.Flags().StringVarP(&Selector, "selector", "s", "", "The selector to use for cluster filtering.")
107+
ShowUserCmd.Flags().StringVarP(&Expired, "expired", "", "", "Shows passwords that will expire in X days.")
108+
107109
}
108110

109111
var ShowConfigCmd = &cobra.Command{
@@ -237,7 +239,7 @@ var ShowUserCmd = &cobra.Command{
237239
},
238240
}
239241

240-
// ShowUserCmd represents the show user command
242+
// ShowScheduleCmd represents the show schedule command
241243
var ShowScheduleCmd = &cobra.Command{
242244
Use: "schedule",
243245
Short: "Show schedule information",

pgo/cmd/user.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ var userCmd = &cobra.Command{
5555
Long: `USER allows you to manage users and passwords across a set of clusters. For example:
5656
5757
pgo user --selector=name=mycluster --update-passwords
58-
pgo user --expired=7 --selector=name=mycluster
5958
pgo user --change-password=bob --selector=name=mycluster --password=newpass`,
6059
Run: func(cmd *cobra.Command, args []string) {
6160
log.Debug("user called")
@@ -67,7 +66,7 @@ func init() {
6766
RootCmd.AddCommand(userCmd)
6867

6968
userCmd.Flags().StringVarP(&Selector, "selector", "s", "", "The selector to use for cluster filtering.")
70-
userCmd.Flags().StringVarP(&Expired, "expired", "", "", "Shows passwords that will expire in X days.")
69+
userCmd.Flags().StringVarP(&Expired, "expired", "", "", "required flag when updating passwords that will expire in X days using --update-passwords flag.")
7170
userCmd.Flags().IntVarP(&PasswordAgeDays, "valid-days", "", 30, "Sets passwords for new users to X days.")
7271
userCmd.Flags().StringVarP(&ChangePasswordForUser, "change-password", "", "", "Updates the password for a user on selective clusters.")
7372
userCmd.Flags().StringVarP(&UserDBAccess, "db", "", "", "Grants the user access to a database.")
@@ -184,7 +183,7 @@ func showUser(args []string) {
184183

185184
for _, v := range args {
186185

187-
response, err := api.ShowUser(httpclient, v, Selector, &SessionCredentials)
186+
response, err := api.ShowUser(httpclient, v, Selector, Expired, &SessionCredentials)
188187
if err != nil {
189188
fmt.Println("Error: ", err.Error())
190189
os.Exit(2)
@@ -227,5 +226,11 @@ func printUsers(detail *msgs.ShowUserDetail) {
227226
fmt.Println(TreeBranch + "username: " + s.Username)
228227
fmt.Println(TreeTrunk + "password: " + s.Password)
229228
}
229+
if len(detail.ExpiredMsgs) > 0 {
230+
fmt.Printf("\nexpired passwords: \n")
231+
for _, e := range detail.ExpiredMsgs {
232+
fmt.Println(e)
233+
}
234+
}
230235

231236
}

0 commit comments

Comments
 (0)