-
Notifications
You must be signed in to change notification settings - Fork 141
/
management-api.go
179 lines (145 loc) · 5.13 KB
/
management-api.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
/*
Copyright NetFoundry Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package api_impl
import (
"crypto/x509"
"fmt"
"github.com/go-openapi/loads"
"github.com/gorilla/websocket"
"github.com/michaelquigley/pfxlog"
"github.com/openziti/channel/v2"
"github.com/openziti/channel/v2/websockets"
"github.com/openziti/ziti/controller/handler_mgmt"
"github.com/openziti/ziti/controller/network"
"github.com/openziti/ziti/controller/xmgmt"
"github.com/openziti/ziti/controller/rest_client"
"github.com/openziti/ziti/controller/rest_server"
"github.com/openziti/ziti/controller/rest_server/operations"
"github.com/openziti/identity"
"github.com/openziti/xweb/v2"
"net/http"
"strings"
)
const (
ServerHeader = "server"
)
var _ xweb.ApiHandlerFactory = &ManagementApiFactory{}
type ManagementApiFactory struct {
InitFunc func(managementApi *ManagementApiHandler) error
network *network.Network
nodeId identity.Identity
xmgmts []xmgmt.Xmgmt
}
func (factory *ManagementApiFactory) Validate(_ *xweb.InstanceConfig) error {
return nil
}
func NewManagementApiFactory(nodeId identity.Identity, network *network.Network, xmgmts []xmgmt.Xmgmt) *ManagementApiFactory {
return &ManagementApiFactory{
network: network,
nodeId: nodeId,
xmgmts: xmgmts,
}
}
func (factory *ManagementApiFactory) Binding() string {
return FabricApiBinding
}
func (factory *ManagementApiFactory) New(_ *xweb.ServerConfig, options map[interface{}]interface{}) (xweb.ApiHandler, error) {
managementSpec, err := loads.Embedded(rest_server.SwaggerJSON, rest_server.FlatSwaggerJSON)
if err != nil {
pfxlog.Logger().Fatalln(err)
}
fabricAPI := operations.NewZitiFabricAPI(managementSpec)
fabricAPI.ServeError = ServeError
if requestWrapper == nil {
requestWrapper = &FabricRequestWrapper{
nodeId: factory.nodeId,
network: factory.network,
}
}
for _, router := range routers {
router.Register(fabricAPI, requestWrapper)
}
managementApiHandler, err := NewManagementApiHandler(fabricAPI, options)
if err != nil {
return nil, err
}
managementApiHandler.bindHandler = handler_mgmt.NewBindHandler(factory.network, factory.xmgmts)
if factory.InitFunc != nil {
if err := factory.InitFunc(managementApiHandler); err != nil {
return nil, fmt.Errorf("error running on init func: %v", err)
}
}
return managementApiHandler, nil
}
func NewManagementApiHandler(fabricApi *operations.ZitiFabricAPI, options map[interface{}]interface{}) (*ManagementApiHandler, error) {
managementApi := &ManagementApiHandler{
fabricApi: fabricApi,
options: options,
}
managementApi.handler = managementApi.newHandler()
managementApi.wsHandler = requestWrapper.WrapWsHandler(http.HandlerFunc(managementApi.handleWebSocket))
managementApi.wsUrl = rest_client.DefaultBasePath + "/ws-api"
return managementApi, nil
}
type ManagementApiHandler struct {
fabricApi *operations.ZitiFabricAPI
handler http.Handler
wsHandler http.Handler
wsUrl string
options map[interface{}]interface{}
bindHandler channel.BindHandler
}
func (managementApi *ManagementApiHandler) Binding() string {
return FabricApiBinding
}
func (managementApi *ManagementApiHandler) Options() map[interface{}]interface{} {
return managementApi.options
}
func (managementApi *ManagementApiHandler) RootPath() string {
return rest_client.DefaultBasePath
}
func (managementApi *ManagementApiHandler) IsHandler(r *http.Request) bool {
return strings.HasPrefix(r.URL.Path, managementApi.RootPath())
}
func (managementApi *ManagementApiHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
if request.URL.Path == managementApi.wsUrl {
managementApi.wsHandler.ServeHTTP(writer, request)
} else {
managementApi.handler.ServeHTTP(writer, request)
}
}
func (managementApi *ManagementApiHandler) newHandler() http.Handler {
innerManagementHandler := managementApi.fabricApi.Serve(nil)
return requestWrapper.WrapHttpHandler(innerManagementHandler)
}
func (managementApi *ManagementApiHandler) handleWebSocket(writer http.ResponseWriter, request *http.Request) {
log := pfxlog.Logger()
log.Debug("handling mgmt channel websocket upgrade")
upgrader := websocket.Upgrader{}
conn, err := upgrader.Upgrade(writer, request, nil)
if err != nil {
log.WithError(err).Error("unable to upgrade request to websocket")
return
}
var certs []*x509.Certificate
if request.TLS != nil {
certs = request.TLS.PeerCertificates
}
id := &identity.TokenId{Token: "mgmt"}
underlayFactory := websockets.NewUnderlayFactory(id, conn, certs)
_, err = channel.NewChannel("mgmt", underlayFactory, managementApi.bindHandler, nil)
if err != nil {
log.WithError(err).Error("unable to create channel over websocket")
return
}
}