2018-09-19 18:57:15 +02:00
|
|
|
// Copyright Project Harbor Authors
|
2017-04-13 12:54:58 +02:00
|
|
|
//
|
|
|
|
// 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.
|
2016-06-16 08:57:45 +02:00
|
|
|
|
2016-05-10 13:38:50 +02:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
2016-06-06 11:31:47 +02:00
|
|
|
|
|
|
|
"github.com/astaxie/beego/validation"
|
2018-08-23 09:02:20 +02:00
|
|
|
"github.com/goharbor/harbor/src/common/utils"
|
2016-05-10 13:38:50 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2018-09-05 10:16:31 +02:00
|
|
|
// RepOpTransfer represents the operation of a job to transfer repository to a remote registry/harbor instance.
|
2016-05-10 13:38:50 +02:00
|
|
|
RepOpTransfer string = "transfer"
|
2018-09-05 10:16:31 +02:00
|
|
|
// RepOpDelete represents the operation of a job to remove repository from a remote registry/harbor instance.
|
2016-05-30 08:44:03 +02:00
|
|
|
RepOpDelete string = "delete"
|
2018-09-05 10:16:31 +02:00
|
|
|
// RepOpSchedule represents the operation of a job to schedule the real replication process
|
2018-03-29 16:42:34 +02:00
|
|
|
RepOpSchedule string = "schedule"
|
2018-09-05 10:16:31 +02:00
|
|
|
// RepTargetTable is the table name for replication targets
|
2017-05-23 07:33:20 +02:00
|
|
|
RepTargetTable = "replication_target"
|
2018-09-05 10:16:31 +02:00
|
|
|
// RepJobTable is the table name for replication jobs
|
2017-05-23 07:33:20 +02:00
|
|
|
RepJobTable = "replication_job"
|
2018-09-05 10:16:31 +02:00
|
|
|
// RepPolicyTable is table name for replication policies
|
2017-05-23 07:33:20 +02:00
|
|
|
RepPolicyTable = "replication_policy"
|
2016-05-10 13:38:50 +02:00
|
|
|
)
|
|
|
|
|
2016-05-30 08:44:03 +02:00
|
|
|
// RepPolicy is the model for a replication policy, which associate to a project and a target (destination)
|
2016-05-10 13:38:50 +02:00
|
|
|
type RepPolicy struct {
|
2017-11-15 07:41:26 +01:00
|
|
|
ID int64 `orm:"pk;auto;column(id)"`
|
|
|
|
ProjectID int64 `orm:"column(project_id)" `
|
|
|
|
TargetID int64 `orm:"column(target_id)"`
|
|
|
|
Name string `orm:"column(name)"`
|
|
|
|
Description string `orm:"column(description)"`
|
|
|
|
Trigger string `orm:"column(cron_str)"`
|
|
|
|
Filters string `orm:"column(filters)"`
|
|
|
|
ReplicateDeletion bool `orm:"column(replicate_deletion)"`
|
|
|
|
CreationTime time.Time `orm:"column(creation_time);auto_now_add"`
|
|
|
|
UpdateTime time.Time `orm:"column(update_time);auto_now"`
|
2018-04-27 11:27:12 +02:00
|
|
|
Deleted bool `orm:"column(deleted)"`
|
2017-11-08 10:53:06 +01:00
|
|
|
}
|
|
|
|
|
2016-05-30 08:44:03 +02:00
|
|
|
// RepJob is the model for a replication job, which is the execution unit on job service, currently it is used to transfer/remove
|
|
|
|
// a repository to/from a remote registry instance.
|
2016-05-10 13:38:50 +02:00
|
|
|
type RepJob struct {
|
2018-10-21 15:05:34 +02:00
|
|
|
ID int64 `orm:"pk;auto;column(id)" json:"id"`
|
|
|
|
Status string `orm:"column(status)" json:"status"`
|
|
|
|
Repository string `orm:"column(repository)" json:"repository"`
|
|
|
|
PolicyID int64 `orm:"column(policy_id)" json:"policy_id"`
|
|
|
|
OpUUID string `orm:"column(op_uuid)" json:"op_uuid"`
|
|
|
|
Operation string `orm:"column(operation)" json:"operation"`
|
|
|
|
Tags string `orm:"column(tags)" json:"-"`
|
|
|
|
TagList []string `orm:"-" json:"tags"`
|
|
|
|
UUID string `orm:"column(job_uuid)" json:"-"`
|
2016-05-10 13:38:50 +02:00
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
2016-05-30 08:44:03 +02:00
|
|
|
// RepTarget is the model for a replication targe, i.e. destination, which wraps the endpoint URL and username/password of a remote registry.
|
2016-05-10 13:38:50 +02:00
|
|
|
type RepTarget struct {
|
2017-04-27 12:19:28 +02:00
|
|
|
ID int64 `orm:"pk;auto;column(id)" json:"id"`
|
2016-06-03 10:16:31 +02:00
|
|
|
URL string `orm:"column(url)" json:"endpoint"`
|
2016-05-10 13:38:50 +02:00
|
|
|
Name string `orm:"column(name)" json:"name"`
|
|
|
|
Username string `orm:"column(username)" json:"username"`
|
|
|
|
Password string `orm:"column(password)" json:"password"`
|
2016-06-17 12:54:29 +02:00
|
|
|
Type int `orm:"column(target_type)" json:"type"`
|
2017-09-22 09:08:10 +02:00
|
|
|
Insecure bool `orm:"column(insecure)" json:"insecure"`
|
2016-05-10 13:38:50 +02:00
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
2016-06-06 11:31:47 +02:00
|
|
|
// Valid ...
|
|
|
|
func (r *RepTarget) Valid(v *validation.Validation) {
|
|
|
|
if len(r.Name) == 0 {
|
|
|
|
v.SetError("name", "can not be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(r.Name) > 64 {
|
|
|
|
v.SetError("name", "max length is 64")
|
|
|
|
}
|
|
|
|
|
2017-12-27 07:33:31 +01:00
|
|
|
url, err := utils.ParseEndpoint(r.URL)
|
|
|
|
if err != nil {
|
|
|
|
v.SetError("endpoint", err.Error())
|
|
|
|
} else {
|
|
|
|
// Prevent SSRF security issue #3755
|
|
|
|
r.URL = url.Scheme + "://" + url.Host + url.Path
|
|
|
|
if len(r.URL) > 64 {
|
|
|
|
v.SetError("endpoint", "max length is 64")
|
|
|
|
}
|
2016-06-06 11:31:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// password is encoded using base64, the length of this field
|
|
|
|
// in DB is 64, so the max length in request is 48
|
|
|
|
if len(r.Password) > 48 {
|
|
|
|
v.SetError("password", "max length is 48")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-05 10:16:31 +02:00
|
|
|
// TableName is required by by beego orm to map RepTarget to table replication_target
|
2016-06-06 11:36:48 +02:00
|
|
|
func (r *RepTarget) TableName() string {
|
2017-05-23 07:33:20 +02:00
|
|
|
return RepTargetTable
|
2016-05-10 13:38:50 +02:00
|
|
|
}
|
|
|
|
|
2018-09-05 10:16:31 +02:00
|
|
|
// TableName is required by by beego orm to map RepJob to table replication_job
|
2016-06-06 11:36:48 +02:00
|
|
|
func (r *RepJob) TableName() string {
|
2017-05-23 07:33:20 +02:00
|
|
|
return RepJobTable
|
2016-05-10 13:38:50 +02:00
|
|
|
}
|
|
|
|
|
2018-09-05 10:16:31 +02:00
|
|
|
// TableName is required by by beego orm to map RepPolicy to table replication_policy
|
2016-06-06 11:36:48 +02:00
|
|
|
func (r *RepPolicy) TableName() string {
|
2017-05-23 07:33:20 +02:00
|
|
|
return RepPolicyTable
|
2016-05-10 13:38:50 +02:00
|
|
|
}
|
2018-04-02 19:11:55 +02:00
|
|
|
|
|
|
|
// RepJobQuery holds query conditions for replication job
|
|
|
|
type RepJobQuery struct {
|
|
|
|
PolicyID int64
|
2018-10-21 15:05:34 +02:00
|
|
|
OpUUID string
|
2018-04-02 19:11:55 +02:00
|
|
|
Repository string
|
|
|
|
Statuses []string
|
|
|
|
Operations []string
|
|
|
|
StartTime *time.Time
|
|
|
|
EndTime *time.Time
|
|
|
|
Pagination
|
|
|
|
}
|