Merge pull request #4216 from wenchajun/notification
Add notification setting validation API
This commit is contained in:
@@ -77,6 +77,7 @@ import (
|
||||
networkv1alpha2 "kubesphere.io/kubesphere/pkg/kapis/network/v1alpha2"
|
||||
notificationv1 "kubesphere.io/kubesphere/pkg/kapis/notification/v1"
|
||||
notificationkapisv2beta1 "kubesphere.io/kubesphere/pkg/kapis/notification/v2beta1"
|
||||
notificationkapisv2beta2 "kubesphere.io/kubesphere/pkg/kapis/notification/v2beta2"
|
||||
"kubesphere.io/kubesphere/pkg/kapis/oauth"
|
||||
openpitrixv1 "kubesphere.io/kubesphere/pkg/kapis/openpitrix/v1"
|
||||
operationsv1alpha2 "kubesphere.io/kubesphere/pkg/kapis/operations/v1alpha2"
|
||||
@@ -271,6 +272,7 @@ func (s *APIServer) installKubeSphereAPIs() {
|
||||
urlruntime.Must(kubeedgev1alpha1.AddToContainer(s.container, s.Config.KubeEdgeOptions.Endpoint))
|
||||
urlruntime.Must(notificationkapisv2beta1.AddToContainer(s.container, s.InformerFactory, s.KubernetesClient.Kubernetes(),
|
||||
s.KubernetesClient.KubeSphere()))
|
||||
urlruntime.Must(notificationkapisv2beta2.AddToContainer(s.container, s.Config.NotificationOptions))
|
||||
urlruntime.Must(gatewayv1alpha1.AddToContainer(s.container, s.Config.GatewayOptions, s.RuntimeCache, s.RuntimeClient))
|
||||
}
|
||||
|
||||
|
||||
114
pkg/kapis/notification/v2beta2/handler.go
Normal file
114
pkg/kapis/notification/v2beta2/handler.go
Normal file
@@ -0,0 +1,114 @@
|
||||
package v2beta2
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
|
||||
"github.com/emicklei/go-restful"
|
||||
"kubesphere.io/api/notification/v2beta2"
|
||||
|
||||
nm "kubesphere.io/kubesphere/pkg/simple/client/notification"
|
||||
)
|
||||
|
||||
const (
|
||||
VerificationAPIPath = "/api/v2/verify"
|
||||
)
|
||||
|
||||
type handler struct {
|
||||
option *nm.Options
|
||||
}
|
||||
|
||||
type Result struct {
|
||||
Code int `json:"Status"`
|
||||
Message string `json:"Message"`
|
||||
}
|
||||
type notification struct {
|
||||
Config v2beta2.Config `json:"config"`
|
||||
Receiver v2beta2.Receiver `json:"receiver"`
|
||||
}
|
||||
|
||||
func newHandler(option *nm.Options) *handler {
|
||||
return &handler{
|
||||
option,
|
||||
}
|
||||
}
|
||||
|
||||
func (h handler) Verify(request *restful.Request, response *restful.Response) {
|
||||
opt := h.option
|
||||
if opt == nil || len(opt.Endpoint) == 0 {
|
||||
response.WriteAsJson(Result{
|
||||
http.StatusBadRequest,
|
||||
"Cannot find Notification Manager endpoint",
|
||||
})
|
||||
}
|
||||
host := opt.Endpoint
|
||||
notification := notification{}
|
||||
reqBody, err := ioutil.ReadAll(request.Request.Body)
|
||||
if err != nil {
|
||||
response.WriteHeaderAndEntity(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
err = json.Unmarshal(reqBody, ¬ification)
|
||||
if err != nil {
|
||||
response.WriteHeaderAndEntity(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
receiver := notification.Receiver
|
||||
user := request.PathParameter("user")
|
||||
|
||||
if receiver.Labels["type"] == "tenant" {
|
||||
if user != receiver.Labels["user"] {
|
||||
response.WriteAsJson(Result{
|
||||
http.StatusForbidden,
|
||||
"Permission denied",
|
||||
})
|
||||
return
|
||||
}
|
||||
}
|
||||
if receiver.Labels["type"] == "global" {
|
||||
if user != "" {
|
||||
response.WriteAsJson(Result{
|
||||
http.StatusForbidden,
|
||||
"Permission denied",
|
||||
})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", fmt.Sprintf("%s%s", host, VerificationAPIPath), bytes.NewReader(reqBody))
|
||||
if err != nil {
|
||||
response.WriteHeaderAndEntity(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
req.Header = request.Request.Header
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
response.WriteHeaderAndEntity(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
// return 500
|
||||
response.WriteHeaderAndEntity(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
var result Result
|
||||
err = json.Unmarshal(body, &result)
|
||||
if err != nil {
|
||||
response.WriteHeaderAndEntity(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
response.WriteAsJson(result)
|
||||
}
|
||||
50
pkg/kapis/notification/v2beta2/register.go
Normal file
50
pkg/kapis/notification/v2beta2/register.go
Normal file
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
|
||||
Copyright 2021 The KubeSphere Authors.
|
||||
|
||||
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
|
||||
|
||||
http://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 v2beta2
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
nm "kubesphere.io/kubesphere/pkg/simple/client/notification"
|
||||
|
||||
"github.com/emicklei/go-restful"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
|
||||
"kubesphere.io/kubesphere/pkg/api"
|
||||
"kubesphere.io/kubesphere/pkg/apiserver/runtime"
|
||||
)
|
||||
|
||||
var GroupVersion = schema.GroupVersion{Group: "notification.kubesphere.io", Version: "v2beta2"}
|
||||
|
||||
func AddToContainer(container *restful.Container, option *nm.Options) error {
|
||||
h := newHandler(option)
|
||||
ws := runtime.NewWebService(GroupVersion)
|
||||
ws.Route(ws.POST("/configs/notification/verification").
|
||||
Reads("").
|
||||
To(h.Verify).
|
||||
Returns(http.StatusOK, api.StatusOK, http.Response{}.Body)).
|
||||
Doc("Provide validation for notification-manager information")
|
||||
ws.Route(ws.POST("/configs/notification/users/{user}/verification").
|
||||
To(h.Verify).
|
||||
Param(ws.PathParameter("user", "user name")).
|
||||
Returns(http.StatusOK, api.StatusOK, http.Response{}.Body)).
|
||||
Doc("Provide validation for notification-manager information")
|
||||
container.Add(ws)
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user