Skip to content

Commit 8179b10

Browse files
1gtmtamalsaha
andauthored
[cherry-pick] Use username/password as keys in Postgres secret (#270) (#276)
/cherry-pick Signed-off-by: Tamal Saha <tamal@appscode.com> Co-authored-by: Tamal Saha <tamal@appscode.com>
1 parent afe907a commit 8179b10

File tree

9 files changed

+45
-31
lines changed

9 files changed

+45
-31
lines changed

docs/examples/backup/appbinding.yaml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,5 @@ spec:
2323
scheme: postgresql
2424
secret:
2525
name: sample-postgres-auth
26-
secretTransforms:
27-
- renameKey:
28-
from: POSTGRES_USER
29-
to: username
30-
- renameKey:
31-
from: POSTGRES_PASSWORD
32-
to: password
3326
type: kubedb.com/postgres
3427
version: "11.9"

docs/standalone.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,6 @@ spec:
148148
scheme: postgresql
149149
secret:
150150
name: sample-postgres-auth
151-
secretTransforms:
152-
- renameKey:
153-
from: POSTGRES_USER
154-
to: username
155-
- renameKey:
156-
from: POSTGRES_PASSWORD
157-
to: password
158151
type: kubedb.com/postgres
159152
version: "11.9"
160153
```

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ require (
1818
k8s.io/client-go v12.0.0+incompatible
1919
k8s.io/kube-openapi v0.0.0-20200410145947-61e04a5be9a6
2020
k8s.io/kubernetes v1.18.9 // indirect
21-
kmodules.xyz/client-go v0.0.0-20201007024140-3223988adf40
21+
kmodules.xyz/client-go v0.0.0-20201008164401-74d81f261ec5
2222
kmodules.xyz/crd-schema-fuzz v0.0.0-20200922204806-c1426cd7fcf4
2323
kmodules.xyz/custom-resources v0.0.0-20201008012351-6d8090f759d4
2424
kmodules.xyz/objectstore-api v0.0.0-20200922210707-59bab27e5d41 // indirect

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -955,8 +955,8 @@ kmodules.xyz/client-go v0.0.0-20200818143024-600fef263e03/go.mod h1:sY/eoe4ktxZE
955955
kmodules.xyz/client-go v0.0.0-20200915091229-7df16c29f4e8 h1:C6+M9aTLhPCmsJ8dmhPvkr7Qe2MN+iiY3kZvbonhS9E=
956956
kmodules.xyz/client-go v0.0.0-20200915091229-7df16c29f4e8/go.mod h1:sY/eoe4ktxZEoHpr5NpAQ5s22VSwTE8psJtKVeVgLRY=
957957
kmodules.xyz/client-go v0.0.0-20200922200830-63d86b6e5b63/go.mod h1:JZN34jqk6ZlR+QOnBPpnUVBab4rmfamqxfSvLaulBMY=
958-
kmodules.xyz/client-go v0.0.0-20201007024140-3223988adf40 h1:XQLn2whq+TYbGxVloiyopxSM9jsqjqvWQjltXFKd+aA=
959-
kmodules.xyz/client-go v0.0.0-20201007024140-3223988adf40/go.mod h1:pnRh7gtJ6ErPJQBkQeRlpD95KRtxhD4eGrYagZEU8RM=
958+
kmodules.xyz/client-go v0.0.0-20201008164401-74d81f261ec5 h1:mGySTT2dC8u2FQDUFbDLcOt7GM+IkXqlH2xzATyddKg=
959+
kmodules.xyz/client-go v0.0.0-20201008164401-74d81f261ec5/go.mod h1:pnRh7gtJ6ErPJQBkQeRlpD95KRtxhD4eGrYagZEU8RM=
960960
kmodules.xyz/constants v0.0.0-20200506032633-a21e58ceec72/go.mod h1:DbiFk1bJ1KEO94t1SlAn7tzc+Zz95rSXgyUKa2nzPmY=
961961
kmodules.xyz/crd-schema-fuzz v0.0.0-20200521005638-2433a187de95 h1:v0S/+ftzL6Xrs9XevgchAOJyPKlRQXPiZf87xotj3X4=
962962
kmodules.xyz/crd-schema-fuzz v0.0.0-20200521005638-2433a187de95/go.mod h1:jpu8xFsDKd6kAWUAKk8oTu/GQGBWqhrcaDeOJdaCJnk=

pkg/backup.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@ import (
3030
"github.com/appscode/go/flags"
3131
"github.com/spf13/cobra"
3232
license "go.bytebuilders.dev/license-verifier/kubernetes"
33+
core "k8s.io/api/core/v1"
3334
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3435
"k8s.io/client-go/kubernetes"
3536
"k8s.io/client-go/tools/clientcmd"
37+
meta_util "kmodules.xyz/client-go/meta"
3638
appcatalog "kmodules.xyz/custom-resources/apis/appcatalog/v1alpha1"
3739
appcatalog_cs "kmodules.xyz/custom-resources/client/clientset/versioned"
3840
v1 "kmodules.xyz/offshoot-api/api/v1"
@@ -210,12 +212,12 @@ func (opt *postgresOptions) backupPostgreSQL(targetRef api_v1beta1.TargetRef) (*
210212
}
211213

212214
// set env for pg_dump/pg_dumpall
213-
resticWrapper.SetEnv(EnvPgPassword, string(appBindingSecret.Data[PostgresPassword]))
215+
resticWrapper.SetEnv(EnvPgPassword, must(meta_util.GetBytesForKeys(appBindingSecret.Data, core.BasicAuthPasswordKey, envPostgresPassword)))
214216
// setup pipe command
215217
opt.backupOptions.StdinPipeCommand = restic.Command{
216218
Name: pgBackupCMD,
217219
Args: []interface{}{
218-
"-U", string(appBindingSecret.Data[PostgresUser]),
220+
"-U", must(meta_util.GetBytesForKeys(appBindingSecret.Data, core.BasicAuthUsernameKey, envPostgresUser)),
219221
"-h", appBinding.Spec.ClientConfig.Service.Name,
220222
},
221223
}

pkg/restore.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ import (
2727
"github.com/appscode/go/flags"
2828
"github.com/spf13/cobra"
2929
license "go.bytebuilders.dev/license-verifier/kubernetes"
30+
core "k8s.io/api/core/v1"
3031
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3132
"k8s.io/client-go/kubernetes"
3233
"k8s.io/client-go/tools/clientcmd"
34+
meta_util "kmodules.xyz/client-go/meta"
3335
appcatalog "kmodules.xyz/custom-resources/apis/appcatalog/v1alpha1"
3436
appcatalog_cs "kmodules.xyz/custom-resources/client/clientset/versioned"
3537
v1 "kmodules.xyz/offshoot-api/api/v1"
@@ -171,12 +173,12 @@ func (opt *postgresOptions) restorePostgreSQL(targetRef api_v1beta1.TargetRef) (
171173
}
172174

173175
// set env for psql
174-
resticWrapper.SetEnv(EnvPgPassword, string(appBindingSecret.Data[PostgresPassword]))
176+
resticWrapper.SetEnv(EnvPgPassword, must(meta_util.GetBytesForKeys(appBindingSecret.Data, core.BasicAuthPasswordKey, envPostgresPassword)))
175177
// setup pipe command
176178
opt.dumpOptions.StdoutPipeCommand = restic.Command{
177179
Name: PgRestoreCMD,
178180
Args: []interface{}{
179-
"-U", string(appBindingSecret.Data[PostgresUser]),
181+
"-U", must(meta_util.GetBytesForKeys(appBindingSecret.Data, core.BasicAuthUsernameKey, envPostgresUser)),
180182
"-h", appBinding.Spec.ClientConfig.Service.Name,
181183
},
182184
}

pkg/util.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,22 @@ import (
2626
"github.com/codeskyblue/go-sh"
2727
core "k8s.io/api/core/v1"
2828
"k8s.io/client-go/kubernetes"
29+
meta_util "kmodules.xyz/client-go/meta"
2930
"kmodules.xyz/custom-resources/apis/appcatalog/v1alpha1"
3031
appcatalog_cs "kmodules.xyz/custom-resources/client/clientset/versioned"
3132
)
3233

3334
const (
34-
PostgresUser = "POSTGRES_USER"
35-
PostgresPassword = "POSTGRES_PASSWORD"
36-
EnvPgPassword = "PGPASSWORD"
37-
PgDumpFile = "dumpfile.sql"
38-
PgDumpCMD = "pg_dump"
39-
PgDumpallCMD = "pg_dumpall"
40-
PgRestoreCMD = "psql"
35+
EnvPgPassword = "PGPASSWORD"
36+
PgDumpFile = "dumpfile.sql"
37+
PgDumpCMD = "pg_dump"
38+
PgDumpallCMD = "pg_dumpall"
39+
PgRestoreCMD = "psql"
40+
41+
// Deprecated
42+
envPostgresUser = "POSTGRES_USER"
43+
// Deprecated
44+
envPostgresPassword = "POSTGRES_PASSWORD"
4145
)
4246

4347
type postgresOptions struct {
@@ -58,14 +62,21 @@ type postgresOptions struct {
5862
dumpOptions restic.DumpOptions
5963
}
6064

65+
func must(v []byte, err error) string {
66+
if err != nil {
67+
panic(err)
68+
}
69+
return string(v)
70+
}
71+
6172
func waitForDBReady(appBinding *v1alpha1.AppBinding, secret *core.Secret, waitTimeout int32) error {
6273
log.Infoln("Waiting for the database to be ready.....")
6374
shell := sh.NewSession()
64-
shell.SetEnv(EnvPgPassword, string(secret.Data[PostgresPassword]))
75+
shell.SetEnv(EnvPgPassword, must(meta_util.GetBytesForKeys(secret.Data, core.BasicAuthPasswordKey, envPostgresPassword)))
6576
args := []interface{}{
6677
fmt.Sprintf("--host=%s", appBinding.Spec.ClientConfig.Service.Name),
6778
fmt.Sprintf("--port=%d", appBinding.Spec.ClientConfig.Service.Port),
68-
fmt.Sprintf("--username=%s", secret.Data[PostgresUser]),
79+
fmt.Sprintf("--username=%s", must(meta_util.GetBytesForKeys(secret.Data, core.BasicAuthUsernameKey, envPostgresUser))),
6980
fmt.Sprintf("--timeout=%d", waitTimeout),
7081
}
7182
return shell.Command("pg_isready", args...).Run()

vendor/kmodules.xyz/client-go/meta/annotations.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,3 +187,16 @@ func GetStringValueForKeys(m map[string]string, key string, alts ...string) (str
187187
}
188188
return "", kutil.ErrNotFound
189189
}
190+
191+
func GetBytesForKeys(m map[string][]byte, key string, alts ...string) ([]byte, error) {
192+
if m == nil {
193+
return nil, kutil.ErrNotFound
194+
}
195+
keys := append([]string{key}, alts...)
196+
for _, k := range keys {
197+
if v, ok := m[k]; ok {
198+
return v, nil
199+
}
200+
}
201+
return nil, kutil.ErrNotFound
202+
}

vendor/modules.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -843,7 +843,7 @@ k8s.io/utils/net
843843
k8s.io/utils/path
844844
k8s.io/utils/pointer
845845
k8s.io/utils/trace
846-
# kmodules.xyz/client-go v0.0.0-20201007024140-3223988adf40
846+
# kmodules.xyz/client-go v0.0.0-20201008164401-74d81f261ec5
847847
kmodules.xyz/client-go
848848
kmodules.xyz/client-go/api/v1
849849
kmodules.xyz/client-go/apiextensions

0 commit comments

Comments
 (0)