From 961c3f3fa5b46f302d63669215ecabd267ba28cc Mon Sep 17 00:00:00 2001 From: Davide Caruso Date: Sun, 4 Oct 2020 14:49:10 +0200 Subject: [PATCH] Do not store password in history; optimizations --- .gitignore | 1 - README.md | 44 ++++++++++++++++++++++------------------ assets/.gitignore | 1 + cmd/add.go | 26 +++++++++++++++++++----- cmd/del.go | 12 ++++++++++- cmd/get.go | 12 ++++++++++- cmd/root.go | 12 +---------- cmd/version.go | 10 --------- internal/service/main.go | 4 ++-- internal/storage/main.go | 6 +++--- tools/main.go | 6 ++++++ 11 files changed, 80 insertions(+), 54 deletions(-) create mode 100644 assets/.gitignore diff --git a/.gitignore b/.gitignore index ba3cf15..69c2742 100644 --- a/.gitignore +++ b/.gitignore @@ -16,4 +16,3 @@ vendor/ .idea/ go.sum -assets/.hosts diff --git a/README.md b/README.md index d82a4ab..924381d 100644 --- a/README.md +++ b/README.md @@ -2,24 +2,28 @@ ``` Store your services' credentials and load passwords in clipboard when you need them: - - kcc add -s facebook.com -u john@doe.com -p secret - kcc get -s facebook.com -u john@doe.com - - Usage: - kcc [command] - - Available Commands: - add Store service credentials - del Delete service credentials - get Get password - help Help about any command - version Show current version - - Flags: - --config string config file (default is $HOME/.kcc.yaml) - -h, --help help for kcc - -t, --toggle Help message for toggle - - Use "kcc [command] --help" for more information about a command. + +kcc add -s facebook.com -u john@doe.com +kcc get -s facebook.com -u john@doe.com + +Usage: + kcc [command] + +Available Commands: + add Store service credentials + del Delete service credentials + get Get password + help Help about any command + version Show current version + +Flags: + -h, --help help for kcc + +Use "kcc [command] --help" for more information about a command. ``` + +## Author +[Davide Caruso](https://about.me/davidecaruso) + +## License +Licensed under [MIT](LICENSE). diff --git a/assets/.gitignore b/assets/.gitignore new file mode 100644 index 0000000..01aa009 --- /dev/null +++ b/assets/.gitignore @@ -0,0 +1 @@ +.storage diff --git a/cmd/add.go b/cmd/add.go index c1fa813..bd63bc1 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -26,6 +26,7 @@ import ( "github.com/spf13/cobra" "kcc/internal/service" "kcc/internal/storage" + "kcc/tools" ) var s service.Service @@ -36,11 +37,27 @@ var addCmd = &cobra.Command{ Short: "Store service credentials", Long: `Examples: -kcc add -s facebook.com -u john@doe.com -p secret -kcc add -s 176.69.100.144 -u johndoe -p secret`, +kcc add -s facebook.com -u john@doe.com +kcc add -s 176.69.100.144 -u johndoe`, Run: func(cmd *cobra.Command, args []string) { - if _, err := storage.S.Add(s); err != nil { + if s.User == "" || len(s.User) == 0 { + fmt.Println("Invalid user") + return + } + + if s.Service == "" || len(s.Service) == 0 { + fmt.Println("Invalid service") + return + } + + password, err := tools.Input("Enter service password: ") + if err != nil { fmt.Println(err) + return + } + + s.Password = password + if _, err := storage.S.Add(s); err != nil { } else { fmt.Println("Ok") } @@ -50,6 +67,5 @@ kcc add -s 176.69.100.144 -u johndoe -p secret`, func init() { rootCmd.AddCommand(addCmd) addCmd.Flags().StringVarP(&s.User, "user", "u", "", "The user name") - addCmd.Flags().StringVarP(&s.Host, "service", "s", "", "The service name") - addCmd.Flags().StringVarP(&s.Password, "password", "p", "", "The user password") + addCmd.Flags().StringVarP(&s.Service, "service", "s", "", "The service name") } diff --git a/cmd/del.go b/cmd/del.go index 450bf0e..61a2182 100644 --- a/cmd/del.go +++ b/cmd/del.go @@ -37,6 +37,16 @@ var delCmd = &cobra.Command{ kcc del -s facebook.com -u john@doe.com kcc del -s 176.69.100.144 -u johndoe`, Run: func(cmd *cobra.Command, args []string) { + if s.User == "" || len(s.User) == 0 { + fmt.Println("Invalid user") + return + } + + if s.Service == "" || len(s.Service) == 0 { + fmt.Println("Invalid service") + return + } + if _, err := storage.S.Delete(s); err != nil { fmt.Println(err) } else { @@ -48,5 +58,5 @@ kcc del -s 176.69.100.144 -u johndoe`, func init() { rootCmd.AddCommand(delCmd) delCmd.Flags().StringVarP(&s.User, "user", "u", "", "The user name") - delCmd.Flags().StringVarP(&s.Host, "service", "s", "", "The service name") + delCmd.Flags().StringVarP(&s.Service, "service", "s", "", "The service name") } diff --git a/cmd/get.go b/cmd/get.go index 551c2cc..f642e16 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -37,6 +37,16 @@ var getCmd = &cobra.Command{ kcc get -s facebook.com -u john@doe.com kcc get -s 176.69.100.144 -u johndoe`, Run: func(cmd *cobra.Command, args []string) { + if s.User == "" || len(s.User) == 0 { + fmt.Println("Invalid user") + return + } + + if s.Service == "" || len(s.Service) == 0 { + fmt.Println("Invalid service") + return + } + if err := storage.S.Get(s); err != nil { fmt.Println(err) } else { @@ -48,5 +58,5 @@ kcc get -s 176.69.100.144 -u johndoe`, func init() { rootCmd.AddCommand(getCmd) getCmd.Flags().StringVarP(&s.User, "user", "u", "", "The user name") - getCmd.Flags().StringVarP(&s.Host, "service", "s", "", "The service name") + getCmd.Flags().StringVarP(&s.Service, "service", "s", "", "The service name") } diff --git a/cmd/root.go b/cmd/root.go index 7a64d7f..0285452 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -38,7 +38,7 @@ var rootCmd = &cobra.Command{ Short: "KeyChainClipboard", Long: `Store your services' credentials and load passwords in clipboard when you need them: -kcc add -s facebook.com -u john@doe.com -p secret +kcc add -s facebook.com -u john@doe.com kcc get -s facebook.com -u john@doe.com`, // Uncomment the following line if your bare application // has an action associated with it: @@ -56,16 +56,6 @@ func Execute() { func init() { cobra.OnInitialize(initConfig) - - // Here you will define your flags and configuration settings. - // Cobra supports persistent flags, which, if defined here, - // will be global for your application. - - rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.kcc.yaml)") - - // Cobra also supports local flags, which will only run - // when this action is called directly. - rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") } // initConfig reads in config file and ENV variables if set. diff --git a/cmd/version.go b/cmd/version.go index 719f88b..0de0254 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -39,14 +39,4 @@ var versionCmd = &cobra.Command{ func init() { rootCmd.AddCommand(versionCmd) - - // Here you will define your flags and configuration settings. - - // Cobra supports Persistent Flags which will work for this command - // and all subcommands, e.g.: - // versionCmd.PersistentFlags().String("foo", "", "A help for foo") - - // Cobra supports local flags which will only run when this command - // is called directly, e.g.: - // versionCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") } diff --git a/internal/service/main.go b/internal/service/main.go index 1a892c6..e7ecd1e 100644 --- a/internal/service/main.go +++ b/internal/service/main.go @@ -28,12 +28,12 @@ import ( type Service struct { User string `json:"user"` - Host string `json:"host"` + Service string `json:"service"` Password string `json:"password"` } func (s Service) Key() string { - text := s.User + s.Host + text := s.User + s.Service hash := md5.New() hash.Write([]byte(text)) return hex.EncodeToString(hash.Sum(nil)) diff --git a/internal/storage/main.go b/internal/storage/main.go index 6cbf92c..d512166 100644 --- a/internal/storage/main.go +++ b/internal/storage/main.go @@ -40,11 +40,11 @@ type Storage struct { var ( cwd, _ = os.Getwd() - S = Storage{path: cwd + "/assets/.hosts"} + S = Storage{path: cwd + "/assets/.storage"} ) func (s *Storage) lock() error { - cmd := exec.Command("/bin/sh", "-c", "sudo chown root:root "+s.path) + cmd := exec.Command("/bin/sh", "-c", "sudo chown 0:0 "+s.path) cmd.Stderr = os.Stderr cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout @@ -130,7 +130,7 @@ func (s *Storage) Get(se service.Service) error { } if _, exists := s.data[se.Key()]; !exists { - fmt.Println("Not found") + fmt.Println("Service not found") return nil } diff --git a/tools/main.go b/tools/main.go index df48559..f2a37fa 100644 --- a/tools/main.go +++ b/tools/main.go @@ -48,3 +48,9 @@ func Confirm(s string) bool { } } } + +func Input(s string) (string, error) { + reader := bufio.NewReader(os.Stdin) + fmt.Print(s) + return reader.ReadString('\n') +}