-
Notifications
You must be signed in to change notification settings - Fork 15
/
redisinstance.go
140 lines (111 loc) · 3.41 KB
/
redisinstance.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
135
136
137
138
139
140
package redis
import (
"crypto/aes"
"encoding/base64"
"errors"
"fmt"
"strconv"
"github.com/18F/aws-broker/base"
"github.com/18F/aws-broker/helpers"
"github.com/18F/aws-broker/catalog"
"github.com/18F/aws-broker/config"
)
// RedisInstance represents the information of a Redis Service instance.
type RedisInstance struct {
base.Instance
Description string `sql:"size(255)"`
Password string `sql:"size(255)"`
Salt string `sql:"size(255)"`
ClearPassword string `sql:"-"`
EngineVersion string `sql:"size(255)"`
ClusterID string `sql:"size(255)"`
CacheNodeType string `sql:"size(255)"`
NumCacheClusters int `sql:"size(255)"`
ParameterGroup string `sql:"size(255)"`
PreferredMaintenanceWindow string `sql:"size(255)"`
SnapshotWindow string `sql:"size(255)"`
SnapshotRetentionLimit int `sql:"size(255)"`
AutomaticFailoverEnabled bool `sql:"size(255)"`
Tags map[string]string `sql:"-"`
DbSubnetGroup string `sql:"-"`
SecGroup string `sql:"-"`
}
func (i *RedisInstance) 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 := helpers.Encrypt(password, key, iv)
if err != nil {
return err
}
i.Password = encrypted
i.ClearPassword = password
return nil
}
func (i *RedisInstance) 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 := helpers.Decrypt(i.Password, key, iv)
if err != nil {
return "", err
}
return decrypted, nil
}
func (i *RedisInstance) getCredentials(password string) (map[string]string, error) {
var credentials map[string]string
uri := fmt.Sprintf("redis://:%s@%s:%d",
password,
i.Host,
i.Port)
credentials = map[string]string{
"uri": uri,
"password": password,
"host": i.Host,
"hostname": i.Host,
"port": strconv.FormatInt(i.Port, 10),
}
return credentials, nil
}
func (i *RedisInstance) init(uuid string,
orgGUID string,
spaceGUID string,
serviceID string,
plan catalog.RedisPlan,
options RedisOptions,
s *config.Settings) error {
i.Uuid = uuid
i.ServiceID = serviceID
i.PlanID = plan.ID
i.OrganizationGUID = orgGUID
i.SpaceGUID = spaceGUID
// Load AWS values
i.DbSubnetGroup = plan.SubnetGroup
i.SecGroup = plan.SecurityGroup
// Load tags
i.Tags = plan.Tags
i.Description = plan.Description
i.ClusterID = s.DbShorthandPrefix + "-" + uuid
i.Salt = helpers.GenerateSalt(aes.BlockSize)
password := helpers.RandStr(25)
if err := i.setPassword(password, s.EncryptionKey); err != nil {
return err
}
i.EngineVersion = plan.EngineVersion
i.NumCacheClusters = plan.NumCacheClusters
i.CacheNodeType = plan.CacheNodeType
i.ParameterGroup = plan.ParameterGroup
i.PreferredMaintenanceWindow = plan.PreferredMaintenanceWindow
i.SnapshotWindow = plan.SnapshotWindow
i.SnapshotRetentionLimit = plan.SnapshotRetentionLimit
i.AutomaticFailoverEnabled = plan.AutomaticFailoverEnabled
// Tag instance with broker details
i.Tags["Instance GUID"] = uuid
i.Tags["Space GUID"] = spaceGUID
i.Tags["Organization GUID"] = orgGUID
i.Tags["Plan GUID"] = plan.ID
i.Tags["Service GUID"] = serviceID
return nil
}