forked from openshift/origin
/
asset_apiserver.go
333 lines (288 loc) · 12.8 KB
/
asset_apiserver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package apiserver
import (
"net"
"net/http"
"net/url"
"path"
"strconv"
"time"
"github.com/golang/glog"
"github.com/elazarl/go-bindata-assetfs"
genericapifilters "k8s.io/apiserver/pkg/endpoints/filters"
apirequest "k8s.io/apiserver/pkg/endpoints/request"
"k8s.io/apiserver/pkg/features"
"k8s.io/apiserver/pkg/server"
genericapiserver "k8s.io/apiserver/pkg/server"
genericfilters "k8s.io/apiserver/pkg/server/filters"
genericmux "k8s.io/apiserver/pkg/server/mux"
genericapiserveroptions "k8s.io/apiserver/pkg/server/options"
utilfeature "k8s.io/apiserver/pkg/util/feature"
utilflag "k8s.io/apiserver/pkg/util/flag"
kapi "k8s.io/kubernetes/pkg/api"
kversion "k8s.io/kubernetes/pkg/version"
"github.com/openshift/origin/pkg/api"
"github.com/openshift/origin/pkg/assets"
"github.com/openshift/origin/pkg/assets/java"
oapi "github.com/openshift/origin/pkg/cmd/server/api"
"github.com/openshift/origin/pkg/cmd/server/crypto"
cmdutil "github.com/openshift/origin/pkg/cmd/util"
oauthutil "github.com/openshift/origin/pkg/oauth/util"
overrideapi "github.com/openshift/origin/pkg/quota/admission/clusterresourceoverride/api"
"github.com/openshift/origin/pkg/util/httprequest"
oversion "github.com/openshift/origin/pkg/version"
)
const (
OpenShiftWebConsoleClientID = "openshift-web-console"
)
type ExtraConfig struct {
Options oapi.AssetConfig
LimitRequestOverrides *overrideapi.ClusterResourceOverrideConfig
PublicURL url.URL
}
type AssetServerConfig struct {
GenericConfig *genericapiserver.RecommendedConfig
ExtraConfig ExtraConfig
}
// AssetServer serves non-API endpoints for openshift.
type AssetServer struct {
GenericAPIServer *genericapiserver.GenericAPIServer
PublicURL url.URL
}
type completedConfig struct {
GenericConfig genericapiserver.CompletedConfig
ExtraConfig *ExtraConfig
}
type CompletedConfig struct {
// Embed a private pointer that cannot be instantiated outside of this package.
*completedConfig
}
func NewAssetServerConfig(assetConfig oapi.AssetConfig, limitRequestOverrides *overrideapi.ClusterResourceOverrideConfig) (*AssetServerConfig, error) {
publicURL, err := url.Parse(assetConfig.PublicURL)
if err != nil {
glog.Fatal(err)
}
_, portString, err := net.SplitHostPort(assetConfig.ServingInfo.BindAddress)
if err != nil {
return nil, err
}
port, err := strconv.Atoi(portString)
if err != nil {
return nil, err
}
secureServingOptions := genericapiserveroptions.SecureServingOptions{}
secureServingOptions.BindPort = port
secureServingOptions.ServerCert.CertKey.CertFile = assetConfig.ServingInfo.ServerCert.CertFile
secureServingOptions.ServerCert.CertKey.KeyFile = assetConfig.ServingInfo.ServerCert.KeyFile
for _, nc := range assetConfig.ServingInfo.NamedCertificates {
sniCert := utilflag.NamedCertKey{
CertFile: nc.CertFile,
KeyFile: nc.KeyFile,
Names: nc.Names,
}
secureServingOptions.SNICertKeys = append(secureServingOptions.SNICertKeys, sniCert)
}
genericConfig := genericapiserver.NewConfig(kapi.Codecs)
genericConfig.EnableDiscovery = false
genericConfig.BuildHandlerChainFunc = buildHandlerChainForAssets(publicURL.Path)
if err := secureServingOptions.ApplyTo(genericConfig); err != nil {
return nil, err
}
genericConfig.SecureServingInfo.BindAddress = assetConfig.ServingInfo.BindAddress
genericConfig.SecureServingInfo.BindNetwork = assetConfig.ServingInfo.BindNetwork
genericConfig.SecureServingInfo.MinTLSVersion = crypto.TLSVersionOrDie(assetConfig.ServingInfo.MinTLSVersion)
genericConfig.SecureServingInfo.CipherSuites = crypto.CipherSuitesOrDie(assetConfig.ServingInfo.CipherSuites)
return &AssetServerConfig{
GenericConfig: &genericapiserver.RecommendedConfig{Config: *genericConfig},
ExtraConfig: ExtraConfig{
Options: assetConfig,
LimitRequestOverrides: limitRequestOverrides,
PublicURL: *publicURL,
},
}, nil
}
// Complete fills in any fields not set that are required to have valid data. It's mutating the receiver.
func (c *AssetServerConfig) Complete() completedConfig {
cfg := completedConfig{
c.GenericConfig.Complete(),
&c.ExtraConfig,
}
return cfg
}
func (c completedConfig) New(delegationTarget genericapiserver.DelegationTarget) (*AssetServer, error) {
genericServer, err := c.GenericConfig.New("openshift-non-api-routes", delegationTarget)
if err != nil {
return nil, err
}
s := &AssetServer{
GenericAPIServer: genericServer,
PublicURL: c.ExtraConfig.PublicURL,
}
if err := c.addAssets(s.GenericAPIServer.Handler.NonGoRestfulMux); err != nil {
return nil, err
}
if err := c.addExtensionScripts(s.GenericAPIServer.Handler.NonGoRestfulMux); err != nil {
return nil, err
}
if err := c.addExtensionStyleSheets(s.GenericAPIServer.Handler.NonGoRestfulMux); err != nil {
return nil, err
}
c.addExtensionFiles(s.GenericAPIServer.Handler.NonGoRestfulMux)
if err := c.addWebConsoleConfig(s.GenericAPIServer.Handler.NonGoRestfulMux); err != nil {
return nil, err
}
return s, nil
}
// buildHandlerChainForAssets is the handling chain used to protect the asset server. With no secret information to protect
// the chain is very short.
func buildHandlerChainForAssets(consoleRedirectPath string) func(startingHandler http.Handler, c *genericapiserver.Config) http.Handler {
return func(startingHandler http.Handler, c *genericapiserver.Config) http.Handler {
handler := WithAssetServerRedirect(startingHandler, consoleRedirectPath)
handler = genericfilters.WithMaxInFlightLimit(handler, c.MaxRequestsInFlight, c.MaxMutatingRequestsInFlight, c.RequestContextMapper, c.LongRunningFunc)
if utilfeature.DefaultFeatureGate.Enabled(features.AdvancedAuditing) {
handler = genericapifilters.WithAudit(handler, c.RequestContextMapper, c.AuditBackend, c.AuditPolicyChecker, c.LongRunningFunc)
}
handler = genericfilters.WithTimeoutForNonLongRunningRequests(handler, c.RequestContextMapper, c.LongRunningFunc, c.RequestTimeout)
handler = genericapifilters.WithRequestInfo(handler, genericapiserver.NewRequestInfoResolver(c), c.RequestContextMapper)
handler = apirequest.WithRequestContext(handler, c.RequestContextMapper)
handler = genericfilters.WithPanicRecovery(handler)
return handler
}
}
func (c completedConfig) addAssets(serverMux *genericmux.PathRecorderMux) error {
assetHandler, err := c.buildAssetHandler()
if err != nil {
return err
}
serverMux.UnlistedHandlePrefix(c.ExtraConfig.PublicURL.Path, http.StripPrefix(c.ExtraConfig.PublicURL.Path, assetHandler))
serverMux.UnlistedHandle(c.ExtraConfig.PublicURL.Path[0:len(c.ExtraConfig.PublicURL.Path)-1], http.RedirectHandler(c.ExtraConfig.PublicURL.Path, http.StatusMovedPermanently))
return nil
}
func (c completedConfig) addExtensionScripts(serverMux *genericmux.PathRecorderMux) error {
// Extension scripts
extScriptsPath := path.Join(c.ExtraConfig.PublicURL.Path, "scripts/extensions.js")
extScriptsHandler, err := assets.ExtensionScriptsHandler(c.ExtraConfig.Options.ExtensionScripts, c.ExtraConfig.Options.ExtensionDevelopment)
if err != nil {
return err
}
extScriptsHandler = assets.SecurityHeadersHandler(extScriptsHandler)
serverMux.UnlistedHandle(extScriptsPath, assets.GzipHandler(extScriptsHandler))
return nil
}
func (c completedConfig) addExtensionStyleSheets(serverMux *genericmux.PathRecorderMux) error {
// Extension stylesheets
extStylesheetsPath := path.Join(c.ExtraConfig.PublicURL.Path, "styles/extensions.css")
extStylesheetsHandler, err := assets.ExtensionStylesheetsHandler(c.ExtraConfig.Options.ExtensionStylesheets, c.ExtraConfig.Options.ExtensionDevelopment)
if err != nil {
return err
}
extStylesheetsHandler = assets.SecurityHeadersHandler(extStylesheetsHandler)
serverMux.UnlistedHandle(extStylesheetsPath, assets.GzipHandler(extStylesheetsHandler))
return nil
}
func (c completedConfig) addExtensionFiles(serverMux *genericmux.PathRecorderMux) {
// Extension files
for _, extConfig := range c.ExtraConfig.Options.Extensions {
extBasePath := path.Join(c.ExtraConfig.PublicURL.Path, "extensions", extConfig.Name)
extPath := extBasePath + "/"
extHandler := assets.AssetExtensionHandler(extConfig.SourceDirectory, extPath, extConfig.HTML5Mode)
serverMux.UnlistedHandlePrefix(extPath, http.StripPrefix(extBasePath, extHandler))
serverMux.UnlistedHandle(extBasePath, http.RedirectHandler(extPath, http.StatusMovedPermanently))
}
}
func (c *completedConfig) addWebConsoleConfig(serverMux *genericmux.PathRecorderMux) error {
masterURL, err := url.Parse(c.ExtraConfig.Options.MasterPublicURL)
if err != nil {
return err
}
// Generated web console config and server version
config := assets.WebConsoleConfig{
APIGroupAddr: masterURL.Host,
APIGroupPrefix: server.APIGroupPrefix,
MasterAddr: masterURL.Host,
MasterPrefix: api.Prefix,
KubernetesAddr: masterURL.Host,
KubernetesPrefix: server.DefaultLegacyAPIPrefix,
OAuthAuthorizeURI: oauthutil.OpenShiftOAuthAuthorizeURL(masterURL.String()),
OAuthTokenURI: oauthutil.OpenShiftOAuthTokenURL(masterURL.String()),
OAuthRedirectBase: c.ExtraConfig.Options.PublicURL,
OAuthClientID: OpenShiftWebConsoleClientID,
LogoutURI: c.ExtraConfig.Options.LogoutURL,
LoggingURL: c.ExtraConfig.Options.LoggingPublicURL,
MetricsURL: c.ExtraConfig.Options.MetricsPublicURL,
LimitRequestOverrides: c.ExtraConfig.LimitRequestOverrides,
}
kVersionInfo := kversion.Get()
oVersionInfo := oversion.Get()
versionInfo := assets.WebConsoleVersion{
KubernetesVersion: kVersionInfo.GitVersion,
OpenShiftVersion: oVersionInfo.GitVersion,
}
extensionProps := assets.WebConsoleExtensionProperties{
ExtensionProperties: extensionPropertyArray(c.ExtraConfig.Options.ExtensionProperties),
}
configPath := path.Join(c.ExtraConfig.PublicURL.Path, "config.js")
configHandler, err := assets.GeneratedConfigHandler(config, versionInfo, extensionProps)
configHandler = assets.SecurityHeadersHandler(configHandler)
if err != nil {
return err
}
serverMux.UnlistedHandle(configPath, assets.GzipHandler(configHandler))
return nil
}
func (c completedConfig) buildAssetHandler() (http.Handler, error) {
assets.RegisterMimeTypes()
assetFunc := assets.JoinAssetFuncs(assets.Asset, java.Asset)
assetDirFunc := assets.JoinAssetDirFuncs(assets.AssetDir, java.AssetDir)
handler := http.FileServer(&assetfs.AssetFS{Asset: assetFunc, AssetDir: assetDirFunc, Prefix: ""})
// Map of context roots (no leading or trailing slash) to the asset path to serve for requests to a missing asset
subcontextMap := map[string]string{
"": "index.html",
"java": "java/index.html",
}
var err error
handler, err = assets.HTML5ModeHandler(c.ExtraConfig.PublicURL.Path, subcontextMap, handler, assetFunc)
if err != nil {
return nil, err
}
// Cache control should happen after all Vary headers are added, but before
// any asset related routing (HTML5ModeHandler and FileServer)
handler = assets.CacheControlHandler(oversion.Get().GitCommit, handler)
handler = assets.SecurityHeadersHandler(handler)
// Gzip first so that inner handlers can react to the addition of the Vary header
handler = assets.GzipHandler(handler)
return handler, nil
}
// Have to convert to arrays because go templates are limited and we need to be able to know
// if we are on the last index for trailing commas in JSON
func extensionPropertyArray(extensionProperties map[string]string) []assets.WebConsoleExtensionProperty {
extensionPropsArray := []assets.WebConsoleExtensionProperty{}
for key, value := range extensionProperties {
extensionPropsArray = append(extensionPropsArray, assets.WebConsoleExtensionProperty{
Key: key,
Value: value,
})
}
return extensionPropsArray
}
// Run starts an http server for the static assets listening on the configured
// bind address
func RunAssetServer(assetServer *AssetServer, stopCh <-chan struct{}) error {
go assetServer.GenericAPIServer.PrepareRun().Run(stopCh)
glog.Infof("Web console listening at https://%s", assetServer.GenericAPIServer.SecureServingInfo.BindAddress)
glog.Infof("Web console available at %s", assetServer.PublicURL.String())
// Attempt to verify the server came up for 20 seconds (100 tries * 100ms, 100ms timeout per try)
return cmdutil.WaitForSuccessfulDial(true, assetServer.GenericAPIServer.SecureServingInfo.BindNetwork, assetServer.GenericAPIServer.SecureServingInfo.BindAddress, 100*time.Millisecond, 100*time.Millisecond, 100)
}
// If we know the location of the asset server, redirect to it when / is requested
// and the Accept header supports text/html
func WithAssetServerRedirect(handler http.Handler, assetPublicURL string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if req.URL.Path == "/" {
if httprequest.PrefersHTML(req) {
http.Redirect(w, req, assetPublicURL, http.StatusFound)
}
}
// Dispatch to the next handler
handler.ServeHTTP(w, req)
})
}