Skip to content

Commit

Permalink
Merge branch 'develop' into NET-792-remove-freebsd
Browse files Browse the repository at this point in the history
  • Loading branch information
theguy951357 authored Jan 3, 2024
2 parents 6dbf580 + 3b739ed commit 0b2f5e3
Show file tree
Hide file tree
Showing 31 changed files with 464 additions and 496 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/deletedroplets.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
if: ${{ github.event.workflow_run.conclusion == 'success' }}
steps:
- name: get logs
uses: dawidd6/action-download-artifact@v2
uses: dawidd6/action-download-artifact@v3
with:
run_id: ${{ github.event.workflow_run.id}}
if_no_artifact_found: warn
Expand Down Expand Up @@ -60,7 +60,7 @@ jobs:
if: ${{ github.event.workflow_run.conclusion == 'failure' }}
steps:
- name: get logs
uses: dawidd6/action-download-artifact@v2
uses: dawidd6/action-download-artifact@v3
with:
run_id: ${{ github.event.workflow_run.id}}
if_no_artifact_found: warn
Expand Down
9 changes: 9 additions & 0 deletions auth/azure-ad.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ func handleAzureCallback(w http.ResponseWriter, r *http.Request) {
return
}
}
user, err := logic.GetUser(content.Email)
if err != nil {
handleOauthUserNotFound(w)
return
}
if !(user.IsSuperAdmin || user.IsAdmin) {
handleOauthUserNotAllowed(w)
return
}
var newPass, fetchErr = fetchPassValue("")
if fetchErr != nil {
return
Expand Down
25 changes: 25 additions & 0 deletions auth/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,31 @@ const oauthNotConfigured = `<!DOCTYPE html><html>
</body>
</html>`

const userNotAllowed = `<!DOCTYPE html><html>
<body>
<h3>Only Admins are allowed to access Dashboard.</h3>
<p>Non-Admins can access the netmaker networks using <a href="https://docs.netmaker.io/pro/rac.html" target="_blank" rel="noopener">RemoteAccessClient.</a></p>
</body>
</html>
`
const userNotFound = `<!DOCTYPE html><html>
<body>
<h3>User Not Found.</h3>
</body>
</html>`

func handleOauthUserNotFound(response http.ResponseWriter) {
response.Header().Set("Content-Type", "text/html; charset=utf-8")
response.WriteHeader(http.StatusNotFound)
response.Write([]byte(userNotFound))
}

func handleOauthUserNotAllowed(response http.ResponseWriter) {
response.Header().Set("Content-Type", "text/html; charset=utf-8")
response.WriteHeader(http.StatusForbidden)
response.Write([]byte(userNotAllowed))
}

// handleOauthNotConfigured - returns an appropriate html page when oauth is not configured on netmaker server but an oauth login was attempted
func handleOauthNotConfigured(response http.ResponseWriter) {
response.Header().Set("Content-Type", "text/html; charset=utf-8")
Expand Down
9 changes: 9 additions & 0 deletions auth/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ func handleGithubCallback(w http.ResponseWriter, r *http.Request) {
return
}
}
user, err := logic.GetUser(content.Email)
if err != nil {
handleOauthUserNotFound(w)
return
}
if !(user.IsSuperAdmin || user.IsAdmin) {
handleOauthUserNotAllowed(w)
return
}
var newPass, fetchErr = fetchPassValue("")
if fetchErr != nil {
return
Expand Down
9 changes: 9 additions & 0 deletions auth/google.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ func handleGoogleCallback(w http.ResponseWriter, r *http.Request) {
return
}
}
user, err := logic.GetUser(content.Email)
if err != nil {
handleOauthUserNotFound(w)
return
}
if !(user.IsSuperAdmin || user.IsAdmin) {
handleOauthUserNotAllowed(w)
return
}
var newPass, fetchErr = fetchPassValue("")
if fetchErr != nil {
return
Expand Down
9 changes: 9 additions & 0 deletions auth/oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ func handleOIDCCallback(w http.ResponseWriter, r *http.Request) {
return
}
}
user, err := logic.GetUser(content.Email)
if err != nil {
handleOauthUserNotFound(w)
return
}
if !(user.IsSuperAdmin || user.IsAdmin) {
handleOauthUserNotAllowed(w)
return
}
var newPass, fetchErr = fetchPassValue("")
if fetchErr != nil {
return
Expand Down
36 changes: 27 additions & 9 deletions cli/cmd/acl/allow.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package acl

import (
"fmt"
"log"

"github.com/gravitl/netmaker/cli/functions"
"github.com/gravitl/netmaker/logic/acls"
Expand All @@ -14,17 +15,34 @@ var aclAllowCmd = &cobra.Command{
Short: "Allow access from one node to another",
Long: `Allow access from one node to another`,
Run: func(cmd *cobra.Command, args []string) {
network := args[0]
fromNodeID := args[1]
toNodeID := args[2]
payload := acls.ACLContainer(map[acls.AclID]acls.ACL{
acls.AclID(fromNodeID): map[acls.AclID]byte{
acls.AclID(toNodeID): acls.Allowed,
},
acls.AclID(toNodeID): map[acls.AclID]byte{
acls.AclID(fromNodeID): acls.Allowed,
},
})
functions.UpdateACL(args[0], &payload)

if fromNodeID == toNodeID {
log.Fatal("Cannot allow access from a node to itself")
}

// get current acls
res := functions.GetACL(network)
if res == nil {
log.Fatalf("Could not load network ACLs")
}

payload := *res

if _, ok := payload[acls.AclID(fromNodeID)]; !ok {
log.Fatalf("Node %s does not exist", fromNodeID)
}
if _, ok := payload[acls.AclID(toNodeID)]; !ok {
log.Fatalf("Node %s does not exist", toNodeID)
}

// update acls
payload[acls.AclID(fromNodeID)][acls.AclID(toNodeID)] = acls.Allowed
payload[acls.AclID(toNodeID)][acls.AclID(fromNodeID)] = acls.Allowed

functions.UpdateACL(network, &payload)
fmt.Println("Success")
},
}
Expand Down
36 changes: 27 additions & 9 deletions cli/cmd/acl/deny.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package acl

import (
"fmt"
"log"

"github.com/gravitl/netmaker/cli/functions"
"github.com/gravitl/netmaker/logic/acls"
Expand All @@ -14,17 +15,34 @@ var aclDenyCmd = &cobra.Command{
Short: "Deny access from one node to another",
Long: `Deny access from one node to another`,
Run: func(cmd *cobra.Command, args []string) {
network := args[0]
fromNodeID := args[1]
toNodeID := args[2]
payload := acls.ACLContainer(map[acls.AclID]acls.ACL{
acls.AclID(fromNodeID): map[acls.AclID]byte{
acls.AclID(toNodeID): acls.NotAllowed,
},
acls.AclID(toNodeID): map[acls.AclID]byte{
acls.AclID(fromNodeID): acls.NotAllowed,
},
})
functions.UpdateACL(args[0], &payload)

if fromNodeID == toNodeID {
log.Fatal("Cannot deny access to self")
}

// get current acls
res := functions.GetACL(network)
if res == nil {
log.Fatalf("Could not load network ACLs")
}

payload := *res

if _, ok := payload[acls.AclID(fromNodeID)]; !ok {
log.Fatalf("Node [%s] does not exist", fromNodeID)
}
if _, ok := payload[acls.AclID(toNodeID)]; !ok {
log.Fatalf("Node [%s] does not exist", toNodeID)
}

// update acls
payload[acls.AclID(fromNodeID)][acls.AclID(toNodeID)] = acls.NotAllowed
payload[acls.AclID(toNodeID)][acls.AclID(fromNodeID)] = acls.NotAllowed

functions.UpdateACL(network, &payload)
fmt.Println("Success")
},
}
Expand Down
1 change: 1 addition & 0 deletions compose/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ services:
- "443:443"

coredns:
#network_mode: host
container_name: coredns
image: coredns/coredns:1.10.1
command: -conf /root/dnsconfig/Corefile
Expand Down
2 changes: 1 addition & 1 deletion controllers/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func HandleRESTRequests(wg *sync.WaitGroup, ctx context.Context) {

// Currently allowed dev origin is all. Should change in prod
// should consider analyzing the allowed methods further
headersOk := handlers.AllowedHeaders([]string{"Access-Control-Allow-Origin", "X-Requested-With", "Content-Type", "authorization"})
headersOk := handlers.AllowedHeaders([]string{"Access-Control-Allow-Origin", "X-Requested-With", "Content-Type", "authorization", "From-Ui"})
originsOk := handlers.AllowedOrigins(strings.Split(servercfg.GetAllowedOrigin(), ","))
methodsOk := handlers.AllowedMethods([]string{http.MethodGet, http.MethodPut, http.MethodPost, http.MethodDelete})

Expand Down
56 changes: 23 additions & 33 deletions controllers/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controller

import (
"encoding/json"
"errors"
"fmt"
"net/http"

Expand All @@ -10,7 +11,6 @@ import (
"github.com/gravitl/netmaker/logger"
"github.com/gravitl/netmaker/logic"
"github.com/gravitl/netmaker/models"
"github.com/gravitl/netmaker/mq"
"github.com/gravitl/netmaker/servercfg"
)

Expand Down Expand Up @@ -170,24 +170,17 @@ func createDNS(w http.ResponseWriter, r *http.Request) {
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
return
}
err = logic.SetDNS()
if err != nil {
logger.Log(0, r.Header.Get("user"),
fmt.Sprintf("Failed to set DNS entries on file: %v", err))
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
return
if servercfg.IsDNSMode() {
err = logic.SetDNS()
if err != nil {
logger.Log(0, r.Header.Get("user"),
fmt.Sprintf("Failed to set DNS entries on file: %v", err))
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
return
}
}

logger.Log(1, "new DNS record added:", entry.Name)
if servercfg.IsMessageQueueBackend() {
go func() {
if err = mq.PublishPeerUpdate(); err != nil {
logger.Log(0, "failed to publish peer update after ACL update on", entry.Network)
}
if err := mq.PublishCustomDNS(&entry); err != nil {
logger.Log(0, "error publishing custom dns", err.Error())
}
}()
}
logger.Log(2, r.Header.Get("user"),
fmt.Sprintf("DNS entry is set: %+v", entry))
w.WriteHeader(http.StatusOK)
Expand Down Expand Up @@ -221,23 +214,17 @@ func deleteDNS(w http.ResponseWriter, r *http.Request) {
return
}
logger.Log(1, "deleted dns entry: ", entrytext)
err = logic.SetDNS()
if err != nil {
logger.Log(0, r.Header.Get("user"),
fmt.Sprintf("Failed to set DNS entries on file: %v", err))
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
return
if servercfg.IsDNSMode() {
err = logic.SetDNS()
if err != nil {
logger.Log(0, r.Header.Get("user"),
fmt.Sprintf("Failed to set DNS entries on file: %v", err))
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
return
}
}

json.NewEncoder(w).Encode(entrytext + " deleted.")
go func() {
dns := models.DNSUpdate{
Action: models.DNSDeleteByName,
Name: entrytext,
}
if err := mq.PublishDNSUpdate(params["network"], dns); err != nil {
logger.Log(0, "failed to publish dns update", err.Error())
}
}()

}

Expand Down Expand Up @@ -271,7 +258,10 @@ func GetDNSEntry(domain string, network string) (models.DNSEntry, error) {
func pushDNS(w http.ResponseWriter, r *http.Request) {
// Set header
w.Header().Set("Content-Type", "application/json")

if !servercfg.IsDNSMode() {
logic.ReturnErrorResponse(w, r, logic.FormatError(errors.New("DNS Mode is set to off"), "badrequest"))
return
}
err := logic.SetDNS()

if err != nil {
Expand Down
30 changes: 30 additions & 0 deletions controllers/enrollmentkeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"time"

"github.com/go-playground/validator/v10"
"github.com/google/uuid"
"github.com/gorilla/mux"

Expand Down Expand Up @@ -115,6 +116,35 @@ func createEnrollmentKey(w http.ResponseWriter, r *http.Request) {
if enrollmentKeyBody.Expiration > 0 {
newTime = time.Unix(enrollmentKeyBody.Expiration, 0)
}
v := validator.New()
err = v.Struct(enrollmentKeyBody)
if err != nil {
logger.Log(0, r.Header.Get("user"), "error validating request body: ",
err.Error())
logic.ReturnErrorResponse(w, r, logic.FormatError(fmt.Errorf("validation error: name length must be between 3 and 32: %w", err), "badrequest"))
return
}

if existingKeys, err := logic.GetAllEnrollmentKeys(); err != nil {
logger.Log(0, r.Header.Get("user"), "error validating request body: ",
err.Error())
logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
return
} else {
// check if any tags are duplicate
existingTags := make(map[string]struct{})
for _, existingKey := range existingKeys {
for _, t := range existingKey.Tags {
existingTags[t] = struct{}{}
}
}
for _, t := range enrollmentKeyBody.Tags {
if _, ok := existingTags[t]; ok {
logic.ReturnErrorResponse(w, r, logic.FormatError(fmt.Errorf("key names must be unique"), "badrequest"))
return
}
}
}

relayId := uuid.Nil
if enrollmentKeyBody.Relay != "" {
Expand Down
Loading

0 comments on commit 0b2f5e3

Please sign in to comment.