123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- package mq
- import (
- "encoding/json"
- "errors"
- "fmt"
- "io"
- "net/http"
- "strings"
- "github.com/gravitl/netmaker/servercfg"
- )
- type EmqxCloud struct {
- URL string
- AppID string
- AppSecret string
- }
- type userCreateReq struct {
- UserName string `json:"username"`
- Password string `json:"password"`
- }
- type cloudAcl struct {
- UserName string `json:"username"`
- Topic string `json:"topic"`
- Action string `json:"action"`
- Access string `json:"access"`
- }
- func (e *EmqxCloud) GetType() servercfg.Emqxdeploy { return servercfg.EmqxCloudDeploy }
- func (e *EmqxCloud) CreateEmqxUser(username, pass string, admin bool) error {
- payload := userCreateReq{
- UserName: username,
- Password: pass,
- }
- data, _ := json.Marshal(payload)
- client := &http.Client{}
- req, err := http.NewRequest(http.MethodPost, e.URL, strings.NewReader(string(data)))
- if err != nil {
- return err
- }
- req.SetBasicAuth(e.AppID, e.AppSecret)
- req.Header.Add("Content-Type", "application/json")
- res, err := client.Do(req)
- if err != nil {
- return err
- }
- defer res.Body.Close()
- body, err := io.ReadAll(res.Body)
- if err != nil {
- return err
- }
- if res.StatusCode != http.StatusOK {
- return errors.New("request failed " + string(body))
- }
- return nil
- }
- func (e *EmqxCloud) CreateEmqxDefaultAuthenticator() error { return nil } // ignore
- func (e *EmqxCloud) CreateEmqxDefaultAuthorizer() error { return nil } // ignore
- func (e *EmqxCloud) CreateDefaultDenyRule() error {
- return nil
- }
- func (e *EmqxCloud) CreateHostACL(hostID, serverName string) error {
- acls := []cloudAcl{
- {
- UserName: hostID,
- Topic: fmt.Sprintf("peers/host/%s/%s", hostID, serverName),
- Access: "allow",
- Action: "pubsub",
- },
- {
- UserName: hostID,
- Topic: fmt.Sprintf("host/update/%s/%s", hostID, serverName),
- Access: "allow",
- Action: "pubsub",
- },
- {
- UserName: hostID,
- Topic: fmt.Sprintf("host/serverupdate/%s/%s", serverName, hostID),
- Access: "allow",
- Action: "pubsub",
- },
- }
- payload, err := json.Marshal(acls)
- if err != nil {
- return err
- }
- client := &http.Client{}
- req, err := http.NewRequest(http.MethodPost, e.URL, strings.NewReader(string(payload)))
- if err != nil {
- return err
- }
- req.Header.Add("Content-Type", "application/json")
- req.SetBasicAuth(e.AppID, e.AppSecret)
- res, err := client.Do(req)
- if err != nil {
- return err
- }
- defer res.Body.Close()
- body, err := io.ReadAll(res.Body)
- if err != nil {
- return err
- }
- if res.StatusCode != http.StatusOK {
- return errors.New("request failed " + string(body))
- }
- return nil
- }
- func (e *EmqxCloud) AppendNodeUpdateACL(hostID, nodeNetwork, nodeID, serverName string) error {
- acls := []cloudAcl{
- {
- Topic: fmt.Sprintf("node/update/%s/%s", nodeNetwork, nodeID),
- Permission: "allow",
- Action: "subscribe",
- },
- {
- Topic: fmt.Sprintf("ping/%s/%s", serverName, nodeID),
- Permission: "allow",
- Action: "all",
- },
- {
- Topic: fmt.Sprintf("update/%s/%s", serverName, nodeID),
- Permission: "allow",
- Action: "all",
- },
- {
- Topic: fmt.Sprintf("signal/%s/%s", serverName, nodeID),
- Permission: "allow",
- Action: "all",
- },
- {
- Topic: fmt.Sprintf("metrics/%s/%s", serverName, nodeID),
- Permission: "allow",
- Action: "all",
- },
- }
- return nil
- }
- func (e *EmqxCloud) GetUserACL(username string) ([]cloudAcl, error) { return nil, nil }
- func (e *EmqxCloud) DeleteEmqxUser(username string) error {
- client := &http.Client{}
- req, err := http.NewRequest(http.MethodDelete, e.URL, nil)
- if err != nil {
- return err
- }
- req.SetBasicAuth(e.AppID, e.AppSecret)
- res, err := client.Do(req)
- if err != nil {
- return err
- }
- defer res.Body.Close()
- body, err := io.ReadAll(res.Body)
- if err != nil {
- return err
- }
- if res.StatusCode != http.StatusOK {
- return errors.New("request failed " + string(body))
- }
- return nil
- }
|