This repository has been archived by the owner on Jan 24, 2023. It is now read-only.
/
main.go
207 lines (160 loc) · 5.92 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
package cloudfoundry
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/url"
"strings"
"errors"
"github.com/SUSE/stratos-ui/config"
"github.com/SUSE/stratos-ui/repository/interfaces"
log "github.com/Sirupsen/logrus"
"github.com/labstack/echo"
)
type CloudFoundrySpecification struct {
portalProxy interfaces.PortalProxy
endpointType string
}
const (
EndpointType = "cf"
CLIENT_ID_KEY = "CF_CLIENT"
)
func Init(portalProxy interfaces.PortalProxy) (interfaces.StratosPlugin, error) {
return &CloudFoundrySpecification{portalProxy: portalProxy, endpointType: EndpointType}, nil
}
func (c *CloudFoundrySpecification) GetEndpointPlugin() (interfaces.EndpointPlugin, error) {
return c, nil
}
func (c *CloudFoundrySpecification) GetRoutePlugin() (interfaces.RoutePlugin, error) {
return c, nil
}
func (c *CloudFoundrySpecification) GetMiddlewarePlugin() (interfaces.MiddlewarePlugin, error) {
return nil, errors.New("Not implemented!")
}
func (c *CloudFoundrySpecification) GetType() string {
return EndpointType
}
func (c *CloudFoundrySpecification) GetClientId() string {
if clientId, err := config.GetValue(CLIENT_ID_KEY); err == nil {
return clientId
}
return "cf"
}
func (c *CloudFoundrySpecification) Register(echoContext echo.Context) error {
log.Info("CloudFoundry Register...")
return c.portalProxy.RegisterEndpoint(echoContext, c.Info)
}
func (c *CloudFoundrySpecification) Connect(ec echo.Context, cnsiRecord interfaces.CNSIRecord, userId string) (*interfaces.TokenRecord, bool, error) {
log.Info("CloudFoundry Connect...")
connectType := ec.FormValue("connect_type")
if len(connectType) == 0 {
connectType = interfaces.AuthConnectTypeCreds
}
if connectType != interfaces.AuthConnectTypeCreds {
return nil, false, errors.New("Only username/password accepted for Cloud Foundry endpoints")
}
cfAdmin := false
tokenRecord, err := c.portalProxy.ConnectOAuth2(ec, cnsiRecord)
if err != nil {
return nil, false, err
}
userTokenInfo, err := c.portalProxy.GetUserTokenInfo(tokenRecord.AuthToken)
if err == nil {
cfAdmin = strings.Contains(strings.Join(userTokenInfo.Scope, ""), c.portalProxy.GetConfig().CFAdminIdentifier)
}
return tokenRecord, cfAdmin, nil
}
func (c *CloudFoundrySpecification) Init() error {
// Add login hook to automatically register and connect to the Cloud Foundry when the user logs in
c.portalProxy.GetConfig().LoginHook = c.cfLoginHook
return nil
}
func (c *CloudFoundrySpecification) cfLoginHook(context echo.Context) error {
cfAPI, cfCnsi, err := c.fetchAutoRegisterEndpoint()
// CF auto reg url missing, continue as normal
if cfAPI == "" {
return nil
}
// CF auto reg cnsi entry missing, attempt to register
if cfCnsi.CNSIType == "" {
log.Infof("Auto-registering cloud foundry endpoint %s", cfAPI)
cfEndpointSpec, _ := c.portalProxy.GetEndpointTypeSpec("cf")
// Auto-register the Cloud Foundry
cfCnsi, err = c.portalProxy.DoRegisterEndpoint("Cloud Foundry", cfAPI, true, cfEndpointSpec.Info)
if err != nil {
log.Fatal("Could not auto-register Cloud Foundry endpoint", err)
return nil
}
if c.portalProxy.GetConfig().CloudFoundryInfo != nil {
c.portalProxy.GetConfig().CloudFoundryInfo.EndpointGUID = cfCnsi.GUID
}
} else {
log.Infof("Found existing cloud foundry endpoint matching %s. Will not auto-register", cfAPI)
}
log.Infof("Determining if user should auto-connect to %s.", cfAPI)
userGUID, err := c.portalProxy.GetSessionStringValue(context, "user_id")
if err != nil {
return fmt.Errorf("Could not determine user_id from session: %s", err)
}
cfTokenRecord, ok := c.portalProxy.GetCNSITokenRecordWithDisconnected(cfCnsi.GUID, userGUID)
if ok && cfTokenRecord.Disconnected {
// There exists a record but it's been cleared. This means user has disconnected manually. Don't auto-reconnect
log.Infof("No, user should not auto-connect to auto-registered cloud foundry %s (previsouly disoconnected). ", cfAPI)
} else {
log.Infof("Yes, user should auto-connect to auto-registered cloud foundry %s.", cfAPI)
_, err := c.portalProxy.DoLoginToCNSI(context, cfCnsi.GUID)
return err
}
return nil
}
func (c *CloudFoundrySpecification) fetchAutoRegisterEndpoint() (string, interfaces.CNSIRecord, error) {
cfAPI := c.portalProxy.GetConfig().AutoRegisterCFUrl
if cfAPI == "" {
return "", interfaces.CNSIRecord{}, nil
}
// Error is populated if there was an error OR there was no record
cfCnsi, err := c.portalProxy.GetCNSIRecordByEndpoint(cfAPI)
return cfAPI, cfCnsi, err
}
func (c *CloudFoundrySpecification) AddAdminGroupRoutes(echoGroup *echo.Group) {
// no-op
}
func (c *CloudFoundrySpecification) AddSessionGroupRoutes(echoGroup *echo.Group) {
// Firehose Stream
echoGroup.GET("/:cnsiGuid/firehose", c.firehose)
// Applications Log Streams
echoGroup.GET("/:cnsiGuid/apps/:appGuid/stream", c.appStream)
}
func (c *CloudFoundrySpecification) Info(apiEndpoint string, skipSSLValidation bool) (interfaces.CNSIRecord, interface{}, error) {
log.Debug("Info")
var v2InfoResponse interfaces.V2Info
var newCNSI interfaces.CNSIRecord
newCNSI.CNSIType = EndpointType
uri, err := url.Parse(apiEndpoint)
if err != nil {
return newCNSI, nil, err
}
uri.Path = "v2/info"
h := c.portalProxy.GetHttpClient(skipSSLValidation)
res, err := h.Get(uri.String())
if err != nil {
return newCNSI, nil, err
}
if res.StatusCode != 200 {
buf := &bytes.Buffer{}
io.Copy(buf, res.Body)
defer res.Body.Close()
return newCNSI, nil, fmt.Errorf("%s endpoint returned %d\n%s", uri.String(), res.StatusCode, buf)
}
dec := json.NewDecoder(res.Body)
if err = dec.Decode(&v2InfoResponse); err != nil {
return newCNSI, nil, err
}
newCNSI.TokenEndpoint = v2InfoResponse.TokenEndpoint
newCNSI.AuthorizationEndpoint = v2InfoResponse.AuthorizationEndpoint
newCNSI.DopplerLoggingEndpoint = v2InfoResponse.DopplerLoggingEndpoint
return newCNSI, v2InfoResponse, nil
}
func (c *CloudFoundrySpecification) UpdateMetadata(info *interfaces.Info, userGUID string, echoContext echo.Context) {
}