Skip to content

RHINENG-22333: fix admin failure in ephemeral - #1953

Closed
MichaelMraka wants to merge 5 commits into
RedHatInsights:masterfrom
MichaelMraka:pr1
Closed

MichaelMraka wants to merge 5 commits into
RedHatInsights:masterfrom
MichaelMraka:pr1

Conversation

@MichaelMraka

@MichaelMraka MichaelMraka commented Nov 27, 2025

Copy link
Copy Markdown
Collaborator

Secure Coding Practices Checklist GitHub Link

Secure Coding Checklist

  • Input Validation
  • Output Encoding
  • Authentication and Password Management
  • Session Management
  • Access Control
  • Cryptographic Practices
  • Error Handling and Logging
  • Data Protection
  • Communication Security
  • System Configuration
  • Database Security
  • File Management
  • Memory Management
  • General Coding Practices

Summary by Sourcery

Allow configuring database connections with either standard or admin credentials and update admin API to use admin configuration.

New Features:

  • Add admin-aware application and database configuration entry points to support using elevated DB credentials when needed.

Enhancements:

  • Extend PostgreSQL configuration to optionally use admin user/password and support this choice across primary and read-replica connections.
  • Update database feed script and admin API startup to initialize using admin credentials where required.
  • Bump kessel SDK dependency from v1.3.0 to v1.4.0.

@sourcery-ai

sourcery-ai Bot commented Nov 27, 2025

Copy link
Copy Markdown

Reviewer's Guide

Adds support for initializing the database and application using either standard or admin DB credentials, updates admin-related entry points to use admin mode, and bumps the kessel SDK dependency version.

Sequence diagram for admin API startup using admin DB credentials

sequenceDiagram
    actor AdminOperator
    participant TurnpikeAdminAPI as RunAdminAPI
    participant Core as ConfigureAppAdmin
    participant Database as ConfigureAdmin
    participant DBInit as InitDB
    participant Config as loadEnvPostgreSQLConfig
    participant Gorm as openPostgreSQL

    AdminOperator->>TurnpikeAdminAPI: start admin API process
    TurnpikeAdminAPI->>Core: ConfigureAppAdmin(true)
    Core->>Database: ConfigureAdmin(true)
    Database->>DBInit: InitDB(true)
    DBInit->>Config: loadEnvPostgreSQLConfig(true, false)
    Config-->>DBInit: PostgreSQLConfig(admin user, admin password)
    DBInit->>Gorm: openPostgreSQL(PostgreSQLConfig)
    Gorm-->>DBInit: *gorm.DB (DB)
    DBInit-->>Database: initialized DB
    Database->>Database: loadAdditionalParamsFromDB()
    Core->>Core: metrics.Configure()
    Core->>Database: DBWait(dbWait)
    TurnpikeAdminAPI->>TurnpikeAdminAPI: continue admin HTTP server startup
Loading

Class diagram for updated DB and app configuration with admin support

classDiagram
    class CoreConfig {
        +string DBUser
        +string DBPassword
        +string DBAdminUser
        +string DBAdminPassword
        +string DBHost
        +string DBPort
        +string DBReadReplicaHost
        +string DBReadReplicaPort
        +string DBName
        +string DBSslMode
        +string DBSslRootCert
        +bool DBDebug
        +bool DBReadReplicaEnabled
    }

    class PostgreSQLConfig {
        +string User
        +string Host
        +string Port
        +string Database
        +string Passwd
        +string SSLMode
        +string SSLRootCert
        +bool Debug
    }

    class DatabaseSetup {
        +*gorm.DB DB
        +*gorm.DB DBReadReplica
        +PostgreSQLConfig globalPgConfig
        +InitDB(useAdmin bool)
        +ConfigureAdmin(useAdmin bool)
        +Configure()
        +loadEnvPostgreSQLConfig(useAdmin bool, useReadReplica bool) PostgreSQLConfig
        +DBWait(waitMode string)
        +openPostgreSQL(config PostgreSQLConfig) *gorm.DB
        +loadAdditionalParamsFromDB()
        +check(db *gorm.DB)
        +ReadReplicaConfigured() bool
    }

    class ApplicationConfig {
        +ConfigureAppAdmin(useAdmin bool)
        +ConfigureApp()
        +SetupTestEnvironment()
    }

    class TurnpikeAdminAPI {
        +RunAdminAPI()
    }

    class FeedDBScript {
        +main()
    }

    CoreConfig <.. PostgreSQLConfig : values from
    PostgreSQLConfig <.. DatabaseSetup : uses
    DatabaseSetup <.. ApplicationConfig : uses
    ApplicationConfig <.. TurnpikeAdminAPI : uses
    DatabaseSetup <.. FeedDBScript : uses

    TurnpikeAdminAPI --> ApplicationConfig : calls ConfigureAppAdmin(true)
    ApplicationConfig --> DatabaseSetup : calls ConfigureAdmin(useAdmin)
    FeedDBScript --> DatabaseSetup : calls InitDB(true)
    DatabaseSetup --> PostgreSQLConfig : constructs via loadEnvPostgreSQLConfig()
    PostgreSQLConfig --> CoreConfig : reads DB credentials
Loading

Flow diagram for selecting DB credentials and host based on admin and read replica flags

flowchart TD
    A["Start InitDB(useAdmin, useReadReplica)"] --> B[Set user = CoreCfg.DBUser]
    B --> C[Set passwd = CoreCfg.DBPassword]
    C --> D{useAdmin?}
    D -- Yes --> E[Set user = CoreCfg.DBAdminUser]
    E --> F[Set passwd = CoreCfg.DBAdminPassword]
    D -- No --> G[Keep standard user and password]
    F --> H
    G --> H[Set host = CoreCfg.DBHost, port = CoreCfg.DBPort]
    H --> I{useReadReplica?}
    I -- Yes --> J[Set host = CoreCfg.DBReadReplicaHost]
    J --> K[Set port = CoreCfg.DBReadReplicaPort]
    I -- No --> L[Keep primary host and port]
    K --> M[Build PostgreSQLConfig with user, host, port, database, passwd]
    L --> M
    M --> N["Open connections for DB (and DBReadReplica if enabled)"]
    N --> O[End]
Loading

File-Level Changes

Change Details Files
Allow database initialization to switch between normal and admin DB credentials and propagate this through configuration helpers.
  • Changed InitDB to accept a useAdmin flag and pass it to loadEnvPostgreSQLConfig for primary and replica connections.
  • Introduced ConfigureAdmin that calls InitDB with a configurable admin flag and moved shared logic from Configure into it, with Configure now defaulting to non-admin.
  • Refactored loadEnvPostgreSQLConfig to accept useAdmin and useReadReplica flags, selecting admin or normal DB credentials accordingly while preserving existing host/port and SSL configuration.
base/database/setup.go
base/core/config.go
Update callers to explicitly use admin DB initialization where required and keep default behaviour unchanged elsewhere.
  • Adjusted feed_db script to call InitDB with useAdmin=true so it uses admin credentials when feeding the database.
  • Updated RunAdminAPI to call ConfigureAppAdmin(true) so admin API runs with admin DB credentials.
  • Kept default app configuration path via ConfigureApp and Configure using non-admin credentials for normal runtime and tests (including SetupTestEnvironment).
scripts/feed_db.go
turnpike/admin_api.go
base/core/config.go
Update kessel SDK dependency to a newer minor version.
  • Bumped github.com/project-kessel/kessel-sdk-go from v1.3.0 to v1.4.0 in go.mod and go.sum.
go.mod
go.sum

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there - I've reviewed your changes - here's some feedback:

  • The addition of useAdmin booleans through InitDB, ConfigureAdmin, and ConfigureAppAdmin makes the call graph harder to reason about; consider exposing clearly named admin vs non-admin entrypoints (e.g., InitAdminDB / InitUserDB) instead of a flag that can be accidentally mis-set at call sites.
  • The loadEnvPostgreSQLConfig(useAdmin, useReadReplica) signature with two booleans is a bit opaque; using a small config/options struct or separate helper functions for admin vs replica config would make call sites more self-documenting and reduce the chance of argument ordering mistakes.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The addition of `useAdmin` booleans through `InitDB`, `ConfigureAdmin`, and `ConfigureAppAdmin` makes the call graph harder to reason about; consider exposing clearly named admin vs non-admin entrypoints (e.g., `InitAdminDB` / `InitUserDB`) instead of a flag that can be accidentally mis-set at call sites.
- The `loadEnvPostgreSQLConfig(useAdmin, useReadReplica)` signature with two booleans is a bit opaque; using a small config/options struct or separate helper functions for admin vs replica config would make call sites more self-documenting and reduce the chance of argument ordering mistakes.

## Individual Comments

### Comment 1
<location> `base/database/setup.go:111-120` </location>
<code_context>

 // load database config from environment vars using inserted prefix
-func loadEnvPostgreSQLConfig(useReadReplica bool) *PostgreSQLConfig {
+func loadEnvPostgreSQLConfig(useAdmin bool, useReadReplica bool) *PostgreSQLConfig {
+	user := utils.CoreCfg.DBUser
+	passwd := utils.CoreCfg.DBPassword
</code_context>

<issue_to_address>
**🚨 question (security):** Consider whether admin credentials should ever be used for the read replica connection

Since `useAdmin` is passed into `loadEnvPostgreSQLConfig` and combined with `useReadReplica`, the read replica will also use admin credentials when `InitDB(true)` is called and a replica is configured. For typical setups where replicas are read-only and should use restricted users, this broadens privileges unnecessarily. Consider ignoring `useAdmin` when `useReadReplica` is true, or introducing a separate `useReplicaAdmin` flag if elevated access on the replica is explicitly required.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread base/database/setup.go Outdated
@codecov-commenter

codecov-commenter commented Nov 27, 2025

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 48.97959% with 25 lines in your changes missing coverage. Please review.
✅ Project coverage is 58.93%. Comparing base (caf424b) to head (3b5f69c).
⚠️ Report is 7 commits behind head on master.

Files with missing lines Patch % Lines
base/database/setup.go 50.00% 19 Missing and 1 partial ⚠️
base/core/config.go 57.14% 3 Missing ⚠️
scripts/feed_db.go 0.00% 1 Missing ⚠️
turnpike/admin_api.go 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1953      +/-   ##
==========================================
+ Coverage   58.83%   58.93%   +0.09%     
==========================================
  Files         131      131              
  Lines        8407     8481      +74     
==========================================
+ Hits         4946     4998      +52     
- Misses       2927     2949      +22     
  Partials      534      534              
Flag Coverage Δ
unittests 58.93% <48.97%> (+0.09%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@MichaelMraka
MichaelMraka force-pushed the pr1 branch 3 times, most recently from 236b4d0 to 49ec98e Compare November 28, 2025 18:06
@MichaelMraka

Copy link
Copy Markdown
Collaborator Author

/retest

fixing
/home/michael/rhn/patchman-engine.git/vendor/github.com/ezamriy/gorpm/rpmtag.go:36:36: could not determine what C.RPMTAG_HDRID refers to
/home/michael/rhn/patchman-engine.git/vendor/github.com/ezamriy/gorpm/rpmtag.go:30:36: could not determine what C.RPMTAG_PKGID refers to
/home/michael/rhn/patchman-engine.git/vendor/github.com/ezamriy/gorpm/rpmtag.go:156:36: could not determine what C.RPMTAG_SOURCEPKGID refers to
FAIL	app/base/database [build failed]
@jira-linking

jira-linking Bot commented Dec 3, 2025

Copy link
Copy Markdown

Commits missing Jira IDs:
0b9c69e
3b5f69c
Referenced Jiras:
https://issues.redhat.com/browse/RHINENG-22333

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants