|
| 1 | +// Copyright (c) 2025, Oracle and/or its affiliates. |
| 2 | +// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. |
| 3 | + |
| 4 | +//go:build goora |
| 5 | + |
| 6 | +package collector |
| 7 | + |
| 8 | +import ( |
| 9 | + "database/sql" |
| 10 | + "errors" |
| 11 | + "fmt" |
| 12 | + _ "github.com/sijms/go-ora/v2" |
| 13 | + "github.com/sijms/go-ora/v2/network" |
| 14 | + "log/slog" |
| 15 | +) |
| 16 | + |
| 17 | +func connect(logger *slog.Logger, dbname string, dbconfig DatabaseConfig) *sql.DB { |
| 18 | + logger.Debug("Launching connection to "+maskDsn(dbconfig.URL), "database", dbname) |
| 19 | + |
| 20 | + password := dbconfig.GetPassword() |
| 21 | + username := dbconfig.GetUsername() |
| 22 | + dbconfig.ExternalAuth = password == "" |
| 23 | + |
| 24 | + logger.Debug(fmt.Sprintf("external authentication set to %t", dbconfig.ExternalAuth), "database", dbname) |
| 25 | + |
| 26 | + msg := "Using Username/Password Authentication." |
| 27 | + if dbconfig.ExternalAuth { |
| 28 | + msg = "Database Password not specified; will attempt to use external authentication (ignoring user input)." |
| 29 | + dbconfig.Username = "" |
| 30 | + } |
| 31 | + logger.Info(msg, "database", dbname) |
| 32 | + |
| 33 | + // Build connection string for go-ora |
| 34 | + var dsn string |
| 35 | + if dbconfig.ExternalAuth { |
| 36 | + // go-ora doesn't directly support external authentication |
| 37 | + // So we rely on OS authentication (set Oracle wallet/env) |
| 38 | + dsn = fmt.Sprintf("oracle://@%s", dbconfig.URL) |
| 39 | + } else if username != "" { |
| 40 | + dsn = fmt.Sprintf("oracle://%s:%s@%s", username, password, dbconfig.URL) |
| 41 | + } else { |
| 42 | + dsn = fmt.Sprintf("oracle://%s", dbconfig.URL) |
| 43 | + } |
| 44 | + |
| 45 | + // open connection (lazy until first use) |
| 46 | + db, err := sql.Open("oracle", dsn) |
| 47 | + if err != nil { |
| 48 | + logger.Error("Failed to create DB handle", "error", err, "database", dbname) |
| 49 | + return nil |
| 50 | + } |
| 51 | + |
| 52 | + // Configure connection pool (sql.DB handles pooling) |
| 53 | + setConnectionPool(logger, dbname, dbconfig, db) |
| 54 | + initdb(logger, dbname, dbconfig, db) |
| 55 | + return db |
| 56 | +} |
| 57 | + |
| 58 | +func setConnectionPool(logger *slog.Logger, dbname string, dbconfig DatabaseConfig, db *sql.DB) { |
| 59 | + if dbconfig.GetPoolMaxConnections() > 0 { |
| 60 | + logger.Debug(fmt.Sprintf("set pool max connections to %d", dbconfig.PoolMaxConnections), "database", dbname) |
| 61 | + db.SetMaxOpenConns(dbconfig.GetPoolMaxConnections()) |
| 62 | + } else { |
| 63 | + db.SetMaxOpenConns(dbconfig.GetMaxOpenConns()) |
| 64 | + } |
| 65 | + if dbconfig.GetPoolMinConnections() > 0 { |
| 66 | + logger.Debug(fmt.Sprintf("set pool min connections to %d", dbconfig.PoolMinConnections), "database", dbname) |
| 67 | + db.SetMaxIdleConns(dbconfig.GetPoolMinConnections()) |
| 68 | + } else { |
| 69 | + db.SetMaxIdleConns(dbconfig.GetMaxIdleConns()) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func isInvalidCredentialsError(err error) bool { |
| 74 | + if err == nil { |
| 75 | + return false |
| 76 | + } |
| 77 | + var oraErr *network.OracleError |
| 78 | + ok := errors.As(err, &oraErr) |
| 79 | + if !ok { |
| 80 | + return false |
| 81 | + } |
| 82 | + return oraErr.ErrCode == ora01017code || oraErr.ErrCode == ora28000code |
| 83 | +} |
0 commit comments