-
Notifications
You must be signed in to change notification settings - Fork 131
refactor(robot): unify config loading and permission building functions #531
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
Open
qcserestipy
wants to merge
15
commits into
goharbor:main
Choose a base branch
from
qcserestipy:robot_refactor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
6e083d3
refactor(robot): unify config loading and permission building functions
qcserestipy c85fe3e
Update pkg/views/base/multiselect/model.go
qcserestipy 63a3335
Update pkg/views/base/multiselect/model.go
qcserestipy f653dd0
Fix: revert some changes from copilot review
8c88555
refactor(robot): centralize prompts and simplify project/system permi…
qcserestipy b0e9633
fix(merge): merge upstream changes and resolve conflicts in prompt pa…
qcserestipy bb470d4
fix(dagger): update dagger go version to get rid of govulncheck go ve…
qcserestipy 8dc53ea
Merge branch 'main' into robot_refactor
bupd 1240e68
refactor(robot): create robot package and move shared functions; reus…
qcserestipy be5e5cd
fix(conflict): merge upstream and resolve conflicts
qcserestipy c6d52eb
Merge branch 'robot_refactor' of github.com:qcserestipy/harbor-cli in…
qcserestipy 6ee9ab6
fix(lint): remove trailing white space
qcserestipy 8750fd8
fix(lint): export docs
qcserestipy 02c6b61
refactor(robot): add RobotBuilder for permission handling; clean up p…
qcserestipy 60fcfa5
refactor(builder): slim down builder for merge; add funcs to update
qcserestipy 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
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
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,131 @@ | ||
| // Copyright Project Harbor Authors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| package robot | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/goharbor/go-client/pkg/sdk/v2.0/models" | ||
| config "github.com/goharbor/harbor-cli/pkg/config/robot" | ||
| rmodel "github.com/goharbor/harbor-cli/pkg/models/robot" | ||
| "github.com/goharbor/harbor-cli/pkg/views/robot/create" | ||
| "github.com/goharbor/harbor-cli/pkg/views/robot/update" | ||
| "github.com/sirupsen/logrus" | ||
| ) | ||
|
|
||
| func loadRobotConfigFromFile(configFile string, permissions *[]models.Permission, projectPermissionsMap map[string][]models.Permission, isUpdate bool, createOpts *create.CreateView, updateOpts *update.UpdateView) error { | ||
| fmt.Println("Loading configuration from: ", configFile) | ||
|
|
||
| loadedOpts, err := config.LoadRobotConfigFromFile(configFile) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to load robot config from file: %v", err) | ||
| } | ||
|
|
||
| logrus.Info("Successfully loaded robot configuration") | ||
|
|
||
| // Apply configuration based on operation type | ||
| if isUpdate && updateOpts != nil { | ||
| // Update mode: Only update specific fields selectively | ||
| if loadedOpts.Description != "" { | ||
| updateOpts.Description = loadedOpts.Description | ||
| } | ||
| if loadedOpts.Duration != 0 { | ||
| updateOpts.Duration = loadedOpts.Duration | ||
| } | ||
| } else if !isUpdate && createOpts != nil { | ||
| // Create mode: Full assignment | ||
| *createOpts = *loadedOpts | ||
| } | ||
|
|
||
| var systemPermFound bool | ||
| for _, perm := range loadedOpts.Permissions { | ||
| if perm.Kind == "system" && perm.Namespace == "/" { | ||
| systemPermFound = true | ||
|
|
||
| if isUpdate { | ||
| // Append for updates | ||
| for _, access := range perm.Access { | ||
| *permissions = append(*permissions, models.Permission{ | ||
| Resource: access.Resource, | ||
| Action: access.Action, | ||
| }) | ||
| } | ||
| } else { | ||
| // Replace for creates | ||
| *permissions = make([]models.Permission, len(perm.Access)) | ||
| for i, access := range perm.Access { | ||
| (*permissions)[i] = models.Permission{ | ||
| Resource: access.Resource, | ||
| Action: access.Action, | ||
| } | ||
| } | ||
| } | ||
| } else if perm.Kind == "project" { | ||
| var projectPerms []models.Permission | ||
| for _, access := range perm.Access { | ||
| projectPerms = append(projectPerms, models.Permission{ | ||
| Resource: access.Resource, | ||
| Action: access.Action, | ||
| }) | ||
| } | ||
| projectPermissionsMap[perm.Namespace] = projectPerms | ||
| } | ||
| } | ||
|
|
||
| if !systemPermFound { | ||
| return fmt.Errorf("robot configuration must include system-level permissions") | ||
| } | ||
|
|
||
| logrus.Infof("Loaded robot config with %d system permissions and %d project-specific permissions", | ||
| len(*permissions), len(projectPermissionsMap)) | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func loadFromConfigFileForCreate(opts *create.CreateView, configFile string, permissions *[]models.Permission, projectPermissionsMap map[string][]models.Permission) error { | ||
| return loadRobotConfigFromFile(configFile, permissions, projectPermissionsMap, false, opts, nil) | ||
| } | ||
|
|
||
| func loadFromConfigFileForUpdate(opts *update.UpdateView, configFile string, permissions *[]models.Permission, projectPermissionsMap map[string][]models.Permission) error { | ||
| return loadRobotConfigFromFile(configFile, permissions, projectPermissionsMap, true, nil, opts) | ||
| } | ||
|
|
||
| func buildMergedPermissions(projectPermissionsMap map[string][]models.Permission, accessesSystem []*models.Access) []*rmodel.RobotPermission { | ||
| var mergedPermissions []*rmodel.RobotPermission | ||
|
|
||
| // Add project permissions | ||
| for projectName, projectPermissions := range projectPermissionsMap { | ||
| var accessesProject []*models.Access | ||
| for _, perm := range projectPermissions { | ||
| accessesProject = append(accessesProject, &models.Access{ | ||
| Resource: perm.Resource, | ||
| Action: perm.Action, | ||
| }) | ||
| } | ||
| mergedPermissions = append(mergedPermissions, &rmodel.RobotPermission{ | ||
| Namespace: projectName, | ||
| Access: accessesProject, | ||
| Kind: "project", | ||
| }) | ||
| } | ||
|
|
||
| // Add system permissions | ||
| mergedPermissions = append(mergedPermissions, &rmodel.RobotPermission{ | ||
| Namespace: "/", | ||
| Access: accessesSystem, | ||
| Kind: "system", | ||
| }) | ||
|
|
||
| return mergedPermissions | ||
| } | ||
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
Oops, something went wrong.
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.
System permissions are always added regardless of whether
accessesSystemis empty. This could create empty system permission entries. Consider checking iflen(accessesSystem) > 0before adding system permissions, similar to how it was handled in the originalbuildMergedPermissionsForUpdatefunction.