Skip to content

Commit

Permalink
RHINENG-6298: avoid modification of data in vmaas cache
Browse files Browse the repository at this point in the history
  • Loading branch information
psegedy committed Jan 4, 2024
1 parent 1c0dd89 commit 004fe3e
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 2 deletions.
18 changes: 16 additions & 2 deletions evaluator/evaluate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"sync"
"time"

"github.com/jinzhu/copier"
"github.com/pkg/errors"
"gorm.io/gorm"
"gorm.io/gorm/clause"
Expand Down Expand Up @@ -312,10 +313,16 @@ func getUpdatesData(ctx context.Context, tx *gorm.DB, system *models.SystemPlatf

func getVmaasUpdates(ctx context.Context, tx *gorm.DB,
system *models.SystemPlatform) (*vmaas.UpdatesV3Response, error) {
var vmaasDataCopy vmaas.UpdatesV3Response
// first check if we have data in cache
vmaasData, ok := memoryVmaasCache.Get(system.JSONChecksum)
if ok {
return vmaasData, nil
// return copy of vmaasData to avoid modification of cached data e.g. by templates
err := copier.CopyWithOption(&vmaasDataCopy, vmaasData, copier.Option{DeepCopy: true})
if err != nil {
return nil, err
}
return &vmaasDataCopy, nil
}
updatesReq, err := tryGetVmaasRequest(system)
if err != nil {
Expand Down Expand Up @@ -343,7 +350,14 @@ func getVmaasUpdates(ctx context.Context, tx *gorm.DB,
return nil, errors.Wrap(err, "vmaas API call failed")
}

memoryVmaasCache.Add(system.JSONChecksum, vmaasData)
if memoryVmaasCache.enabled {
// store copy of vmaasData to cache to avoid modification of cached data e.g. by templates
err := copier.CopyWithOption(&vmaasDataCopy, vmaasData, copier.Option{DeepCopy: true})
if err != nil {
return nil, err
}
memoryVmaasCache.Add(system.JSONChecksum, &vmaasDataCopy)
}
return vmaasData, nil
}

Expand Down
41 changes: 41 additions & 0 deletions evaluator/evaluate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,3 +371,44 @@ func TestGetUpdatesDataVmaas400(t *testing.T) {
assert.Nil(t, err)
assert.Nil(t, res)
}

func TestGetVmaasDataCached(t *testing.T) {
utils.SkipWithoutDB(t)
utils.SkipWithoutPlatform(t)
configure()
loadCache()
req := vmaas.UpdatesV3Request{
PackageList: []string{"firefox-0:76.0.1-1.fc31.x86_64"}, RepositoryList: []string{"repo"},
}
reqJSON, _ := json.Marshal(req)
reqString := string(reqJSON)
chsum := "123"
sp := models.SystemPlatform{VmaasJSON: &reqString, JSONChecksum: &chsum}

var assertInstallable = func() (*vmaas.UpdatesV3Response, []vmaas.UpdatesV3ResponseAvailableUpdates) {
vmaasData, _ := getVmaasUpdates(context.Background(), database.Db, &sp)
updates := (*vmaasData.UpdateList)["firefox-0:76.0.1-1.fc31.x86_64"].GetAvailableUpdates()
assert.Equal(t, INSTALLABLE, updates[0].StatusID)
return vmaasData, updates
}
vmaasData, updates := assertInstallable()

// modify update status id, change data returned by vmaas
var setApplicable = func() {
updates[0].StatusID = APPLICABLE
modifiedUpdateList := vmaas.UpdatesV3ResponseUpdateList{AvailableUpdates: &updates}
(*vmaasData.UpdateList)["firefox-0:76.0.1-1.fc31.x86_64"] = &modifiedUpdateList
updates = (*vmaasData.UpdateList)["firefox-0:76.0.1-1.fc31.x86_64"].GetAvailableUpdates()
assert.Equal(t, APPLICABLE, updates[0].StatusID)
}
setApplicable()

// cached value mustn't be changed
vmaasData, updates = assertInstallable()

// modify data again to change data returned from cache
setApplicable()

// cached value mustn't be changed
assertInstallable()
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/gocarina/gocsv v0.0.0-20231116093920-b87c2d0e983a
github.com/golang-migrate/migrate/v4 v4.16.2
github.com/hashicorp/golang-lru/v2 v2.0.7
github.com/jinzhu/copier v0.4.0
github.com/joho/godotenv v1.5.1
github.com/lestrrat-go/backoff v1.0.1
github.com/lib/pq v1.10.9
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ github.com/jackc/pgx/v5 v5.5.0 h1:NxstgwndsTRy7eq9/kqYc/BZh5w2hHJV86wjvO+1xPw=
github.com/jackc/pgx/v5 v5.5.0/go.mod h1:Ig06C2Vu0t5qXC60W8sqIthScaEnFvojjj9dSljmHRA=
github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=
github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/jinzhu/copier v0.4.0 h1:w3ciUoD19shMCRargcpm0cm91ytaBhDvuRpz1ODO/U8=
github.com/jinzhu/copier v0.4.0/go.mod h1:DfbEm0FYsaqBcKcFuvmOZb218JkPGtvSHsKg8S8hyyg=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
Expand Down

0 comments on commit 004fe3e

Please sign in to comment.