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

update robot creator database scheme #20918

Merged
merged 1 commit into from
Sep 14, 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
7 changes: 5 additions & 2 deletions api/v2.0/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7846,9 +7846,12 @@ definitions:
type: array
items:
$ref: '#/definitions/RobotPermission'
creator:
creator_type:
type: string
description: The creator of the robot
description: The type of the robot creator, like local(harbor_user) or robot.
creator_ref:
type: integer
description: The reference of the robot creator, like the id of harbor user.
creation_time:
type: string
format: date-time
Expand Down
6 changes: 3 additions & 3 deletions make/migrations/postgresql/0150_2.12.0_schema.up.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Add new column creator for robot table to add a new column to record the creator of the robot
Add new column creator_ref and creator_type for robot table to record the creator information of the robot
*/
ALTER TABLE robot ADD COLUMN IF NOT EXISTS creator varchar(255);
UPDATE robot SET creator = 'unknown' WHERE creator IS NULL;
ALTER TABLE robot ADD COLUMN IF NOT EXISTS creator_ref integer default 0;
ALTER TABLE robot ADD COLUMN IF NOT EXISTS creator_type varchar(255);
3 changes: 2 additions & 1 deletion src/controller/robot/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ func (d *controller) Create(ctx context.Context, r *Robot) (int64, string, error
Duration: r.Duration,
Salt: salt,
Visible: r.Visible,
Creator: r.Creator,
CreatorRef: r.CreatorRef,
CreatorType: r.CreatorType,
}
robotID, err := d.robotMgr.Create(ctx, rCreate)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion src/controller/scan/base_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -864,7 +864,8 @@ func (bc *basicController) makeRobotAccount(ctx context.Context, projectID int64
Description: "for scan",
ProjectID: projectID,
Duration: -1,
Creator: "harbor-core-for-scan-all",
CreatorType: "local",
CreatorRef: int64(0),
},
Level: robot.LEVELPROJECT,
Permissions: []*robot.Permission{
Expand Down
6 changes: 4 additions & 2 deletions src/controller/scan/base_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,8 @@ func (suite *ControllerTestSuite) SetupSuite() {
Description: "for scan",
ProjectID: suite.artifact.ProjectID,
Duration: -1,
Creator: "harbor-core-for-scan-all",
CreatorType: "local",
CreatorRef: int64(0),
},
Level: robot.LEVELPROJECT,
Permissions: []*robot.Permission{
Expand Down Expand Up @@ -267,7 +268,8 @@ func (suite *ControllerTestSuite) SetupSuite() {
Description: "for scan",
ProjectID: suite.artifact.ProjectID,
Duration: -1,
Creator: "harbor-core-for-scan-all",
CreatorType: "local",
CreatorRef: int64(0),
},
Level: "project",
}, nil)
Expand Down
5 changes: 3 additions & 2 deletions src/pkg/robot/dao/dao_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ func (suite *DaoTestSuite) robots() {
Description: "test3 description",
ProjectID: 1,
Secret: suite.RandString(10),
Creator: "tester",
CreatorType: "local",
CreatorRef: int64(1),
})
suite.Nil(err)

Expand Down Expand Up @@ -121,7 +122,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)
suite.Equal("local", r.CreatorType)
}

func (suite *DaoTestSuite) TestCount() {
Expand Down
3 changes: 2 additions & 1 deletion src/pkg/robot/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ 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"`
CreatorRef int64 `orm:"column(creator_ref)" json:"creator_ref"`
CreatorType string `orm:"column(creator_type)" json:"creator_type"`
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
3 changes: 2 additions & 1 deletion src/server/v2.0/handler/model/robot.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
Level: r.Level,
Disable: r.Disabled,
Editable: r.Editable,
Creator: r.Creator,
CreatorType: r.CreatorType,
CreatorRef: r.CreatorRef,

Check warning on line 52 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-L52

Added lines #L51 - L52 were not covered by tests
CreationTime: strfmt.DateTime(r.CreationTime),
UpdateTime: strfmt.DateTime(r.UpdateTime),
Permissions: perms,
Expand Down
15 changes: 14 additions & 1 deletion src/server/v2.0/handler/robot.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
"github.com/go-openapi/strfmt"

"github.com/goharbor/harbor/src/common/rbac"
"github.com/goharbor/harbor/src/common/security/local"
robotSc "github.com/goharbor/harbor/src/common/security/robot"
"github.com/goharbor/harbor/src/common/utils"
"github.com/goharbor/harbor/src/controller/robot"
"github.com/goharbor/harbor/src/lib"
Expand Down Expand Up @@ -67,13 +69,24 @@
return rAPI.SendError(ctx, err)
}

var creatorRef int64
switch s := sc.(type) {
case *local.SecurityContext:
creatorRef = int64(s.User().UserID)
case *robotSc.SecurityContext:
creatorRef = s.User().ID
default:
return rAPI.SendError(ctx, errors.New(nil).WithMessage("invalid security context"))

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

View check run for this annotation

Codecov / codecov/patch

src/server/v2.0/handler/robot.go#L72-L79

Added lines #L72 - L79 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(),
CreatorRef: creatorRef,
CreatorType: sc.Name(),

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

View check run for this annotation

Codecov / codecov/patch

src/server/v2.0/handler/robot.go#L88-L89

Added lines #L88 - L89 were not covered by tests
},
Level: params.Robot.Level,
}
Expand Down
Loading