mirror of
https://github.com/goharbor/harbor.git
synced 2024-11-29 05:35:43 +01:00
Merge pull request #443 from ywk253100/dev
add access logs when deleting repositories
This commit is contained in:
commit
3f1fc90b39
@ -144,6 +144,18 @@ func (ra *RepositoryAPI) Delete() {
|
|||||||
tags = append(tags, tag)
|
tags = append(tags, tag)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
project := ""
|
||||||
|
if strings.Contains(repoName, "/") {
|
||||||
|
project = repoName[0:strings.LastIndex(repoName, "/")]
|
||||||
|
}
|
||||||
|
user, _, ok := ra.Ctx.Request.BasicAuth()
|
||||||
|
if !ok {
|
||||||
|
user, err = ra.getUsername()
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("failed to get user: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for _, t := range tags {
|
for _, t := range tags {
|
||||||
if err := rc.DeleteTag(t); err != nil {
|
if err := rc.DeleteTag(t); err != nil {
|
||||||
if regErr, ok := err.(*registry_error.Error); ok {
|
if regErr, ok := err.(*registry_error.Error); ok {
|
||||||
@ -156,6 +168,11 @@ func (ra *RepositoryAPI) Delete() {
|
|||||||
log.Infof("delete tag: %s %s", repoName, t)
|
log.Infof("delete tag: %s %s", repoName, t)
|
||||||
go TriggerReplicationByRepository(repoName, []string{t}, models.RepOpDelete)
|
go TriggerReplicationByRepository(repoName, []string{t}, models.RepOpDelete)
|
||||||
|
|
||||||
|
go func(tag string) {
|
||||||
|
if err := dao.AccessLog(user, project, repoName, tag, "delete"); err != nil {
|
||||||
|
log.Errorf("failed to add access log: %v", err)
|
||||||
|
}
|
||||||
|
}(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
@ -164,7 +181,6 @@ func (ra *RepositoryAPI) Delete() {
|
|||||||
log.Errorf("error occurred while refresh catalog cache: %v", err)
|
log.Errorf("error occurred while refresh catalog cache: %v", err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type tag struct {
|
type tag struct {
|
||||||
|
@ -39,55 +39,85 @@ const manifestPattern = `^application/vnd.docker.distribution.manifest.v\d\+json
|
|||||||
// Post handles POST request, and records audit log or refreshes cache based on event.
|
// Post handles POST request, and records audit log or refreshes cache based on event.
|
||||||
func (n *NotificationHandler) Post() {
|
func (n *NotificationHandler) Post() {
|
||||||
var notification models.Notification
|
var notification models.Notification
|
||||||
//log.Info("Notification Handler triggered!\n")
|
|
||||||
// log.Infof("request body in string: %s", string(n.Ctx.Input.CopyBody()))
|
|
||||||
err := json.Unmarshal(n.Ctx.Input.CopyBody(1<<32), ¬ification)
|
err := json.Unmarshal(n.Ctx.Input.CopyBody(1<<32), ¬ification)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("error while decoding json: %v", err)
|
log.Errorf("failed to decode notification: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var username, action, repo, project, repoTag string
|
|
||||||
var matched bool
|
events, err := filterEvents(¬ification)
|
||||||
for _, e := range notification.Events {
|
|
||||||
matched, err = regexp.MatchString(manifestPattern, e.Target.MediaType)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Failed to match the media type against pattern, error: %v", err)
|
log.Errorf("failed to filter events: %v", err)
|
||||||
matched = false
|
|
||||||
}
|
|
||||||
if matched && (strings.HasPrefix(e.Request.UserAgent, "docker") ||
|
|
||||||
strings.ToLower(strings.TrimSpace(e.Request.UserAgent)) == "harbor-registry-client") {
|
|
||||||
username = e.Actor.Name
|
|
||||||
action = e.Action
|
|
||||||
repo = e.Target.Repository
|
|
||||||
repoTag = e.Target.Tag
|
|
||||||
log.Debugf("repo tag is : %v ", repoTag)
|
|
||||||
|
|
||||||
if strings.Contains(repo, "/") {
|
|
||||||
project = repo[0:strings.LastIndex(repo, "/")]
|
|
||||||
}
|
|
||||||
if username == "" {
|
|
||||||
username = "anonymous"
|
|
||||||
}
|
|
||||||
|
|
||||||
if action == "pull" && username == "job-service-user" {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
go dao.AccessLog(username, project, repo, repoTag, action)
|
for _, event := range events {
|
||||||
|
repository := event.Target.Repository
|
||||||
|
|
||||||
|
project := ""
|
||||||
|
if strings.Contains(repository, "/") {
|
||||||
|
project = repository[0:strings.LastIndex(repository, "/")]
|
||||||
|
}
|
||||||
|
|
||||||
|
tag := event.Target.Tag
|
||||||
|
action := event.Action
|
||||||
|
|
||||||
|
user := event.Actor.Name
|
||||||
|
if len(user) == 0 {
|
||||||
|
user = "anonymous"
|
||||||
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
if err := dao.AccessLog(user, project, repository, tag, action); err != nil {
|
||||||
|
log.Errorf("failed to add access log: %v", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
if action == "push" {
|
if action == "push" {
|
||||||
go func() {
|
go func() {
|
||||||
err2 := cache.RefreshCatalogCache()
|
if err := cache.RefreshCatalogCache(); err != nil {
|
||||||
if err2 != nil {
|
log.Errorf("failed to refresh cache: %v", err)
|
||||||
log.Errorf("Error happens when refreshing cache: %v", err2)
|
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
go api.TriggerReplicationByRepository(repo, []string{repoTag}, models.RepOpTransfer)
|
operation := ""
|
||||||
|
if action == "push" {
|
||||||
|
operation = models.RepOpTransfer
|
||||||
|
}
|
||||||
|
|
||||||
|
go api.TriggerReplicationByRepository(repository, []string{tag}, operation)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func filterEvents(notification *models.Notification) ([]*models.Event, error) {
|
||||||
|
events := []*models.Event{}
|
||||||
|
|
||||||
|
for _, event := range notification.Events {
|
||||||
|
isManifest, err := regexp.MatchString(manifestPattern, event.Target.MediaType)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("failed to match the media type against pattern: %v", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if !isManifest {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
//pull and push manifest by docker-client
|
||||||
|
if strings.HasPrefix(event.Request.UserAgent, "docker") && (event.Action == "pull" || event.Action == "push") {
|
||||||
|
events = append(events, &event)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
//push manifest by docker-client or job-service
|
||||||
|
if strings.ToLower(strings.TrimSpace(event.Request.UserAgent)) == "harbor-registry-client" && event.Action == "push" {
|
||||||
|
events = append(events, &event)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return events, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Render returns nil as it won't render any template.
|
// Render returns nil as it won't render any template.
|
||||||
|
Loading…
Reference in New Issue
Block a user