This repository has been archived by the owner on Jul 6, 2022. It is now read-only.
/
common_bind.go
144 lines (133 loc) · 3.27 KB
/
common_bind.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
141
142
143
144
package postgresql
import (
"fmt"
"net/url"
"github.com/Azure/open-service-broker-azure/pkg/generate"
"github.com/Azure/open-service-broker-azure/pkg/service"
log "github.com/Sirupsen/logrus"
)
func createBinding(
enforceSSL bool,
administratorLogin string,
serverName string,
administratorLoginPassword string,
fullyQualifiedDomainName string,
databaseName string,
) (*bindingDetails, error) {
roleName := generate.NewIdentifier()
password := generate.NewPassword()
db, err := getDBConnection(
enforceSSL,
administratorLogin,
serverName,
administratorLoginPassword,
fullyQualifiedDomainName,
primaryDB,
)
if err != nil {
return nil, err
}
defer db.Close() // nolint: errcheck
tx, err := db.Begin()
if err != nil {
return nil, fmt.Errorf("error starting transaction: %s", err)
}
defer func() {
if err != nil {
if err = tx.Rollback(); err != nil {
log.WithField("error", err).Error("error rolling back transaction")
}
}
}()
if _, err = tx.Exec(
fmt.Sprintf("create role %s with password '%s' login", roleName, password),
); err != nil {
return nil, fmt.Errorf(
`error creating role "%s": %s`,
roleName,
err,
)
}
if _, err = tx.Exec(
fmt.Sprintf("grant %s to %s", databaseName, roleName),
); err != nil {
return nil, fmt.Errorf(
`error adding role "%s" to role "%s": %s`,
databaseName,
roleName,
err,
)
}
if _, err = tx.Exec(
fmt.Sprintf("alter role %s set role %s", roleName, databaseName),
); err != nil {
return nil, fmt.Errorf(
`error making "%s" the default role for "%s" sessions: %s`,
databaseName,
roleName,
err,
)
}
if err = tx.Commit(); err != nil {
return nil, fmt.Errorf("error committing transaction: %s", err)
}
bd := &bindingDetails{
LoginName: roleName,
Password: service.SecureString(password),
}
return bd, err
}
// Create a credential to be returned for binding purposes. This includes a CF
// compatible uri string and a flag to indicate if this connection should
// use ssl. URI is built with the username passed to url.QueryEscape to escape
// the @ in the username
func createCredential(
fqdn string,
sslRequired bool,
serverName string,
databaseName string,
bindDetails *bindingDetails,
) credentials {
username := fmt.Sprintf("%s@%s", bindDetails.LoginName, serverName)
port := 5432
var connectionTemplate string
if sslRequired {
connectionTemplate = "postgresql://%s:%s@%s:%d/%s?&sslmode=require"
} else {
connectionTemplate = "postgresql://%s:%s@%s:%d/%s"
}
connectionString := fmt.Sprintf(
connectionTemplate,
url.QueryEscape(username),
url.QueryEscape(string(bindDetails.Password)),
fqdn,
port,
databaseName,
)
var jdbcTemplate string
if sslRequired {
jdbcTemplate =
"jdbc:postgresql://%s:%d/%s?user=%s&password=%s&sslmode=require"
} else {
jdbcTemplate = "jdbc:postgresql://%s:%d/%s?user=%s&password=%s"
}
jdbc := fmt.Sprintf(
jdbcTemplate,
fqdn,
port,
databaseName,
url.QueryEscape(username),
url.QueryEscape(string(bindDetails.Password)),
)
return credentials{
Host: fqdn,
Port: port,
Database: databaseName,
Username: username,
Password: string(bindDetails.Password),
SSLRequired: sslRequired,
URI: connectionString,
JDBC: jdbc,
Tags: []string{"postgresql"},
}
}