harbor/dao/accesslog.go

247 lines
6.2 KiB
Go
Raw Normal View History

2016-02-01 12:59:10 +01:00
/*
Copyright (c) 2016 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-02-26 11:54:14 +01:00
2016-02-01 12:59:10 +01:00
package dao
import (
"strings"
2016-09-13 11:41:32 +02:00
"time"
2016-06-06 11:53:36 +02:00
2016-02-01 12:59:10 +01:00
"github.com/vmware/harbor/models"
2016-04-21 18:28:59 +02:00
"github.com/vmware/harbor/utils/log"
2016-02-01 12:59:10 +01:00
)
// AddAccessLog persists the access logs
2016-02-01 12:59:10 +01:00
func AddAccessLog(accessLog models.AccessLog) error {
2016-05-20 10:36:10 +02:00
o := GetOrmer()
2016-02-01 12:59:10 +01:00
p, err := o.Raw(`insert into access_log
2016-04-21 18:28:59 +02:00
(user_id, project_id, repo_name, repo_tag, guid, operation, op_time)
2016-09-13 11:41:32 +02:00
values (?, ?, ?, ?, ?, ?, ?)`).Prepare()
2016-02-01 12:59:10 +01:00
if err != nil {
return err
}
defer p.Close()
2016-09-13 11:41:32 +02:00
_, err = p.Exec(accessLog.UserID, accessLog.ProjectID, accessLog.RepoName, accessLog.RepoTag,
accessLog.GUID, accessLog.Operation, time.Now())
2016-02-01 12:59:10 +01:00
return err
}
// GetTotalOfAccessLogs ...
func GetTotalOfAccessLogs(query models.AccessLog) (int64, error) {
2016-05-20 10:36:10 +02:00
o := GetOrmer()
queryParam := []interface{}{}
sql := `select count(*) from access_log al
where al.project_id = ?`
queryParam = append(queryParam, query.ProjectID)
2016-02-01 12:59:10 +01:00
if query.Username != "" {
sql = `select count(*) from access_log al
left join user u
on al.user_id = u.user_id
where al.project_id = ? and u.username like ? `
queryParam = append(queryParam, "%"+query.Username+"%")
2016-02-01 12:59:10 +01:00
}
sql += genFilterClauses(query, &queryParam)
var total int64
if err := o.Raw(sql, queryParam).QueryRow(&total); err != nil {
return 0, err
2016-02-01 12:59:10 +01:00
}
return total, nil
}
//GetAccessLogs gets access logs according to different conditions
func GetAccessLogs(query models.AccessLog, limit, offset int64) ([]models.AccessLog, error) {
o := GetOrmer()
queryParam := []interface{}{}
sql := `select al.log_id, u.username, al.repo_name,
al.repo_tag, al.operation, al.op_time
from access_log al
left join user u
on al.user_id = u.user_id
where al.project_id = ? `
queryParam = append(queryParam, query.ProjectID)
if query.Username != "" {
sql += ` and u.username like ? `
queryParam = append(queryParam, "%"+query.Username+"%")
}
sql += genFilterClauses(query, &queryParam)
sql += ` order by al.op_time desc `
sql = paginateForRawSQL(sql, limit, offset)
logs := []models.AccessLog{}
_, err := o.Raw(sql, queryParam).QueryRows(&logs)
if err != nil {
return logs, err
}
return logs, nil
}
func genFilterClauses(query models.AccessLog, queryParam *[]interface{}) string {
sql := ""
if query.Operation != "" {
sql += ` and al.operation = ? `
*queryParam = append(*queryParam, query.Operation)
2016-02-01 12:59:10 +01:00
}
if query.RepoName != "" {
sql += ` and al.repo_name = ? `
*queryParam = append(*queryParam, query.RepoName)
2016-05-03 10:54:01 +02:00
}
if query.RepoTag != "" {
sql += ` and al.repo_tag = ? `
*queryParam = append(*queryParam, query.RepoTag)
2016-05-03 10:54:01 +02:00
}
if query.Keywords != "" {
sql += ` and al.operation in ( `
keywordList := strings.Split(query.Keywords, "/")
2016-02-01 12:59:10 +01:00
num := len(keywordList)
for i := 0; i < num; i++ {
if keywordList[i] != "" {
if i == num-1 {
sql += `?)`
2016-02-01 12:59:10 +01:00
} else {
sql += `?,`
2016-02-01 12:59:10 +01:00
}
*queryParam = append(*queryParam, keywordList[i])
2016-02-01 12:59:10 +01:00
}
}
}
if query.BeginTimestamp > 0 {
sql += ` and al.op_time >= ? `
*queryParam = append(*queryParam, query.BeginTime)
2016-02-01 12:59:10 +01:00
}
if query.EndTimestamp > 0 {
sql += ` and al.op_time <= ? `
*queryParam = append(*queryParam, query.EndTime)
2016-02-01 12:59:10 +01:00
}
return sql
2016-02-01 12:59:10 +01:00
}
// AccessLog ...
2016-04-21 18:28:59 +02:00
func AccessLog(username, projectName, repoName, repoTag, action string) error {
2016-05-20 10:36:10 +02:00
o := GetOrmer()
2016-04-21 18:28:59 +02:00
sql := "insert into access_log (user_id, project_id, repo_name, repo_tag, operation, op_time) " +
2016-02-01 12:59:10 +01:00
"select (select user_id as user_id from user where username=?), " +
2016-09-13 11:41:32 +02:00
"(select project_id as project_id from project where name=?), ?, ?, ?, ? "
_, err := o.Raw(sql, username, projectName, repoName, repoTag, action, time.Now()).Exec()
2016-02-01 12:59:10 +01:00
2016-04-21 18:28:59 +02:00
if err != nil {
log.Errorf("error in AccessLog: %v ", err)
}
2016-02-01 12:59:10 +01:00
return err
}
//GetRecentLogs returns recent logs according to parameters
func GetRecentLogs(userID, linesNum int, startTime, endTime string) ([]models.AccessLog, error) {
2016-09-20 05:25:01 +02:00
logs := []models.AccessLog{}
isAdmin, err := IsAdminRole(userID)
if err != nil {
return logs, err
}
queryParam := []interface{}{}
sql := `select log_id, access_log.user_id, project_id, repo_name, repo_tag, GUID, operation, op_time, username
from access_log
join user
on access_log.user_id=user.user_id `
hasWhere := false
if !isAdmin {
sql += ` where project_id in
(select distinct project_id
from project_member
where user_id = ?) `
queryParam = append(queryParam, userID)
hasWhere = true
}
2016-05-31 11:38:51 +02:00
if startTime != "" {
2016-09-20 05:25:01 +02:00
if hasWhere {
sql += " and op_time >= ?"
} else {
sql += " where op_time >= ?"
hasWhere = true
}
queryParam = append(queryParam, startTime)
}
2016-05-31 11:38:51 +02:00
if endTime != "" {
2016-09-20 05:25:01 +02:00
if hasWhere {
sql += " and op_time <= ?"
} else {
sql += " where op_time <= ?"
hasWhere = true
}
queryParam = append(queryParam, endTime)
}
sql += " order by op_time desc"
2016-05-31 11:38:51 +02:00
if linesNum != 0 {
sql += " limit ?"
queryParam = append(queryParam, linesNum)
}
2016-09-20 05:25:01 +02:00
_, err = GetOrmer().Raw(sql, queryParam).QueryRows(&logs)
if err != nil {
2016-09-20 05:25:01 +02:00
return logs, err
}
2016-09-20 05:25:01 +02:00
return logs, nil
}
2016-06-01 11:01:43 +02:00
// GetAccessLogCreator ...
func GetAccessLogCreator(repoName string) (string, error) {
o := GetOrmer()
sql := "select * from user where user_id = (select user_id from access_log where operation = 'push' and repo_name = ? order by op_time desc limit 1)"
var u []models.User
n, err := o.Raw(sql, repoName).QueryRows(&u)
if err != nil {
return "", err
}
if n == 0 {
return "", nil
}
return u[0].Username, nil
}
// CountPull ...
func CountPull(repoName string) (int64, error) {
o := GetOrmer()
num, err := o.QueryTable("access_log").Filter("repo_name", repoName).Filter("operation", "pull").Count()
if err != nil {
log.Errorf("error in CountPull: %v ", err)
return 0, err
}
return num, nil
}