diff --git a/cmd/get/get.go b/cmd/get/get.go deleted file mode 100644 index 9e16b237..00000000 --- a/cmd/get/get.go +++ /dev/null @@ -1,19 +0,0 @@ -package get - -import ( - "github.com/spf13/cobra" -) - -var GetCmd = &cobra.Command{ - Use: "get", - Short: "get is a pallete which contains get commands", - Long: `get is a pallete which contains get commands`, - RunE: func(cmd *cobra.Command, args []string) error { - err := cmd.Help() - return err - }, -} - -func init() { - -} diff --git a/cmd/license/install.go b/cmd/license/install.go new file mode 100644 index 00000000..d1b5f674 --- /dev/null +++ b/cmd/license/install.go @@ -0,0 +1,82 @@ +package license + +import ( + "context" + "fmt" + "os" + "strconv" + + "github.com/accuknox/accuknox-cli/k8s" + "github.com/accuknox/accuknox-cli/utils" + pb "github.com/accuknox/auto-policy-discovery/src/protobuf/v1/license" + "github.com/spf13/cobra" + "google.golang.org/grpc" + "google.golang.org/grpc/credentials/insecure" +) + +var ( + key string + user string +) + +var installCmd = &cobra.Command{ + Use: "install", + Short: "Install License", + Long: `Install license for discovery engine`, + RunE: func(cmd *cobra.Command, args []string) error { + client, err := k8s.ConnectK8sClient() + if err != nil { + fmt.Printf("unable to create Kubernetes clients: %s\n", err.Error()) + return err + } + + gRPC := "" + targetSvc := "discovery-engine" + + if val, ok := os.LookupEnv("DISCOVERY_SERVICE"); ok { + gRPC = val + } else { + pf, err := utils.InitiatePortForward(client, port, port, matchLabels, targetSvc) + if err != nil { + return err + } + gRPC = "localhost:" + strconv.FormatInt(pf.LocalPort, 10) + } + + conn, err := grpc.Dial(gRPC, grpc.WithTransportCredentials(insecure.NewCredentials())) + if err != nil { + return err + } + defer conn.Close() + + licenseClient := pb.NewLicenseClient(conn) + + req := &pb.LicenseInstallRequest{ + Key: key, + UserId: user, + } + _, err = licenseClient.InstallLicense(context.Background(), req) + if err != nil { + return err + } + fmt.Printf("License installed successfully for discovery engine.\n") + + return nil + }, +} + +func init() { + LicenseCmd.AddCommand(installCmd) + + installCmd.Flags().StringVar(&key, "key", "", "license key for installing license (required)") + installCmd.Flags().StringVar(&user, "user", "", "user id for installing license") + + err := installCmd.MarkFlagRequired("key") + if err != nil { + fmt.Printf("key flag is required : %s\n", err) + } + err = installCmd.MarkFlagRequired("user") + if err != nil { + fmt.Printf("user flag is required : %s\n", err) + } +} diff --git a/cmd/license/license.go b/cmd/license/license.go new file mode 100644 index 00000000..8536688f --- /dev/null +++ b/cmd/license/license.go @@ -0,0 +1,22 @@ +package license + +import ( + "github.com/spf13/cobra" +) + +var matchLabels = map[string]string{"app": "discovery-engine"} +var port int64 = 9089 + +var LicenseCmd = &cobra.Command{ + Use: "license", + Short: "license is a pallete which contains license commands", + Long: `license is a pallete which contains license commands`, + RunE: func(cmd *cobra.Command, args []string) error { + err := cmd.Help() + return err + }, +} + +func init() { + +} diff --git a/cmd/get/licenseStatus.go b/cmd/license/status.go similarity index 87% rename from cmd/get/licenseStatus.go rename to cmd/license/status.go index 836df21e..9376ab1e 100644 --- a/cmd/get/licenseStatus.go +++ b/cmd/license/status.go @@ -1,4 +1,4 @@ -package get +package license import ( "context" @@ -12,13 +12,11 @@ import ( pb "github.com/accuknox/auto-policy-discovery/src/protobuf/v1/license" "github.com/spf13/cobra" "google.golang.org/grpc" + "google.golang.org/grpc/credentials/insecure" ) -var matchLabels = map[string]string{"app": "discovery-engine"} -var port int64 = 9089 - var licenseStatusCmd = &cobra.Command{ - Use: "license-status", + Use: "status", Short: "get license status", Long: `get license status`, @@ -43,12 +41,13 @@ var licenseStatusCmd = &cobra.Command{ gRPC = "localhost:" + strconv.FormatInt(pf.LocalPort, 10) } - conn, err := grpc.Dial(gRPC, grpc.WithInsecure()) + conn, err := grpc.Dial(gRPC, grpc.WithTransportCredentials(insecure.NewCredentials())) if err != nil { fmt.Printf("unable to dial to the target grpc: %s\n", err.Error()) return err } defer conn.Close() + licenseClient := pb.NewLicenseClient(conn) req := &pb.LicenseStatusRequest{} resp, err := licenseClient.GetLicenseStatus(context.Background(), req) @@ -69,5 +68,5 @@ var licenseStatusCmd = &cobra.Command{ } func init() { - GetCmd.AddCommand(licenseStatusCmd) + LicenseCmd.AddCommand(licenseStatusCmd) } diff --git a/cmd/get/uuid.go b/cmd/license/uuid.go similarity index 94% rename from cmd/get/uuid.go rename to cmd/license/uuid.go index d819a4c8..00abbb26 100644 --- a/cmd/get/uuid.go +++ b/cmd/license/uuid.go @@ -1,4 +1,4 @@ -package get +package license import ( "fmt" @@ -32,6 +32,6 @@ var uuidCmd = &cobra.Command{ } func init() { - GetCmd.AddCommand(uuidCmd) + LicenseCmd.AddCommand(uuidCmd) } diff --git a/cmd/root.go b/cmd/root.go index 430c8fa4..229085ff 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -5,7 +5,7 @@ package cmd import ( - "github.com/accuknox/accuknox-cli/cmd/get" + "github.com/accuknox/accuknox-cli/cmd/license" "github.com/accuknox/accuknox-cli/k8s" "github.com/rs/zerolog/log" "github.com/spf13/cobra" @@ -41,7 +41,7 @@ operation) of containers at the system level. // adding all the commands with sub commands func addSubCommandPalettes() { - rootCmd.AddCommand(get.GetCmd) + rootCmd.AddCommand(license.LicenseCmd) } func init() {