2021-11-23 09:45:38 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
_ "github.com/jackc/pgx/v4/stdlib" // Load the PostgreSQL Driver used in the connection string.
|
|
|
|
|
|
|
|
"github.com/authelia/authelia/v4/internal/configuration/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
// PostgreSQLProvider is a PostgreSQL provider.
|
|
|
|
type PostgreSQLProvider struct {
|
|
|
|
SQLProvider
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewPostgreSQLProvider a PostgreSQL provider.
|
2021-12-01 12:11:29 +00:00
|
|
|
func NewPostgreSQLProvider(config *schema.Configuration) (provider *PostgreSQLProvider) {
|
2021-11-23 09:45:38 +00:00
|
|
|
provider = &PostgreSQLProvider{
|
2021-12-01 12:11:29 +00:00
|
|
|
SQLProvider: NewSQLProvider(config, providerPostgres, "pgx", dataSourceNamePostgreSQL(*config.Storage.PostgreSQL)),
|
2021-11-23 09:45:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// All providers have differing SELECT existing table statements.
|
|
|
|
provider.sqlSelectExistingTables = queryPostgreSelectExistingTables
|
|
|
|
|
|
|
|
// Specific alterations to this provider.
|
|
|
|
// PostgreSQL doesn't have a UPSERT statement but has an ON CONFLICT operation instead.
|
|
|
|
provider.sqlUpsertU2FDevice = fmt.Sprintf(queryFmtPostgresUpsertU2FDevice, tableU2FDevices)
|
2021-12-02 04:16:45 +00:00
|
|
|
provider.sqlUpsertDuoDevice = fmt.Sprintf(queryFmtPostgresUpsertDuoDevice, tableDuoDevices)
|
2021-11-23 09:45:38 +00:00
|
|
|
provider.sqlUpsertTOTPConfig = fmt.Sprintf(queryFmtPostgresUpsertTOTPConfiguration, tableTOTPConfigurations)
|
|
|
|
provider.sqlUpsertPreferred2FAMethod = fmt.Sprintf(queryFmtPostgresUpsertPreferred2FAMethod, tableUserPreferences)
|
2021-11-25 01:56:58 +00:00
|
|
|
provider.sqlUpsertEncryptionValue = fmt.Sprintf(queryFmtPostgresUpsertEncryptionValue, tableEncryption)
|
2021-11-23 09:45:38 +00:00
|
|
|
|
|
|
|
// PostgreSQL requires rebinding of any query that contains a '?' placeholder to use the '$#' notation placeholders.
|
|
|
|
provider.sqlFmtRenameTable = provider.db.Rebind(provider.sqlFmtRenameTable)
|
|
|
|
provider.sqlSelectPreferred2FAMethod = provider.db.Rebind(provider.sqlSelectPreferred2FAMethod)
|
|
|
|
provider.sqlSelectUserInfo = provider.db.Rebind(provider.sqlSelectUserInfo)
|
2021-12-04 04:34:20 +00:00
|
|
|
provider.sqlSelectIdentityVerification = provider.db.Rebind(provider.sqlSelectIdentityVerification)
|
2021-11-23 09:45:38 +00:00
|
|
|
provider.sqlInsertIdentityVerification = provider.db.Rebind(provider.sqlInsertIdentityVerification)
|
2021-12-03 00:04:11 +00:00
|
|
|
provider.sqlConsumeIdentityVerification = provider.db.Rebind(provider.sqlConsumeIdentityVerification)
|
2021-11-23 09:45:38 +00:00
|
|
|
provider.sqlSelectTOTPConfig = provider.db.Rebind(provider.sqlSelectTOTPConfig)
|
|
|
|
provider.sqlDeleteTOTPConfig = provider.db.Rebind(provider.sqlDeleteTOTPConfig)
|
2021-11-25 01:56:58 +00:00
|
|
|
provider.sqlSelectTOTPConfigs = provider.db.Rebind(provider.sqlSelectTOTPConfigs)
|
|
|
|
provider.sqlUpdateTOTPConfigSecret = provider.db.Rebind(provider.sqlUpdateTOTPConfigSecret)
|
|
|
|
provider.sqlUpdateTOTPConfigSecretByUsername = provider.db.Rebind(provider.sqlUpdateTOTPConfigSecretByUsername)
|
2021-11-23 09:45:38 +00:00
|
|
|
provider.sqlSelectU2FDevice = provider.db.Rebind(provider.sqlSelectU2FDevice)
|
2021-12-03 00:04:11 +00:00
|
|
|
provider.sqlSelectU2FDevices = provider.db.Rebind(provider.sqlSelectU2FDevices)
|
|
|
|
provider.sqlUpdateU2FDevicePublicKey = provider.db.Rebind(provider.sqlUpdateU2FDevicePublicKey)
|
|
|
|
provider.sqlUpdateU2FDevicePublicKeyByUsername = provider.db.Rebind(provider.sqlUpdateU2FDevicePublicKeyByUsername)
|
2021-12-02 06:06:04 +00:00
|
|
|
provider.sqlSelectDuoDevice = provider.db.Rebind(provider.sqlSelectDuoDevice)
|
|
|
|
provider.sqlDeleteDuoDevice = provider.db.Rebind(provider.sqlDeleteDuoDevice)
|
2021-11-23 09:45:38 +00:00
|
|
|
provider.sqlInsertAuthenticationAttempt = provider.db.Rebind(provider.sqlInsertAuthenticationAttempt)
|
|
|
|
provider.sqlSelectAuthenticationAttemptsByUsername = provider.db.Rebind(provider.sqlSelectAuthenticationAttemptsByUsername)
|
|
|
|
provider.sqlInsertMigration = provider.db.Rebind(provider.sqlInsertMigration)
|
2021-12-02 06:06:04 +00:00
|
|
|
provider.sqlSelectMigrations = provider.db.Rebind(provider.sqlSelectMigrations)
|
|
|
|
provider.sqlSelectLatestMigration = provider.db.Rebind(provider.sqlSelectLatestMigration)
|
2021-11-25 01:56:58 +00:00
|
|
|
provider.sqlSelectEncryptionValue = provider.db.Rebind(provider.sqlSelectEncryptionValue)
|
2021-11-23 09:45:38 +00:00
|
|
|
|
2021-12-03 06:29:55 +00:00
|
|
|
provider.schema = config.Storage.PostgreSQL.Schema
|
|
|
|
|
2021-11-23 09:45:38 +00:00
|
|
|
return provider
|
|
|
|
}
|
|
|
|
|
|
|
|
func dataSourceNamePostgreSQL(config schema.PostgreSQLStorageConfiguration) (dataSourceName string) {
|
|
|
|
args := []string{
|
2021-12-02 05:36:03 +00:00
|
|
|
fmt.Sprintf("host=%s", config.Host),
|
2021-11-23 09:45:38 +00:00
|
|
|
fmt.Sprintf("user='%s'", config.Username),
|
|
|
|
fmt.Sprintf("password='%s'", config.Password),
|
2021-12-02 05:36:03 +00:00
|
|
|
fmt.Sprintf("dbname=%s", config.Database),
|
2021-12-03 06:29:55 +00:00
|
|
|
fmt.Sprintf("search_path=%s", config.Schema),
|
|
|
|
fmt.Sprintf("sslmode=%s", config.SSL.Mode),
|
2021-11-23 09:45:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if config.Port > 0 {
|
|
|
|
args = append(args, fmt.Sprintf("port=%d", config.Port))
|
|
|
|
}
|
|
|
|
|
2021-12-02 05:36:03 +00:00
|
|
|
if config.SSL.RootCertificate != "" {
|
|
|
|
args = append(args, fmt.Sprintf("sslrootcert=%s", config.SSL.RootCertificate))
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.SSL.Certificate != "" {
|
|
|
|
args = append(args, fmt.Sprintf("sslcert=%s", config.SSL.Certificate))
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.SSL.Key != "" {
|
|
|
|
args = append(args, fmt.Sprintf("sslkey=%s", config.SSL.Key))
|
2021-11-23 09:45:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
args = append(args, fmt.Sprintf("connect_timeout=%d", int32(config.Timeout/time.Second)))
|
|
|
|
|
|
|
|
return strings.Join(args, " ")
|
|
|
|
}
|