This repository has been archived by the owner on Jun 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from yousefvand/cli
Cli
- Loading branch information
Showing
45 changed files
with
1,391 additions
and
421 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package cmd | ||
|
||
import ( | ||
"bufio" | ||
"io/ioutil" | ||
"os" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/yousefvand/secret-service/pkg/crypto" | ||
) | ||
|
||
func init() { | ||
rootCmd.AddCommand(decryptCmd) | ||
decryptCmd.Flags().StringP("password", "p", "", "database password") | ||
|
||
decryptCmd.Flags().StringP("input", "i", "", "input file") | ||
decryptCmd.Flags().StringP("output", "o", "", "output file") | ||
} | ||
|
||
var decryptCmd = &cobra.Command{ | ||
Use: "decrypt", | ||
Short: "decrypt a database", | ||
Long: `decrypt a credential database using provided password`, | ||
Run: func(cmd *cobra.Command, _ []string) { | ||
|
||
password, _ := cmd.Flags().GetString("password") | ||
input, _ := cmd.Flags().GetString("input") | ||
output, _ := cmd.Flags().GetString("output") | ||
|
||
if len(password) != 32 { | ||
panic("Wrong password length. Password should be exactly 32 characters.") | ||
} | ||
|
||
if exist, _ := fileOrFolderExists(input); !exist { | ||
panic("Input file doesn't exist") | ||
} | ||
|
||
file, err := os.Open(input) | ||
|
||
if err != nil { | ||
panic("failed to open input file") | ||
} | ||
defer file.Close() | ||
|
||
scanner := bufio.NewScanner(file) | ||
scanner.Split(bufio.ScanLines) | ||
var i int = 0 | ||
var signature bool = false | ||
var decrypted []string | ||
|
||
for scanner.Scan() { | ||
i++ | ||
line := scanner.Text() | ||
if line == " \"encrypted\": true," { | ||
signature = true | ||
} | ||
if i > 2 && !signature { | ||
panic("Wrong type of file. This file is not marked as \"encrypted\"!") | ||
} | ||
if index := strings.Index(line, "secretText"); index > 0 { | ||
cipher := line[21 : len(line)-1] | ||
decrypt, err := crypto.DecryptAESCBC256(password, cipher) | ||
if err != nil { | ||
panic("Decryption failed: " + err.Error()) | ||
} | ||
newLine := line[:21] + decrypt + "\"" | ||
decrypted = append(decrypted, newLine) | ||
} else { | ||
decrypted = append(decrypted, line) | ||
} | ||
} | ||
|
||
fileContent := strings.Join(decrypted, "\n") | ||
err = ioutil.WriteFile(output, []byte(fileContent), 0755) | ||
if err != nil { | ||
panic("Writing to output file failed: " + output) | ||
} | ||
|
||
}, | ||
} | ||
|
||
func fileOrFolderExists(path string) (bool, error) { | ||
_, err := os.Stat(path) | ||
if err == nil { | ||
return true, nil | ||
} | ||
if os.IsNotExist(err) { | ||
return false, nil | ||
} | ||
return false, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package cmd | ||
|
||
import ( | ||
"bufio" | ||
"io/ioutil" | ||
"os" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/yousefvand/secret-service/pkg/crypto" | ||
) | ||
|
||
func init() { | ||
rootCmd.AddCommand(encryptCmd) | ||
encryptCmd.Flags().StringP("password", "p", "", "database password") | ||
|
||
encryptCmd.Flags().StringP("input", "i", "", "input file") | ||
encryptCmd.Flags().StringP("output", "o", "", "output file") | ||
} | ||
|
||
var encryptCmd = &cobra.Command{ | ||
Use: "encrypt", | ||
Short: "encrypt a database", | ||
Long: `encrypt a credential database using provided password`, | ||
Run: func(cmd *cobra.Command, _ []string) { | ||
|
||
password, _ := cmd.Flags().GetString("password") | ||
input, _ := cmd.Flags().GetString("input") | ||
output, _ := cmd.Flags().GetString("output") | ||
|
||
if len(password) != 32 { | ||
panic("Wrong password length. Password should be exactly 32 characters.") | ||
} | ||
|
||
if exist, _ := fileOrFolderExists(input); !exist { | ||
panic("Input file doesn't exist") | ||
} | ||
|
||
file, err := os.Open(input) | ||
|
||
if err != nil { | ||
panic("failed to open input file") | ||
} | ||
defer file.Close() | ||
|
||
scanner := bufio.NewScanner(file) | ||
scanner.Split(bufio.ScanLines) | ||
var i int = 0 | ||
var signature bool = false | ||
var encrypted []string | ||
|
||
for scanner.Scan() { | ||
i++ | ||
line := scanner.Text() | ||
if line == " \"encrypted\": false," { | ||
signature = true | ||
} | ||
if i > 2 && !signature { | ||
panic("Wrong type of file. This file is not marked as non \"encrypted\"!") | ||
} | ||
if index := strings.Index(line, "secretText"); index > 0 { | ||
text := line[21 : len(line)-1] | ||
cipher, err := crypto.EncryptAESCBC256(password, text) | ||
if err != nil { | ||
panic("Encryption failed: " + err.Error()) | ||
} | ||
newLine := line[:21] + cipher + "\"" | ||
encrypted = append(encrypted, newLine) | ||
} else { | ||
encrypted = append(encrypted, line) | ||
} | ||
} | ||
|
||
fileContent := strings.Join(encrypted, "\n") | ||
err = ioutil.WriteFile(output, []byte(fileContent), 0755) | ||
if err != nil { | ||
panic("Writing to output file failed: " + output) | ||
} | ||
|
||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/yousefvand/secret-service/pkg/client" | ||
) | ||
|
||
func init() { | ||
rootCmd.AddCommand(exportDbCmd) | ||
} | ||
|
||
var exportDbCmd = &cobra.Command{ | ||
Use: "export db", | ||
Short: "export db exports an unencrypted version of db", | ||
Long: `export db exports an unencrypted version of credentials database`, | ||
Run: func(_ *cobra.Command, _ []string) { | ||
|
||
ssClient, _ := client.New() | ||
response, _ := ssClient.SecretServiceCommand("export database", "") | ||
|
||
if response == "ok" { | ||
fmt.Println("database export completed successfully") | ||
} else { | ||
fmt.Println("database export failed!") | ||
} | ||
}, | ||
} |
Oops, something went wrong.