handler.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. "net/http"
  20. "net/url"
  21. "path"
  22. "strconv"
  23. "strings"
  24. "sync"
  25. "time"
  26. "github.com/golang/protobuf/proto"
  27. openapi_v3 "github.com/google/gnostic-models/openapiv3"
  28. "github.com/google/uuid"
  29. "github.com/munnerz/goautoneg"
  30. "k8s.io/klog/v2"
  31. "k8s.io/kube-openapi/pkg/cached"
  32. "k8s.io/kube-openapi/pkg/common"
  33. "k8s.io/kube-openapi/pkg/spec3"
  34. )
  35. const (
  36. subTypeProtobufDeprecated = "com.github.proto-openapi.spec.v3@v1.0+protobuf"
  37. subTypeProtobuf = "com.github.proto-openapi.spec.v3.v1.0+protobuf"
  38. subTypeJSON = "json"
  39. )
  40. // OpenAPIV3Discovery is the format of the Discovery document for OpenAPI V3
  41. // It maps Discovery paths to their corresponding URLs with a hash parameter included
  42. type OpenAPIV3Discovery struct {
  43. Paths map[string]OpenAPIV3DiscoveryGroupVersion `json:"paths"`
  44. }
  45. // OpenAPIV3DiscoveryGroupVersion includes information about a group version and URL
  46. // for accessing the OpenAPI. The URL includes a hash parameter to support client side caching
  47. type OpenAPIV3DiscoveryGroupVersion struct {
  48. // Path is an absolute path of an OpenAPI V3 document in the form of /openapi/v3/apis/apps/v1?hash=014fbff9a07c
  49. ServerRelativeURL string `json:"serverRelativeURL"`
  50. }
  51. func ToV3ProtoBinary(json []byte) ([]byte, error) {
  52. document, err := openapi_v3.ParseDocument(json)
  53. if err != nil {
  54. return nil, err
  55. }
  56. return proto.Marshal(document)
  57. }
  58. type timedSpec struct {
  59. spec []byte
  60. lastModified time.Time
  61. }
  62. // This type is protected by the lock on OpenAPIService.
  63. type openAPIV3Group struct {
  64. specCache cached.LastSuccess[*spec3.OpenAPI]
  65. pbCache cached.Value[timedSpec]
  66. jsonCache cached.Value[timedSpec]
  67. }
  68. func newOpenAPIV3Group() *openAPIV3Group {
  69. o := &openAPIV3Group{}
  70. o.jsonCache = cached.Transform[*spec3.OpenAPI](func(spec *spec3.OpenAPI, etag string, err error) (timedSpec, string, error) {
  71. if err != nil {
  72. return timedSpec{}, "", err
  73. }
  74. json, err := json.Marshal(spec)
  75. if err != nil {
  76. return timedSpec{}, "", err
  77. }
  78. return timedSpec{spec: json, lastModified: time.Now()}, computeETag(json), nil
  79. }, &o.specCache)
  80. o.pbCache = cached.Transform(func(ts timedSpec, etag string, err error) (timedSpec, string, error) {
  81. if err != nil {
  82. return timedSpec{}, "", err
  83. }
  84. proto, err := ToV3ProtoBinary(ts.spec)
  85. if err != nil {
  86. return timedSpec{}, "", err
  87. }
  88. return timedSpec{spec: proto, lastModified: ts.lastModified}, etag, nil
  89. }, o.jsonCache)
  90. return o
  91. }
  92. func (o *openAPIV3Group) UpdateSpec(openapi cached.Value[*spec3.OpenAPI]) {
  93. o.specCache.Store(openapi)
  94. }
  95. // OpenAPIService is the service responsible for serving OpenAPI spec. It has
  96. // the ability to safely change the spec while serving it.
  97. type OpenAPIService struct {
  98. // Mutex protects the schema map.
  99. mutex sync.Mutex
  100. v3Schema map[string]*openAPIV3Group
  101. discoveryCache cached.LastSuccess[timedSpec]
  102. }
  103. func computeETag(data []byte) string {
  104. if data == nil {
  105. return ""
  106. }
  107. return fmt.Sprintf("%X", sha512.Sum512(data))
  108. }
  109. func constructServerRelativeURL(gvString, etag string) string {
  110. u := url.URL{Path: path.Join("/openapi/v3", gvString)}
  111. query := url.Values{}
  112. query.Set("hash", etag)
  113. u.RawQuery = query.Encode()
  114. return u.String()
  115. }
  116. // NewOpenAPIService builds an OpenAPIService starting with the given spec.
  117. func NewOpenAPIService() *OpenAPIService {
  118. o := &OpenAPIService{}
  119. o.v3Schema = make(map[string]*openAPIV3Group)
  120. // We're not locked because we haven't shared the structure yet.
  121. o.discoveryCache.Store(o.buildDiscoveryCacheLocked())
  122. return o
  123. }
  124. func (o *OpenAPIService) buildDiscoveryCacheLocked() cached.Value[timedSpec] {
  125. caches := make(map[string]cached.Value[timedSpec], len(o.v3Schema))
  126. for gvName, group := range o.v3Schema {
  127. caches[gvName] = group.jsonCache
  128. }
  129. return cached.Merge(func(results map[string]cached.Result[timedSpec]) (timedSpec, string, error) {
  130. discovery := &OpenAPIV3Discovery{Paths: make(map[string]OpenAPIV3DiscoveryGroupVersion)}
  131. for gvName, result := range results {
  132. if result.Err != nil {
  133. return timedSpec{}, "", result.Err
  134. }
  135. discovery.Paths[gvName] = OpenAPIV3DiscoveryGroupVersion{
  136. ServerRelativeURL: constructServerRelativeURL(gvName, result.Etag),
  137. }
  138. }
  139. j, err := json.Marshal(discovery)
  140. if err != nil {
  141. return timedSpec{}, "", err
  142. }
  143. return timedSpec{spec: j, lastModified: time.Now()}, computeETag(j), nil
  144. }, caches)
  145. }
  146. func (o *OpenAPIService) getSingleGroupBytes(getType string, group string) ([]byte, string, time.Time, error) {
  147. o.mutex.Lock()
  148. defer o.mutex.Unlock()
  149. v, ok := o.v3Schema[group]
  150. if !ok {
  151. return nil, "", time.Now(), fmt.Errorf("Cannot find CRD group %s", group)
  152. }
  153. switch getType {
  154. case subTypeJSON:
  155. ts, etag, err := v.jsonCache.Get()
  156. return ts.spec, etag, ts.lastModified, err
  157. case subTypeProtobuf, subTypeProtobufDeprecated:
  158. ts, etag, err := v.pbCache.Get()
  159. return ts.spec, etag, ts.lastModified, err
  160. default:
  161. return nil, "", time.Now(), fmt.Errorf("Invalid accept clause %s", getType)
  162. }
  163. }
  164. // UpdateGroupVersionLazy adds or updates an existing group with the new cached.
  165. func (o *OpenAPIService) UpdateGroupVersionLazy(group string, openapi cached.Value[*spec3.OpenAPI]) {
  166. o.mutex.Lock()
  167. defer o.mutex.Unlock()
  168. if _, ok := o.v3Schema[group]; !ok {
  169. o.v3Schema[group] = newOpenAPIV3Group()
  170. // Since there is a new item, we need to re-build the cache map.
  171. o.discoveryCache.Store(o.buildDiscoveryCacheLocked())
  172. }
  173. o.v3Schema[group].UpdateSpec(openapi)
  174. }
  175. func (o *OpenAPIService) UpdateGroupVersion(group string, openapi *spec3.OpenAPI) {
  176. o.UpdateGroupVersionLazy(group, cached.Static(openapi, uuid.New().String()))
  177. }
  178. func (o *OpenAPIService) DeleteGroupVersion(group string) {
  179. o.mutex.Lock()
  180. defer o.mutex.Unlock()
  181. delete(o.v3Schema, group)
  182. // Rebuild the merge cache map since the items have changed.
  183. o.discoveryCache.Store(o.buildDiscoveryCacheLocked())
  184. }
  185. func (o *OpenAPIService) HandleDiscovery(w http.ResponseWriter, r *http.Request) {
  186. ts, etag, err := o.discoveryCache.Get()
  187. if err != nil {
  188. klog.Errorf("Error serving discovery: %s", err)
  189. w.WriteHeader(http.StatusInternalServerError)
  190. return
  191. }
  192. w.Header().Set("Etag", strconv.Quote(etag))
  193. w.Header().Set("Content-Type", "application/json")
  194. http.ServeContent(w, r, "/openapi/v3", ts.lastModified, bytes.NewReader(ts.spec))
  195. }
  196. func (o *OpenAPIService) HandleGroupVersion(w http.ResponseWriter, r *http.Request) {
  197. url := strings.SplitAfterN(r.URL.Path, "/", 4)
  198. group := url[3]
  199. decipherableFormats := r.Header.Get("Accept")
  200. if decipherableFormats == "" {
  201. decipherableFormats = "*/*"
  202. }
  203. clauses := goautoneg.ParseAccept(decipherableFormats)
  204. w.Header().Add("Vary", "Accept")
  205. if len(clauses) == 0 {
  206. return
  207. }
  208. accepted := []struct {
  209. Type string
  210. SubType string
  211. ReturnedContentType string
  212. }{
  213. {"application", subTypeJSON, "application/" + subTypeJSON},
  214. {"application", subTypeProtobuf, "application/" + subTypeProtobuf},
  215. {"application", subTypeProtobufDeprecated, "application/" + subTypeProtobuf},
  216. }
  217. for _, clause := range clauses {
  218. for _, accepts := range accepted {
  219. if clause.Type != accepts.Type && clause.Type != "*" {
  220. continue
  221. }
  222. if clause.SubType != accepts.SubType && clause.SubType != "*" {
  223. continue
  224. }
  225. data, etag, lastModified, err := o.getSingleGroupBytes(accepts.SubType, group)
  226. if err != nil {
  227. return
  228. }
  229. // Set Content-Type header in the reponse
  230. w.Header().Set("Content-Type", accepts.ReturnedContentType)
  231. // ETag must be enclosed in double quotes: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag
  232. w.Header().Set("Etag", strconv.Quote(etag))
  233. if hash := r.URL.Query().Get("hash"); hash != "" {
  234. if hash != etag {
  235. u := constructServerRelativeURL(group, etag)
  236. http.Redirect(w, r, u, 301)
  237. return
  238. }
  239. // The Vary header is required because the Accept header can
  240. // change the contents returned. This prevents clients from caching
  241. // protobuf as JSON and vice versa.
  242. w.Header().Set("Vary", "Accept")
  243. // Only set these headers when a hash is given.
  244. w.Header().Set("Cache-Control", "public, immutable")
  245. // Set the Expires directive to the maximum value of one year from the request,
  246. // effectively indicating that the cache never expires.
  247. w.Header().Set("Expires", time.Now().AddDate(1, 0, 0).Format(time.RFC1123))
  248. }
  249. http.ServeContent(w, r, "", lastModified, bytes.NewReader(data))
  250. return
  251. }
  252. }
  253. w.WriteHeader(406)
  254. return
  255. }
  256. func (o *OpenAPIService) RegisterOpenAPIV3VersionedService(servePath string, handler common.PathHandlerByGroupVersion) error {
  257. handler.Handle(servePath, http.HandlerFunc(o.HandleDiscovery))
  258. handler.HandlePrefix(servePath+"/", http.HandlerFunc(o.HandleGroupVersion))
  259. return nil
  260. }