forked from cloudfoundry/stratos
-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.go
235 lines (199 loc) · 7.89 KB
/
main.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
package cloudfoundryhosting
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
log "github.com/Sirupsen/logrus"
"github.com/gorilla/sessions"
"github.com/labstack/echo"
"github.com/labstack/echo/engine/standard"
uuid "github.com/satori/go.uuid"
"github.com/SUSE/stratos-ui/config"
"github.com/SUSE/stratos-ui/repository/interfaces"
)
const (
VCapApplication = "VCAP_APPLICATION"
CFApiURLOverride = "CF_API_URL"
CFApiForceSecure = "CF_API_FORCE_SECURE"
cfSessionCookieName = "JSESSIONID"
ForceEndpointDashboard = "FORCE_ENDPOINT_DASHBOARD"
)
type CFHosting struct {
portalProxy interfaces.PortalProxy
endpointType string
}
func Init(portalProxy interfaces.PortalProxy) (interfaces.StratosPlugin, error) {
return &CFHosting{portalProxy: portalProxy}, nil
}
func (ch *CFHosting) GetMiddlewarePlugin() (interfaces.MiddlewarePlugin, error) {
if config.IsSet(VCapApplication) {
return ch, nil
}
return nil, errors.New("Not running as a Cloud Foundry application")
}
func (ch *CFHosting) GetEndpointPlugin() (interfaces.EndpointPlugin, error) {
return nil, errors.New("Not implemented!")
}
func (ch *CFHosting) GetRoutePlugin() (interfaces.RoutePlugin, error) {
return nil, errors.New("Not implemented!")
}
func (ch *CFHosting) Init() error {
// Determine if we are running CF by presence of env var "VCAP_APPLICATION" and configure appropriately
if config.IsSet(VCapApplication) {
log.Info("Detected that Console is deployed as a Cloud Foundry Application")
ch.portalProxy.GetConfig().ConsoleConfig = new(interfaces.ConsoleConfig)
// We are using the CF UAA - so the Console must use the same Client and Secret as CF
ch.portalProxy.GetConfig().ConsoleConfig.ConsoleClient = ch.portalProxy.GetConfig().CFClient
ch.portalProxy.GetConfig().ConsoleConfig.ConsoleClientSecret = ch.portalProxy.GetConfig().CFClientSecret
// Ensure that the identifier for an admin is the standard Cloud Foundry one
ch.portalProxy.GetConfig().ConsoleConfig.ConsoleAdminScope = ch.portalProxy.GetConfig().CFAdminIdentifier
// Allow Console Application manifest to override the Admin Scope if really desired
if config.IsSet("STRATOS_ADMIN_SCOPE") {
stratosAdminScope, err := config.GetValue("STRATOS_ADMIN_SCOPE")
if err == nil {
ch.portalProxy.GetConfig().ConsoleConfig.ConsoleAdminScope = stratosAdminScope
log.Infof("Overriden Console Admin Scope to: %s", stratosAdminScope)
}
}
// Need to run as HTTP on the port we were told to use
ch.portalProxy.GetConfig().HTTPS = false
if config.IsSet("PORT") {
port, err := config.GetValue("PORT")
if err != nil {
log.Warnf("Unable to read Port")
} else {
ch.portalProxy.GetConfig().TLSAddress = ":" + port
log.Infof("Updated Console address to: %s", ch.portalProxy.GetConfig().TLSAddress)
}
}
// Get the cf_api value from the JSON
var appData interfaces.VCapApplicationData
vCapApp, _ := config.GetValue(VCapApplication)
data := []byte(vCapApp)
err := json.Unmarshal(data, &appData)
if err != nil {
log.Fatal("Could not get the Cloud Foundry API URL", err)
return nil
}
log.Infof("CF API URL: %s", appData.API)
// Allow the URL to be overridden by an application environment variable
if config.IsSet(CFApiURLOverride) {
apiUrl, _ := config.GetValue(CFApiURLOverride)
appData.API = apiUrl
log.Infof("Overriden CF API URL from environment variable %s", apiUrl)
}
if config.IsSet(CFApiForceSecure) {
// Force the API URL protocol to be https
appData.API = strings.Replace(appData.API, "http://", "https://", 1)
log.Infof("Ensuring that CF API URL is accessed over HTTPS")
} else {
log.Info("No forced override to HTTPS")
}
disableEndpointDashboard := true
if config.IsSet(ForceEndpointDashboard) {
// Force the Endpoint Dashboard to be visible?
if forceStr, err := config.GetValue(ForceEndpointDashboard); err == nil {
if force, err := strconv.ParseBool(forceStr); err == nil {
disableEndpointDashboard = !force
}
}
}
if disableEndpointDashboard {
log.Info("Endpoint Dashboard has been DISABLED")
} else {
log.Info("Endpoint Dashboard has been ENABLED")
}
ch.portalProxy.GetConfig().PluginConfig["endpointsDashboardDisabled"] = strconv.FormatBool(disableEndpointDashboard)
log.Infof("Using Cloud Foundry API URL: %s", appData.API)
cfEndpointSpec, _ := ch.portalProxy.GetEndpointTypeSpec("cf")
newCNSI, _, err := cfEndpointSpec.Info(appData.API, true)
if err != nil {
log.Fatal("Could not get the info for Cloud Foundry", err)
return nil
}
// Override the configuration to set the authorization endpoint
url, err := url.Parse(newCNSI.AuthorizationEndpoint)
if err != nil {
return fmt.Errorf("Invalid authorization endpoint URL %s %s", newCNSI.AuthorizationEndpoint, err)
}
ch.portalProxy.GetConfig().ConsoleConfig.UAAEndpoint = url
log.Infof("Cloud Foundry UAA is: %s", ch.portalProxy.GetConfig().ConsoleConfig.UAAEndpoint)
skipSsl, err := config.GetValue("SKIP_SSL_VALIDATION")
if err != nil {
// Not set in the environment and failed to read from the Secrets file
ch.portalProxy.GetConfig().ConsoleConfig.SkipSSLValidation = false
}
skipSslBool, err := strconv.ParseBool(skipSsl)
if err != nil {
// Not set in the environment and failed to read from the Secrets file
ch.portalProxy.GetConfig().ConsoleConfig.SkipSSLValidation = false
}
ch.portalProxy.GetConfig().ConsoleConfig.SkipSSLValidation = skipSslBool
// Save to Console DB
err = ch.portalProxy.SaveConsoleConfig(ch.portalProxy.GetConfig().ConsoleConfig, nil)
if err != nil {
log.Fatalf("Failed to save console configuration due to %s", err)
return fmt.Errorf("Failed to save console configuration due to %s", err)
}
log.Info("Setting AUTO_REG_CF_URL config to ", appData.API)
ch.portalProxy.GetConfig().AutoRegisterCFUrl = appData.API
// Store the space and id of the ConsocfLoginHookle application - we can use these to prevent stop/delete in the front-end
ch.portalProxy.GetConfig().CloudFoundryInfo = &interfaces.CFInfo{
SpaceGUID: appData.SpaceID,
AppGUID: appData.ApplicationID,
}
log.Info("All done for Cloud Foundry deployment")
}
return nil
}
func (ch *CFHosting) EchoMiddleware(h echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
// If request is a WebSocket request, don't do anything special
if c.Request().Header().Contains("Upgrade") &&
c.Request().Header().Contains("Sec-Websocket-Key") {
log.Infof("Not redirecting this request")
return h(c)
}
// Check that we are on HTTPS - redirect if not
if c.Request().Header().Contains("X-Forwarded-Proto") {
proto := c.Request().Header().Get("X-Forwarded-Proto")
if proto != "https" {
redirect := fmt.Sprintf("https://%s%s", c.Request().Host(), c.Request().URI())
return c.Redirect(301, redirect)
}
return h(c)
}
return interfaces.NewHTTPShadowError(
http.StatusBadRequest,
"X-Forwarded-Proto not found and is required",
"X-Forwarded-Proto not found and is required",
)
}
}
// For cloud foundry session affinity
// Ensure we add a cookie named "JSESSIONID" for Cloud Foundry session affinity
func (ch *CFHosting) SessionEchoMiddleware(h echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
// Make sure there is a JSESSIONID cookie set to the session ID
session, err := ch.portalProxy.GetSession(c)
if err == nil {
// We have a session
guid, err := ch.portalProxy.GetSessionValue(c, cfSessionCookieName)
if err != nil || guid == nil {
guid = uuid.NewV4().String()
session.Values[cfSessionCookieName] = guid
ch.portalProxy.SaveSession(c, session)
}
sessionGUID := fmt.Sprintf("%s", guid)
// Set the JSESSIONID coolie for Cloud Foundry session affinity
w := c.Response().(*standard.Response).ResponseWriter
cookie := sessions.NewCookie(cfSessionCookieName, sessionGUID, session.Options)
http.SetCookie(w, cookie)
}
return h(c)
}
}