handler.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. Copyright 2021 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package handler3
  14. import (
  15. "bytes"
  16. "crypto/sha512"
  17. "encoding/json"
  18. "fmt"
  19. "mime"
  20. "net/http"
  21. "net/url"
  22. "path"
  23. "sort"
  24. "strconv"
  25. "strings"
  26. "sync"
  27. "time"
  28. "github.com/golang/protobuf/proto"
  29. openapi_v3 "github.com/google/gnostic/openapiv3"
  30. "github.com/munnerz/goautoneg"
  31. "k8s.io/kube-openapi/pkg/common"
  32. "k8s.io/kube-openapi/pkg/internal/handler"
  33. "k8s.io/kube-openapi/pkg/spec3"
  34. "k8s.io/kube-openapi/pkg/validation/spec"
  35. )
  36. const (
  37. jsonExt = ".json"
  38. mimeJson = "application/json"
  39. // TODO(mehdy): change @68f4ded to a version tag when gnostic add version tags.
  40. mimePb = "application/com.github.googleapis.gnostic.OpenAPIv3@68f4ded+protobuf"
  41. mimePbGz = "application/x-gzip"
  42. subTypeProtobuf = "com.github.proto-openapi.spec.v3@v1.0+protobuf"
  43. subTypeJSON = "json"
  44. )
  45. // OpenAPIV3Discovery is the format of the Discovery document for OpenAPI V3
  46. // It maps Discovery paths to their corresponding URLs with a hash parameter included
  47. type OpenAPIV3Discovery struct {
  48. Paths map[string]OpenAPIV3DiscoveryGroupVersion `json:"paths"`
  49. }
  50. // OpenAPIV3DiscoveryGroupVersion includes information about a group version and URL
  51. // for accessing the OpenAPI. The URL includes a hash parameter to support client side caching
  52. type OpenAPIV3DiscoveryGroupVersion struct {
  53. // Path is an absolute path of an OpenAPI V3 document in the form of /openapi/v3/apis/apps/v1?hash=014fbff9a07c
  54. ServerRelativeURL string `json:"serverRelativeURL"`
  55. }
  56. // OpenAPIService is the service responsible for serving OpenAPI spec. It has
  57. // the ability to safely change the spec while serving it.
  58. type OpenAPIService struct {
  59. // rwMutex protects All members of this service.
  60. rwMutex sync.RWMutex
  61. lastModified time.Time
  62. v3Schema map[string]*OpenAPIV3Group
  63. }
  64. type OpenAPIV3Group struct {
  65. rwMutex sync.RWMutex
  66. lastModified time.Time
  67. pbCache handler.HandlerCache
  68. jsonCache handler.HandlerCache
  69. etagCache handler.HandlerCache
  70. }
  71. func init() {
  72. mime.AddExtensionType(".json", mimeJson)
  73. mime.AddExtensionType(".pb-v1", mimePb)
  74. mime.AddExtensionType(".gz", mimePbGz)
  75. }
  76. func computeETag(data []byte) string {
  77. if data == nil {
  78. return ""
  79. }
  80. return fmt.Sprintf("%X", sha512.Sum512(data))
  81. }
  82. func constructServerRelativeURL(gvString, etag string) string {
  83. u := url.URL{Path: path.Join("/openapi/v3", gvString)}
  84. query := url.Values{}
  85. query.Set("hash", etag)
  86. u.RawQuery = query.Encode()
  87. return u.String()
  88. }
  89. // NewOpenAPIService builds an OpenAPIService starting with the given spec.
  90. func NewOpenAPIService(spec *spec.Swagger) (*OpenAPIService, error) {
  91. o := &OpenAPIService{}
  92. o.v3Schema = make(map[string]*OpenAPIV3Group)
  93. return o, nil
  94. }
  95. func (o *OpenAPIService) getGroupBytes() ([]byte, error) {
  96. o.rwMutex.RLock()
  97. defer o.rwMutex.RUnlock()
  98. keys := make([]string, len(o.v3Schema))
  99. i := 0
  100. for k := range o.v3Schema {
  101. keys[i] = k
  102. i++
  103. }
  104. sort.Strings(keys)
  105. discovery := &OpenAPIV3Discovery{Paths: make(map[string]OpenAPIV3DiscoveryGroupVersion)}
  106. for gvString, groupVersion := range o.v3Schema {
  107. etagBytes, err := groupVersion.etagCache.Get()
  108. if err != nil {
  109. return nil, err
  110. }
  111. discovery.Paths[gvString] = OpenAPIV3DiscoveryGroupVersion{
  112. ServerRelativeURL: constructServerRelativeURL(gvString, string(etagBytes)),
  113. }
  114. }
  115. j, err := json.Marshal(discovery)
  116. if err != nil {
  117. return nil, err
  118. }
  119. return j, nil
  120. }
  121. func (o *OpenAPIService) getSingleGroupBytes(getType string, group string) ([]byte, string, time.Time, error) {
  122. o.rwMutex.RLock()
  123. defer o.rwMutex.RUnlock()
  124. v, ok := o.v3Schema[group]
  125. if !ok {
  126. return nil, "", time.Now(), fmt.Errorf("Cannot find CRD group %s", group)
  127. }
  128. if getType == subTypeJSON {
  129. specBytes, err := v.jsonCache.Get()
  130. if err != nil {
  131. return nil, "", v.lastModified, err
  132. }
  133. etagBytes, err := v.etagCache.Get()
  134. return specBytes, string(etagBytes), v.lastModified, err
  135. } else if getType == subTypeProtobuf {
  136. specPb, err := v.pbCache.Get()
  137. if err != nil {
  138. return nil, "", v.lastModified, err
  139. }
  140. etagBytes, err := v.etagCache.Get()
  141. return specPb, string(etagBytes), v.lastModified, err
  142. }
  143. return nil, "", time.Now(), fmt.Errorf("Invalid accept clause %s", getType)
  144. }
  145. func (o *OpenAPIService) UpdateGroupVersion(group string, openapi *spec3.OpenAPI) (err error) {
  146. o.rwMutex.Lock()
  147. defer o.rwMutex.Unlock()
  148. if _, ok := o.v3Schema[group]; !ok {
  149. o.v3Schema[group] = &OpenAPIV3Group{}
  150. }
  151. return o.v3Schema[group].UpdateSpec(openapi)
  152. }
  153. func (o *OpenAPIService) DeleteGroupVersion(group string) {
  154. o.rwMutex.Lock()
  155. defer o.rwMutex.Unlock()
  156. delete(o.v3Schema, group)
  157. }
  158. func ToV3ProtoBinary(json []byte) ([]byte, error) {
  159. document, err := openapi_v3.ParseDocument(json)
  160. if err != nil {
  161. return nil, err
  162. }
  163. return proto.Marshal(document)
  164. }
  165. func (o *OpenAPIService) HandleDiscovery(w http.ResponseWriter, r *http.Request) {
  166. data, _ := o.getGroupBytes()
  167. http.ServeContent(w, r, "/openapi/v3", time.Now(), bytes.NewReader(data))
  168. }
  169. func (o *OpenAPIService) HandleGroupVersion(w http.ResponseWriter, r *http.Request) {
  170. url := strings.SplitAfterN(r.URL.Path, "/", 4)
  171. group := url[3]
  172. decipherableFormats := r.Header.Get("Accept")
  173. if decipherableFormats == "" {
  174. decipherableFormats = "*/*"
  175. }
  176. clauses := goautoneg.ParseAccept(decipherableFormats)
  177. w.Header().Add("Vary", "Accept")
  178. if len(clauses) == 0 {
  179. return
  180. }
  181. accepted := []struct {
  182. Type string
  183. SubType string
  184. }{
  185. {"application", subTypeJSON},
  186. {"application", subTypeProtobuf},
  187. }
  188. for _, clause := range clauses {
  189. for _, accepts := range accepted {
  190. if clause.Type != accepts.Type && clause.Type != "*" {
  191. continue
  192. }
  193. if clause.SubType != accepts.SubType && clause.SubType != "*" {
  194. continue
  195. }
  196. data, etag, lastModified, err := o.getSingleGroupBytes(accepts.SubType, group)
  197. if err != nil {
  198. return
  199. }
  200. // ETag must be enclosed in double quotes: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag
  201. w.Header().Set("Etag", strconv.Quote(etag))
  202. if hash := r.URL.Query().Get("hash"); hash != "" {
  203. if hash != etag {
  204. u := constructServerRelativeURL(group, etag)
  205. http.Redirect(w, r, u, 301)
  206. return
  207. }
  208. // The Vary header is required because the Accept header can
  209. // change the contents returned. This prevents clients from caching
  210. // protobuf as JSON and vice versa.
  211. w.Header().Set("Vary", "Accept")
  212. // Only set these headers when a hash is given.
  213. w.Header().Set("Cache-Control", "public, immutable")
  214. // Set the Expires directive to the maximum value of one year from the request,
  215. // effectively indicating that the cache never expires.
  216. w.Header().Set("Expires", time.Now().AddDate(1, 0, 0).Format(time.RFC1123))
  217. }
  218. http.ServeContent(w, r, "", lastModified, bytes.NewReader(data))
  219. return
  220. }
  221. }
  222. w.WriteHeader(406)
  223. return
  224. }
  225. func (o *OpenAPIService) RegisterOpenAPIV3VersionedService(servePath string, handler common.PathHandlerByGroupVersion) error {
  226. handler.Handle(servePath, http.HandlerFunc(o.HandleDiscovery))
  227. handler.HandlePrefix(servePath+"/", http.HandlerFunc(o.HandleGroupVersion))
  228. return nil
  229. }
  230. func (o *OpenAPIV3Group) UpdateSpec(openapi *spec3.OpenAPI) (err error) {
  231. o.rwMutex.Lock()
  232. defer o.rwMutex.Unlock()
  233. o.jsonCache = o.jsonCache.New(func() ([]byte, error) {
  234. return json.Marshal(openapi)
  235. })
  236. o.pbCache = o.pbCache.New(func() ([]byte, error) {
  237. json, err := o.jsonCache.Get()
  238. if err != nil {
  239. return nil, err
  240. }
  241. return ToV3ProtoBinary(json)
  242. })
  243. // TODO: This forces a json marshal of corresponding group-versions.
  244. // We should look to replace this with a faster hashing mechanism.
  245. o.etagCache = o.etagCache.New(func() ([]byte, error) {
  246. json, err := o.jsonCache.Get()
  247. if err != nil {
  248. return nil, err
  249. }
  250. return []byte(computeETag(json)), nil
  251. })
  252. o.lastModified = time.Now()
  253. return nil
  254. }