netmaker/netclient/functions/register.go

101 lines
3.1 KiB
Go
Raw Normal View History

2022-04-14 03:25:35 +08:00
package functions
import (
2022-04-14 19:15:50 +08:00
"crypto/ed25519"
"crypto/rand"
2022-04-14 03:25:35 +08:00
"encoding/json"
"errors"
"net/http"
2022-04-14 19:15:50 +08:00
"os"
2022-04-14 03:25:35 +08:00
"github.com/gravitl/netmaker/logger"
"github.com/gravitl/netmaker/netclient/config"
"github.com/gravitl/netmaker/netclient/ncutils"
"github.com/gravitl/netmaker/tls"
)
2022-04-16 03:03:54 +08:00
// Register - the function responsible for registering with the server and acquiring certs
func Register(cfg *config.ClientConfig, key string) error {
2022-05-31 20:42:12 +08:00
2022-04-17 04:43:10 +08:00
//generate new key if one doesn' exist
var private *ed25519.PrivateKey
var err error
2022-04-26 08:07:47 +08:00
private, err = tls.ReadKey(ncutils.GetNetclientPath() + ncutils.GetSeparator() + "client.key")
2022-04-14 19:15:50 +08:00
if err != nil {
_, newKey, err := ed25519.GenerateKey(rand.Reader)
2022-04-17 04:43:10 +08:00
if err != nil {
return err
}
2022-04-26 08:07:47 +08:00
if err := tls.SaveKey(ncutils.GetNetclientPath(), ncutils.GetSeparator()+"client.key", newKey); err != nil {
2022-04-17 04:43:10 +08:00
return err
}
private = &newKey
2022-04-14 19:15:50 +08:00
}
//check if cert exists
2022-04-26 08:07:47 +08:00
_, err = tls.ReadCert(ncutils.GetNetclientServerPath(cfg.Server.Server) + ncutils.GetSeparator() + "client.pem")
if errors.Is(err, os.ErrNotExist) {
if err := RegisterWithServer(private, cfg); err != nil {
return err
}
} else if err != nil {
return err
}
2022-04-25 22:38:29 +08:00
return nil
}
// RegisterWithServer calls the register endpoint with privatekey and commonname - api returns ca and client certificate
func RegisterWithServer(private *ed25519.PrivateKey, cfg *config.ClientConfig) error {
2022-04-14 19:15:50 +08:00
data := config.RegisterRequest{
2022-04-17 04:43:10 +08:00
Key: *private,
2022-04-25 18:33:06 +08:00
CommonName: tls.NewCName(cfg.Node.Name),
2022-04-14 19:15:50 +08:00
}
2022-04-17 03:35:05 +08:00
url := "https://" + cfg.Server.API + "/api/server/register"
2022-04-26 08:37:05 +08:00
logger.Log(1, "register at "+url)
2022-04-25 22:38:29 +08:00
token, err := Authenticate(cfg)
2022-04-14 03:25:35 +08:00
if err != nil {
return err
}
2022-04-25 23:07:21 +08:00
response, err := API(data, http.MethodPost, url, token)
2022-04-14 03:25:35 +08:00
if err != nil {
return err
}
if response.StatusCode != http.StatusOK {
return errors.New(response.Status)
}
2022-04-14 06:22:03 +08:00
var resp config.RegisterResponse
if err := json.NewDecoder(response.Body).Decode(&resp); err != nil {
2022-04-14 06:22:03 +08:00
return errors.New("unmarshal cert error " + err.Error())
2022-04-14 03:25:35 +08:00
}
// set broker information on register
2022-06-01 00:07:56 +08:00
var modServer bool
if resp.Broker != "" && resp.Broker != cfg.Server.Server {
cfg.Server.Server = resp.Broker
modServer = true
}
if resp.Port != "" && resp.Port != cfg.Server.MQPort {
cfg.Server.MQPort = resp.Port
modServer = true
}
if modServer {
if err = config.ModServerConfig(&cfg.Server, cfg.Node.Network); err != nil {
logger.Log(0, "error overwriting config with broker information: "+err.Error())
}
}
2022-04-17 04:43:10 +08:00
//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
2022-04-16 03:20:46 +08:00
resp.CA.PublicKey = resp.CAPubKey
resp.Cert.PublicKey = resp.CertPubKey
2022-04-26 08:07:47 +08:00
if err := tls.SaveCert(ncutils.GetNetclientServerPath(cfg.Server.Server)+ncutils.GetSeparator(), "root.pem", &resp.CA); err != nil {
2022-04-14 03:25:35 +08:00
return err
}
2022-04-26 08:07:47 +08:00
if err := tls.SaveCert(ncutils.GetNetclientServerPath(cfg.Server.Server)+ncutils.GetSeparator(), "client.pem", &resp.Cert); err != nil {
2022-04-14 19:15:50 +08:00
return err
}
logger.Log(0, "certificates/key saved ")
2022-04-17 04:43:10 +08:00
//join the network defined in the token
return nil
2022-04-14 03:25:35 +08:00
}