This repository has been archived by the owner on Jul 6, 2022. It is now read-only.
/
common_bind.go
121 lines (109 loc) · 2.26 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
package mssql
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 bind(
administratorLogin string,
administratorPassword string,
fqdn string,
databaseName string,
) (service.BindingDetails, error) {
username := generate.NewIdentifier()
password := generate.NewPassword()
// connect to new database to create user
db, err := getDBConnection(
administratorLogin,
administratorPassword,
fqdn,
databaseName,
)
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 on the new database: %s",
err,
)
}
defer func() {
if err != nil {
if err = tx.Rollback(); err != nil {
log.WithField("error", err).
Error("error rolling back transaction on the new database")
}
}
}()
if _, err = tx.Exec(
fmt.Sprintf("CREATE USER \"%s\" WITH PASSWORD='%s'", username, password),
); err != nil {
return nil, fmt.Errorf(
`error creating user "%s": %s`,
username,
err,
)
}
if _, err = tx.Exec(
fmt.Sprintf("GRANT CONTROL to \"%s\"", username),
); err != nil {
return nil, fmt.Errorf(
`error granting CONTROL to user "%s": %s`,
username,
err,
)
}
if err = tx.Commit(); err != nil {
return nil, fmt.Errorf(
"error committing transaction on the new database: %s",
err,
)
}
return &bindingDetails{
Username: username,
Password: service.SecureString(password),
}, nil
}
func createCredential(
fqdn string,
database string,
username string,
password string,
) credentials {
port := 1433
jdbcTemplate := "jdbc:sqlserver://%s:%d;database=%s;user=%s;" +
"password=%s;encrypt=true;trustServerCertificate=true;"
jdbc := fmt.Sprintf(
jdbcTemplate,
fqdn,
port,
database,
username,
password,
)
uriTemplate :=
"sqlserver://%s:%s@%s:%d/%s;encrypt=true;trustServerCertificate=true"
uri := fmt.Sprintf(
uriTemplate,
url.QueryEscape(username),
password,
fqdn,
port,
database,
)
return credentials{
Host: fqdn,
Port: port,
Database: database,
Username: username,
Password: password,
JDBC: jdbc,
URI: uri,
Encrypt: true,
}
}