diff --git a/api/v2.0/swagger.yaml b/api/v2.0/swagger.yaml index da1e9b25f4b..c995f705e8d 100644 --- a/api/v2.0/swagger.yaml +++ b/api/v2.0/swagger.yaml @@ -7846,7 +7846,7 @@ definitions: type: array items: $ref: '#/definitions/RobotPermission' - Creator: + creator: type: string description: The creator of the robot creation_time: diff --git a/make/migrations/postgresql/0150_2.12.0_schema.up.sql b/make/migrations/postgresql/0150_2.12.0_schema.up.sql index 82c36bc9cbc..82f1061722d 100644 --- a/make/migrations/postgresql/0150_2.12.0_schema.up.sql +++ b/make/migrations/postgresql/0150_2.12.0_schema.up.sql @@ -1,5 +1,5 @@ /* -Add new column robot for artifact table to add a new column to record the creator of the robot +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; diff --git a/src/controller/robot/controller.go b/src/controller/robot/controller.go index b2dc81dbbfa..cd6469c31e5 100644 --- a/src/controller/robot/controller.go +++ b/src/controller/robot/controller.go @@ -121,6 +121,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) } + robotID, err := d.robotMgr.Create(ctx, &model.Robot{ Name: name, Description: r.Description, diff --git a/src/controller/robot/controller_test.go b/src/controller/robot/controller_test.go index 529c4b4bb46..50a02daa839 100644 --- a/src/controller/robot/controller_test.go +++ b/src/controller/robot/controller_test.go @@ -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" @@ -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" @@ -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) @@ -113,7 +117,6 @@ func (suite *ControllerTestSuite) TestCreate() { Name: "testcreate", Description: "testcreate", Duration: 0, - Creator: "tester", }, ProjectName: "library", Level: LEVELPROJECT, diff --git a/src/controller/scan/base_controller.go b/src/controller/scan/base_controller.go index f6a0427b4d6..fe4a15faf8f 100644 --- a/src/controller/scan/base_controller.go +++ b/src/controller/scan/base_controller.go @@ -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{ diff --git a/src/server/v2.0/handler/robot.go b/src/server/v2.0/handler/robot.go index 6e910cdc1dd..316db7cd5f0 100644 --- a/src/server/v2.0/handler/robot.go +++ b/src/server/v2.0/handler/robot.go @@ -62,7 +62,7 @@ func (rAPI *robotAPI) CreateRobot(ctx context.Context, params operation.CreateRo return rAPI.SendError(ctx, err) } - secCtx, err := rAPI.GetSecurityContext(ctx) + sc, err := rAPI.GetSecurityContext(ctx) if err != nil { return rAPI.SendError(ctx, err) } @@ -73,7 +73,7 @@ func (rAPI *robotAPI) CreateRobot(ctx context.Context, params operation.CreateRo Description: params.Robot.Description, Duration: params.Robot.Duration, Visible: true, - Creator: secCtx.GetUsername(), + Creator: sc.GetUsername(), }, Level: params.Robot.Level, }