forked from cloudfoundry-community/rds-broker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.go
134 lines (105 loc) · 2.65 KB
/
models.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
// "github.com/jinzhu/gorm"
// _ "github.com/lib/pq"
"crypto/aes"
"encoding/base64"
"errors"
"fmt"
"time"
)
type Instance struct {
Id int64
Uuid string `sql:"size(255)"`
Database string `sql:"size(255)"`
Username string `sql:"size(255)"`
Password string `sql:"size(255)"`
Salt string `sql:"size(255)"`
ClearPassword string `sql:"-"`
PlanId string `sql:"size(255)"`
OrgGuid string `sql:"size(255)"`
SpaceGuid string `sql:"size(255)"`
Tags map[string]string `sql:"-"`
DbSubnetGroup string `sql:"-"`
SecGroup string `sql:"-"`
Adapter string `sql:"size(255)"`
Host string `sql:"size(255)"`
Port int64
DbType string `sql:"size(255)"`
State DBInstanceState
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt time.Time
}
func (i *Instance) SetPassword(password, key string) error {
if i.Salt == "" {
return errors.New("Salt has to be set before writing the password")
}
iv, _ := base64.StdEncoding.DecodeString(i.Salt)
encrypted, err := Encrypt(password, key, iv)
if err != nil {
return err
}
i.Password = encrypted
i.ClearPassword = password
return nil
}
func (i *Instance) GetPassword(key string) (string, error) {
if i.Salt == "" || i.Password == "" {
return "", errors.New("Salt and password has to be set before writing the password")
}
iv, _ := base64.StdEncoding.DecodeString(i.Salt)
decrypted, err := Decrypt(i.Password, key, iv)
if err != nil {
return "", err
}
return decrypted, nil
}
func (i *Instance) GetCredentials(password string) (map[string]string, error) {
var credentials map[string]string
switch i.DbType {
case "postgres":
uri := fmt.Sprintf("postgres://%s:%s@%s:%d/%s",
i.Username,
password,
i.Host,
i.Port,
i.Database)
credentials = map[string]string{
"uri": uri,
"username": i.Username,
"password": password,
"host": i.Host,
"db_name": i.Database,
}
default:
return nil, errors.New("Cannot generate credentials for unsupported db type: " + i.DbType)
}
return credentials, nil
}
func (i *Instance) Init(uuid string,
orgGuid string,
spaceGuid string,
plan *Plan,
s *Settings) error {
i.Uuid = uuid
i.PlanId = plan.Id
i.OrgGuid = orgGuid
i.SpaceGuid = spaceGuid
i.Adapter = plan.Adapter
// Build random values
i.Database = "db" + randStr(15)
i.Username = "u" + randStr(15)
i.Salt = GenerateSalt(aes.BlockSize)
password := randStr(25)
if err := i.SetPassword(password, s.EncryptionKey); err != nil {
return err
}
// Load tags
i.Tags = s.InstanceTags
// Load AWS values
i.DbType = plan.DbType
i.DbSubnetGroup = s.SubnetGroup
i.SecGroup = s.SecGroup
return nil
}