mirror of
https://github.com/mainflux/mainflux.git
synced 2025-04-24 13:48:49 +08:00

* adding certificate issuing Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * adding cert endpoint Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * update envs Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * update envs Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * move certs creation to sdk Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * move certs creation to sdk Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * move certs creation to sdk Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix env vars Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add comment Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * update sdk Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix vars Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add volumes Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix merge config for int Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove env Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix error handling Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add cert test, change receiver to pointer Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add docs Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix var naming Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * correct error naming Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * adding certs service Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add certs Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add certs Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * change func receiever Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add default cert issue method Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add config Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * small fix Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove some testing code Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add cert issue Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add vault api client Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * additional endpoints Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add swagger for certs Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove certs from provision Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * clean provision from certs Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add list certificates endpoint Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add vault api in vendor Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add certs Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add revoke, fix bugs Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix sdk for certs Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * minor changes, add env, doc Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * minor changes, add env, doc Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * minor changes, add env, doc Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * small changes Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove CA for signing from provision Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add docker file for certs Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix mock sdk Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add line Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix RevokeCert Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * renam ENV Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove tests temporarily Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix naming Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * renam vars Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add cli for issue cert Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add cli for issue cert Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add cli for issue cert Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add cli for issue cert Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove not needed envs Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix linter errors, add cli Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix linter errors, add cli, var rename Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix reviews, add viewcert, fix view all certs Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove view cert, as it will be retrieved from PKI Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * change endpoints Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add default env val Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove some errors Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * refactor, make wrapper lib for vault Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * refactor, make wrapper lib for vault Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * refactor, make wrapper lib for vault Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix revoking Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * refactor, make wrapper lib for vault Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * update vendor Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix comment Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * add comments Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove unused Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove unused field Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * update vendor Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * refactor pki Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * refactor pki Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * refactor pki, update vendor Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * refactor pki Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix comment Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * minor fix Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * remove methods, use fields Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix comments and package desc Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com> * fix comments and package desc Signed-off-by: Mirko Teodorovic <mirko.teodorovic@gmail.com>
279 lines
8.6 KiB
Go
279 lines
8.6 KiB
Go
// Copyright (c) Mainflux
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package sdk
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
)
|
|
|
|
const (
|
|
// CTJSON represents JSON content type.
|
|
CTJSON ContentType = "application/json"
|
|
|
|
// CTJSONSenML represents JSON SenML content type.
|
|
CTJSONSenML ContentType = "application/senml+json"
|
|
|
|
// CTBinary represents binary content type.
|
|
CTBinary ContentType = "application/octet-stream"
|
|
)
|
|
|
|
const minPassLen = 8
|
|
|
|
var (
|
|
// ErrUnauthorized indicates that entity creation failed.
|
|
ErrUnauthorized = errors.New("unauthorized, missing credentials")
|
|
|
|
// ErrFailedCreation indicates that entity creation failed.
|
|
ErrFailedCreation = errors.New("failed to create entity")
|
|
|
|
// ErrFailedUpdate indicates that entity update failed.
|
|
ErrFailedUpdate = errors.New("failed to update entity")
|
|
|
|
// ErrFailedFetch indicates that fetching of entity data failed.
|
|
ErrFailedFetch = errors.New("failed to fetch entity")
|
|
|
|
// ErrFailedRemoval indicates that entity removal failed.
|
|
ErrFailedRemoval = errors.New("failed to remove entity")
|
|
|
|
// ErrFailedConnect indicates that connecting thing to channel failed.
|
|
ErrFailedConnect = errors.New("failed to connect thing to channel")
|
|
|
|
// ErrFailedDisconnect indicates that disconnecting thing from a channel failed.
|
|
ErrFailedDisconnect = errors.New("failed to disconnect thing from channel")
|
|
|
|
// ErrFailedPublish indicates that publishing message failed.
|
|
ErrFailedPublish = errors.New("failed to publish message")
|
|
|
|
// ErrFailedRead indicates that read messages failed.
|
|
ErrFailedRead = errors.New("failed to read messages")
|
|
|
|
// ErrInvalidContentType indicates that non-existent message content type
|
|
// was passed.
|
|
ErrInvalidContentType = errors.New("Unknown Content Type")
|
|
|
|
// ErrFetchVersion indicates that fetching of version failed.
|
|
ErrFetchVersion = errors.New("failed to fetch version")
|
|
|
|
// ErrFailedWhitelist failed to whitelist configs
|
|
ErrFailedWhitelist = errors.New("failed to whitelist")
|
|
|
|
// ErrCerts indicates error fetching certificates.
|
|
ErrCerts = errors.New("failed to fetch certs data")
|
|
|
|
// ErrCertsRemove indicates failure while cleaning up from the Certs service.
|
|
ErrCertsRemove = errors.New("failed to remove certificate")
|
|
)
|
|
|
|
// ContentType represents all possible content types.
|
|
type ContentType string
|
|
|
|
var _ SDK = (*mfSDK)(nil)
|
|
|
|
// User represents mainflux user its credentials.
|
|
type User struct {
|
|
ID string `json:"id,omitempty"`
|
|
Email string `json:"email,omitempty"`
|
|
Password string `json:"password,omitempty"`
|
|
Metadata map[string]interface{} `json:"metadata,omitempty"`
|
|
}
|
|
|
|
// Thing represents mainflux thing.
|
|
type Thing struct {
|
|
ID string `json:"id,omitempty"`
|
|
Name string `json:"name,omitempty"`
|
|
Key string `json:"key,omitempty"`
|
|
Metadata map[string]interface{} `json:"metadata,omitempty"`
|
|
}
|
|
|
|
// Channel represents mainflux channel.
|
|
type Channel struct {
|
|
ID string `json:"id,omitempty"`
|
|
Name string `json:"name,omitempty"`
|
|
Metadata map[string]interface{} `json:"metadata,omitempty"`
|
|
}
|
|
|
|
// SDK contains Mainflux API.
|
|
type SDK interface {
|
|
// CreateUser registers mainflux user.
|
|
CreateUser(user User) error
|
|
|
|
// User returns user object.
|
|
User(token string) (User, error)
|
|
|
|
// CreateToken receives credentials and returns user token.
|
|
CreateToken(user User) (string, error)
|
|
|
|
// UpdateUser updates existing user.
|
|
UpdateUser(user User, token string) error
|
|
|
|
// UpdatePassword updates user password.
|
|
UpdatePassword(oldPass, newPass, token string) error
|
|
|
|
// CreateThing registers new thing and returns its id.
|
|
CreateThing(thing Thing, token string) (string, error)
|
|
|
|
// CreateThings registers new things and returns their ids.
|
|
CreateThings(things []Thing, token string) ([]Thing, error)
|
|
|
|
// Things returns page of things.
|
|
Things(token string, offset, limit uint64, name string) (ThingsPage, error)
|
|
|
|
// ThingsByChannel returns page of things that are connected to specified
|
|
// channel.
|
|
ThingsByChannel(token, chanID string, offset, limit uint64) (ThingsPage, error)
|
|
|
|
// Thing returns thing object by id.
|
|
Thing(id, token string) (Thing, error)
|
|
|
|
// UpdateThing updates existing thing.
|
|
UpdateThing(thing Thing, token string) error
|
|
|
|
// DeleteThing removes existing thing.
|
|
DeleteThing(id, token string) error
|
|
|
|
// Connect bulk connects things to channels specified by id.
|
|
Connect(conns ConnectionIDs, token string) error
|
|
|
|
// DisconnectThing disconnect thing from specified channel by id.
|
|
DisconnectThing(thingID, chanID, token string) error
|
|
|
|
// CreateChannel creates new channel and returns its id.
|
|
CreateChannel(channel Channel, token string) (string, error)
|
|
|
|
// CreateChannels registers new channels and returns their ids.
|
|
CreateChannels(channels []Channel, token string) ([]Channel, error)
|
|
|
|
// Channels returns page of channels.
|
|
Channels(token string, offset, limit uint64, name string) (ChannelsPage, error)
|
|
|
|
// ChannelsByThing returns page of channels that are connected to specified
|
|
// thing.
|
|
ChannelsByThing(token, thingID string, offset, limit uint64) (ChannelsPage, error)
|
|
|
|
// Channel returns channel data by id.
|
|
Channel(id, token string) (Channel, error)
|
|
|
|
// UpdateChannel updates existing channel.
|
|
UpdateChannel(channel Channel, token string) error
|
|
|
|
// DeleteChannel removes existing channel.
|
|
DeleteChannel(id, token string) error
|
|
|
|
// SendMessage send message to specified channel.
|
|
SendMessage(chanID, msg, token string) error
|
|
|
|
// ReadMessages read messages of specified channel.
|
|
ReadMessages(chanID, token string) (MessagesPage, error)
|
|
|
|
// SetContentType sets message content type.
|
|
SetContentType(ct ContentType) error
|
|
|
|
// Version returns used mainflux version.
|
|
Version() (string, error)
|
|
|
|
// AddBootstrap add bootstrap configuration
|
|
AddBootstrap(token string, cfg BootstrapConfig) (string, error)
|
|
|
|
// View returns Thing Config with given ID belonging to the user identified by the given token.
|
|
ViewBootstrap(token, id string) (BootstrapConfig, error)
|
|
|
|
// Update updates editable fields of the provided Config.
|
|
UpdateBootstrap(token string, cfg BootstrapConfig) error
|
|
|
|
// Remove removes Config with specified token that belongs to the user identified by the given token.
|
|
RemoveBootstrap(token, id string) error
|
|
|
|
// Bootstrap returns Config to the Thing with provided external ID using external key.
|
|
Bootstrap(externalKey, externalID string) (BootstrapConfig, error)
|
|
|
|
// Whitelist updates Thing state Config with given ID belonging to the user identified by the given token.
|
|
Whitelist(token string, cfg BootstrapConfig) error
|
|
|
|
// IssueCert issues a certificate for a thing required for mtls.
|
|
IssueCert(thingID string, keyBits int, keyType, valid, token string) (Cert, error)
|
|
|
|
// RemoveCert removes a certificate
|
|
RemoveCert(id, token string) error
|
|
|
|
// RevokeCert revokes certificate with certID for thing with thingID
|
|
RevokeCert(thingID, certID, token string) error
|
|
}
|
|
|
|
type mfSDK struct {
|
|
baseURL string
|
|
readerURL string
|
|
bootstrapURL string
|
|
certsURL string
|
|
readerPrefix string
|
|
usersPrefix string
|
|
thingsPrefix string
|
|
certsPrefix string
|
|
channelsPrefix string
|
|
httpAdapterPrefix string
|
|
bootstrapPrefix string
|
|
msgContentType ContentType
|
|
client *http.Client
|
|
}
|
|
|
|
// Config contains sdk configuration parameters.
|
|
type Config struct {
|
|
BaseURL string
|
|
ReaderURL string
|
|
BootstrapURL string
|
|
CertsURL string
|
|
ReaderPrefix string
|
|
UsersPrefix string
|
|
ThingsPrefix string
|
|
HTTPAdapterPrefix string
|
|
BootstrapPrefix string
|
|
MsgContentType ContentType
|
|
TLSVerification bool
|
|
}
|
|
|
|
// NewSDK returns new mainflux SDK instance.
|
|
func NewSDK(conf Config) SDK {
|
|
return &mfSDK{
|
|
baseURL: conf.BaseURL,
|
|
readerURL: conf.ReaderURL,
|
|
bootstrapURL: conf.BootstrapURL,
|
|
certsURL: conf.CertsURL,
|
|
readerPrefix: conf.ReaderPrefix,
|
|
usersPrefix: conf.UsersPrefix,
|
|
thingsPrefix: conf.ThingsPrefix,
|
|
httpAdapterPrefix: conf.HTTPAdapterPrefix,
|
|
bootstrapPrefix: conf.BootstrapPrefix,
|
|
msgContentType: conf.MsgContentType,
|
|
client: &http.Client{
|
|
Transport: &http.Transport{
|
|
TLSClientConfig: &tls.Config{
|
|
InsecureSkipVerify: !conf.TLSVerification,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func (sdk mfSDK) sendRequest(req *http.Request, token, contentType string) (*http.Response, error) {
|
|
if token != "" {
|
|
req.Header.Set("Authorization", token)
|
|
}
|
|
|
|
if contentType != "" {
|
|
req.Header.Add("Content-Type", contentType)
|
|
}
|
|
|
|
return sdk.client.Do(req)
|
|
}
|
|
|
|
func createURL(baseURL, prefix, endpoint string) string {
|
|
if prefix == "" {
|
|
return fmt.Sprintf("%s/%s", baseURL, endpoint)
|
|
}
|
|
|
|
return fmt.Sprintf("%s/%s/%s", baseURL, prefix, endpoint)
|
|
}
|