From 5878ffac7b42b80aca984f502a54fbf6a679e23b Mon Sep 17 00:00:00 2001 From: CharlieRoot Date: Wed, 9 Oct 2024 14:52:57 +0200 Subject: [PATCH] Add option for ACL --- cmd/acl.go | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ host/server.go | 13 +++++++++++++ main.go | 2 ++ 3 files changed, 64 insertions(+) create mode 100644 cmd/acl.go diff --git a/cmd/acl.go b/cmd/acl.go new file mode 100644 index 0000000..0ecf826 --- /dev/null +++ b/cmd/acl.go @@ -0,0 +1,49 @@ +package cmd + +import ( + "github.com/Charlie-Root/smcli/host" + "github.com/spf13/cobra" +) + +// Parent ACL command (no direct functionality) +var AclCmd = &cobra.Command{ + Use: "acl", + Short: "Manage ACL settings", +} + +// Subcommand to enable ACL on the host +var EnableAclCmd = &cobra.Command{ + Use: "enable ", + Short: "Enable ACL on the specified host", + Args: cobra.ExactArgs(1), // Expect exactly one argument for the host + Run: func(cmd *cobra.Command, args []string) { + hostName := args[0] + host.SelectHost(hostName) + host.ServerEnableACL() // Call to enable ACL on the host + }, +} + +// Subcommand to add an ACL entry +var AddAclCmd = &cobra.Command{ + Use: "add
", + Short: "Add new ACL entry", + Args: cobra.ExactArgs(4), // Expect address, prefix, policy, and host as arguments + Run: func(cmd *cobra.Command, args []string) { + address := args[0] + prefix := args[1] + policy := args[2] + hostName := args[3] + + host.SelectHost(hostName) + host.ServerCreateACL(address, prefix, policy) // Call to add ACL entry + }, +} + +func init() { + // Register subcommands under the acl command + AclCmd.AddCommand(EnableAclCmd) + AclCmd.AddCommand(AddAclCmd) + + // Register acl command to the root command (assuming RootCmd is the root cobra command) + RootCmd.AddCommand(AclCmd) +} diff --git a/host/server.go b/host/server.go index 42847ae..587c113 100644 --- a/host/server.go +++ b/host/server.go @@ -57,3 +57,16 @@ func ServerCreateUser(username string, password string) { MakeRequest("POST", url, body) } + +func ServerEnableACL() { + url := fmt.Sprintf("https://%s/redfish/v1/Managers/1/Oem/Supermicro/IPAccessControl", CurrentHost.BMCAddress) + body := []byte(`{"ServiceEnabled": true}`) + + MakeRequest("PATCH", url, body) +} +func ServerCreateACL(address string, netmask string, policy string) { + url := fmt.Sprintf("https://%s/redfish/v1/Managers/1/Oem/Supermicro/IPAccessControl/FilterRules", CurrentHost.BMCAddress) + body := []byte(fmt.Sprintf(`{"Address": "%s", "PrefixLength": %s, "Policy": "%s"}`, address, netmask, policy)) + + MakeRequest("POST", url, body) +} diff --git a/main.go b/main.go index 7af84ca..df64cb7 100644 --- a/main.go +++ b/main.go @@ -20,6 +20,8 @@ func main() { rootCmd.AddCommand(cmd.MediaCmd) rootCmd.AddCommand(cmd.BootCmd) rootCmd.AddCommand(cmd.UserCmd) + rootCmd.AddCommand(cmd.AclCmd) + if err := rootCmd.Execute(); err != nil { log.Fatal(err)