Skip to content

Commit 8846e3d

Browse files
author
Jeff McCormick
committed
psw code
1 parent 4ec53df commit 8846e3d

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

client/cmd/cluster.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"k8s.io/apimachinery/pkg/labels"
2626
"k8s.io/client-go/pkg/api/v1"
2727
"k8s.io/client-go/pkg/apis/extensions/v1beta1"
28+
"time"
2829
)
2930

3031
func showCluster(args []string) {
@@ -67,6 +68,7 @@ func showCluster(args []string) {
6768
if PostgresVersion == "" || (PostgresVersion != "" && cluster.Spec.POSTGRES_FULL_VERSION == PostgresVersion) {
6869
fmt.Println("cluster : " + cluster.Spec.Name + " (" + cluster.Spec.POSTGRES_FULL_VERSION + ")")
6970
log.Debug("listing cluster " + arg)
71+
fmt.Printf("last password update %v\n", cluster.Spec.PSW_LAST_UPDATE)
7072
//list the deployments
7173
listDeployments(cluster.Spec.Name)
7274
//list the replicasets
@@ -199,6 +201,8 @@ func createCluster(args []string) {
199201
// Create an instance of our TPR
200202
newInstance := getClusterParams(arg)
201203

204+
newInstance.Spec.PSW_LAST_UPDATE = time.Now()
205+
202206
err = Tprclient.Post().
203207
Resource(tpr.CLUSTER_RESOURCE).
204208
Namespace(Namespace).
@@ -293,6 +297,7 @@ func getClusterParams(name string) *tpr.PgCluster {
293297
if SecretFrom != "" {
294298
spec.SECRET_FROM = SecretFrom
295299
}
300+
296301
spec.BACKUP_PATH = BackupPath
297302
if BackupPVC != "" {
298303
spec.BACKUP_PVC_NAME = BackupPVC

client/cmd/psw.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616
package cmd
1717

1818
import (
19+
"database/sql"
1920
"fmt"
2021
log "github.com/Sirupsen/logrus"
22+
_ "github.com/lib/pq"
23+
//"k8s.io/apimachinery/pkg/labels"
2124
//"github.com/crunchydata/postgres-operator/operator/util"
2225
//"github.com/crunchydata/postgres-operator/tpr"
2326
"github.com/spf13/cobra"
@@ -29,6 +32,11 @@ import (
2932
//"strings"
3033
)
3134

35+
type PswResult struct {
36+
Rolname string
37+
Rolvaliduntil string
38+
}
39+
3240
var pswCmd = &cobra.Command{
3341
Use: "psw",
3442
Short: "manage passwords",
@@ -78,9 +86,95 @@ func updatePasswords() {
7886

7987
for _, d := range deployments.Items {
8088
fmt.Println("deployment : " + d.ObjectMeta.Name)
89+
getExpiredInfo(d.ObjectMeta.Name, "7")
8190
if !DryRun {
8291
}
8392

8493
}
8594

8695
}
96+
97+
func getExpiredInfo(clusterName, MAX_DAYS string) {
98+
//var err error
99+
100+
results := callDB(clusterName, MAX_DAYS)
101+
for _, v := range results {
102+
fmt.Printf("RoleName %s Role Valid Until %s\n", v.Rolname, v.Rolvaliduntil)
103+
}
104+
105+
}
106+
107+
func callDB(clusterName, maxdays string) []PswResult {
108+
var conn *sql.DB
109+
110+
results := []PswResult{}
111+
112+
//get the service for the cluster
113+
service, err := Clientset.CoreV1().Services(Namespace).Get(clusterName, meta_v1.GetOptions{})
114+
if err != nil {
115+
log.Error("error getting list of services" + err.Error())
116+
return results
117+
}
118+
119+
//get the secrets for this cluster
120+
lo := meta_v1.ListOptions{LabelSelector: "pg-database=" + clusterName}
121+
secrets, err := Clientset.Secrets(Namespace).List(lo)
122+
if err != nil {
123+
log.Error("error getting list of secrets" + err.Error())
124+
return results
125+
}
126+
127+
//get the postgres user secret info
128+
var username, password, database, hostip string
129+
for _, s := range secrets.Items {
130+
username = string(s.Data["username"][:])
131+
password = string(s.Data["password"][:])
132+
database = "postgres"
133+
hostip = service.Spec.ClusterIP
134+
if username == "postgres" {
135+
log.Debug("got postgres user secrets")
136+
break
137+
}
138+
}
139+
140+
//query the database for users that have expired
141+
strPort := fmt.Sprint(service.Spec.Ports[0].Port)
142+
conn, err = sql.Open("postgres", "sslmode=disable user="+username+" host="+hostip+" port="+strPort+" dbname="+database+" password="+password)
143+
if err != nil {
144+
log.Debug(err.Error())
145+
return results
146+
}
147+
148+
var ts string
149+
var rows *sql.Rows
150+
151+
querystr := "SELECT rolname, rolvaliduntil as expiring_soon FROM pg_authid WHERE rolvaliduntil < now() + '" + maxdays + " days'"
152+
log.Debug(querystr)
153+
rows, err = conn.Query(querystr)
154+
if err != nil {
155+
log.Debug(err.Error())
156+
return results
157+
}
158+
159+
defer func() {
160+
if conn != nil {
161+
conn.Close()
162+
}
163+
if rows != nil {
164+
rows.Close()
165+
}
166+
}()
167+
168+
for rows.Next() {
169+
p := PswResult{}
170+
if err = rows.Scan(&p.Rolname, &p.Rolvaliduntil); err != nil {
171+
log.Debug(err.Error())
172+
return results
173+
}
174+
results = append(results, p)
175+
log.Debug("returned " + ts)
176+
}
177+
178+
return results
179+
180+
}

tpr/cluster.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"encoding/json"
2323
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2424
"k8s.io/apimachinery/pkg/runtime/schema"
25+
"time"
2526
)
2627

2728
const CLUSTER_RESOURCE = "pgclusters"
@@ -52,6 +53,7 @@ type PgClusterSpec struct {
5253
PGROOT_SECRET_NAME string `json:"pgrootsecretname"`
5354
PGMASTER_SECRET_NAME string `json:"pgmastersecretname"`
5455
STATUS string `json:"status"`
56+
PSW_LAST_UPDATE time.Time `json:"pswlastupdate"`
5557
}
5658

5759
type PgCluster struct {

0 commit comments

Comments
 (0)