Skip to content

Commit

Permalink
fix(registry): 仓库样式调整
Browse files Browse the repository at this point in the history
  • Loading branch information
ssongliu committed Jun 23, 2022
1 parent 753c660 commit 7977e54
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 26 deletions.
3 changes: 3 additions & 0 deletions migration/126_alter_registry_user.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALTER TABLE `ko`.`ko_system_registry` ADD COLUMN `nexus_user` VARCHAR(255) NULL AFTER `registry_hosted_port`;

UPDATE `ko`.`ko_system_registry` SET `nexus_user` = "admin";
34 changes: 33 additions & 1 deletion pkg/controller/system_setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,12 +298,44 @@ func (s SystemSettingController) PostRegistryCheckConn() error {
return err
}

if err := nexus.CheckConn(req.Username, req.Password, fmt.Sprintf("%s://%s:%d", req.Protocol, req.Hostname, req.RepoPort)); err != nil {
if err := nexus.CheckConn(req.NexusUser, req.NexusPassword, fmt.Sprintf("%s://%s:%d", req.Protocol, req.Hostname, req.RepoPort)); err != nil {
return err
}
return nil
}

// Change Nexus Password
// @Tags SystemSetting
// @Summary Change user password
// @Description 更新 Nexus 密码
// @Accept json
// @Produce json
// @Param request body dto.RepoChangePassword true "request"
// @Success 200
// @Security ApiKeyAuth
// @Router /settings/registry/change/password [post]
func (s *SystemSettingController) PostRegistryChangePassword() error {
var req dto.RepoChangePassword
err := s.Ctx.ReadJSON(&req)
if err != nil {
return err
}
validate := validator.New()
err = validate.Struct(req)
if err != nil {
return err
}
err = s.SystemSettingService.ChangePassword(req)
if err != nil {
return err
}

operator := s.Ctx.Values().GetString("operator")
go kolog.Save(operator, constant.UPDATE_NEXUS_PASSWORD, "-")

return err
}

// Delete Registry
// @Tags SystemSetting
// @Summary Delete a Registry
Expand Down
32 changes: 16 additions & 16 deletions pkg/dto/system_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,24 @@ type SystemRegistry struct {
}

type SystemRegistryCreate struct {
model.SystemRegistry
Hostname string `json:"hostname" validate:"required"`
Protocol string `json:"protocol" validate:"required"`
Architecture string `json:"architecture" validate:"required"`
RepoPort int `json:"repoPort" validate:"required"`
RegistryPort int `json:"registryPort" validate:"required"`
RegistryHostedPort int `json:"registryHostedPort" validate:"required"`
NexusUser string `json:"nexusUser" validate:"required"`
NexusPassword string `json:"nexusPassword" validate:"required"`
}

type SystemRegistryUpdate struct {
ID string `json:"id" validate:"required"`
Hostname string `json:"hostname" validate:"required"`
Protocol string `json:"protocol" validate:"required"`
RepoPort int `json:"repoPort" validate:"required"`
RegistryPort int `json:"registryPort" validate:"required"`
RegistryHostedPort int `json:"registryHostedPort" validate:"required"`
NexusPassword string `json:"nexusPassword" validate:"required"`
ID string `json:"id"`
Hostname string `json:"hostname"`
Protocol string `json:"protocol"`
Architecture string `json:"architecture"`
RepoPort int `json:"repoPort"`
RegistryPort int `json:"registryPort"`
RegistryHostedPort int `json:"registryHostedPort"`
}

type SystemRegistryDelete struct {
Expand All @@ -39,15 +39,15 @@ type SystemRegistryBatchOp struct {
}

type RepoChangePassword struct {
ID string `json:"id"`
Password string `json:"password"`
Original string `json:"original"`
ID string `json:"id"`
NexusUser string `json:"nexusUser"`
NexusPassword string `json:"nexusPassword"`
}

type SystemRegistryConn struct {
Hostname string `json:"hostname" validate:"required"`
Protocol string `json:"protocol" validate:"required"`
RepoPort int `json:"repoPort" validate:"required"`
Username string `json:"username" validate:"required"`
Password string `json:"password" validate:"required"`
Hostname string `json:"hostname" validate:"required"`
Protocol string `json:"protocol" validate:"required"`
RepoPort int `json:"repoPort" validate:"required"`
NexusUser string `json:"nexusUser"`
NexusPassword string `json:"nexusPassword"`
}
3 changes: 2 additions & 1 deletion pkg/model/system_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ type SystemRegistry struct {
RepoPort int `json:"repoPort" gorm:"type:int(64)"`
RegistryPort int `json:"registryPort" gorm:"type:int(64)"`
RegistryHostedPort int `json:"registryHostedPort" gorm:"type:int(64)"`
NexusPassword string `json:"nexusPassword" gorm:"type:varchar(256);not null;"`
NexusUser string `json:"nexusUser" gorm:"type:varchar(256);not null;"`
NexusPassword string `json:"-" gorm:"type:varchar(256);not null;"`
}

func (s *SystemRegistry) BeforeCreate() (err error) {
Expand Down
25 changes: 17 additions & 8 deletions pkg/service/system_setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type SystemSettingService interface {
UpdateRegistry(arch string, creation dto.SystemRegistryUpdate) (*dto.SystemRegistry, error)
BatchRegistry(op dto.SystemRegistryBatchOp) error
DeleteRegistry(id string) error
ChangePassword(repo dto.RepoChangePassword) error
}

type systemSettingService struct {
Expand Down Expand Up @@ -208,6 +209,7 @@ func (s systemSettingService) GetRegistryByID(id string) (dto.SystemRegistry, er
RepoPort: r.RepoPort,
RegistryPort: r.RegistryPort,
RegistryHostedPort: r.RegistryHostedPort,
NexusUser: r.NexusUser,
NexusPassword: pass,
},
}
Expand Down Expand Up @@ -263,7 +265,7 @@ func (s systemSettingService) PageRegistry(num, size int, conditions condition.C
return
}
if err := nexus.CheckConn(
"admin",
repo.NexusUser,
pass,
fmt.Sprintf("%s://%s:%d", repo.Protocol, repo.Hostname, repo.RepoPort),
); err != nil {
Expand All @@ -287,13 +289,13 @@ func (s systemSettingService) CreateRegistry(creation dto.SystemRegistryCreate)
return nil, err
}
systemRegistry := model.SystemRegistry{
ID: creation.ID,
Architecture: creation.Architecture,
Protocol: creation.Protocol,
Hostname: creation.Hostname,
RepoPort: creation.RepoPort,
RegistryPort: creation.RegistryPort,
RegistryHostedPort: creation.RegistryHostedPort,
NexusUser: creation.NexusUser,
NexusPassword: password,
}
if err := s.systemRegistryRepo.Save(&systemRegistry); err != nil {
Expand All @@ -303,10 +305,6 @@ func (s systemSettingService) CreateRegistry(creation dto.SystemRegistryCreate)
}

func (s systemSettingService) UpdateRegistry(arch string, creation dto.SystemRegistryUpdate) (*dto.SystemRegistry, error) {
password, err := encrypt.StringEncrypt(creation.NexusPassword)
if err != nil {
return nil, err
}
systemRegistry := model.SystemRegistry{
ID: creation.ID,
Architecture: arch,
Expand All @@ -315,9 +313,8 @@ func (s systemSettingService) UpdateRegistry(arch string, creation dto.SystemReg
RepoPort: creation.RepoPort,
RegistryPort: creation.RegistryPort,
RegistryHostedPort: creation.RegistryHostedPort,
NexusPassword: password,
}
if err := s.systemRegistryRepo.Save(&systemRegistry); err != nil {
if err := db.DB.Model(&model.SystemRegistry{}).Update(&systemRegistry).Error; err != nil {
return nil, err
}
return &dto.SystemRegistry{SystemRegistry: systemRegistry}, nil
Expand Down Expand Up @@ -346,3 +343,15 @@ func (s systemSettingService) DeleteRegistry(id string) error {
}
return nil
}

func (u *systemSettingService) ChangePassword(ch dto.RepoChangePassword) error {
password, err := encrypt.StringEncrypt(ch.NexusPassword)
if err != nil {
return err
}
if err := db.DB.Model(&model.SystemRegistry{}).Where("id = ?", ch.ID).
Update(map[string]interface{}{"nexus_password": password, "nexus_user": ch.NexusUser}).Error; err != nil {
return err
}
return nil
}

0 comments on commit 7977e54

Please sign in to comment.