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 service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2016-03-01 11:21:56 +01:00
|
|
|
"regexp"
|
2016-02-01 12:59:10 +01:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/vmware/harbor/dao"
|
|
|
|
"github.com/vmware/harbor/models"
|
|
|
|
svc_utils "github.com/vmware/harbor/service/utils"
|
|
|
|
|
|
|
|
"github.com/astaxie/beego"
|
|
|
|
)
|
|
|
|
|
2016-02-26 11:35:55 +01:00
|
|
|
// NotificationHandler handles request on /service/notifications/, which listens to registry's events.
|
2016-02-01 12:59:10 +01:00
|
|
|
type NotificationHandler struct {
|
|
|
|
beego.Controller
|
|
|
|
}
|
|
|
|
|
2016-03-01 11:21:56 +01:00
|
|
|
const manifestPattern = `^application/vnd.docker.distribution.manifest.v\d\+json`
|
2016-02-01 12:59:10 +01:00
|
|
|
|
2016-02-26 11:35:55 +01:00
|
|
|
// Post handles POST request, and records audit log or refreshes cache based on event.
|
2016-02-01 12:59:10 +01:00
|
|
|
func (n *NotificationHandler) Post() {
|
|
|
|
var notification models.Notification
|
|
|
|
// log.Printf("Notification Handler triggered!\n")
|
|
|
|
// log.Printf("request body in string: %s", string(n.Ctx.Input.CopyBody()))
|
|
|
|
err := json.Unmarshal(n.Ctx.Input.CopyBody(1<<32), ¬ification)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
beego.Error("error while decoding json: ", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var username, action, repo, project string
|
2016-03-01 11:21:56 +01:00
|
|
|
var matched bool
|
2016-02-01 12:59:10 +01:00
|
|
|
for _, e := range notification.Events {
|
2016-03-01 11:21:56 +01:00
|
|
|
matched, err = regexp.MatchString(manifestPattern, e.Target.MediaType)
|
|
|
|
if err != nil {
|
2016-03-01 11:37:51 +01:00
|
|
|
beego.Error("Failed to match the media type against pattern, error: ", err)
|
2016-03-01 11:21:56 +01:00
|
|
|
matched = false
|
|
|
|
}
|
|
|
|
if matched && strings.HasPrefix(e.Request.UserAgent, "docker") {
|
2016-02-01 12:59:10 +01:00
|
|
|
username = e.Actor.Name
|
|
|
|
action = e.Action
|
|
|
|
repo = e.Target.Repository
|
|
|
|
if strings.Contains(repo, "/") {
|
|
|
|
project = repo[0:strings.LastIndex(repo, "/")]
|
|
|
|
}
|
|
|
|
if username == "" {
|
|
|
|
username = "anonymous"
|
|
|
|
}
|
|
|
|
go dao.AccessLog(username, project, repo, action)
|
|
|
|
if action == "push" {
|
|
|
|
go func() {
|
|
|
|
err2 := svc_utils.RefreshCatalogCache()
|
|
|
|
if err2 != nil {
|
|
|
|
beego.Error("Error happens when refreshing cache:", err2)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-02-26 11:35:55 +01:00
|
|
|
// Render returns nil as it won't render any template.
|
2016-02-01 12:59:10 +01:00
|
|
|
func (n *NotificationHandler) Render() error {
|
|
|
|
return nil
|
|
|
|
}
|