Skip to content
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

add creator for robot #20846

Merged
merged 4 commits into from
Aug 15, 2024
Merged
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
3 changes: 3 additions & 0 deletions api/v2.0/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7846,6 +7846,9 @@ definitions:
type: array
items:
$ref: '#/definitions/RobotPermission'
creator:
type: string
description: The creator of the robot
creation_time:
type: string
format: date-time
Expand Down
5 changes: 5 additions & 0 deletions make/migrations/postgresql/0150_2.12.0_schema.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/*
Add new column creator for robot table to add a new column to record the creator of the robot
*/
ALTER TABLE robot ADD COLUMN IF NOT EXISTS creator varchar(255);
UPDATE robot SET creator = 'unknown' WHERE creator IS NULL;
2 changes: 2 additions & 0 deletions src/controller/robot/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ func (d *controller) Create(ctx context.Context, r *Robot) (int64, string, error
if r.Level == LEVELPROJECT {
name = fmt.Sprintf("%s+%s", r.ProjectName, r.Name)
}

rCreate := &model.Robot{
Name: name,
Description: r.Description,
Expand All @@ -132,6 +133,7 @@ func (d *controller) Create(ctx context.Context, r *Robot) (int64, string, error
Duration: r.Duration,
Salt: salt,
Visible: r.Visible,
Creator: r.Creator,
}
robotID, err := d.robotMgr.Create(ctx, rCreate)
if err != nil {
Expand Down
6 changes: 5 additions & 1 deletion src/controller/robot/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/stretchr/testify/suite"

"github.com/goharbor/harbor/src/common"
"github.com/goharbor/harbor/src/common/security"
"github.com/goharbor/harbor/src/common/utils/test"
"github.com/goharbor/harbor/src/lib/config"
"github.com/goharbor/harbor/src/lib/q"
Expand All @@ -18,6 +19,7 @@ import (
rbac_model "github.com/goharbor/harbor/src/pkg/rbac/model"
"github.com/goharbor/harbor/src/pkg/robot/model"
htesting "github.com/goharbor/harbor/src/testing"
testsec "github.com/goharbor/harbor/src/testing/common/security"
"github.com/goharbor/harbor/src/testing/mock"
"github.com/goharbor/harbor/src/testing/pkg/project"
"github.com/goharbor/harbor/src/testing/pkg/rbac"
Expand Down Expand Up @@ -102,7 +104,9 @@ func (suite *ControllerTestSuite) TestCreate() {
robotMgr := &robot.Manager{}

c := controller{robotMgr: robotMgr, rbacMgr: rbacMgr, proMgr: projectMgr}
ctx := context.TODO()
secCtx := &testsec.Context{}
secCtx.On("GetUsername").Return("security-context-user")
ctx := security.NewContext(context.Background(), secCtx)
projectMgr.On("Get", mock.Anything, mock.Anything).Return(&proModels.Project{ProjectID: 1, Name: "library"}, nil)
robotMgr.On("Create", mock.Anything, mock.Anything).Return(int64(1), nil)
rbacMgr.On("CreateRbacPolicy", mock.Anything, mock.Anything, mock.Anything).Return(int64(1), nil)
Expand Down
1 change: 1 addition & 0 deletions src/controller/scan/base_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,7 @@ func (bc *basicController) makeRobotAccount(ctx context.Context, projectID int64
Description: "for scan",
ProjectID: projectID,
Duration: -1,
Creator: "harbor-core-for-scan-all",
},
Level: robot.LEVELPROJECT,
Permissions: []*robot.Permission{
Expand Down
2 changes: 2 additions & 0 deletions src/controller/scan/base_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ func (suite *ControllerTestSuite) SetupSuite() {
Description: "for scan",
ProjectID: suite.artifact.ProjectID,
Duration: -1,
Creator: "harbor-core-for-scan-all",
},
Level: robot.LEVELPROJECT,
Permissions: []*robot.Permission{
Expand Down Expand Up @@ -266,6 +267,7 @@ func (suite *ControllerTestSuite) SetupSuite() {
Description: "for scan",
ProjectID: suite.artifact.ProjectID,
Duration: -1,
Creator: "harbor-core-for-scan-all",
},
Level: "project",
}, nil)
Expand Down
2 changes: 2 additions & 0 deletions src/pkg/robot/dao/dao_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func (suite *DaoTestSuite) robots() {
Description: "test3 description",
ProjectID: 1,
Secret: suite.RandString(10),
Creator: "tester",
})
suite.Nil(err)

Expand Down Expand Up @@ -120,6 +121,7 @@ func (suite *DaoTestSuite) TestGet() {
r, err := suite.dao.Get(orm.Context(), suite.robotID3)
suite.Nil(err)
suite.Equal("test3", r.Name)
suite.Equal("tester", r.Creator)
}

func (suite *DaoTestSuite) TestCount() {
Expand Down
1 change: 1 addition & 0 deletions src/pkg/robot/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type Robot struct {
ExpiresAt int64 `orm:"column(expiresat)" json:"expires_at"`
Disabled bool `orm:"column(disabled)" json:"disabled"`
Visible bool `orm:"column(visible)" json:"-"`
Creator string `orm:"column(creator)" json:"creator"`
CreationTime time.Time `orm:"column(creation_time);auto_now_add" json:"creation_time"`
UpdateTime time.Time `orm:"column(update_time);auto_now" json:"update_time"`
}
Expand Down
1 change: 1 addition & 0 deletions src/server/v2.0/handler/model/robot.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
Level: r.Level,
Disable: r.Disabled,
Editable: r.Editable,
Creator: r.Creator,

Check warning on line 51 in src/server/v2.0/handler/model/robot.go

View check run for this annotation

Codecov / codecov/patch

src/server/v2.0/handler/model/robot.go#L51

Added line #L51 was not covered by tests
CreationTime: strfmt.DateTime(r.CreationTime),
UpdateTime: strfmt.DateTime(r.UpdateTime),
Permissions: perms,
Expand Down
6 changes: 6 additions & 0 deletions src/server/v2.0/handler/robot.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,18 @@
return rAPI.SendError(ctx, err)
}

sc, err := rAPI.GetSecurityContext(ctx)
if err != nil {
return rAPI.SendError(ctx, err)
}

Check warning on line 68 in src/server/v2.0/handler/robot.go

View check run for this annotation

Codecov / codecov/patch

src/server/v2.0/handler/robot.go#L65-L68

Added lines #L65 - L68 were not covered by tests

r := &robot.Robot{
Robot: pkg.Robot{
Name: params.Robot.Name,
Description: params.Robot.Description,
Duration: params.Robot.Duration,
Visible: true,
Creator: sc.GetUsername(),

Check warning on line 76 in src/server/v2.0/handler/robot.go

View check run for this annotation

Codecov / codecov/patch

src/server/v2.0/handler/robot.go#L76

Added line #L76 was not covered by tests
},
Level: params.Robot.Level,
}
Expand Down
Loading