首页
Kubernetes 源码开发之旅
GitHub (opens new window)

brook-w

K8s 源码学习、二次开发、自定义组件开发
首页
Kubernetes 源码开发之旅
GitHub (opens new window)
  • (一)环境搭建
  • (二)编译运行并调试源码
  • (三)API Server 源码刨析

    • 1. API Server 源码简介
    • 2. Generic Server
    • 3. Master API Server
    • 4. Extension API Server
    • 5. Aggregator Server
    • 6. Admission
    • 7. Http Req 处理过程和 Default Filters
      • Http Req 处理过程
        • 回顾
      • Default Filters
        • WithAuthorization
      • 编解码
    • 8. 登录与鉴权的实现
  • (四)Aggregated API Server 的实现
  • Kubernetes 源码开发之旅
  • (三)API Server 源码刨析
brook-w
2023-02-09
目录

7. Http Req 处理过程和 Default Filters

# Http Req 处理过程

# 回顾

image

# Default Filters

  • # WithAuthorization

// WithAuthorizationCheck passes all authorized requests on to handler, and returns a forbidden error otherwise.
func WithAuthorization(handler http.Handler, a authorizer.Authorizer, s runtime.NegotiatedSerializer) http.Handler {
	if a == nil {
		klog.Warning("Authorization is disabled")
		return handler
	}
    
    /** CUSTOM-ADD-LINE
    // The HandlerFunc type is an adapter to allow the use of
    // ordinary functions as HTTP handlers. If f is a function
    // with the appropriate signature, HandlerFunc(f) is a
    // Handler that calls f.
    type HandlerFunc func(ResponseWriter, *Request)

    // ServeHTTP calls f(w, r).
    func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) {
        f(w, r)
    }
    */
    
	return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		ctx := req.Context()

		attributes, err := GetAuthorizerAttributes(ctx)
		if err != nil {
			responsewriters.InternalError(w, req, err)
			return
		}
		authorized, reason, err := a.Authorize(ctx, attributes)
		// an authorizer like RBAC could encounter evaluation errors and still allow the request, so authorizer decision is checked before error here.
		if authorized == authorizer.DecisionAllow {
			audit.AddAuditAnnotations(ctx,
				decisionAnnotationKey, decisionAllow,
				reasonAnnotationKey, reason)
			handler.ServeHTTP(w, req)
			return
		}
		if err != nil {
			audit.AddAuditAnnotation(ctx, reasonAnnotationKey, reasonError)
			responsewriters.InternalError(w, req, err)
			return
		}

		klog.V(4).InfoS("Forbidden", "URI", req.RequestURI, "Reason", reason)
		audit.AddAuditAnnotations(ctx,
			decisionAnnotationKey, decisionForbid,
			reasonAnnotationKey, reason)
		responsewriters.Forbidden(ctx, attributes, w, req, reason, s)
	})
}

# 编解码

Http Payload 与 Go 结构实例之间的转换

关于编解码转换相关的内容可以参考《Kubernetes 编程》此书

上次更新: 2023/02/15, 03:43:27
6. Admission
8. 登录与鉴权的实现

← 6. Admission 8. 登录与鉴权的实现→

最近更新
01
概述
02-10
02
(四)Aggregated API Server 的实现
02-10
03
8. 登录与鉴权的实现
02-09
更多文章>
友站: www.brook-w.com
Theme by Vdoing | Copyright © 2019-2023 Brook-w | GPL License
京ICP备2020045721号-2
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式