2020-04-29 06:39:23 +08:00
|
|
|
// Copyright (c) Mainflux
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package provision
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
|
2020-06-03 21:16:19 +08:00
|
|
|
"github.com/mainflux/mainflux/pkg/errors"
|
2022-01-05 02:42:13 +08:00
|
|
|
"github.com/mainflux/mainflux/things"
|
2020-04-29 06:39:23 +08:00
|
|
|
"github.com/pelletier/go-toml"
|
|
|
|
)
|
|
|
|
|
2022-01-05 02:42:13 +08:00
|
|
|
// ServiceConf represents service config.
|
2020-04-29 06:39:23 +08:00
|
|
|
type ServiceConf struct {
|
|
|
|
Port string `toml:"port"`
|
|
|
|
LogLevel string `toml:"log_level"`
|
|
|
|
TLS bool `toml:"tls"`
|
|
|
|
ServerCert string `toml:"server_cert"`
|
|
|
|
ServerKey string `toml:"server_key"`
|
2021-08-11 22:58:10 +08:00
|
|
|
ThingsURL string `toml:"things_url"`
|
|
|
|
UsersURL string `toml:"users_url"`
|
2020-04-29 06:39:23 +08:00
|
|
|
HTTPPort string `toml:"http_port"`
|
|
|
|
MfUser string `toml:"mf_user"`
|
|
|
|
MfPass string `toml:"mf_pass"`
|
|
|
|
MfAPIKey string `toml:"mf_api_key"`
|
|
|
|
MfBSURL string `toml:"mf_bs_url"`
|
2021-02-27 08:01:48 +08:00
|
|
|
MfWhiteListURL string `toml:"mf_white_list"`
|
2020-04-29 06:39:23 +08:00
|
|
|
MfCertsURL string `toml:"mf_certs_url"`
|
|
|
|
}
|
|
|
|
|
2022-01-05 02:42:13 +08:00
|
|
|
// Bootstrap represetns the Bootstrap config
|
2020-04-29 06:39:23 +08:00
|
|
|
type Bootstrap struct {
|
2020-07-29 20:20:43 +08:00
|
|
|
X509Provision bool `toml:"x509_provision"`
|
|
|
|
Provision bool `toml:"provision"`
|
|
|
|
AutoWhiteList bool `toml:"autowhite_list"`
|
|
|
|
Content map[string]interface{} `toml:"content"`
|
2020-04-29 06:39:23 +08:00
|
|
|
}
|
|
|
|
|
2022-01-05 02:42:13 +08:00
|
|
|
// Gateway represetns the Gateway config
|
2020-05-06 22:53:32 +08:00
|
|
|
type Gateway struct {
|
|
|
|
Type string `toml:"type" json:"type"`
|
|
|
|
ExternalID string `toml:"external_id" json:"external_id"`
|
|
|
|
ExternalKey string `toml:"external_key" json:"external_key"`
|
|
|
|
CtrlChannelID string `toml:"ctrl_channel_id" json:"ctrl_channel_id"`
|
|
|
|
DataChannelID string `toml:"data_channel_id" json:"data_channel_id"`
|
|
|
|
ExportChannelID string `toml:"export_channel_id" json:"export_channel_id"`
|
|
|
|
CfgID string `toml:"cfg_id" json:"cfg_id"`
|
|
|
|
}
|
|
|
|
|
2022-01-05 02:42:13 +08:00
|
|
|
// Cert represetns the certificate config
|
|
|
|
type Cert struct {
|
|
|
|
TTL string `json:"ttl" toml:"ttl"`
|
|
|
|
KeyBits int `json:"key_bits" toml:"key_bits"`
|
|
|
|
KeyType string `json:"key_type"`
|
MF-1179 - Add a certificate service and certs endpoint to SDK (#1188)
* 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>
2020-07-21 18:53:21 +08:00
|
|
|
}
|
|
|
|
|
2020-04-29 06:39:23 +08:00
|
|
|
// Config struct of Provision
|
|
|
|
type Config struct {
|
2022-01-05 02:42:13 +08:00
|
|
|
File string `toml:"file"`
|
|
|
|
Server ServiceConf `toml:"server" mapstructure:"server"`
|
|
|
|
Bootstrap Bootstrap `toml:"bootstrap" mapstructure:"bootstrap"`
|
|
|
|
Things []things.Thing `toml:"things" mapstructure:"things"`
|
|
|
|
Channels []things.Channel `toml:"channels" mapstructure:"channels"`
|
|
|
|
Cert Cert `toml:"cert" mapstructure:"cert"`
|
2020-04-29 06:39:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save - store config in a file
|
|
|
|
func Save(c Config, file string) error {
|
|
|
|
b, err := toml.Marshal(c)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New(fmt.Sprintf("Error reading config file: %s", err))
|
|
|
|
}
|
|
|
|
if err := ioutil.WriteFile(file, b, 0644); err != nil {
|
|
|
|
return errors.New(fmt.Sprintf("Error writing toml: %s", err))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read - retrieve config from a file
|
|
|
|
func Read(file string) (Config, error) {
|
|
|
|
data, err := ioutil.ReadFile(file)
|
|
|
|
c := Config{}
|
|
|
|
if err != nil {
|
|
|
|
return c, errors.New(fmt.Sprintf("Error reading config file: %s", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := toml.Unmarshal(data, &c); err != nil {
|
|
|
|
return Config{}, errors.New(fmt.Sprintf("Error unmarshaling toml: %s", err))
|
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
}
|