netmaker/netclient/functions/register.go

91 lines
2.7 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"
2022-04-14 05:33:40 +08:00
"log"
2022-04-14 03:25:35 +08:00
"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-04-14 03:25:35 +08:00
if cfg.Server.Server == "" {
return errors.New("no server provided")
}
if cfg.Server.AccessKey == "" {
return errors.New("no access key provided")
}
2022-04-17 04:43:10 +08:00
//generate new key if one doesn' exist
var private *ed25519.PrivateKey
var err error
private, err = tls.ReadKey(ncutils.GetNetclientPath() + "/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
}
if err := tls.SaveKey(ncutils.GetNetclientPath(), "/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
_, err = tls.ReadCert(ncutils.GetNetclientServerPath(cfg.Server.Server) + "/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
cfg.Registered = true
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-15 21:54:35 +08:00
log.Println("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 22:38:29 +08:00
response, err := API(data, http.MethodPut, 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
}
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
if err := tls.SaveCert(ncutils.GetNetclientServerPath(cfg.Server.Server)+"/", "root.pem", &resp.CA); err != nil {
2022-04-14 03:25:35 +08:00
return err
}
if err := tls.SaveCert(ncutils.GetNetclientServerPath(cfg.Server.Server)+"/", "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
}