changing broker retrieval/setting logic

This commit is contained in:
afeiszli
2022-05-30 12:39:33 -04:00
parent 0281149899
commit 0865a535c7
8 changed files with 36 additions and 13 deletions

View File

@@ -141,6 +141,8 @@ func register(w http.ResponseWriter, r *http.Request) {
CAPubKey: (ca.PublicKey).(ed25519.PublicKey),
Cert: *cert,
CertPubKey: (cert.PublicKey).(ed25519.PublicKey),
Broker: servercfg.GetServer(),
Port: servercfg.GetMQPort(),
}
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(response)

View File

@@ -54,7 +54,7 @@ func CreateAccessKey(accesskey models.AccessKey, network models.Network) (models
var accessToken models.AccessToken
s := servercfg.GetServerConfig()
servervals := models.ServerConfig{
Server: s.Server,
//Server: s.Server,
APIConnString: s.APIConnString,
}
accessToken.ServerConfig = servervals

View File

@@ -12,6 +12,5 @@ type ClientConfig struct {
}
type ServerConfig struct {
Server string `json:"server"`
APIConnString string `json:"apiconnstring"`
}

View File

@@ -38,6 +38,7 @@ type ServerConfig struct {
CoreDNSAddr string `yaml:"corednsaddr"`
AccessKey string `yaml:"accesskey"`
Server string `yaml:"server"`
BrokerPort string `yaml:"brokerport"`
API string `yaml:"api"`
Version string `yaml:"version"`
}
@@ -54,6 +55,8 @@ type RegisterResponse struct {
CAPubKey ed25519.PublicKey
Cert x509.Certificate
CertPubKey ed25519.PublicKey
Broker string
Port string
}
// Write - writes the config of a client to disk
@@ -198,7 +201,7 @@ func GetCLIConfig(c *cli.Context) (ClientConfig, string, error) {
cfg.Node.Network = accesstoken.ClientConfig.Network
cfg.Server.AccessKey = accesstoken.ClientConfig.Key
cfg.Node.LocalRange = accesstoken.ClientConfig.LocalRange
cfg.Server.Server = accesstoken.ServerConfig.Server
//cfg.Server.Server = accesstoken.ServerConfig.Server
cfg.Server.API = accesstoken.ServerConfig.APIConnString
if c.String("key") != "" {
cfg.Server.AccessKey = c.String("key")

View File

@@ -248,11 +248,10 @@ func setupMQTT(cfg *config.ClientConfig, publish bool) (mqtt.Client, error) {
} else {
err = token.Error()
}
if err := checkBroker(cfg.Server.Server); err != nil {
return nil, err
}
logger.Log(0, "could not connect to broker", cfg.Server.Server, err.Error())
if strings.Contains(err.Error(), "connectex") || strings.Contains(err.Error(), "connect timeout") {
if err = checkBroker(cfg.Server.Server, cfg.Server.BrokerPort); err != nil &&
(strings.Contains(err.Error(), "connectex") ||
strings.Contains(err.Error(), "connect timeout")) ||
strings.Contains(err.Error(), EMPTY_BROKER_ERR) {
logger.Log(0, "connection issue detected.. attempt connection with new certs")
key, err := ssl.ReadKey(ncutils.GetNetclientPath() + ncutils.GetSeparator() + "client.key")
if err != nil {

View File

@@ -190,6 +190,9 @@ func JoinNetwork(cfg *config.ClientConfig, privateKey string) error {
if err := Register(cfg, privateKey); err != nil {
return err
}
if cfg.Server.Server == "" {
return errors.New("did not recieve broker address from registration")
}
_ = UpdateLocalListenPort(cfg)

View File

@@ -16,13 +16,15 @@ import (
"github.com/gravitl/netmaker/netclient/auth"
"github.com/gravitl/netmaker/netclient/config"
"github.com/gravitl/netmaker/netclient/ncutils"
"github.com/gravitl/netmaker/servercfg"
"github.com/gravitl/netmaker/tls"
)
// pubNetworks hold the currently publishable networks
var pubNetworks []string
// EMPTY_BROKER_ERR is the error to return if no broker address is provided
var EMPTY_BROKER_ERR = "error: broker address is blank"
// Checkin -- go routine that checks for public or local ip changes, publishes changes
// if there are no updates, simply "pings" the server as a checkin
func Checkin(ctx context.Context, wg *sync.WaitGroup) {
@@ -166,20 +168,27 @@ func checkCertExpiry(cfg *config.ClientConfig) error {
return nil
}
func checkBroker(broker string) error {
func checkBroker(broker string, port string) error {
if broker == "" {
return errors.New(EMPTY_BROKER_ERR)
}
_, err := net.LookupIP(broker)
if err != nil {
return errors.New("nslookup failed for broker ... check dns records")
}
pinger := ping.NewTCPing()
port, err := strconv.Atoi(servercfg.GetMQPort())
intPort, err := strconv.Atoi(port)
if err != nil {
port = 8883
logger.Log(1, "error converting port to int: "+err.Error())
}
if intPort == 0 {
logger.Log(1, "port unset in config. Using default of 8883, which may be incorrect.")
intPort = 8883
}
pinger.SetTarget(&ping.Target{
Protocol: ping.TCP,
Host: broker,
Port: port,
Port: intPort,
Counter: 3,
Interval: 1 * time.Second,
Timeout: 2 * time.Second,

View File

@@ -76,6 +76,14 @@ func RegisterWithServer(private *ed25519.PrivateKey, cfg *config.ClientConfig) e
if err := json.NewDecoder(response.Body).Decode(&resp); err != nil {
return errors.New("unmarshal cert error " + err.Error())
}
// set broker information on register
cfg.Server.Server = resp.Broker
cfg.Server.BrokerPort = resp.Port
if err = config.Write(cfg, cfg.Node.Network); err != nil {
logger.Log(0, "error overwriting config with broker information: "+err.Error())
}
//x509.Certificate.PublicKey is an interface so json encoding/decoding results in a string rather that []byte
//the pubkeys are included in the response so the values in the certificate can be updated appropriately
resp.CA.PublicKey = resp.CAPubKey