1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-28 13:48:49 +08:00
Mainflux.mainflux/pkg/sdk/go/bootstrap.go
Drasko DRASKOVIC d73a5d53fe
MF 1413 - Use per-service URL in SDK (#1444)
* Use per-service URL in SDK

Signed-off-by: Drasko DRASKOVIC <drasko.draskovic@gmail.com>

* Fix CLI

Signed-off-by: Drasko Draskovic <drasko.draskovic@gmail.com>

* Fix CLI messaging

Signed-off-by: Drasko Draskovic <drasko.draskovic@gmail.com>

* Fix message tests

Signed-off-by: Drasko Draskovic <drasko.draskovic@gmail.com>

* Simplify Bootstrap

Signed-off-by: Drasko Draskovic <drasko.draskovic@gmail.com>

* Update API doc and responses

Signed-off-by: Drasko Draskovic <drasko.draskovic@gmail.com>

* fix failing certs, bootstrap tests

Signed-off-by: mteodor <mirko.teodorovic@gmail.com>

* fix failing certs, bootstrap tests

Signed-off-by: mteodor <mirko.teodorovic@gmail.com>

* Fix tests and rename to auth service

Signed-off-by: Drasko Draskovic <drasko.draskovic@gmail.com>

* Clean the code

Signed-off-by: Drasko Draskovic <drasko.draskovic@gmail.com>

* Remove unnecessary Repository logs

Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>

* Always return error in case of repo failure

Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>

* Cleanup SDK and CLI

Update tests, remove linter warnings, remove dead code.

Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>

* Clean the code

Signed-off-by: Drasko Draskovic <drasko.draskovic@gmail.com>

* Undo Bootstrap changes

Signed-off-by: Drasko Draskovic <drasko.draskovic@gmail.com>

* Fix tests

Signed-off-by: Drasko Draskovic <drasko.draskovic@gmail.com>

* Fix linter

Signed-off-by: Drasko Draskovic <drasko.draskovic@gmail.com>

Co-authored-by: mteodor <mirko.teodorovic@gmail.com>
Co-authored-by: dusanb94 <dusan.borovcanin@mainflux.com>
2021-08-11 16:58:10 +02:00

234 lines
6.1 KiB
Go

// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0
package sdk
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"github.com/mainflux/mainflux/pkg/errors"
)
const configsEndpoint = "configs"
const bootstrapEndpoint = "bootstrap"
const whitelistEndpoint = "state"
const bootstrapCertsEndpoint = "configs/certs"
// BootstrapConfig represents Configuration entity. It wraps information about external entity
// as well as info about corresponding Mainflux entities.
// MFThing represents corresponding Mainflux Thing ID.
// MFKey is key of corresponding Mainflux Thing.
// MFChannels is a list of Mainflux Channels corresponding Mainflux Thing connects to.
type BootstrapConfig struct {
ThingID string `json:"thing_id,omitempty"`
Channels []string `json:"channels,omitempty"`
ExternalID string `json:"external_id,omitempty"`
ExternalKey string `json:"external_key,omitempty"`
MFThing string `json:"mainflux_id,omitempty"`
MFChannels []Channel `json:"mainflux_channels,omitempty"`
MFKey string `json:"mainflux_key,omitempty"`
Name string `json:"name,omitempty"`
ClientCert string `json:"client_cert,omitempty"`
ClientKey string `json:"client_key,omitempty"`
CACert string `json:"ca_cert,omitempty"`
Content string `json:"content,omitempty"`
State int `json:"state,omitempty"`
}
type ConfigUpdateCertReq struct {
ClientCert string `json:"client_cert"`
ClientKey string `json:"client_key"`
CACert string `json:"ca_cert"`
}
func (sdk mfSDK) AddBootstrap(token string, cfg BootstrapConfig) (string, error) {
data, err := json.Marshal(cfg)
if err != nil {
return "", err
}
url := fmt.Sprintf("%s/%s", sdk.bootstrapURL, configsEndpoint)
req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(data))
if err != nil {
return "", err
}
resp, err := sdk.sendRequest(req, token, string(CTJSON))
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusCreated && resp.StatusCode != http.StatusOK {
return "", errors.Wrap(ErrFailedCreation, errors.New(resp.Status))
}
id := strings.TrimPrefix(resp.Header.Get("Location"), "/things/configs/")
return id, nil
}
func (sdk mfSDK) Whitelist(token string, cfg BootstrapConfig) error {
data, err := json.Marshal(BootstrapConfig{State: cfg.State})
if err != nil {
return errors.Wrap(ErrFailedWhitelist, err)
}
if cfg.MFThing == "" {
return ErrFailedWhitelist
}
url := fmt.Sprintf("%s/%s/%s", sdk.bootstrapURL, whitelistEndpoint, cfg.MFThing)
req, err := http.NewRequest(http.MethodPut, url, bytes.NewReader(data))
if err != nil {
return errors.Wrap(ErrFailedWhitelist, err)
}
resp, err := sdk.sendRequest(req, token, string(CTJSON))
if err != nil {
return errors.Wrap(ErrFailedWhitelist, err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusCreated && resp.StatusCode != http.StatusOK {
return errors.Wrap(ErrFailedWhitelist, errors.New(resp.Status))
}
return nil
}
func (sdk mfSDK) ViewBootstrap(token, id string) (BootstrapConfig, error) {
url := fmt.Sprintf("%s/%s/%s", sdk.bootstrapURL, configsEndpoint, id)
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return BootstrapConfig{}, err
}
resp, err := sdk.sendRequest(req, token, string(CTJSON))
if err != nil {
return BootstrapConfig{}, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return BootstrapConfig{}, err
}
if resp.StatusCode != http.StatusOK {
return BootstrapConfig{}, errors.Wrap(ErrFailedFetch, errors.New(resp.Status))
}
var bc BootstrapConfig
if err := json.Unmarshal(body, &bc); err != nil {
return BootstrapConfig{}, err
}
return bc, nil
}
func (sdk mfSDK) UpdateBootstrap(token string, cfg BootstrapConfig) error {
data, err := json.Marshal(cfg)
if err != nil {
return err
}
url := fmt.Sprintf("%s/%s/%s", sdk.bootstrapURL, configsEndpoint, cfg.MFThing)
req, err := http.NewRequest(http.MethodPut, url, bytes.NewReader(data))
if err != nil {
return err
}
resp, err := sdk.sendRequest(req, token, string(CTJSON))
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return errors.Wrap(ErrFailedUpdate, errors.New(resp.Status))
}
return nil
}
func (sdk mfSDK) UpdateBootstrapCerts(token, id, clientCert, clientKey, ca string) error {
url := fmt.Sprintf("%s/%s/%s", sdk.bootstrapURL, bootstrapCertsEndpoint, id)
request := ConfigUpdateCertReq{
ClientCert: clientCert,
ClientKey: clientKey,
CACert: ca,
}
data, err := json.Marshal(request)
if err != nil {
return errors.Wrap(ErrFailedCertUpdate, err)
}
req, err := http.NewRequest(http.MethodPatch, url, bytes.NewReader(data))
if err != nil {
return err
}
resp, err := sdk.sendRequest(req, token, string(CTJSON))
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return errors.Wrap(ErrFailedCertUpdate, errors.New(resp.Status))
}
return nil
}
func (sdk mfSDK) RemoveBootstrap(token, id string) error {
url := fmt.Sprintf("%s/%s/%s", sdk.bootstrapURL, configsEndpoint, id)
req, err := http.NewRequest(http.MethodDelete, url, nil)
if err != nil {
return err
}
resp, err := sdk.sendRequest(req, token, string(CTJSON))
if err != nil {
return err
}
if resp.StatusCode != http.StatusNoContent {
return errors.Wrap(ErrFailedRemoval, errors.New(resp.Status))
}
return nil
}
func (sdk mfSDK) Bootstrap(externalKey, externalID string) (BootstrapConfig, error) {
url := fmt.Sprintf("%s/%s/%s", sdk.bootstrapURL, bootstrapEndpoint, externalID)
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return BootstrapConfig{}, err
}
resp, err := sdk.sendRequest(req, externalKey, string(CTJSON))
if err != nil {
return BootstrapConfig{}, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return BootstrapConfig{}, err
}
if resp.StatusCode != http.StatusOK {
return BootstrapConfig{}, errors.Wrap(ErrFailedFetch, errors.New(resp.Status))
}
var bc BootstrapConfig
if err := json.Unmarshal(body, &bc); err != nil {
return BootstrapConfig{}, err
}
return bc, nil
}