Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion internal/service/mappers/inbound.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,22 +119,30 @@ func (f *SourceUpdateForm) ToSource(source *model.Source) {
}
}

func (f *SourceUpdateForm) ToImageInfra(imageInfra *model.ImageInfra) {
func (f *SourceUpdateForm) ToImageInfra(sourceID uuid.UUID) model.ImageInfra {
imageInfra := model.ImageInfra{
SourceID: sourceID,
}

if f.SshPublicKey != nil {
imageInfra.SshPublicKey = *f.SshPublicKey
}
if f.CertificateChain != nil {
imageInfra.CertificateChain = *f.CertificateChain
}

if f.HttpUrl != nil {
imageInfra.HttpProxyUrl = *f.HttpUrl
}

if f.HttpsUrl != nil {
imageInfra.HttpsProxyUrl = *f.HttpsUrl
}

if f.NoProxy != nil {
imageInfra.NoProxyDomains = *f.NoProxy
}

if f.IpAddress != nil {
imageInfra.IpAddress = *f.IpAddress
}
Expand All @@ -147,6 +155,8 @@ func (f *SourceUpdateForm) ToImageInfra(imageInfra *model.ImageInfra) {
if f.Dns != nil {
imageInfra.Dns = *f.Dns
}

return imageInfra
}

func (f *SourceUpdateForm) ToLabels() []model.Label {
Expand Down
4 changes: 2 additions & 2 deletions internal/service/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ func (s *SourceService) UpdateSource(ctx context.Context, id uuid.UUID, form map
}

// 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
}
Comment on lines 150 to 154
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.


Expand Down
3 changes: 2 additions & 1 deletion internal/store/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package store
import (
"context"

"github.com/kubev2v/migration-planner/internal/store/model"
"gorm.io/gorm"

"github.com/kubev2v/migration-planner/internal/store/model"
)

type ImageInfra interface {
Expand Down
Loading