Skip to content

Commit

Permalink
Merge pull request #2834 from gravitl/release-v0.23.0
Browse files Browse the repository at this point in the history
v0.23.0
  • Loading branch information
abhishek9686 authored Feb 29, 2024
2 parents b9563fb + 6f61f8a commit 089df3d
Show file tree
Hide file tree
Showing 54 changed files with 1,873 additions and 679 deletions.
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/bug-report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ body:
label: Version
description: What version are you running?
options:
- v0.23.0
- v0.22.0
- v0.21.2
- v0.21.1
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ COPY . .

RUN GOOS=linux CGO_ENABLED=1 go build -ldflags="-s -w " -tags ${tags} .
# RUN go build -tags=ee . -o netmaker main.go
FROM alpine:3.19.0
FROM alpine:3.19.1

# add a c lib
# set the working directory
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile-quick
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#first stage - builder
FROM alpine:3.19.0
FROM alpine:3.19.1
ARG version
WORKDIR /app
COPY ./netmaker /root/netmaker
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

<p align="center">
<a href="https://github.com/gravitl/netmaker/releases">
<img src="https://img.shields.io/badge/Version-0.22.0-informational?style=flat-square" />
<img src="https://img.shields.io/badge/Version-0.23.0-informational?style=flat-square" />
</a>
<a href="https://hub.docker.com/r/gravitl/netmaker/tags">
<img src="https://img.shields.io/docker/pulls/gravitl/netmaker?label=downloads" />
Expand Down
48 changes: 35 additions & 13 deletions auth/host_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,24 @@ func SessionHandler(conn *websocket.Conn) {
return
}
req.Pass = req.Host.ID.String()
user, err := logic.GetUser(req.User)
if err != nil {
logger.Log(0, "failed to get user", req.User, "from database")
err = conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
if err != nil {
logger.Log(0, "error during message writing:", err.Error())
}
return
}
if !user.IsAdmin && !user.IsSuperAdmin {
logger.Log(0, "user", req.User, "is neither an admin or superadmin. denying registeration")
conn.WriteMessage(messageType, []byte("cannot register with a non-admin or non-superadmin"))
err = conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
if err != nil {
logger.Log(0, "error during message writing:", err.Error())
}
return
}

if err = netcache.Set(stateStr, req); err != nil { // give the user's host access in the DB
logger.Log(0, "machine failed to complete join on network,", registerMessage.Network, "-", err.Error())
Expand Down Expand Up @@ -129,14 +147,13 @@ func SessionHandler(conn *websocket.Conn) {
select {
case result := <-answer: // a read from req.answerCh has occurred
// add the host, if not exists, handle like enrollment registration
hostPass := result.Host.HostPass
if !logic.HostExists(&result.Host) { // check if host already exists, add if not
if servercfg.GetBrokerType() == servercfg.EmqxBrokerType {
if err := mq.CreateEmqxUser(result.Host.ID.String(), result.Host.HostPass, false); err != nil {
if err := mq.GetEmqxHandler().CreateEmqxUser(result.Host.ID.String(), result.Host.HostPass); err != nil {
logger.Log(0, "failed to create host credentials for EMQX: ", err.Error())
return
}
if err := mq.CreateHostACL(result.Host.ID.String(), servercfg.GetServerInfo().Server); err != nil {
if err := mq.GetEmqxHandler().CreateHostACL(result.Host.ID.String(), servercfg.GetServerInfo().Server); err != nil {
logger.Log(0, "failed to add host ACL rules to EMQX: ", err.Error())
return
}
Expand Down Expand Up @@ -185,11 +202,6 @@ func SessionHandler(conn *websocket.Conn) {
}
server := servercfg.GetServerInfo()
server.TrafficKey = key
if servercfg.GetBrokerType() == servercfg.EmqxBrokerType {
// set MQ username and password for EMQX clients
server.MQUserName = result.Host.ID.String()
server.MQPassword = hostPass
}
result.Host.HostPass = ""
response := models.RegisterResponse{
ServerConf: server,
Expand Down Expand Up @@ -233,11 +245,15 @@ func CheckNetRegAndHostUpdate(networks []string, h *models.Host, relayNodeId uui
continue
}
if relayNodeId != uuid.Nil && !newNode.IsRelayed {
newNode.IsRelayed = true
newNode.RelayedBy = relayNodeId.String()
slog.Info(fmt.Sprintf("adding relayed node %s to relay %s on network %s", newNode.ID.String(), relayNodeId.String(), network))
if err := logic.UpsertNode(newNode); err != nil {
slog.Error("failed to update node", "nodeid", relayNodeId.String())
// check if relay node exists and acting as relay
relaynode, err := logic.GetNodeByID(relayNodeId.String())
if err == nil && relaynode.IsRelay {
newNode.IsRelayed = true
newNode.RelayedBy = relayNodeId.String()
slog.Info(fmt.Sprintf("adding relayed node %s to relay %s on network %s", newNode.ID.String(), relayNodeId.String(), network))
if err := logic.UpsertNode(newNode); err != nil {
slog.Error("failed to update node", "nodeid", relayNodeId.String())
}
}
}
logger.Log(1, "added new node", newNode.ID.String(), "to host", h.Name)
Expand All @@ -246,6 +262,12 @@ func CheckNetRegAndHostUpdate(networks []string, h *models.Host, relayNodeId uui
Host: *h,
Node: *newNode,
})
if h.IsDefault {
// make host failover
logic.CreateFailOver(*newNode)
// make host remote access gateway
logic.CreateIngressGateway(network, newNode.ID.String(), models.IngressRequest{})
}
}
}
if servercfg.IsMessageQueueBackend() {
Expand Down
2 changes: 1 addition & 1 deletion compose/docker-compose.netclient.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version: "3.4"
services:
netclient:
container_name: netclient
image: 'gravitl/netclient:v0.22.0'
image: 'gravitl/netclient:v0.23.0'
hostname: netmaker-1
network_mode: host
restart: on-failure
Expand Down
2 changes: 1 addition & 1 deletion controllers/config/dnsconfig/Corefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
skynet {
. {
reload 15s
hosts /root/dnsconfig/netmaker.hosts {
fallthrough
Expand Down
2 changes: 1 addition & 1 deletion controllers/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//
// Schemes: https
// BasePath: /
// Version: 0.22.0
// Version: 0.23.0
// Host: api.demo.netmaker.io
//
// Consumes:
Expand Down
10 changes: 2 additions & 8 deletions controllers/enrollmentkeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,18 +305,17 @@ func handleHostRegister(w http.ResponseWriter, r *http.Request) {
)
return
}
hostPass := newHost.HostPass
if !hostExists {
newHost.PersistentKeepalive = models.DefaultPersistentKeepAlive
// register host
logic.CheckHostPorts(&newHost)
// create EMQX credentials and ACLs for host
if servercfg.GetBrokerType() == servercfg.EmqxBrokerType {
if err := mq.CreateEmqxUser(newHost.ID.String(), newHost.HostPass, false); err != nil {
if err := mq.GetEmqxHandler().CreateEmqxUser(newHost.ID.String(), newHost.HostPass); err != nil {
logger.Log(0, "failed to create host credentials for EMQX: ", err.Error())
return
}
if err := mq.CreateHostACL(newHost.ID.String(), servercfg.GetServerInfo().Server); err != nil {
if err := mq.GetEmqxHandler().CreateHostACL(newHost.ID.String(), servercfg.GetServerInfo().Server); err != nil {
logger.Log(0, "failed to add host ACL rules to EMQX: ", err.Error())
return
}
Expand Down Expand Up @@ -361,11 +360,6 @@ func handleHostRegister(w http.ResponseWriter, r *http.Request) {
// ready the response
server := servercfg.GetServerInfo()
server.TrafficKey = key
if servercfg.GetBrokerType() == servercfg.EmqxBrokerType {
// set MQ username and password for EMQX clients
server.MQUserName = newHost.ID.String()
server.MQPassword = hostPass
}
response := models.RegisterResponse{
ServerConf: server,
RequestedHost: newHost,
Expand Down
63 changes: 54 additions & 9 deletions controllers/ext_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ import (
"fmt"
"net"
"net/http"
"reflect"
"strconv"
"strings"

"github.com/go-playground/validator/v10"
"github.com/gorilla/mux"
"github.com/gravitl/netmaker/database"
"github.com/gravitl/netmaker/logger"
"github.com/gravitl/netmaker/logic"
"github.com/gravitl/netmaker/logic/acls"
"github.com/gravitl/netmaker/servercfg"

"github.com/gravitl/netmaker/models"
Expand Down Expand Up @@ -218,7 +222,7 @@ func getExtClientConf(w http.ResponseWriter, r *http.Request) {
gwendpoint = fmt.Sprintf("%s:%d", host.EndpointIP.String(), host.ListenPort)
}
var newAllowedIPs string
if logic.IsInternetGw(gwnode) {
if logic.IsInternetGw(gwnode) || gwnode.InternetGwID != "" {
egressrange := "0.0.0.0/0"
if gwnode.Address6.IP != nil && client.Address6 != "" {
egressrange += "," + "::/0"
Expand Down Expand Up @@ -250,11 +254,28 @@ func getExtClientConf(w http.ResponseWriter, r *http.Request) {
if host.MTU != 0 {
defaultMTU = host.MTU
}

postUp := strings.Builder{}
if client.PostUp != "" && params["type"] != "qr" {
for _, loc := range strings.Split(client.PostUp, "\n") {
postUp.WriteString(fmt.Sprintf("PostUp = %s\n", loc))
}
}

postDown := strings.Builder{}
if client.PostDown != "" && params["type"] != "qr" {
for _, loc := range strings.Split(client.PostDown, "\n") {
postDown.WriteString(fmt.Sprintf("PostDown = %s\n", loc))
}
}

config := fmt.Sprintf(`[Interface]
Address = %s
PrivateKey = %s
MTU = %d
%s
%s
%s
[Peer]
PublicKey = %s
Expand All @@ -266,10 +287,13 @@ Endpoint = %s
client.PrivateKey,
defaultMTU,
defaultDNS,
postUp.String(),
postDown.String(),
host.PublicKey,
newAllowedIPs,
gwendpoint,
keepalive)
keepalive,
)

if params["type"] == "qr" {
bytes, err := qrcode.Encode(config, qrcode.Medium, 220)
Expand Down Expand Up @@ -330,7 +354,6 @@ func createExtClient(w http.ResponseWriter, r *http.Request) {
return
}
var customExtClient models.CustomExtClient

if err := json.NewDecoder(r.Body).Decode(&customExtClient); err != nil {
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest"))
return
Expand Down Expand Up @@ -407,14 +430,14 @@ func createExtClient(w http.ResponseWriter, r *http.Request) {
extclient.Enabled = parentNetwork.DefaultACL == "yes"
}

if err := logic.SetClientDefaultACLs(&extclient); err != nil {
slog.Error("failed to set default acls for extclient", "user", r.Header.Get("user"), "network", node.Network, "error", err)
if err = logic.CreateExtClient(&extclient); err != nil {
slog.Error("failed to create extclient", "user", r.Header.Get("user"), "network", node.Network, "error", err)
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
return
}

if err = logic.CreateExtClient(&extclient); err != nil {
slog.Error("failed to create extclient", "user", r.Header.Get("user"), "network", node.Network, "error", err)
if err := logic.SetClientDefaultACLs(&extclient); err != nil {
slog.Error("failed to set default acls for extclient", "user", r.Header.Get("user"), "network", node.Network, "error", err)
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
return
}
Expand Down Expand Up @@ -486,7 +509,7 @@ func updateExtClient(w http.ResponseWriter, r *http.Request) {
}
var changedID = update.ClientID != oldExtClient.ClientID

if len(update.DeniedACLs) != len(oldExtClient.DeniedACLs) {
if !reflect.DeepEqual(update.DeniedACLs, oldExtClient.DeniedACLs) {
sendPeerUpdate = true
logic.SetClientACLs(&oldExtClient, update.DeniedACLs)
}
Expand All @@ -499,7 +522,6 @@ func updateExtClient(w http.ResponseWriter, r *http.Request) {
}
newclient := logic.UpdateExtClient(&oldExtClient, &update)
if err := logic.DeleteExtClient(oldExtClient.Network, oldExtClient.ClientID); err != nil {

slog.Error("failed to delete ext client", "user", r.Header.Get("user"), "id", oldExtClient.ClientID, "network", oldExtClient.Network, "error", err)
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
return
Expand Down Expand Up @@ -593,6 +615,24 @@ func deleteExtClient(w http.ResponseWriter, r *http.Request) {
return
}

// delete client acls
var networkAcls acls.ACLContainer
networkAcls, err = networkAcls.Get(acls.ContainerID(network))
if err != nil {
slog.Error("failed to get network acls", "err", err)
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
return
}
for objId := range networkAcls {
delete(networkAcls[objId], acls.AclID(clientid))
}
delete(networkAcls, acls.AclID(clientid))
if _, err = networkAcls.Save(acls.ContainerID(network)); err != nil {
slog.Error("failed to update network acls", "err", err)
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
return
}

go func() {
if err := mq.PublishDeletedClientPeerUpdate(&extclient); err != nil {
logger.Log(1, "error setting ext peers on "+ingressnode.ID.String()+": "+err.Error())
Expand All @@ -609,6 +649,11 @@ func deleteExtClient(w http.ResponseWriter, r *http.Request) {

// validateCustomExtClient Validates the extclient object
func validateCustomExtClient(customExtClient *models.CustomExtClient, checkID bool) error {
v := validator.New()
err := v.Struct(customExtClient)
if err != nil {
return err
}
//validate clientid
if customExtClient.ClientID != "" {
if err := isValid(customExtClient.ClientID, checkID); err != nil {
Expand Down
22 changes: 14 additions & 8 deletions controllers/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,6 @@ func pull(w http.ResponseWriter, r *http.Request) {
return
}
serverConf := servercfg.GetServerInfo()
if servercfg.GetBrokerType() == servercfg.EmqxBrokerType {
serverConf.MQUserName = hostID
}
key, keyErr := logic.RetrievePublicTrafficKey()
if keyErr != nil {
logger.Log(0, "error retrieving key:", keyErr.Error())
Expand All @@ -144,6 +141,9 @@ func pull(w http.ResponseWriter, r *http.Request) {
HostNetworkInfo: hPU.HostNetworkInfo,
EgressRoutes: hPU.EgressRoutes,
FwUpdate: hPU.FwUpdate,
ChangeDefaultGw: hPU.ChangeDefaultGw,
DefaultGwIp: hPU.DefaultGwIp,
IsInternetGw: hPU.IsInternetGw,
}

logger.Log(1, hostID, "completed a pull")
Expand Down Expand Up @@ -254,7 +254,7 @@ func hostUpdateFallback(w http.ResponseWriter, r *http.Request) {
return
}
case models.UpdateMetrics:
// mq.UpdateMetricsFallBack(hostUpdate.Node.ID.String(), hostUpdate.NewMetrics)
mq.UpdateMetricsFallBack(hostUpdate.Node.ID.String(), hostUpdate.NewMetrics)
}
logic.ReturnSuccessResponse(w, r, "updated host data")

Expand Down Expand Up @@ -298,7 +298,7 @@ func deleteHost(w http.ResponseWriter, r *http.Request) {
}
if servercfg.GetBrokerType() == servercfg.EmqxBrokerType {
// delete EMQX credentials for host
if err := mq.DeleteEmqxUser(currHost.ID.String()); err != nil {
if err := mq.GetEmqxHandler().DeleteEmqxUser(currHost.ID.String()); err != nil {
slog.Error("failed to remove host credentials from EMQX", "id", currHost.ID, "error", err)
}
}
Expand Down Expand Up @@ -354,6 +354,12 @@ func addHostToNetwork(w http.ResponseWriter, r *http.Request) {
return
}
logger.Log(1, "added new node", newNode.ID.String(), "to host", currHost.Name)
if currHost.IsDefault {
// make host failover
logic.CreateFailOver(*newNode)
// make host remote access gateway
logic.CreateIngressGateway(network, newNode.ID.String(), models.IngressRequest{})
}
go func() {
mq.HostUpdate(&models.HostUpdate{
Action: models.JoinHostToNetwork,
Expand Down Expand Up @@ -549,15 +555,15 @@ func authenticateHost(response http.ResponseWriter, request *http.Request) {

// Create EMQX creds and ACLs if not found
if servercfg.GetBrokerType() == servercfg.EmqxBrokerType {
if err := mq.CreateEmqxUser(host.ID.String(), authRequest.Password, false); err != nil {
if err := mq.GetEmqxHandler().CreateEmqxUser(host.ID.String(), authRequest.Password); err != nil {
slog.Error("failed to create host credentials for EMQX: ", err.Error())
} else {
if err := mq.CreateHostACL(host.ID.String(), servercfg.GetServerInfo().Server); err != nil {
if err := mq.GetEmqxHandler().CreateHostACL(host.ID.String(), servercfg.GetServerInfo().Server); err != nil {
slog.Error("failed to add host ACL rules to EMQX: ", err.Error())
}
for _, nodeID := range host.Nodes {
if node, err := logic.GetNodeByID(nodeID); err == nil {
if err = mq.AppendNodeUpdateACL(host.ID.String(), node.Network, node.ID.String(), servercfg.GetServer()); err != nil {
if err = mq.GetEmqxHandler().AppendNodeUpdateACL(host.ID.String(), node.Network, node.ID.String(), servercfg.GetServer()); err != nil {
slog.Error("failed to add ACLs for EMQX node", "error", err)
}
} else {
Expand Down
Loading

0 comments on commit 089df3d

Please sign in to comment.