2017-04-13 12:54:58 +02:00
|
|
|
// Copyright (c) 2017 VMware, Inc. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// 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-04-11 10:43:13 +02:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-08-29 15:21:49 +02:00
|
|
|
"os"
|
2017-07-07 15:02:16 +02:00
|
|
|
"reflect"
|
2018-07-13 05:15:41 +02:00
|
|
|
"strconv"
|
2016-04-11 10:43:13 +02:00
|
|
|
|
2016-10-19 08:32:00 +02:00
|
|
|
"github.com/vmware/harbor/src/common/utils"
|
|
|
|
"github.com/vmware/harbor/src/common/utils/log"
|
2016-09-13 11:41:32 +02:00
|
|
|
|
|
|
|
"github.com/astaxie/beego"
|
|
|
|
_ "github.com/astaxie/beego/session/redis"
|
2016-04-11 10:43:13 +02:00
|
|
|
|
2016-11-16 13:31:04 +01:00
|
|
|
"github.com/vmware/harbor/src/common/dao"
|
|
|
|
"github.com/vmware/harbor/src/common/models"
|
2017-07-06 18:38:38 +02:00
|
|
|
"github.com/vmware/harbor/src/common/notifier"
|
|
|
|
"github.com/vmware/harbor/src/common/scheduler"
|
2017-11-29 08:14:13 +01:00
|
|
|
"github.com/vmware/harbor/src/replication/core"
|
2017-11-28 07:38:24 +01:00
|
|
|
_ "github.com/vmware/harbor/src/replication/event"
|
2016-10-19 08:32:00 +02:00
|
|
|
"github.com/vmware/harbor/src/ui/api"
|
|
|
|
_ "github.com/vmware/harbor/src/ui/auth/db"
|
|
|
|
_ "github.com/vmware/harbor/src/ui/auth/ldap"
|
2017-10-01 23:03:53 +02:00
|
|
|
_ "github.com/vmware/harbor/src/ui/auth/uaa"
|
2016-11-16 13:31:04 +01:00
|
|
|
"github.com/vmware/harbor/src/ui/config"
|
2017-04-26 09:28:13 +02:00
|
|
|
"github.com/vmware/harbor/src/ui/filter"
|
2017-05-02 13:14:47 +02:00
|
|
|
"github.com/vmware/harbor/src/ui/proxy"
|
2017-02-26 12:53:13 +01:00
|
|
|
"github.com/vmware/harbor/src/ui/service/token"
|
2016-04-11 10:43:13 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
adminUserID = 1
|
|
|
|
)
|
|
|
|
|
|
|
|
func updateInitPassword(userID int, password string) error {
|
|
|
|
queryUser := models.User{UserID: userID}
|
|
|
|
user, err := dao.GetUser(queryUser)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to get user, userID: %d %v", userID, err)
|
|
|
|
}
|
|
|
|
if user == nil {
|
2016-11-14 07:46:20 +01:00
|
|
|
return fmt.Errorf("user id: %d does not exist", userID)
|
2016-04-11 10:43:13 +02:00
|
|
|
}
|
|
|
|
if user.Salt == "" {
|
2016-09-13 11:41:32 +02:00
|
|
|
salt := utils.GenerateRandomString()
|
2016-04-11 10:43:13 +02:00
|
|
|
|
|
|
|
user.Salt = salt
|
|
|
|
user.Password = password
|
|
|
|
err = dao.ChangeUserPassword(*user)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to update user encrypted password, userID: %d, err: %v", userID, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("User id: %d updated its encypted password successfully.", userID)
|
|
|
|
} else {
|
|
|
|
log.Infof("User id: %d already has its encrypted password.", userID)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
beego.BConfig.WebConfig.Session.SessionOn = true
|
2016-07-27 14:12:53 +02:00
|
|
|
//TODO
|
|
|
|
redisURL := os.Getenv("_REDIS_URL")
|
|
|
|
if len(redisURL) > 0 {
|
|
|
|
beego.BConfig.WebConfig.Session.SessionProvider = "redis"
|
|
|
|
beego.BConfig.WebConfig.Session.SessionProviderConfig = redisURL
|
|
|
|
}
|
2016-04-25 08:31:05 +02:00
|
|
|
beego.AddTemplateExt("htm")
|
2016-09-13 11:41:32 +02:00
|
|
|
|
2016-12-30 11:04:01 +01:00
|
|
|
log.Info("initializing configurations...")
|
|
|
|
if err := config.Init(); err != nil {
|
|
|
|
log.Fatalf("failed to initialize configurations: %v", err)
|
|
|
|
}
|
|
|
|
log.Info("configurations initialization completed")
|
2017-02-26 12:53:13 +01:00
|
|
|
token.InitCreators()
|
2016-12-30 11:04:01 +01:00
|
|
|
database, err := config.Database()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to get database configuration: %v", err)
|
|
|
|
}
|
2018-07-13 11:32:17 +02:00
|
|
|
if err := dao.InitDatabase(database); err != nil {
|
2016-12-30 11:04:01 +01:00
|
|
|
log.Fatalf("failed to initialize database: %v", err)
|
|
|
|
}
|
2017-07-11 15:26:31 +02:00
|
|
|
if config.WithClair() {
|
2017-12-19 08:25:12 +01:00
|
|
|
clairDB, err := config.ClairDB()
|
2017-07-17 09:00:48 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to load clair database information: %v", err)
|
|
|
|
}
|
2017-12-19 08:25:12 +01:00
|
|
|
if err := dao.InitClairDB(clairDB); err != nil {
|
2017-07-11 15:26:31 +02:00
|
|
|
log.Fatalf("failed to initialize clair database: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2016-12-30 11:04:01 +01:00
|
|
|
|
|
|
|
password, err := config.InitialAdminPassword()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to get admin's initia password: %v", err)
|
|
|
|
}
|
|
|
|
if err := updateInitPassword(adminUserID, password); err != nil {
|
2016-04-11 10:43:13 +02:00
|
|
|
log.Error(err)
|
|
|
|
}
|
2017-04-26 09:28:13 +02:00
|
|
|
|
2017-07-06 18:38:38 +02:00
|
|
|
//Enable the policy scheduler here.
|
|
|
|
scheduler.DefaultScheduler.Start()
|
|
|
|
|
|
|
|
//Subscribe the policy change topic.
|
2018-01-29 07:08:35 +01:00
|
|
|
if err = notifier.Subscribe(notifier.ScanAllPolicyTopic, ¬ifier.ScanPolicyNotificationHandler{}); err != nil {
|
|
|
|
log.Errorf("failed to subscribe scan all policy change topic: %v", err)
|
|
|
|
}
|
2017-07-06 18:38:38 +02:00
|
|
|
|
|
|
|
//Get policy configuration.
|
|
|
|
scanAllPolicy := config.ScanAllPolicy()
|
|
|
|
if scanAllPolicy.Type == notifier.PolicyTypeDaily {
|
|
|
|
dailyTime := 0
|
|
|
|
if t, ok := scanAllPolicy.Parm["daily_time"]; ok {
|
2017-07-07 15:02:16 +02:00
|
|
|
if reflect.TypeOf(t).Kind() == reflect.Int {
|
|
|
|
dailyTime = t.(int)
|
|
|
|
}
|
2017-07-06 18:38:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//Send notification to handle first policy change.
|
2018-01-29 07:08:35 +01:00
|
|
|
if err = notifier.Publish(notifier.ScanAllPolicyTopic,
|
|
|
|
notifier.ScanPolicyNotification{Type: scanAllPolicy.Type, DailyTime: (int64)(dailyTime)}); err != nil {
|
|
|
|
log.Errorf("failed to publish scan all policy topic: %v", err)
|
|
|
|
}
|
2017-07-06 18:38:38 +02:00
|
|
|
}
|
|
|
|
|
2017-12-16 14:52:29 +01:00
|
|
|
if err := core.Init(); err != nil {
|
2017-12-05 07:13:01 +01:00
|
|
|
log.Errorf("failed to initialize the replication controller: %v", err)
|
2017-11-29 08:14:13 +01:00
|
|
|
}
|
|
|
|
|
2017-06-18 07:51:42 +02:00
|
|
|
filter.Init()
|
2017-04-26 09:28:13 +02:00
|
|
|
beego.InsertFilter("/*", beego.BeforeRouter, filter.SecurityFilter)
|
2018-03-23 11:16:08 +01:00
|
|
|
beego.InsertFilter("/*", beego.BeforeRouter, filter.ReadonlyFilter)
|
2018-07-14 09:49:38 +02:00
|
|
|
beego.InsertFilter("/api/*", beego.BeforeRouter, filter.MediaTypeFilter("application/json", "multipart/form-data", "application/octet-stream"))
|
2017-04-26 09:28:13 +02:00
|
|
|
|
2016-04-11 10:43:13 +02:00
|
|
|
initRouters()
|
2018-07-13 05:15:41 +02:00
|
|
|
|
|
|
|
syncRegistry := os.Getenv("SYNC_REGISTRY")
|
|
|
|
sync, err := strconv.ParseBool(syncRegistry)
|
2018-07-13 11:32:17 +02:00
|
|
|
if err != nil {
|
2018-07-13 05:15:41 +02:00
|
|
|
log.Errorf("Failed to parse SYNC_REGISTRY: %v", err)
|
|
|
|
//if err set it default to false
|
2018-07-13 11:32:17 +02:00
|
|
|
sync = false
|
2016-08-29 15:21:49 +02:00
|
|
|
}
|
2018-07-13 11:32:17 +02:00
|
|
|
if sync {
|
2018-07-13 05:15:41 +02:00
|
|
|
if err := api.SyncRegistry(config.GlobalProjectMgr); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
2018-07-13 11:32:17 +02:00
|
|
|
} else {
|
2018-07-13 05:15:41 +02:00
|
|
|
log.Infof("Because SYNC_REGISTRY set false , no need to sync registry \n")
|
|
|
|
}
|
|
|
|
|
2017-05-02 13:14:47 +02:00
|
|
|
log.Info("Init proxy")
|
|
|
|
proxy.Init()
|
|
|
|
//go proxy.StartProxy()
|
2016-04-11 10:43:13 +02:00
|
|
|
beego.Run()
|
|
|
|
}
|