Fix db connection of auth

This commit is contained in:
Zoe Roux 2026-03-19 13:44:50 +01:00
parent 36775d7a97
commit c1afbfef7f
No known key found for this signature in database
4 changed files with 5 additions and 39 deletions

View File

@ -23,7 +23,7 @@ export async function setupLogging() {
const minLevel = aliasMap[minLevelRaw] ?? minLevelRaw;
return withFilter(
redactByField(getConsoleSink(), {
fieldPatterns: [/password/i, /secret/i, /apikey/i],
fieldPatterns: [/password/i, /secret/i, /apikey/i, /ca/i, /cert/i],
action: () => "[REDACTED]",
}),
getLevelFilter(parseLogLevel(minLevel)),
@ -38,7 +38,7 @@ export async function setupLogging() {
const minLevel = aliasMap[minLevelRaw] ?? minLevelRaw;
return withFilter(
redactByField(getOpenTelemetrySink({ loggerProvider }), {
fieldPatterns: [/password/i, /secret/i, /apikey/i],
fieldPatterns: [/password/i, /secret/i, /apikey/i, /ca/i, /cert/i],
action: () => "[REDACTED]",
}),
getLevelFilter(parseLogLevel(minLevel)),

View File

@ -3,12 +3,10 @@ package main
import (
"context"
"encoding/base64"
"errors"
"fmt"
"log/slog"
"net/http"
"os"
"os/user"
"slices"
"sort"
"strings"
@ -103,10 +101,10 @@ func GetenvOr(env string, def string) string {
}
func OpenDatabase(ctx context.Context) (*pgxpool.Pool, error) {
connectionString := GetenvOr("POSTGRES_URL", "")
connectionString := os.Getenv("POSTGRES_URL")
config, err := pgxpool.ParseConfig(connectionString)
if err != nil {
return nil, errors.New("failed to create postgres config from environment variables")
return nil, fmt.Errorf("failed to create postgres config from environment variables: %v", err)
}
// Set default values
@ -116,22 +114,6 @@ func OpenDatabase(ctx context.Context) (*pgxpool.Pool, error) {
if config.ConnConfig.Database == "" {
config.ConnConfig.Database = "kyoo"
}
// The pgx library will set the username to the name of the current user if not provided via
// environment variable or connection string. Make a best-effort attempt to see if the user
// was explicitly specified, without implementing full connection string parsing. If not, set
// the username to the default value of "kyoo".
if os.Getenv("PGUSER") == "" {
currentUserName, _ := user.Current()
// If the username matches the current user and it's not in the connection string, then it was set
// by the pgx library. This doesn't cover the case where the system username happens to be in some other part
// of the connection string, but this cannot be checked without full connection string parsing.
if currentUserName.Username == config.ConnConfig.User && !strings.Contains(connectionString, currentUserName.Username) {
config.ConnConfig.User = "kyoo"
}
}
if config.ConnConfig.Password == "" {
config.ConnConfig.Password = "password"
}
if _, ok := config.ConnConfig.RuntimeParams["application_name"]; !ok {
config.ConnConfig.RuntimeParams["application_name"] = "keibi"
}

View File

@ -37,7 +37,6 @@ func setupOtel(ctx context.Context) (func(context.Context) error, error) {
resource.WithAttributes(semconv.ServiceNameKey.String("kyoo.auth")),
resource.WithFromEnv(),
resource.WithTelemetrySDK(),
resource.WithProcess(),
resource.WithOS(),
resource.WithContainer(),
resource.WithHost(),

View File

@ -6,8 +6,6 @@ import (
"errors"
"fmt"
"os"
"os/user"
"strings"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
@ -78,7 +76,7 @@ func (s *MetadataService) setupDb() (*pgxpool.Pool, error) {
connectionString := os.Getenv("POSTGRES_URL")
config, err := pgxpool.ParseConfig(connectionString)
if err != nil {
return nil, errors.New("failed to create postgres config from environment variables")
return nil, fmt.Errorf("failed to create postgres config from environment variables: %v", err)
}
// Set default values
@ -88,19 +86,6 @@ func (s *MetadataService) setupDb() (*pgxpool.Pool, error) {
if config.ConnConfig.Database == "" {
config.ConnConfig.Database = "kyoo"
}
// The pgx library will set the username to the name of the current user if not provided via
// environment variable or connection string. Make a best-effort attempt to see if the user
// was explicitly specified, without implementing full connection string parsing. If not, set
// the username to the default value of "kyoo".
if os.Getenv("PGUSER") == "" {
currentUserName, _ := user.Current()
// If the username matches the current user and it's not in the connection string, then it was set
// by the pgx library. This doesn't cover the case where the system username happens to be in some other part
// of the connection string, but this cannot be checked without full connection string parsing.
if currentUserName.Username == config.ConnConfig.User && !strings.Contains(connectionString, currentUserName.Username) {
config.ConnConfig.User = "kyoo"
}
}
if _, ok := config.ConnConfig.RuntimeParams["application_name"]; !ok {
config.ConnConfig.RuntimeParams["application_name"] = "gocoder"
}