37 Star 411 Fork 76

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
filter.go 2.33 KB
一键复制 编辑 原始数据 按行查看 历史
Aiwantaozi 提交于 2018-06-22 18:08 . add audit log support
package filter
import (
"bufio"
"context"
"errors"
"fmt"
"net"
"net/http"
"github.com/pborman/uuid"
"github.com/sirupsen/logrus"
k8stypes "k8s.io/apimachinery/pkg/types"
"github.com/rancher/rancher/pkg/audit"
"github.com/rancher/rancher/pkg/auth/requests"
"github.com/rancher/rancher/pkg/auth/util"
"github.com/rancher/types/config"
)
type wrapWriter struct {
auditID k8stypes.UID
http.ResponseWriter
auditWriter *audit.LogWriter
statusCode int
}
func (aw *wrapWriter) WriteHeader(statusCode int) {
aw.ResponseWriter.WriteHeader(statusCode)
aw.statusCode = statusCode
}
func (aw *wrapWriter) Write(body []byte) (int, error) {
n, err := aw.ResponseWriter.Write(body)
go func() {
aw.auditWriter.LogResponse(body, aw.auditID, aw.statusCode, aw.Header().Get("Content-Type"))
}()
return n, err
}
func (aw *wrapWriter) Hijack() (net.Conn, *bufio.ReadWriter, error) {
if hijacker, ok := aw.ResponseWriter.(http.Hijacker); ok {
return hijacker.Hijack()
}
return nil, nil, errors.New("the ResponseWriter doesn't support the Hijacker interface")
}
func NewAuthenticationFilter(ctx context.Context, managementContext *config.ScaledContext, auditWriter *audit.LogWriter, next http.Handler) (http.Handler, error) {
if managementContext == nil {
return nil, fmt.Errorf("Failed to build NewAuthenticationFilter, nil ManagementContext")
}
auth := requests.NewAuthenticator(ctx, managementContext)
return &authHandler{
auth: auth,
next: next,
auditWriter: auditWriter,
}, nil
}
type authHandler struct {
auth requests.Authenticator
next http.Handler
auditWriter *audit.LogWriter
}
func (h authHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
authed, user, groups, err := h.auth.Authenticate(req)
if err != nil || !authed {
util.ReturnHTTPError(rw, req, 401, err.Error())
return
}
logrus.Debugf("Impersonating user %v, groups %v", user, groups)
req.Header.Set("Impersonate-User", user)
req.Header.Del("Impersonate-Group")
for _, group := range groups {
req.Header.Add("Impersonate-Group", group)
}
aw := rw
if h.auditWriter != nil {
auditID := k8stypes.UID(uuid.NewRandom().String())
h.auditWriter.LogRequest(req, auditID, authed, req.Header.Get("Content-Type"), user, groups)
aw = &wrapWriter{auditID, rw, h.auditWriter, http.StatusOK}
}
h.next.ServeHTTP(aw, req)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.0.15-rc3

搜索帮助

0d507c66 1850385 C8b1a773 1850385