2021-11-23 09:45:38 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2022-10-22 05:41:27 +00:00
|
|
|
"crypto/x509"
|
2021-11-23 09:45:38 +00:00
|
|
|
"fmt"
|
2022-10-22 05:41:27 +00:00
|
|
|
"path"
|
2021-11-23 09:45:38 +00:00
|
|
|
"time"
|
|
|
|
|
2022-10-22 04:25:12 +00:00
|
|
|
"github.com/go-sql-driver/mysql"
|
2021-11-23 09:45:38 +00:00
|
|
|
|
|
|
|
"github.com/authelia/authelia/v4/internal/configuration/schema"
|
2022-10-22 08:27:59 +00:00
|
|
|
"github.com/authelia/authelia/v4/internal/utils"
|
2021-11-23 09:45:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// MySQLProvider is a MySQL provider.
|
|
|
|
type MySQLProvider struct {
|
|
|
|
SQLProvider
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewMySQLProvider a MySQL provider.
|
2022-10-22 05:41:27 +00:00
|
|
|
func NewMySQLProvider(config *schema.Configuration, caCertPool *x509.CertPool) (provider *MySQLProvider) {
|
2021-11-23 09:45:38 +00:00
|
|
|
provider = &MySQLProvider{
|
2022-10-22 08:27:59 +00:00
|
|
|
SQLProvider: NewSQLProvider(config, providerMySQL, providerMySQL, dsnMySQL(config.Storage.MySQL, caCertPool)),
|
2021-11-23 09:45:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// All providers have differing SELECT existing table statements.
|
|
|
|
provider.sqlSelectExistingTables = queryMySQLSelectExistingTables
|
|
|
|
|
|
|
|
// Specific alterations to this provider.
|
|
|
|
provider.sqlFmtRenameTable = queryFmtMySQLRenameTable
|
|
|
|
|
|
|
|
return provider
|
|
|
|
}
|
|
|
|
|
2022-10-22 08:27:59 +00:00
|
|
|
func dsnMySQL(config *schema.MySQLStorageConfiguration, caCertPool *x509.CertPool) (dataSourceName string) {
|
2022-10-22 05:41:27 +00:00
|
|
|
dsnConfig := mysql.NewConfig()
|
2022-10-22 04:25:12 +00:00
|
|
|
|
|
|
|
switch {
|
2022-10-22 05:41:27 +00:00
|
|
|
case path.IsAbs(config.Host):
|
|
|
|
dsnConfig.Net = sqlNetworkTypeUnixSocket
|
|
|
|
dsnConfig.Addr = config.Host
|
2022-10-22 04:25:12 +00:00
|
|
|
case config.Port == 0:
|
2022-10-22 05:41:27 +00:00
|
|
|
dsnConfig.Net = sqlNetworkTypeTCP
|
|
|
|
dsnConfig.Addr = fmt.Sprintf("%s:%d", config.Host, 3306)
|
2022-10-22 04:25:12 +00:00
|
|
|
default:
|
2022-10-22 05:41:27 +00:00
|
|
|
dsnConfig.Net = sqlNetworkTypeTCP
|
|
|
|
dsnConfig.Addr = fmt.Sprintf("%s:%d", config.Host, config.Port)
|
2021-11-23 09:45:38 +00:00
|
|
|
}
|
|
|
|
|
2022-10-22 08:27:59 +00:00
|
|
|
if config.TLS != nil {
|
|
|
|
_ = mysql.RegisterTLSConfig("storage", utils.NewTLSConfig(config.TLS, caCertPool))
|
|
|
|
|
|
|
|
dsnConfig.TLSConfig = "storage"
|
|
|
|
}
|
|
|
|
|
2022-10-22 04:25:12 +00:00
|
|
|
switch config.Port {
|
|
|
|
case 0:
|
2022-10-22 05:41:27 +00:00
|
|
|
dsnConfig.Addr = config.Host
|
2022-10-22 04:25:12 +00:00
|
|
|
default:
|
2022-10-22 05:41:27 +00:00
|
|
|
dsnConfig.Addr = fmt.Sprintf("%s:%d", config.Host, config.Port)
|
2021-11-23 09:45:38 +00:00
|
|
|
}
|
|
|
|
|
2022-10-22 05:41:27 +00:00
|
|
|
dsnConfig.DBName = config.Database
|
|
|
|
dsnConfig.User = config.Username
|
|
|
|
dsnConfig.Passwd = config.Password
|
|
|
|
dsnConfig.Timeout = config.Timeout
|
|
|
|
dsnConfig.MultiStatements = true
|
|
|
|
dsnConfig.ParseTime = true
|
|
|
|
dsnConfig.Loc = time.Local
|
2021-11-23 09:45:38 +00:00
|
|
|
|
2022-10-22 05:41:27 +00:00
|
|
|
return dsnConfig.FormatDSN()
|
2021-11-23 09:45:38 +00:00
|
|
|
}
|