Browse Source

add ACL management

Anish Mukherjee 2 years ago
parent
commit
b5c9fe4e40

+ 20 - 0
cli/cmd/acl/list.go

@@ -0,0 +1,20 @@
+package acl
+
+import (
+	"github.com/gravitl/netmaker/cli/functions"
+	"github.com/spf13/cobra"
+)
+
+var aclListCmd = &cobra.Command{
+	Use:   "list [NETWORK NAME]",
+	Args:  cobra.ExactArgs(1),
+	Short: "List all ACLs associated with a network",
+	Long:  `List all ACLs associated with a network`,
+	Run: func(cmd *cobra.Command, args []string) {
+		functions.PrettyPrint(functions.GetACL(args[0]))
+	},
+}
+
+func init() {
+	rootCmd.AddCommand(aclListCmd)
+}

+ 37 - 0
cli/cmd/acl/root.go

@@ -0,0 +1,37 @@
+package acl
+
+import (
+	"os"
+
+	"github.com/spf13/cobra"
+)
+
+// rootCmd represents the base command when called without any subcommands
+var rootCmd = &cobra.Command{
+	Use:   "acl",
+	Short: "Manage Access Control Lists (ACLs)",
+	Long:  `Manage Access Control Lists (ACLs)`,
+	// Run: func(cmd *cobra.Command, args []string) { },
+}
+
+func GetRoot() *cobra.Command {
+	return rootCmd
+}
+
+// Execute adds all child commands to the root command and sets flags appropriately.
+// This is called by main.main(). It only needs to happen once to the rootCmd.
+func Execute() {
+	err := rootCmd.Execute()
+	if err != nil {
+		os.Exit(1)
+	}
+}
+
+func init() {
+	// Here you will define your flags and configuration settings.
+	// Cobra supports persistent flags, which, if defined here,
+	// will be global for your application.
+	// Cobra also supports local flags, which will only run
+	// when this action is called directly.
+	rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
+}

+ 33 - 0
cli/cmd/acl/update.go

@@ -0,0 +1,33 @@
+package acl
+
+import (
+	"encoding/json"
+	"log"
+	"os"
+
+	"github.com/gravitl/netmaker/cli/functions"
+	"github.com/gravitl/netmaker/logic/acls"
+	"github.com/spf13/cobra"
+)
+
+var aclUpdatetCmd = &cobra.Command{
+	Use:   "update [NETWORK NAME] [/path/to/updated_acl.json]",
+	Args:  cobra.ExactArgs(2),
+	Short: "Update an ACL associated with a network",
+	Long:  `Update an ACL associated with a network`,
+	Run: func(cmd *cobra.Command, args []string) {
+		content, err := os.ReadFile(args[1])
+		if err != nil {
+			log.Fatal("Error when opening file: ", err)
+		}
+		acl := &acls.ACLContainer{}
+		if err := json.Unmarshal(content, acl); err != nil {
+			log.Fatal(err)
+		}
+		functions.PrettyPrint(functions.UpdateACL(args[0], acl))
+	},
+}
+
+func init() {
+	rootCmd.AddCommand(aclUpdatetCmd)
+}

+ 2 - 2
cli/cmd/network/create.go

@@ -2,8 +2,8 @@ package network
 
 import (
 	"encoding/json"
-	"io/ioutil"
 	"log"
+	"os"
 
 	"github.com/gravitl/netmaker/cli/functions"
 	"github.com/gravitl/netmaker/models"
@@ -17,7 +17,7 @@ var networkCreateCmd = &cobra.Command{
 	Long:  `Create a Network`,
 	Args:  cobra.ExactArgs(1),
 	Run: func(cmd *cobra.Command, args []string) {
-		content, err := ioutil.ReadFile(args[0])
+		content, err := os.ReadFile(args[0])
 		if err != nil {
 			log.Fatal("Error when opening file: ", err)
 		}

+ 2 - 0
cli/cmd/root.go

@@ -3,6 +3,7 @@ package cmd
 import (
 	"os"
 
+	"github.com/gravitl/netmaker/cli/cmd/acl"
 	"github.com/gravitl/netmaker/cli/cmd/context"
 	"github.com/gravitl/netmaker/cli/cmd/keys"
 	"github.com/gravitl/netmaker/cli/cmd/network"
@@ -47,4 +48,5 @@ func init() {
 	rootCmd.AddCommand(network.GetRoot())
 	rootCmd.AddCommand(context.GetRoot())
 	rootCmd.AddCommand(keys.GetRoot())
+	rootCmd.AddCommand(acl.GetRoot())
 }

+ 16 - 0
cli/functions/acl.go

@@ -0,0 +1,16 @@
+package functions
+
+import (
+	"fmt"
+	"net/http"
+
+	"github.com/gravitl/netmaker/logic/acls"
+)
+
+func GetACL(networkName string) *acls.ACLContainer {
+	return request[acls.ACLContainer](http.MethodGet, fmt.Sprintf("/api/networks/%s/acls", networkName), nil)
+}
+
+func UpdateACL(networkName string, payload *acls.ACLContainer) *acls.ACLContainer {
+	return request[acls.ACLContainer](http.MethodPut, fmt.Sprintf("/api/networks/%s/acls", networkName), payload)
+}

+ 1 - 1
cli/functions/http_client.go

@@ -76,7 +76,7 @@ func request[T any](method, route string, payload any) *T {
 	body := new(T)
 	if len(resBodyBytes) > 0 {
 		if err := json.Unmarshal(resBodyBytes, body); err != nil {
-			log.Printf("Error unmarshalling JSON: %s", err)
+			log.Fatalf("Error unmarshalling JSON: %s", err)
 		}
 	}
 	return body