From 1b2384c1d2246c19184073715f40c671841ee091 Mon Sep 17 00:00:00 2001 From: Siddhesh Khandagale Date: Mon, 27 Nov 2023 10:32:30 +0530 Subject: [PATCH] few Updates --- cmd/create.go | 7 ++++++- cmd/delete.go | 10 +++++++--- cmd/get.go | 9 ++++++--- cmd/list.go | 18 ++++++------------ cmd/login.go | 23 +---------------------- cmd/logout.go | 2 -- cmd/update.go | 9 ++++++--- cmd/whoami.go | 1 - lib/store.go | 29 ++++++++--------------------- lib/user.go | 11 ----------- 10 files changed, 40 insertions(+), 79 deletions(-) diff --git a/cmd/create.go b/cmd/create.go index ceccd03..814d9b6 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -11,6 +11,7 @@ import ( "os" "os/user" "path/filepath" + "strings" "github.com/Siddheshk02/Securelee-cli/lib" "github.com/spf13/cobra" @@ -43,13 +44,17 @@ var createCmd = &cobra.Command{ var userData struct { UserID string `json:"userID"` + Email string `json:"email"` } err = json.Unmarshal(jsonData, &userData) if err != nil { log.Fatalln("\033[31m", err.Error(), "\033[0m") } + email := strings.Replace(userData.Email, "@", "-", -1) + Newemail := strings.Replace(email, ".", "-", -1) + fmt.Println(Newemail) - err = lib.Create(userData.UserID) + err = lib.Create(Newemail) if err != nil { log.Fatalln("\033[31m", err.Error(), "\033[0m") } diff --git a/cmd/delete.go b/cmd/delete.go index 85059e2..834742a 100644 --- a/cmd/delete.go +++ b/cmd/delete.go @@ -11,6 +11,7 @@ import ( "os" "os/user" "path/filepath" + "strings" "github.com/Siddheshk02/Securelee-cli/lib" "github.com/spf13/cobra" @@ -22,7 +23,6 @@ var deleteCmd = &cobra.Command{ Short: "Delete a Secret Message or Key.", Long: `Delete a Secret Message or Key.`, Run: func(cmd *cobra.Command, args []string) { - // fmt.Println("delete called") res := lib.Check() if !res { fmt.Print("\033[31m", "\n > No User logged in, You must Login to use Securelee Vault Services.\n", "\033[0m") @@ -44,12 +44,16 @@ var deleteCmd = &cobra.Command{ var userData struct { UserID string `json:"userID"` + Email string `json:"email"` } err = json.Unmarshal(jsonData, &userData) if err != nil { log.Fatal("\033[31m", err, "\033[0m") } + email := strings.Replace(userData.Email, "@", "-", -1) + Newemail := strings.Replace(email, ".", "-", -1) + var choice uint fmt.Print("\033[33m", "\n Select any one option: \n", "\033[0m") fmt.Println("\033[33m", "\n > 1. Delete a Secret", "\033[0m") @@ -60,7 +64,7 @@ var deleteCmd = &cobra.Command{ fmt.Println("") if choice == 1 { - res, err := lib.ListSecrets(userData.UserID) + res, err := lib.ListSecrets(Newemail) if err != nil { log.Fatalln("\033[31m", err.Error(), "\033[0m") } @@ -86,7 +90,7 @@ var deleteCmd = &cobra.Command{ } } else if choice == 2 { - res, err := lib.ListKeys(userData.UserID) + res, err := lib.ListKeys(Newemail) if err != nil { log.Fatalln("\033[31m", err.Error(), "\033[0m") } diff --git a/cmd/get.go b/cmd/get.go index 66bc508..2a5862d 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -13,6 +13,7 @@ import ( "os/user" "path/filepath" "regexp" + "strings" "time" "github.com/Siddheshk02/Securelee-cli/lib" @@ -26,7 +27,6 @@ var getCmd = &cobra.Command{ Short: "Get a Particular Secret or Key.", Long: `Get a Particular Secret or Key.`, Run: func(cmd *cobra.Command, args []string) { - // fmt.Println("get called") res := lib.Check() if !res { fmt.Print("\033[31m", "\n > No User logged in, You must Login to use Securelee Vault Services.\n", "\033[0m") @@ -48,6 +48,7 @@ var getCmd = &cobra.Command{ var userData struct { UserID string `json:"userID"` + Email string `json:"email"` } err = json.Unmarshal(jsonData, &userData) if err != nil { @@ -90,8 +91,10 @@ var getCmd = &cobra.Command{ } } - folderKey := "/" + userData.UserID + "/keys/" - folderSecret := "/" + userData.UserID + "/secrets/" + email := strings.Replace(userData.Email, "@", "-", -1) + Newemail := strings.Replace(email, ".", "-", -1) + folderKey := "/" + Newemail + "/keys/" + folderSecret := "/" + Newemail + "/secrets/" if resp != nil && *resp.Status == "Success" { if resp.Result.Folder != folderKey { diff --git a/cmd/list.go b/cmd/list.go index 9e4c562..47a65b2 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -11,6 +11,7 @@ import ( "os" "os/user" "path/filepath" + "strings" "github.com/Siddheshk02/Securelee-cli/lib" "github.com/spf13/cobra" @@ -22,7 +23,6 @@ var listCmd = &cobra.Command{ Short: "Get all Secret Messages or Keys.", Long: `Get all Secret Messages or Keys.`, Run: func(cmd *cobra.Command, args []string) { - // fmt.Println("list called") res := lib.Check() if !res { fmt.Print("\033[31m", "\n > No User logged in, You must Login to use Securelee Vault Services.\n", "\033[0m") @@ -44,11 +44,14 @@ var listCmd = &cobra.Command{ var userData struct { UserID string `json:"userID"` + Email string `json:"email"` } err = json.Unmarshal(jsonData, &userData) if err != nil { log.Fatalln("\033[31m", err.Error(), "\033[0m") } + email := strings.Replace(userData.Email, "@", "-", -1) + Newemail := strings.Replace(email, ".", "-", -1) var choice int fmt.Print("\033[33m", "\n Select any one option: \n", "\033[0m") @@ -60,25 +63,16 @@ var listCmd = &cobra.Command{ fmt.Println("") if choice == 1 { - _, err := lib.ListSecrets(userData.UserID) + _, err := lib.ListSecrets(Newemail) if err != nil { log.Fatalln("\033[31m", err.Error(), "\033[0m") } } else if choice == 2 { - _, err := lib.ListKeys(userData.UserID) + _, err := lib.ListKeys(Newemail) if err != nil { log.Fatalln("\033[31m", err.Error(), "\033[0m") } - - // for i := 0; i < count; i++ { - // fmt.Println("\n> ", i+1, " id : ", lists[i].ID) - // fmt.Println(" Name : ", lists[i].Name) - // fmt.Println(" Type : ", lists[i].Type) - // fmt.Println(" Purpose : ", lists[i].Purpose) - // fmt.Println(" Algorithm : ", lists[i].Algorithm) - // } - } else { fmt.Println("\033[31m", " > Invalid Choice Entered!!, Please try again", "\033[0m") fmt.Println("") diff --git a/cmd/login.go b/cmd/login.go index b96b7a5..421d397 100644 --- a/cmd/login.go +++ b/cmd/login.go @@ -56,26 +56,18 @@ var loginCmd = &cobra.Command{ fmt.Scanf("%d", &choice) fmt.Println("") if choice == 1 { - // c := color.New(color.FgCyan, color.Bold) - // var str any fmt.Fprintf(out, "%s > Press Enter to Login using Browser%s", yellow, reset) fmt.Fprintf(out, "%s (You can get back to CLI after Successful Authentication) : %s", magenta, reset) - // fmt.Print("") _, _ = term.ReadPassword(int(os.Stdin.Fd())) - // _, key, _ := keyboard.GetSingleKey() fmt.Println("") - // Check if the key is Enter - // if key == keyboard.KeyEnter { lib.Login() time.Sleep(25 * time.Second) fmt.Fprintf(out, "%s\n > Please Enter the Token from the Securelee Authentication Tab: %s", yellow, reset) fmt.Scan(&token) - // time.Sleep() - // } if token == "" { fmt.Fprintf(out, "%s > Invalid Token. Please try again.%s", red, reset) @@ -95,7 +87,7 @@ var loginCmd = &cobra.Command{ fmt.Println("") fmt.Fprintf(out, "%s > Enter your Password %s", yellow, reset) fmt.Fprintf(out, "\n%s { Password must have \n - at least 8 characters,\n - at least 1 number characters,\n - at least 1 special characters } : %s", magenta, reset) - // fmt.Scan(&password) + password, _ := term.ReadPassword(int(os.Stdin.Fd())) fmt.Println("") check := lib.IsValidPassword(string(password)) @@ -146,33 +138,21 @@ var loginCmd = &cobra.Command{ Expiry: parsedTime, } - // err = mailing.SendMail(info.Name, info.Email) - // if err != nil { - // log.Fatalln("\033[31m", err.Error(), "\033[0m") - // } - if choice == 1 { - // fmt.Fprintf(out, "\n%s > Successfully Logged in as :%s", cyan, reset) fmt.Println("\033[36m", "\n > Successfully Logged in as ", info.Name, " (", info.Email, ")", "\033[0m") - // fmt.Print(info.Name, info.Email) fmt.Println("") } else if choice == 2 { if usertype == "Old User" { - // fmt.Fprintf(out, "\n%s > Successfully Logged in as :%s", cyan, reset) fmt.Println("\033[36m", "\n > Successfully Logged in as ", info.Name, " (", info.Email, ")", "\033[0m") - // fmt.Print(info.Name, info.Email) fmt.Println("") } else if usertype == "New User" { - // fmt.Fprintf(out, "\n%s > Successfully Created and Logged in as :%s", cyan, reset) fmt.Println("\033[36m", "\n > Successfully Created and Logged in as ", info.Name, " (", info.Email, ")", "\033[0m") - // fmt.Print(info.Name, info.Email) fmt.Println("") } } currentUser, err := user.Current() if err != nil { - // log.Fatal("\033[31m", " > Error occured!, try again.", "\033[0m") fmt.Fprintf(out, "%s > Error occured!, try again.%s", red, reset) return } @@ -180,7 +160,6 @@ var loginCmd = &cobra.Command{ path := currentUser.HomeDir + "/Securelee" err = os.MkdirAll(path, os.ModePerm) if err != nil { - // log.Fatal("\033[31m", "Error occured!, try again.", "\033[0m") fmt.Fprintf(out, "%s > Error occured!, try again.%s", red, reset) return } diff --git a/cmd/logout.go b/cmd/logout.go index 0de1eec..2082911 100644 --- a/cmd/logout.go +++ b/cmd/logout.go @@ -17,10 +17,8 @@ var logoutCmd = &cobra.Command{ Short: "Logout of Securelee Vault.", Long: `Logout of Securelee Vault.`, Run: func(cmd *cobra.Command, args []string) { - // fmt.Println("logout called") err := lib.Logout() if err != nil { - // log.Fatal(err, "Error, Please try again.") fmt.Print("\033[31m", "\n > Error, Please try Again\n", "\033[0m") os.Exit(0) } diff --git a/cmd/update.go b/cmd/update.go index 4b255f7..de3cf24 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -11,6 +11,7 @@ import ( "os" "os/user" "path/filepath" + "strings" "github.com/Siddheshk02/Securelee-cli/lib" "github.com/spf13/cobra" @@ -22,7 +23,6 @@ var updateCmd = &cobra.Command{ Short: "Update or Modify a Secret Message or Key.", Long: `Update or Modify a Secret Message or Key.`, Run: func(cmd *cobra.Command, args []string) { - // fmt.Println("update called") res := lib.Check() if !res { fmt.Print("\033[31m", "\n > No User logged in, You must Login to use Securelee Vault Services.\n", "\033[0m") @@ -44,11 +44,14 @@ var updateCmd = &cobra.Command{ var userData struct { UserID string `json:"userID"` + Email string `json:"email"` } err = json.Unmarshal(jsonData, &userData) if err != nil { log.Fatal("\033[31m", err, "\033[0m") } + email := strings.Replace(userData.Email, "@", "-", -1) + Newemail := strings.Replace(email, ".", "-", -1) var choice uint fmt.Print("\033[33m", "\n Select any one option: \n", "\033[0m") @@ -60,7 +63,7 @@ var updateCmd = &cobra.Command{ fmt.Println("") if choice == 1 { - res, err := lib.ListSecrets(userData.UserID) + res, err := lib.ListSecrets(Newemail) if err != nil { log.Fatal("\033[31m", err, "\033[0m") } @@ -86,7 +89,7 @@ var updateCmd = &cobra.Command{ } } else if choice == 2 { - res, err := lib.ListKeys(userData.UserID) + res, err := lib.ListKeys(Newemail) if err != nil { log.Fatal("\033[31m", err, "\033[0m") } diff --git a/cmd/whoami.go b/cmd/whoami.go index 8071661..ca71be4 100644 --- a/cmd/whoami.go +++ b/cmd/whoami.go @@ -22,7 +22,6 @@ var whoamiCmd = &cobra.Command{ Short: "The Current User of Securelee Vault.", Long: `The Current User of Securelee Vault.`, Run: func(cmd *cobra.Command, args []string) { - // fmt.Println("whoami called") res := lib.Check() if !res { fmt.Print("\033[31m", "\n > No User logged in, You must Login to use Securelee Vault Services.\n", "\033[0m") diff --git a/lib/store.go b/lib/store.go index 6f0b1f0..a943e33 100644 --- a/lib/store.go +++ b/lib/store.go @@ -16,7 +16,7 @@ import ( "github.com/pangeacyber/pangea-go/pangea-sdk/v2/service/vault" ) -func Create(ID string) error { +func Create(Email string) error { var choice int fmt.Print("\033[33m", "\n Select any one option: \n", "\033[0m") fmt.Println("\033[33m", "\n > 1. Store a Secret Message", "\033[0m") @@ -37,7 +37,7 @@ func Create(ID string) error { os.Exit(0) } - foldername := "/" + ID + foldername := "/" + Email ctx, cancelFn := context.WithTimeout(context.Background(), 60*time.Second) defer cancelFn() @@ -49,9 +49,7 @@ func Create(ID string) error { } _, _ = client.FolderCreate(ctx, input) - // if err != nil { - // continue - // } + folder := foldername + "/" + str if choice == 1 { @@ -65,7 +63,6 @@ func Create(ID string) error { fmt.Scan(&secretname) fmt.Println("") - // time.Sleep(15 * time.Second) input := &vault.SecretStoreRequest{ CommonStoreRequest: vault.CommonStoreRequest{ @@ -124,15 +121,9 @@ func StoreKey(folder string) (string, error, int) { fmt.Print("\033[33m", "\n Select any one option: \n", "\033[0m") fmt.Println("\033[33m", "\n > 1. Import a Key", "\033[0m") fmt.Println("\033[33m", "> 2. Generate a Key", "\033[0m") - // fmt.Println("") fmt.Print("\033[36m", "\n > Enter your choice (for e.g. 1): ", "\033[0m") fmt.Scan(&choice) - // _, err := fmt.Scanf("%d", &choice) - // if err != nil { - // log.Fatal(err) - // } fmt.Println("") - // fmt.Println(choice) if choice == 1 { ctx, cancelFn := context.WithTimeout(context.Background(), 60*time.Second) @@ -144,7 +135,6 @@ func StoreKey(folder string) (string, error, int) { fmt.Println("\033[33m", "> 2. Symmetric Key", "\033[0m") fmt.Println("") fmt.Print("\033[36m", " > Enter your choice (for e.g. 1): ", "\033[0m") - // fmt.Scanf("%d", &choice2) fmt.Scan(&choice2) fmt.Println("") @@ -253,7 +243,7 @@ func StoreKey(folder string) (string, error, int) { match := re.Find([]byte(err.Error())) if match == nil { - return "", errors.New("No JSON data found in the error message"), choice + return "", errors.New("No JSON data found in the error message, Please try again"), choice } var apiError APIError @@ -288,7 +278,6 @@ func StoreKey(folder string) (string, error, int) { fmt.Println("\033[33m", "> 2. Symmetric Key", "\033[0m") fmt.Println("") fmt.Print("\033[36m", " > Enter your choice (for e.g. 1): ", "\033[0m") - // fmt.Scanf("%d", &choice2) fmt.Scan(&choice2) fmt.Println("") @@ -355,7 +344,7 @@ func StoreKey(folder string) (string, error, int) { return "", nil, choice } -func ListSecrets(UserId string) (*pangea.PangeaResponse[vault.ListResult], error) { +func ListSecrets(Email string) (*pangea.PangeaResponse[vault.ListResult], error) { ctx, cancelFn := context.WithTimeout(context.Background(), 60*time.Second) defer cancelFn() client := InitVault() @@ -363,7 +352,7 @@ func ListSecrets(UserId string) (*pangea.PangeaResponse[vault.ListResult], error resp, err := client.List(ctx, &vault.ListRequest{ Filter: map[string]string{ - "folder": "/" + UserId + "/secrets", + "folder": "/" + Email + "/secrets", }, }) @@ -392,7 +381,7 @@ func ListSecrets(UserId string) (*pangea.PangeaResponse[vault.ListResult], error return nil, nil } -func ListKeys(UserId string) (*pangea.PangeaResponse[vault.ListResult], error) { +func ListKeys(Email string) (*pangea.PangeaResponse[vault.ListResult], error) { ctx, cancelFn := context.WithTimeout(context.Background(), 60*time.Second) defer cancelFn() client := InitVault() @@ -400,7 +389,7 @@ func ListKeys(UserId string) (*pangea.PangeaResponse[vault.ListResult], error) { resp, err := client.List(ctx, &vault.ListRequest{ Filter: map[string]string{ - "folder": "/" + UserId + "/keys", + "folder": "/" + Email + "/keys", }, }) @@ -486,8 +475,6 @@ func Update(id string, req string) error { fmt.Println("\033[33m", "> 3. Suspended", "\033[0m") fmt.Println("\033[33m", "> 4. Compromised", "\033[0m") fmt.Println("\033[33m", "> 5. Destroyed", "\033[0m") - // fmt.Println("> 1. Enable") - // fmt.Println("> 2. Inherited") fmt.Println("") fmt.Print("\033[36m", " > Enter your choice (for e.g. 1): ", "\033[0m") fmt.Scan(&choice) diff --git a/lib/user.go b/lib/user.go index 7c9e5d7..1d44df0 100644 --- a/lib/user.go +++ b/lib/user.go @@ -84,9 +84,6 @@ func CheckToken(token string) (*authn.ClientTokenCheckResult, string) { resp, err := client.Client.Token.Check(ctx, input) if err != nil && resp == nil { - // log.Fatal(err, "Error, Please Try Again.") - // fmt.Println("\n > No User logged in, You must Login to use Securelee Vault Services.") - // os.Exit(0) return nil, "No User" } @@ -125,7 +122,6 @@ func LoginWithEmail(Email string, Password string) (string, string, error) { fmt.Println("") fmt.Print("\033[33m", " > Enter your Last Name: ", "\033[0m") fmt.Scan(&last) - // fmt.Println("") profile := &authn.ProfileData{ "first_name": first, @@ -144,7 +140,6 @@ func LoginWithEmail(Email string, Password string) (string, string, error) { if err != nil || out == nil { fmt.Println("\033[31m", "\nFailed to create a new user", "\033[0m") os.Exit(0) - // log.Fatal(err) } //adding password for the user profile @@ -165,8 +160,6 @@ func LoginWithEmail(Email string, Password string) (string, string, error) { return "", str, nil } - // token := fmt.Sprintf("%s", result.ActiveToken.Token) - return result.ActiveToken.Token, usertype, nil } @@ -249,14 +242,12 @@ func NewUser(Email string) (string, error) { match := re.Find([]byte(err.Error())) if match == nil { - // log.Fatal("No JSON data found in the error message") return "", errors.New("No JSON data found in the error message") } var apiError APIError err = json.Unmarshal(match, &apiError) if err != nil { - // log.Fatal("Error unmarshalling JSON:", err) return "", err } @@ -264,7 +255,5 @@ func NewUser(Email string) (string, error) { return apiError.Status, nil } - // fmt.Println(apiError.Status) - return *resp.Status, nil }