-
Notifications
You must be signed in to change notification settings - Fork 2
/
mysql.go
66 lines (57 loc) · 2.08 KB
/
mysql.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
// Package nessusDatabase is used to connect to databases for state and result storage.
//
// This package is a supporting package for nessusCreator and nessusResults which
// handle the file to launched scan pipeline and the result retriever / processing
// functionality.
package nessusDatabase
import (
"crypto/tls"
"crypto/x509"
"database/sql"
"fmt"
"io/ioutil"
"github.com/go-sql-driver/mysql"
)
// ConnectToMySQLDatabase generates a secure or insecure TCP MySQL database connection on port 3306
func ConnectToMySQLDatabase(username, password, database, server string, tlsInfo *TLSCertificates, secure bool) (*sql.DB, error) {
if secure {
// Create a cert pool
rootCertPool := x509.NewCertPool()
// Load the Certificate Authority cert
pem, err := ioutil.ReadFile(tlsInfo.BasePath + tlsInfo.CACertRelativePath)
if err != nil {
return nil, fmt.Errorf("Failed to open ca cert: %s", err.Error())
}
// Append the CA cert to the cert pool
ok := rootCertPool.AppendCertsFromPEM(pem)
if !ok {
return nil, fmt.Errorf("Failed to append Certs from PEM.")
}
// Load the Client Certificate and Key
clientCert := make([]tls.Certificate, 0, 1)
certs, err := tls.LoadX509KeyPair(tlsInfo.BasePath+tlsInfo.ClientCertRelativePath,
tlsInfo.BasePath+tlsInfo.ClientKeyRelativePath)
if err != nil {
return nil, fmt.Errorf("Failed to load x509 client cert and key: %s", err.Error())
}
clientCert = append(clientCert, certs)
// Register the TLS configuration with MySQL
mysql.RegisterTLSConfig("custom", &tls.Config{
RootCAs: rootCertPool,
Certificates: clientCert,
})
// Open the connection
url := fmt.Sprintf("%s:%s@tcp(%s:3306)/%s?tls=skip-verify", username, password, server, database)
db, err := sql.Open("mysql", url)
if err != nil {
return nil, fmt.Errorf("Couldn't connect to database: %s", err.Error())
}
return db, nil
}
url := fmt.Sprintf("%s:%s@tcp(%s:3306)/%s", username, password, server, database)
db, err := sql.Open("mysql", url)
if err != nil {
return nil, fmt.Errorf("Couldn't connect to database: %s", err.Error())
}
return db, nil
}