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
|
|
|
"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
|
|
|
|
2023-05-07 06:39:17 +00:00
|
|
|
dsnConfig.Net = config.Address.Network()
|
|
|
|
dsnConfig.Addr = config.Address.NetworkAddress()
|
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 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
|
|
|
}
|