Skip to content

NO-JIRA | fix: source InfraImage cannot be updated#1042

Open
tupyy wants to merge 1 commit intokubev2v:mainfrom
tupyy:fix-source-update-bug
Open

NO-JIRA | fix: source InfraImage cannot be updated#1042
tupyy wants to merge 1 commit intokubev2v:mainfrom
tupyy:fix-source-update-bug

Conversation

@tupyy
Copy link
Copy Markdown
Collaborator

@tupyy tupyy commented Apr 1, 2026

This commit fixes the bug when source InfraImage cannot be updated once it has been set.

Signed-off-by: Cosmin Tupangiu cosmin@redhat.com

Summary by CodeRabbit

  • Refactor
    • Enhanced internal data structure handling for source updates to follow improved code patterns and better data management practices.

This commit fixes the bug when source InfraImage cannot be updated once
it has been set.

Signed-off-by: Cosmin Tupangiu <cosmin@redhat.com>
@tupyy tupyy requested a review from a team as a code owner April 1, 2026 08:15
@tupyy tupyy requested review from nirarg and ronenav and removed request for a team April 1, 2026 08:15
@tupyy tupyy added the on-hold label Apr 1, 2026
@openshift-ci
Copy link
Copy Markdown

openshift-ci bot commented Apr 1, 2026

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign avielsegev for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 1, 2026

📝 Walkthrough

Walkthrough

The pull request refactors the SourceUpdateForm.ToImageInfra method to return a newly constructed model.ImageInfra instead of mutating an existing instance. The method signature changes from accepting a pointer parameter to accepting a sourceID and returning a value. Corresponding caller code is updated to use the returned value.

Changes

Cohort / File(s) Summary
Method Signature Refactoring
internal/service/mappers/inbound.go
Changed ToImageInfra from in-place mutation pattern to value-returning pattern, now accepts sourceID uuid.UUID parameter and explicitly returns a new model.ImageInfra with conditionally copied form fields.
Service Layer Update
internal/service/source.go
Updated UpdateSource method to invoke the refactored ToImageInfra(source.ID) and pass the returned value to the store's Update method instead of mutating the loaded instance.
Import Reorganization
internal/store/image.go
Reordered imports without functional changes to code logic or behavior.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Suggested labels

lgtm, approved

Suggested reviewers

  • nirarg
  • ronenav
  • machacekondra

Poem

🐰 No more mutations in our warren today,
Fresh ImageInfra values come back our way,
From in-place changes to functional returns,
The mapper learns, the service learns,
Immutable hops through the update lane! ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main bug fix: changing the ToImageInfra method signature to return a new ImageInfra object instead of mutating an existing one, which enables proper updates.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@tupyy
Copy link
Copy Markdown
Collaborator Author

tupyy commented Apr 1, 2026

This is on-hold until we decide together with UI how to move forward.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
internal/service/mappers/inbound.go (1)

122-159: ⚠️ Potential issue | 🔴 Critical

Critical: ImageTokenKey will be zeroed on every update, breaking download URL validation.

The refactored ToImageInfra creates a fresh model.ImageInfra without preserving the existing ImageTokenKey. Since the Update operation would use GORM's Save() (full row overwrite), this will set ImageTokenKey to an empty string in the database on every source update.

The ImageTokenKey is the HMAC signing key generated during CreateSource and used by image.GenerateDownloadURLByToken() to create JWT tokens for image downloads. Zeroing it will break all existing download URLs and prevent generating valid new ones.

Either:

  1. Pass the existing ImageInfra to preserve its values (the original approach), or
  2. Fetch the existing ImageTokenKey and include it in the new struct, or
  3. Change Update to use targeted column updates instead of Save()
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@internal/service/mappers/inbound.go` around lines 122 - 159, The ToImageInfra
method currently builds a new model.ImageInfra and omits ImageTokenKey, which
will be zeroed when the row is saved; update ToImageInfra (or its caller) to
preserve the existing ImageTokenKey by either: (a) change ToImageInfra signature
to accept the existing model.ImageInfra (or the existing ImageTokenKey string)
and copy ImageTokenKey into the returned struct, or (b) before constructing the
new model.ImageInfra fetch the current ImageInfra (or its ImageTokenKey) and set
imageInfra.ImageTokenKey = existing.ImageTokenKey; ensure this change is applied
where SourceUpdateForm.ToImageInfra is used so that Update/Save does not
overwrite ImageTokenKey generated during CreateSource or used by
image.GenerateDownloadURLByToken.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@internal/service/source.go`:
- Around line 150-154: The update currently overwrites and zeroes existing
ImageInfra fields (including ImageTokenKey) because form.ToImageInfra(source.ID)
creates a new struct; instead preserve existing values by merging with the
fetched source.ImageInfra before calling s.store.ImageInfra().Update. Fix by
either mutating the existing source.ImageInfra (update fields on
source.ImageInfra then pass it to s.store.ImageInfra().Update) or change
SourceUpdateForm.ToImageInfra to accept the existing model.ImageInfra (e.g.,
ToImageInfra(existing model.ImageInfra) model.ImageInfra) and call it with
source.ImageInfra so only intended fields are overwritten while ImageTokenKey
and other existing values remain intact when invoking
s.store.ImageInfra().Update.

---

Outside diff comments:
In `@internal/service/mappers/inbound.go`:
- Around line 122-159: The ToImageInfra method currently builds a new
model.ImageInfra and omits ImageTokenKey, which will be zeroed when the row is
saved; update ToImageInfra (or its caller) to preserve the existing
ImageTokenKey by either: (a) change ToImageInfra signature to accept the
existing model.ImageInfra (or the existing ImageTokenKey string) and copy
ImageTokenKey into the returned struct, or (b) before constructing the new
model.ImageInfra fetch the current ImageInfra (or its ImageTokenKey) and set
imageInfra.ImageTokenKey = existing.ImageTokenKey; ensure this change is applied
where SourceUpdateForm.ToImageInfra is used so that Update/Save does not
overwrite ImageTokenKey generated during CreateSource or used by
image.GenerateDownloadURLByToken.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 0efbcce5-0a2b-4c79-8bb6-8a2a620248df

📥 Commits

Reviewing files that changed from the base of the PR and between 495d5b0 and ed2c5d7.

📒 Files selected for processing (3)
  • internal/service/mappers/inbound.go
  • internal/service/source.go
  • internal/store/image.go

Comment on lines 150 to 154
// Update ImageInfra
form.ToImageInfra(&source.ImageInfra)
if _, err := s.store.ImageInfra().Update(ctx, source.ImageInfra); err != nil {
imageInfra := form.ToImageInfra(source.ID)
if _, err := s.store.ImageInfra().Update(ctx, imageInfra); err != nil {
return nil, err
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

This discards the existing ImageInfra values including ImageTokenKey.

The source fetched at line 136 contains source.ImageInfra with all existing values (including ImageTokenKey). However, form.ToImageInfra(source.ID) creates a new struct without those values, which are then overwritten to zero in the database.

To fix the bug while preserving existing data, consider one of these approaches:

Option 1: Revert to mutation pattern (preserves all values)
 	// Update ImageInfra
-	imageInfra := form.ToImageInfra(source.ID)
-	if _, err := s.store.ImageInfra().Update(ctx, imageInfra); err != nil {
+	form.ToImageInfra(&source.ImageInfra)
+	if _, err := s.store.ImageInfra().Update(ctx, source.ImageInfra); err != nil {
 		return nil, err
 	}
Option 2: Pass existing ImageInfra to new mapper (if you prefer value semantics)

Change the mapper to accept and merge with the existing struct:

func (f *SourceUpdateForm) ToImageInfra(existing model.ImageInfra) model.ImageInfra {
    // Start with existing values, then override with non-nil form fields
    if f.SshPublicKey != nil {
        existing.SshPublicKey = *f.SshPublicKey
    }
    // ... etc
    return existing
}

Then call: imageInfra := form.ToImageInfra(source.ImageInfra)

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@internal/service/source.go` around lines 150 - 154, The update currently
overwrites and zeroes existing ImageInfra fields (including ImageTokenKey)
because form.ToImageInfra(source.ID) creates a new struct; instead preserve
existing values by merging with the fetched source.ImageInfra before calling
s.store.ImageInfra().Update. Fix by either mutating the existing
source.ImageInfra (update fields on source.ImageInfra then pass it to
s.store.ImageInfra().Update) or change SourceUpdateForm.ToImageInfra to accept
the existing model.ImageInfra (e.g., ToImageInfra(existing model.ImageInfra)
model.ImageInfra) and call it with source.ImageInfra so only intended fields are
overwritten while ImageTokenKey and other existing values remain intact when
invoking s.store.ImageInfra().Update.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant