-
Notifications
You must be signed in to change notification settings - Fork 113
/
handler.go
162 lines (135 loc) · 5.19 KB
/
handler.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
package kubeconfig
import (
"fmt"
"net/http"
"strings"
"github.com/kyma-project/control-plane/components/kyma-environment-broker/internal/broker"
"github.com/kennygrant/sanitize"
"github.com/kyma-project/control-plane/components/kyma-environment-broker/common/orchestration"
"github.com/kyma-project/control-plane/components/kyma-environment-broker/internal"
"github.com/kyma-project/control-plane/components/kyma-environment-broker/internal/httputil"
"github.com/kyma-project/control-plane/components/kyma-environment-broker/internal/storage"
"github.com/kyma-project/control-plane/components/kyma-environment-broker/internal/storage/dberr"
"github.com/gorilla/mux"
"github.com/pivotal-cf/brokerapi/v8/domain"
"github.com/sirupsen/logrus"
)
const attachmentName = "kubeconfig.yaml"
//go:generate mockery --name=KcBuilder --output=automock --outpkg=automock --case=underscore
type KcBuilder interface {
Build(*internal.Instance) (string, error)
BuildFromAdminKubeconfig(instance *internal.Instance, adminKubeconfig string) (string, error)
}
type Handler struct {
kubeconfigBuilder KcBuilder
allowOrigins string
instanceStorage storage.Instances
operationStorage storage.Operations
log logrus.FieldLogger
}
func NewHandler(storage storage.BrokerStorage, b KcBuilder, origins string, log logrus.FieldLogger) *Handler {
return &Handler{
instanceStorage: storage.Instances(),
operationStorage: storage.Operations(),
kubeconfigBuilder: b,
allowOrigins: origins,
log: log,
}
}
func (h *Handler) AttachRoutes(router *mux.Router) {
router.HandleFunc("/kubeconfig/{instance_id}", h.GetKubeconfig).Methods(http.MethodGet)
router.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
h.handleResponse(w, http.StatusNotFound, fmt.Errorf("instanceID is required"))
})
}
type ErrorResponse struct {
Error string
}
func (h *Handler) GetKubeconfig(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
instanceID := vars["instance_id"]
h.specifyAllowOriginHeader(r, w)
instance, err := h.instanceStorage.GetByID(instanceID)
switch {
case err == nil:
case dberr.IsNotFound(err):
h.handleResponse(w, http.StatusNotFound, fmt.Errorf("instance with ID %s does not exist", instanceID))
return
default:
h.handleResponse(w, http.StatusInternalServerError, err)
return
}
if broker.IsOwnClusterPlan(instance.ServicePlanID) {
h.handleResponse(w, http.StatusNotFound, fmt.Errorf("kubeconfig for instance %s does not exist", instanceID))
return
}
if instance.RuntimeID == "" {
h.handleResponse(w, http.StatusNotFound, fmt.Errorf("kubeconfig for instance %s does not exist. Provisioning could be in progress, please try again later", instanceID))
return
}
operation, err := h.operationStorage.GetProvisioningOperationByInstanceID(instanceID)
switch {
case err == nil:
case dberr.IsNotFound(err):
h.handleResponse(w, http.StatusNotFound, fmt.Errorf("provisioning operation for instance with ID %s does not exist", instanceID))
return
default:
h.handleResponse(w, http.StatusInternalServerError, err)
return
}
if operation.InstanceID != instanceID {
h.handleResponse(w, http.StatusBadRequest, fmt.Errorf("mismatch between operation and instance"))
return
}
switch operation.State {
case domain.InProgress, orchestration.Pending:
h.handleResponse(w, http.StatusNotFound, fmt.Errorf("provisioning operation for instance %s is in progress state, kubeconfig not exist yet, please try again later", instanceID))
return
case domain.Failed:
h.handleResponse(w, http.StatusNotFound, fmt.Errorf("provisioning operation for instance %s failed, kubeconfig does not exist", instanceID))
return
}
var newKubeconfig string
if instance.ServicePlanID == broker.OwnClusterPlanID {
newKubeconfig, err = h.kubeconfigBuilder.BuildFromAdminKubeconfig(instance, instance.InstanceDetails.Kubeconfig)
} else {
newKubeconfig, err = h.kubeconfigBuilder.Build(instance)
}
if err != nil {
h.handleResponse(w, http.StatusInternalServerError, fmt.Errorf("cannot fetch SKR kubeconfig: %s", err))
return
}
writeToResponse(w, newKubeconfig, h.log)
}
func (h *Handler) handleResponse(w http.ResponseWriter, code int, err error) {
errEncode := httputil.JSONEncodeWithCode(w, &ErrorResponse{Error: err.Error()}, code)
if errEncode != nil {
h.log.Errorf("cannot encode error response: %s", errEncode)
}
}
func (h *Handler) specifyAllowOriginHeader(r *http.Request, w http.ResponseWriter) {
origin := r.Header.Get("Origin")
origin = strings.ReplaceAll(origin, "\r", "")
origin = strings.ReplaceAll(origin, "\n", "")
if origin == "" {
return
}
if h.allowOrigins == "*" {
w.Header().Set("Access-Control-Allow-Origin", "*")
return
}
for _, o := range strings.Split(h.allowOrigins, ",") {
if o == origin {
w.Header().Set("Access-Control-Allow-Origin", sanitize.HTML(origin))
return
}
}
}
func writeToResponse(w http.ResponseWriter, data string, l logrus.FieldLogger) {
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", attachmentName))
w.Header().Add("Content-Type", "application/x-yaml")
_, err := w.Write([]byte(data))
if err != nil {
l.Errorf("cannot write response with new kubeconfig: %s", err)
}
}