Skip to content

Commit

Permalink
Showing 6 changed files with 624 additions and 24 deletions.
76 changes: 76 additions & 0 deletions cmd/policy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright 2021 Authors of KubeArmor

package cmd

import (
"errors"

"github.com/kubearmor/kubearmor-client/vm"
"github.com/spf13/cobra"
)

var policyOptions vm.PolicyOptions

// vmPolicyCmd represents the vm command for policy enforcement
var vmPolicyCmd = &cobra.Command{
Use: "policy",
Short: "policy handling for vm nonk8s control plane",
Long: `policy handling for vm nonk8s control plane`,
RunE: func(cmd *cobra.Command, args []string) error {
return errors.New("must specify add/delete policy")
},
}

// vmPolicyAddCmd represents the vm add policy command for policy enforcement
var vmPolicyAddCmd = &cobra.Command{
Use: "add",
Short: "add policy for vm k8s/nonk8s control plane",
Long: `add policy for vm k8s/nonk8s control plane`,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("requires a path to valid policy YAML as argument")
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
if err := vm.PolicyHandling("ADDED", args[0], policyOptions); err != nil {
return err
}
return nil
},
}

// vmPolicyDeleteCmd represents the vm delete policy command for policy enforcement
var vmPolicyDeleteCmd = &cobra.Command{
Use: "delete",
Short: "delete policy for vm k8s/nonk8s control plane",
Long: `delete policy for vm k8s/nonk8s control plane`,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return errors.New("requires a path to valid policy YAML as argument")
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
if err := vm.PolicyHandling("DELETED", args[0], policyOptions); err != nil {
return err
}
return nil
},
}

// ========== //
// == Init == //
// ========== //

func init() {
vmCmd.AddCommand(vmPolicyCmd)

// Subcommand for policy command
vmPolicyCmd.AddCommand(vmPolicyAddCmd)
vmPolicyCmd.AddCommand(vmPolicyDeleteCmd)

// gRPC endpoint flag to communicate with KubeArmor. Available across subcommands.
vmPolicyCmd.PersistentFlags().StringVar(&policyOptions.GRPC, "gRPC", "", "gRPC server information")
}
65 changes: 57 additions & 8 deletions cmd/vm.go
Original file line number Diff line number Diff line change
@@ -10,34 +10,83 @@ import (
"github.com/spf13/cobra"
)

var vmOptions vm.Options
var (
scriptOptions vm.ScriptOptions
)

// vmCmd represents the vm command
var vmCmd = &cobra.Command{
Use: "vm",
Short: "Download VM install script from kvmservice",
Long: `Download VM install script from kvmservice`,
Short: "VM commands for kvmservice",
Long: `VM commands for kvmservice`,
}

// vmAddCmd represents the vm command for vm onboarding
var vmAddCmd = &cobra.Command{
Use: "add",
Short: "add/onboard a new vm for nonk8s control plane",
Long: `add/onboard a new vm for nonk8s control plane`,
RunE: func(cmd *cobra.Command, args []string) error {
if err := vm.FileDownload(client, vmOptions); err != nil {
return nil
},
}

// vmDelCmd represents the vm command for vm onboarding
var vmDelCmd = &cobra.Command{
Use: "delete",
Short: "delete/offboard a vm from nonk8s control plane",
Long: `delete/offboard a vm from nonk8s control plane`,
RunE: func(cmd *cobra.Command, args []string) error {
return nil
},
}

// vmScriptCmd represents the vm command for script download
var vmScriptCmd = &cobra.Command{
Use: "getscript",
Short: "download vm installation script for nonk8s control plane",
Long: `download vm installation script for nonk8s control plane`,
RunE: func(cmd *cobra.Command, args []string) error {
if err := vm.GetScript(client, scriptOptions); err != nil {
return err
}
return nil
},
}

// vmLabelCmd represents the vm command for script download
var vmLabelCmd = &cobra.Command{
Use: "label",
Short: "manage vm labels for nonk8s control plane",
Long: `manage vm labels for nonk8s control plane`,
RunE: func(cmd *cobra.Command, args []string) error {
return nil
},
}

// ========== //
// == Init == //
// ========== //

func init() {
rootCmd.AddCommand(vmCmd)
vmCmd.Flags().StringVarP(&vmOptions.Port, "port", "p", "32770", "Port of kvmservice")
vmCmd.Flags().StringVarP(&vmOptions.VMName, "kvm", "v", "", "Name of configured vm")
vmCmd.Flags().StringVarP(&vmOptions.File, "file", "f", "none", "Filename with path to store the configured vm installation script")

// All subcommands
vmCmd.AddCommand(vmAddCmd)
vmCmd.AddCommand(vmDelCmd)
vmCmd.AddCommand(vmPolicyCmd)
vmCmd.AddCommand(vmScriptCmd)
vmCmd.AddCommand(vmLabelCmd)

// Options for vm script download
vmScriptCmd.Flags().StringVarP(&scriptOptions.Port, "port", "p", "32770", "Port of kvmservice")
vmScriptCmd.Flags().StringVarP(&scriptOptions.VMName, "kvm", "v", "", "Name of configured vm")
vmScriptCmd.Flags().StringVarP(&scriptOptions.File, "file", "f", "none", "Filename with path to store the configured vm installation script")

// Marking this flag as markedFlag and mandatory
err := vmCmd.MarkFlagRequired("kvm")
err := vmScriptCmd.MarkFlagRequired("kvm")
if err != nil {
_ = fmt.Errorf("kvm option not supplied")
}

}
8 changes: 6 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -2,10 +2,13 @@ module github.com/kubearmor/kubearmor-client

go 1.16

replace github.com/kubearmor/KubeArmor/protobuf => github.com/daemon1024/KubeArmor/protobuf v0.0.0-20211216122055-cfd556a829ef

require (
github.com/kubearmor/KubeArmor/KubeArmor v0.0.0-20211214043053-9d191282a73a
github.com/kubearmor/KubeArmor/pkg/KubeArmorHostPolicy v0.0.0-20211028102308-7c7d59ec12b4
github.com/kubearmor/KubeArmor/pkg/KubeArmorPolicy v0.0.0-20211028102308-7c7d59ec12b4
github.com/kubearmor/KubeArmor/protobuf v0.0.0-20211028102308-7c7d59ec12b4 // indirect
github.com/kubearmor/KubeArmor/protobuf v0.0.0-20211028102308-7c7d59ec12b4
github.com/kubearmor/kubearmor-log-client/common v0.0.0-20210706110248-699fa8535e5c // indirect
github.com/kubearmor/kubearmor-log-client/core v0.0.0-20210706110248-699fa8535e5c
github.com/mholt/archiver/v3 v3.5.1-0.20211001174206-d35d4ce7c5b2
@@ -14,10 +17,11 @@ require (
golang.org/x/mod v0.5.1
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
google.golang.org/grpc v1.38.0
google.golang.org/protobuf v1.26.0
google.golang.org/protobuf v1.27.1
k8s.io/api v0.22.3
k8s.io/apiextensions-apiserver v0.22.3
k8s.io/apimachinery v0.22.3
k8s.io/cli-runtime v0.22.3
k8s.io/client-go v0.22.3
sigs.k8s.io/yaml v1.2.0
)
Loading

0 comments on commit 8759441

Please sign in to comment.