2021-12-03 22:50:58 -05:00
|
|
|
//go:build extended
|
2021-05-25 03:38:21 -05:00
|
|
|
// +build extended
|
|
|
|
|
|
|
|
package log_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
godigest "github.com/opencontainers/go-digest"
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
|
|
"gopkg.in/resty.v1"
|
2021-12-03 22:50:58 -05:00
|
|
|
"zotregistry.io/zot/pkg/api"
|
|
|
|
"zotregistry.io/zot/pkg/api/config"
|
|
|
|
. "zotregistry.io/zot/test"
|
2021-05-25 03:38:21 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
username = "test"
|
|
|
|
passphrase = "test"
|
|
|
|
AuthorizedNamespace = "everyone/isallowed"
|
|
|
|
UnauthorizedNamespace = "fortknox/notallowed"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AuditLog struct {
|
|
|
|
Level string `json:"level"`
|
2021-12-13 14:23:31 -05:00
|
|
|
ClientIP string `json:"clientIP"` //nolint:tagliatelle // keep IP
|
2021-05-25 03:38:21 -05:00
|
|
|
Subject string `json:"subject"`
|
|
|
|
Action string `json:"action"`
|
|
|
|
Object string `json:"object"`
|
|
|
|
Status int `json:"status"`
|
|
|
|
Time string `json:"time"`
|
|
|
|
Message string `json:"message"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAuditLogMessages(t *testing.T) {
|
|
|
|
Convey("Make a new controller", t, func() {
|
|
|
|
dir, err := ioutil.TempDir("", "oci-repo-test")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(dir)
|
2021-11-10 09:31:03 -05:00
|
|
|
err = CopyFiles("../../test/data", dir)
|
2021-05-25 03:38:21 -05:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-11-10 09:31:03 -05:00
|
|
|
port := GetFreePort()
|
|
|
|
baseURL := GetBaseURL(port)
|
2021-06-08 15:11:18 -05:00
|
|
|
conf := config.New()
|
2021-05-25 03:38:21 -05:00
|
|
|
|
|
|
|
outputPath := dir + "/zot.log"
|
|
|
|
auditPath := dir + "/zot-audit.log"
|
2021-06-08 15:11:18 -05:00
|
|
|
conf.Log = &config.LogConfig{Level: "debug", Output: outputPath, Audit: auditPath}
|
2021-05-25 03:38:21 -05:00
|
|
|
|
2021-11-10 09:31:03 -05:00
|
|
|
conf.HTTP.Port = port
|
2021-05-25 03:38:21 -05:00
|
|
|
|
2021-11-10 09:31:03 -05:00
|
|
|
htpasswdPath := MakeHtpasswdFile()
|
2021-05-25 03:38:21 -05:00
|
|
|
defer os.Remove(htpasswdPath)
|
2021-06-08 15:11:18 -05:00
|
|
|
conf.HTTP.Auth = &config.AuthConfig{
|
|
|
|
HTPasswd: config.AuthHTPasswd{
|
2021-05-25 03:38:21 -05:00
|
|
|
Path: htpasswdPath,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-12-13 14:23:31 -05:00
|
|
|
ctlr := api.NewController(conf)
|
|
|
|
ctlr.Config.Storage.RootDirectory = dir
|
2021-05-25 03:38:21 -05:00
|
|
|
go func() {
|
|
|
|
// this blocks
|
2021-12-13 14:23:31 -05:00
|
|
|
if err := ctlr.Run(); err != nil {
|
2021-05-25 03:38:21 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// wait till ready
|
|
|
|
for {
|
2021-11-10 09:31:03 -05:00
|
|
|
_, err := resty.R().Get(baseURL)
|
2021-05-25 03:38:21 -05:00
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
ctx := context.Background()
|
2021-12-13 14:23:31 -05:00
|
|
|
_ = ctlr.Server.Shutdown(ctx)
|
2021-05-25 03:38:21 -05:00
|
|
|
}()
|
|
|
|
|
|
|
|
Convey("Open auditLog file", func() {
|
|
|
|
auditFile, err := os.Open(auditPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Log("Cannot open file")
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer auditFile.Close()
|
|
|
|
|
|
|
|
Convey("Test GET request", func() {
|
2021-11-10 09:31:03 -05:00
|
|
|
resp, err := resty.R().SetBasicAuth(username, passphrase).Get(baseURL + "/v2/")
|
2021-05-25 03:38:21 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(resp, ShouldNotBeNil)
|
|
|
|
So(resp.StatusCode(), ShouldEqual, http.StatusOK)
|
|
|
|
|
|
|
|
byteValue, _ := ioutil.ReadAll(auditFile)
|
|
|
|
So(len(byteValue), ShouldEqual, 0)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Test POST request", func() {
|
|
|
|
path := "/v2/" + AuthorizedNamespace + "/blobs/uploads/"
|
2021-11-10 09:31:03 -05:00
|
|
|
resp, err := resty.R().SetBasicAuth(username, passphrase).Post(baseURL + path)
|
2021-05-25 03:38:21 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(resp, ShouldNotBeNil)
|
|
|
|
So(resp.StatusCode(), ShouldEqual, http.StatusAccepted)
|
|
|
|
|
|
|
|
// wait until the file is populated
|
|
|
|
byteValue, _ := ioutil.ReadAll(auditFile)
|
|
|
|
for {
|
|
|
|
if len(byteValue) != 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
byteValue, _ = ioutil.ReadAll(auditFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
var auditLog AuditLog
|
|
|
|
err = json.Unmarshal(byteValue, &auditLog)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
So(auditLog.Subject, ShouldEqual, username)
|
|
|
|
So(auditLog.Action, ShouldEqual, http.MethodPost)
|
|
|
|
So(auditLog.Status, ShouldEqual, http.StatusAccepted)
|
|
|
|
So(auditLog.Object, ShouldEqual, path)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Test PUT and DELETE request", func() {
|
|
|
|
// create upload
|
|
|
|
path := "/v2/repo/blobs/uploads/"
|
2021-11-10 09:31:03 -05:00
|
|
|
resp, err := resty.R().SetBasicAuth(username, passphrase).Post(baseURL + path)
|
2021-05-25 03:38:21 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(resp.StatusCode(), ShouldEqual, http.StatusAccepted)
|
2021-11-10 09:31:03 -05:00
|
|
|
loc := Location(baseURL, resp)
|
2021-05-25 03:38:21 -05:00
|
|
|
So(loc, ShouldNotBeEmpty)
|
|
|
|
location := resp.Header().Get("Location")
|
|
|
|
So(location, ShouldNotBeEmpty)
|
|
|
|
|
|
|
|
// wait until the file is populated
|
|
|
|
byteValue, _ := ioutil.ReadAll(auditFile)
|
|
|
|
for {
|
|
|
|
if len(byteValue) != 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
byteValue, _ = ioutil.ReadAll(auditFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
var auditLog AuditLog
|
|
|
|
err = json.Unmarshal(byteValue, &auditLog)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
So(auditLog.Subject, ShouldEqual, username)
|
|
|
|
So(auditLog.Action, ShouldEqual, http.MethodPost)
|
|
|
|
So(auditLog.Status, ShouldEqual, http.StatusAccepted)
|
|
|
|
So(auditLog.Object, ShouldEqual, path)
|
|
|
|
|
|
|
|
content := []byte("this is a blob")
|
|
|
|
digest := godigest.FromBytes(content)
|
|
|
|
So(digest, ShouldNotBeNil)
|
|
|
|
|
|
|
|
// blob upload
|
|
|
|
resp, err = resty.R().SetQueryParam("digest", digest.String()).
|
|
|
|
SetBasicAuth(username, passphrase).
|
|
|
|
SetHeader("Content-Type", "application/octet-stream").SetBody(content).Put(loc)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(resp.StatusCode(), ShouldEqual, http.StatusCreated)
|
2021-11-10 09:31:03 -05:00
|
|
|
blobLoc := Location(baseURL, resp)
|
2021-05-25 03:38:21 -05:00
|
|
|
So(blobLoc, ShouldNotBeEmpty)
|
|
|
|
So(resp.Header().Get(api.DistContentDigestKey), ShouldNotBeEmpty)
|
|
|
|
|
|
|
|
// wait until the file is populated
|
|
|
|
byteValue, _ = ioutil.ReadAll(auditFile)
|
|
|
|
for {
|
|
|
|
if len(byteValue) != 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
byteValue, _ = ioutil.ReadAll(auditFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(byteValue, &auditLog)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
So(auditLog.Subject, ShouldEqual, username)
|
|
|
|
So(auditLog.Action, ShouldEqual, http.MethodPut)
|
|
|
|
So(auditLog.Status, ShouldEqual, http.StatusCreated)
|
|
|
|
|
|
|
|
putPath := location + "?digest=" + strings.ReplaceAll(digest.String(), ":", "%3A")
|
|
|
|
So(auditLog.Object, ShouldEqual, putPath)
|
|
|
|
|
|
|
|
// delete this blob
|
|
|
|
resp, err = resty.R().SetBasicAuth(username, passphrase).Delete(blobLoc)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(resp.StatusCode(), ShouldEqual, http.StatusAccepted)
|
|
|
|
So(resp.Header().Get("Content-Length"), ShouldEqual, "0")
|
|
|
|
|
|
|
|
// wait until the file is populated
|
|
|
|
byteValue, _ = ioutil.ReadAll(auditFile)
|
|
|
|
for {
|
|
|
|
if len(byteValue) != 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
byteValue, _ = ioutil.ReadAll(auditFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(byteValue, &auditLog)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
So(auditLog.Subject, ShouldEqual, username)
|
|
|
|
So(auditLog.Action, ShouldEqual, http.MethodDelete)
|
|
|
|
So(auditLog.Status, ShouldEqual, http.StatusAccepted)
|
|
|
|
|
|
|
|
deletePath := strings.ReplaceAll(path, "uploads/", digest.String())
|
|
|
|
So(auditLog.Object, ShouldEqual, deletePath)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Test PATCH request", func() {
|
|
|
|
path := "/v2/repo/blobs/uploads/"
|
2021-11-10 09:31:03 -05:00
|
|
|
resp, err := resty.R().SetBasicAuth(username, passphrase).Post(baseURL + path)
|
2021-05-25 03:38:21 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(resp.StatusCode(), ShouldEqual, http.StatusAccepted)
|
2021-11-10 09:31:03 -05:00
|
|
|
loc := Location(baseURL, resp)
|
2021-05-25 03:38:21 -05:00
|
|
|
So(loc, ShouldNotBeEmpty)
|
|
|
|
location := resp.Header().Get("Location")
|
|
|
|
So(location, ShouldNotBeEmpty)
|
|
|
|
|
|
|
|
// wait until the file is populated
|
|
|
|
byteValue, _ := ioutil.ReadAll(auditFile)
|
|
|
|
for {
|
|
|
|
if len(byteValue) != 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
byteValue, _ = ioutil.ReadAll(auditFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
var auditLog AuditLog
|
|
|
|
err = json.Unmarshal(byteValue, &auditLog)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
So(auditLog.Subject, ShouldEqual, username)
|
|
|
|
So(auditLog.Action, ShouldEqual, http.MethodPost)
|
|
|
|
So(auditLog.Status, ShouldEqual, http.StatusAccepted)
|
|
|
|
So(auditLog.Object, ShouldEqual, path)
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
chunk := []byte("this is a chunk")
|
|
|
|
n, err := buf.Write(chunk)
|
|
|
|
So(n, ShouldEqual, len(chunk))
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
// write a chunk
|
|
|
|
contentRange := fmt.Sprintf("%d-%d", 0, len(chunk)-1)
|
|
|
|
resp, err = resty.R().SetBasicAuth(username, passphrase).
|
|
|
|
SetHeader("Content-Type", "application/octet-stream").
|
|
|
|
SetHeader("Content-Range", contentRange).SetBody(chunk).Patch(loc)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(resp.StatusCode(), ShouldEqual, http.StatusAccepted)
|
|
|
|
|
|
|
|
// wait until the file is populated
|
|
|
|
byteValue, _ = ioutil.ReadAll(auditFile)
|
|
|
|
for {
|
|
|
|
if len(byteValue) != 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
byteValue, _ = ioutil.ReadAll(auditFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(byteValue, &auditLog)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
So(auditLog.Subject, ShouldEqual, username)
|
|
|
|
So(auditLog.Action, ShouldEqual, http.MethodPatch)
|
|
|
|
So(auditLog.Status, ShouldEqual, http.StatusAccepted)
|
|
|
|
|
|
|
|
patchPath := location
|
|
|
|
So(auditLog.Object, ShouldEqual, patchPath)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|