Skip to content

Commit

Permalink
Do not store password in history; optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
davidecaruso committed Oct 4, 2020
1 parent 319a1f2 commit 961c3f3
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 54 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,3 @@ vendor/

.idea/
go.sum
assets/.hosts
44 changes: 24 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
1 change: 1 addition & 0 deletions assets/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.storage
26 changes: 21 additions & 5 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/spf13/cobra"
"kcc/internal/service"
"kcc/internal/storage"
"kcc/tools"
)

var s service.Service
Expand All @@ -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")
}
Expand All @@ -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")
}
12 changes: 11 additions & 1 deletion cmd/del.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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")
}
12 changes: 11 additions & 1 deletion cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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")
}
12 changes: 1 addition & 11 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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.
Expand Down
10 changes: 0 additions & 10 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
4 changes: 2 additions & 2 deletions internal/service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
6 changes: 3 additions & 3 deletions internal/storage/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
6 changes: 6 additions & 0 deletions tools/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}

0 comments on commit 961c3f3

Please sign in to comment.