Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

feat: added boostrap check

=ceb4f82b79f3
diff --git a/backend-go/cmd/infisical/main.go b/backend-go/cmd/infisical/main.go
index 3e9be2e96c..49f1983027 100644
--- a/backend-go/cmd/infisical/main.go
+++ b/backend-go/cmd/infisical/main.go
@@ -11,6 +11,8 @@ import (
"syscall"
"github.com/infisical/api/internal/config"
+ "github.com/infisical/api/internal/database/pg"
+ "github.com/infisical/api/internal/pkg/bootstrap"
"github.com/infisical/api/internal/server"
"github.com/infisical/api/internal/services"
)
@@ -37,6 +39,23 @@ func main() {
os.Exit(1)
}
+ // Connect to database.
+ ctx := context.Background()
+ readReplicas := make([]pg.DBReadReplica, len(cfg.ParsedDBReadReplicas))
+ for i, r := range cfg.ParsedDBReadReplicas {
+ readReplicas[i] = pg.DBReadReplica{DBConnectionURI: r.DBConnectionURI, DBRootCert: r.DBRootCert}
+ }
+
+ db, err := pg.NewPostgresDB(ctx, cfg.DBConnectionURI, cfg.DBRootCert, readReplicas)
+ if err != nil {
+ logger.Error("failed to initialize database", "error", err)
+ os.Exit(1)
+ }
+ defer db.Close()
+
+ dbReport := bootstrap.CheckDBConnection(ctx, db)
+ dbReport.PrintReport(logger)
+
// Initialize all services.
svc := services.NewService(logger)
// Create server.
@@ -53,7 +72,7 @@ func main() {
}()
var wg sync.WaitGroup
- ctx, cancel := context.WithCancel(context.Background())
+ ctx, cancel := context.WithCancel(ctx)
// Start HTTP server.
srv.Listen(ctx, cfg.Addr(), &wg, errc)
diff --git a/backend-go/internal/database/pg/pg.go b/backend-go/internal/database/pg/pg.go
index fb2acd24ca..4fae44e73b 100644
--- a/backend-go/internal/database/pg/pg.go
+++ b/backend-go/internal/database/pg/pg.go
@@ -70,6 +70,11 @@ func (db *DB) ReplicaNode() *pgxpool.Pool {
return db.replicas[rand.IntN(len(db.replicas))]
}
+// Replicas returns all read-replica pools.
+func (db *DB) Replicas() []*pgxpool.Pool {
+ return db.replicas
+}
+
// Close closes all connection pools (primary + replicas).
func (db *DB) Close() {
db.primary.Close()
diff --git a/backend-go/internal/pkg/bootstrap/bootstrap.go b/backend-go/internal/pkg/bootstrap/bootstrap.go
new file mode 100644
index 0000000000..52af3c39e0
--- /dev/null
+++ b/backend-go/internal/pkg/bootstrap/bootstrap.go
@@ -0,0 +1,62 @@
+package bootstrap
+
+import (
+ "context"
+ "fmt"
+ "log/slog"
+
+ "github.com/infisical/api/internal/database/pg"
+)
+
+// ConnectionStatus holds the result of a single connection check.
+type ConnectionStatus struct {
+ Name string
+ Ok bool
+ Err error
+}
+
+// DBReport holds the results of checking all database connections.
+type DBReport struct {
+ Primary ConnectionStatus
+ Replicas []ConnectionStatus
+}
+
+// PrintReport logs the full connection report — successful and failed connections.
+func (r *DBReport) PrintReport(logger *slog.Logger) {
+ if r.Primary.Ok {
+ logger.Info("Database connection established", "name", r.Primary.Name)
+ } else {
+ logger.Error("Database connection failed", "name", r.Primary.Name, "error", r.Primary.Err)
+ }
+
+ for _, rep := range r.Replicas {
+ if rep.Ok {
+ logger.Info("Database connection established", "name", rep.Name)
+ } else {
+ logger.Error("Database connection failed", "name", rep.Name, "error", rep.Err)
+ }
+ }
+}
+
+// CheckDBConnection pings the primary database and all read replicas,
+// returning a report with the status of each.
+func CheckDBConnection(ctx context.Context, db *pg.DB) *DBReport {
+ report := &DBReport{}
+
+ if err := db.Primary().Ping(ctx); err != nil {
+ report.Primary = ConnectionStatus{Name: "primary", Ok: false, Err: fmt.Errorf("primary database: %w", err)}
+ } else {
+ report.Primary = ConnectionStatus{Name: "primary", Ok: true}
+ }
+
+ for i, replica := range db.Replicas() {
+ name := fmt.Sprintf("read-replica-%d", i)
+ if err := replica.Ping(ctx); err != nil {
+ report.Replicas = append(report.Replicas, ConnectionStatus{Name: name, Ok: false, Err: fmt.Errorf("%s: %w", name, err)})
+ } else {
+ report.Replicas = append(report.Replicas, ConnectionStatus{Name: name, Ok: true})
+ }
+ }
+
+ return report
+}