-
Notifications
You must be signed in to change notification settings - Fork 174
ROX-11101: Remove deprecated resources #3036
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rhybrillou
merged 15 commits into
master
from
yann/ROX-11101-remove_deprecated_resources
Nov 8, 2022
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
6c7a164
ROX-11101: Remove deprecated resources - Step 1: migration of existin…
rhybrillou cc9ac24
Address review comments
rhybrillou 3289b72
Style fix
rhybrillou 93e2f76
Annonce deprecation of further permissions and remove these further p…
rhybrillou f0ba4dd
Split Role permission back out of Access
rhybrillou 0e1c688
Shift migration versions to avoid conflict on rebase/merge
rhybrillou 9cc7500
Debug commit to observe default permission sets
rhybrillou 8f22474
Fix
rhybrillou b06c0ba
Remove debug traces
rhybrillou acc6dde
Adjust Administration computed level as not aggregating permissions f…
rhybrillou 4c68901
Address code review comments
rhybrillou 01206d8
Avoid version conflict
rhybrillou 4ba8aa3
Address review comments
rhybrillou 1614bca
Fix wrong type
rhybrillou efa8528
Enrich changelog
rhybrillou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
migrator/migrations/m_110_to_m_111_replace_deprecated_resources/migration.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| package m110tom111 | ||
|
|
||
| import ( | ||
| "github.com/gogo/protobuf/proto" | ||
| "github.com/pkg/errors" | ||
| "github.com/stackrox/rox/generated/storage" | ||
| "github.com/stackrox/rox/migrator/migrations" | ||
| "github.com/stackrox/rox/migrator/types" | ||
| "github.com/tecbot/gorocksdb" | ||
| ) | ||
|
|
||
| const ( | ||
| batchSize = 500 | ||
| ) | ||
|
|
||
| // Replacement resources | ||
| const ( | ||
| Access = "Access" | ||
| DeploymentExtension = "DeploymentExtension" | ||
| Image = "Image" | ||
| Integration = "Integration" | ||
| ) | ||
|
|
||
| // Replaced resources | ||
| const ( | ||
| APIToken = "APIToken" | ||
| AuthProvider = "AuthProvider" | ||
| BackupPlugins = "BackupPlugins" | ||
| Group = "Group" | ||
| ImageComponent = "ImageComponent" | ||
| ImageIntegration = "ImageIntegration" | ||
| Indicator = "Indicator" | ||
| Licenses = "Licenses" | ||
| NetworkBaseline = "NetworkBaseline" | ||
| Notifier = "Notifier" | ||
| ProcessWhitelist = "ProcessWhitelist" | ||
| Risk = "Risk" | ||
| SignatureIntegration = "SignatureIntegration" | ||
| User = "User" | ||
| ) | ||
|
|
||
| var ( | ||
| migration = types.Migration{ | ||
| StartingSeqNum: 110, | ||
| VersionAfter: &storage.Version{SeqNum: 111}, | ||
| Run: func(databases *types.Databases) error { | ||
| return migrateReplacedResourcesInPermissionSets(databases.RocksDB) | ||
| }, | ||
| } | ||
|
|
||
| prefix = []byte("permission_sets") | ||
|
|
||
| replacements = map[string]string{ | ||
| APIToken: Integration, | ||
| AuthProvider: Access, | ||
| BackupPlugins: Integration, | ||
| Group: Access, | ||
| ImageComponent: Image, | ||
| ImageIntegration: Integration, | ||
| Indicator: DeploymentExtension, | ||
| Licenses: Access, | ||
| NetworkBaseline: DeploymentExtension, | ||
| Notifier: Integration, | ||
| ProcessWhitelist: DeploymentExtension, | ||
| Risk: DeploymentExtension, | ||
| SignatureIntegration: Integration, | ||
| User: Access, | ||
| } | ||
|
|
||
| readOpts = gorocksdb.NewDefaultReadOptions() | ||
| writeOpts = gorocksdb.NewDefaultWriteOptions() | ||
| ) | ||
|
|
||
| func init() { | ||
| migrations.MustRegisterMigration(migration) | ||
| } | ||
|
|
||
| func propagateAccessForPermission(permission string, accessLevel storage.Access, permissionSet map[string]storage.Access) storage.Access { | ||
| oldLevel, found := permissionSet[permission] | ||
| if !found { | ||
| return accessLevel | ||
| } | ||
| if accessLevel > oldLevel { | ||
| return oldLevel | ||
| } | ||
| return accessLevel | ||
| } | ||
|
|
||
| func migrateReplacedResourcesInPermissionSets(db *gorocksdb.DB) error { | ||
| it := db.NewIterator(readOpts) | ||
| defer it.Close() | ||
| wb := gorocksdb.NewWriteBatch() | ||
| defer wb.Destroy() | ||
| for it.Seek(prefix); it.ValidForPrefix(prefix); it.Next() { | ||
| permissions := &storage.PermissionSet{} | ||
| if err := proto.Unmarshal(it.Value().Data(), permissions); err != nil { | ||
| return errors.Wrap(err, "unable to unmarshal permission set") | ||
| } | ||
| // Copy the permission set, removing the deprecated resource permissions, and keeping the | ||
| // lowest access level between that of deprecated resource and their replacement | ||
| // for the replacement resource. | ||
| newPermissionSet := permissions.Clone() | ||
| newPermissionSet.ResourceToAccess = make(map[string]storage.Access, len(permissions.GetResourceToAccess())) | ||
| for resource, accessLevel := range permissions.GetResourceToAccess() { | ||
| if _, found := replacements[resource]; found { | ||
| resource = replacements[resource] | ||
| } | ||
| newPermissionSet.ResourceToAccess[resource] = | ||
| propagateAccessForPermission(resource, accessLevel, newPermissionSet.ResourceToAccess) | ||
| } | ||
| data, err := proto.Marshal(newPermissionSet) | ||
| if err != nil { | ||
| return errors.Wrap(err, "unable to marshal permission set") | ||
| } | ||
| wb.Put(it.Key().Copy(), data) | ||
| if wb.Count() == batchSize { | ||
| if err := db.Write(writeOpts, wb); err != nil { | ||
| return errors.Wrap(err, "writing to RocksDB") | ||
| } | ||
| wb.Clear() | ||
| } | ||
| } | ||
| if wb.Count() != 0 { | ||
| if err := db.Write(writeOpts, wb); err != nil { | ||
| return errors.Wrap(err, "writing final batch to RocksDB") | ||
| } | ||
| } | ||
| return nil | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ClusterCVE