Merge pull request #1121 from wknet123/dev-show-download-cert

Add download cert to UI.
This commit is contained in:
Daniel Jiang 2016-11-18 16:30:43 +08:00 committed by GitHub
commit 3a23c38aaf
7 changed files with 56 additions and 10 deletions

View File

@ -2,6 +2,7 @@ package api
import ( import (
"net/http" "net/http"
"os"
"path/filepath" "path/filepath"
"syscall" "syscall"
@ -18,6 +19,7 @@ type SystemInfoAPI struct {
} }
const harborStoragePath = "/harbor_storage" const harborStoragePath = "/harbor_storage"
const defaultRootCert = "/harbor_storage/ca-download-dir/ca.crt"
//SystemInfo models for system info. //SystemInfo models for system info.
type SystemInfo struct { type SystemInfo struct {
@ -66,3 +68,17 @@ func (sia *SystemInfoAPI) GetVolumeInfo() {
sia.Data["json"] = systemInfo sia.Data["json"] = systemInfo
sia.ServeJSON() sia.ServeJSON()
} }
//GetCert gets default self-signed certificate.
func (sia *SystemInfoAPI) GetCert() {
if sia.isAdmin {
if _, err := os.Stat(defaultRootCert); !os.IsNotExist(err) {
sia.Ctx.Output.Header("Content-Disposition", "attachment; filename=ca.crt")
http.ServeFile(sia.Ctx.ResponseWriter, sia.Ctx.Request, defaultRootCert)
} else {
log.Error("No certificate found.")
sia.CustomAbort(http.StatusNotFound, "No certificate found.")
}
}
sia.CustomAbort(http.StatusUnauthorized, "")
}

View File

@ -31,9 +31,10 @@ type langType struct {
} }
const ( const (
viewPath = "sections" viewPath = "sections"
prefixNg = "" prefixNg = ""
defaultLang = "en-US" defaultLang = "en-US"
defaultRootCert = "/harbor_storage/ca-download-dir/ca.crt"
) )
var supportLanguages map[string]langType var supportLanguages map[string]langType
@ -45,6 +46,8 @@ func (b *BaseController) Prepare() {
var lang string var lang string
var langHasChanged bool var langHasChanged bool
var showDownloadCert bool
langRequest := b.GetString("lang") langRequest := b.GetString("lang")
if langRequest != "" { if langRequest != "" {
lang = langRequest lang = langRequest
@ -120,6 +123,20 @@ func (b *BaseController) Prepare() {
b.SelfRegistration = config.SelfRegistration() b.SelfRegistration = config.SelfRegistration()
b.Data["SelfRegistration"] = config.SelfRegistration() b.Data["SelfRegistration"] = config.SelfRegistration()
sessionUserID := b.GetSession("userId")
if sessionUserID != nil {
isAdmin, err := dao.IsAdminRole(sessionUserID.(int))
if err != nil {
log.Errorf("Error occurred in IsAdminRole: %v", err)
}
if isAdmin {
if _, err := os.Stat(defaultRootCert); !os.IsNotExist(err) {
showDownloadCert = true
}
}
}
b.Data["ShowDownloadCert"] = showDownloadCert
} }
// Forward to setup layout and template for content for a page. // Forward to setup layout and template for content for a page.

View File

@ -86,6 +86,7 @@ func initRouters() {
beego.Router("/api/logs", &api.LogAPI{}) beego.Router("/api/logs", &api.LogAPI{})
beego.Router("/api/systeminfo/volumes", &api.SystemInfoAPI{}, "get:GetVolumeInfo") beego.Router("/api/systeminfo/volumes", &api.SystemInfoAPI{}, "get:GetVolumeInfo")
beego.Router("/api/systeminfo/getcert", &api.SystemInfoAPI{}, "get:GetCert")
//external service that hosted on harbor process: //external service that hosted on harbor process:
beego.Router("/service/notifications", &service.NotificationHandler{}) beego.Router("/service/notifications", &service.NotificationHandler{})
beego.Router("/service/token", &token.Handler{}) beego.Router("/service/token", &token.Handler{})

View File

@ -63,9 +63,11 @@
function about() { function about() {
$scope.$emit('modalTitle', $filter('tr')('about_harbor')); $scope.$emit('modalTitle', $filter('tr')('about_harbor'));
vm.modalMessage = $filter('tr')('current_version', [vm.version || 'Unknown']); vm.modalMessage = $filter('tr')('current_version', [vm.version || 'Unknown']);
if(vm.showDownloadCert === 'true') {
appendDownloadCertLink();
}
GetVolumeInfoService("data") GetVolumeInfoService("data")
.then(getVolumeInfoSuccess, getVolumeInfoFailed); .then(getVolumeInfoSuccess, getVolumeInfoFailed);
} }
function getVolumeInfoSuccess(response) { function getVolumeInfoSuccess(response) {
var storage = response.data; var storage = response.data;
@ -79,10 +81,15 @@
$scope.$emit('modalMessage', vm.modalMessage); $scope.$emit('modalMessage', vm.modalMessage);
$scope.$emit('raiseInfo', raiseInfo); $scope.$emit('raiseInfo', raiseInfo);
} }
function toGigaBytes(val) { function toGigaBytes(val) {
return Math.round(val / (1024 * 1024 * 1024)); return Math.round(val / (1024 * 1024 * 1024));
} }
function appendDownloadCertLink() {
vm.modalMessage += '<br/>' + $filter('tr')('default_root_cert', ['/api/systeminfo/getcert', $filter('tr')('download')]);
}
} }
function optionalMenu() { function optionalMenu() {
@ -91,7 +98,8 @@
'templateUrl': '/optional_menu?timestamp=' + new Date().getTime(), 'templateUrl': '/optional_menu?timestamp=' + new Date().getTime(),
'scope': { 'scope': {
'version': '@', 'version': '@',
'language': '@' 'language': '@',
'showDownloadCert': '@'
}, },
'controller': OptionalMenuController, 'controller': OptionalMenuController,
'controllerAs': 'vm', 'controllerAs': 'vm',

View File

@ -233,6 +233,8 @@ var locale_messages = {
'about_harbor': 'About Harbor', 'about_harbor': 'About Harbor',
'current_version': '<label>Version</label>&nbsp;&nbsp;<span>$0</span>', 'current_version': '<label>Version</label>&nbsp;&nbsp;<span>$0</span>',
'current_storage': '<label>Storage</label>&nbsp;&nbsp;<span>$0 GB available of $1 GB.</span>', 'current_storage': '<label>Storage</label>&nbsp;&nbsp;<span>$0 GB available of $1 GB.</span>',
'default_root_cert': '<label>Default Root Cert.</label>&nbsp;&nbsp;<span><a href="$0">$1</a></span>',
'download': 'Download',
'failed_to_get_project_member': 'Failed to get current project member.', 'failed_to_get_project_member': 'Failed to get current project member.',
'failed_to_delete_repo': 'Failed to delete repository. ', 'failed_to_delete_repo': 'Failed to delete repository. ',
'failed_to_delete_repo_insuffient_permissions': 'Failed to delete repository, insuffient permissions.', 'failed_to_delete_repo_insuffient_permissions': 'Failed to delete repository, insuffient permissions.',

View File

@ -233,6 +233,8 @@ var locale_messages = {
'about_harbor': '关于 Harbor', 'about_harbor': '关于 Harbor',
'current_version': '<label>当前版本</label>&nbsp;&nbsp;<span>$0</span>', 'current_version': '<label>当前版本</label>&nbsp;&nbsp;<span>$0</span>',
'current_storage': '<label>存储情况</label>&nbsp;&nbsp;<span>可用: $0 GB总共 $1 GB。</span>', 'current_storage': '<label>存储情况</label>&nbsp;&nbsp;<span>可用: $0 GB总共 $1 GB。</span>',
'default_root_cert': '<label>默认根证书</label>&nbsp;&nbsp;<span><a href="$0">$1</a></span>',
'download': '下载',
'failed_to_get_project_member': '无法获取当前项目成员。', 'failed_to_get_project_member': '无法获取当前项目成员。',
'failed_to_delete_repo': '无法删除镜像仓库。', 'failed_to_delete_repo': '无法删除镜像仓库。',
'failed_to_delete_repo_insuffient_permissions': '无法删除镜像仓库,权限不足。', 'failed_to_delete_repo_insuffient_permissions': '无法删除镜像仓库,权限不足。',

View File

@ -25,7 +25,7 @@
</div> </div>
<!-- Collect the nav links, forms, and other content for toggling --> <!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-harbor-navbar-collapse-1"> <div class="collapse navbar-collapse" id="bs-harbor-navbar-collapse-1">
<optional-menu version="{{.Version}}" language="{{.Lang}}" login-status="//vm.loginStatus//"></optional-menu> <optional-menu version="{{.Version}}" language="{{.Lang}}" show-download-cert="{{.ShowDownloadCert}}" login-status="//vm.loginStatus//"></optional-menu>
<ul class="nav navbar-nav navbar-right"> <ul class="nav navbar-nav navbar-right">
<li> <li>
<navigation-header></navigation-header> <navigation-header></navigation-header>