Skip to content

Commit

Permalink
Merge pull request #5 from k1LoW/create-key
Browse files Browse the repository at this point in the history
Add `--create` option to create [USER_HOME_DIR]/.ssh/autorized_keys
  • Loading branch information
k1LoW authored Nov 16, 2020
2 parents febd9ff + 2ec7599 commit b330564
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Example: Collect public keys from [GitHub](https://github.com/) ( user: `alice`,

``` console
$ export GITHUB_TOKEN=xxXXXXxxxxXXXXXXxxxXXX
$ keyp collect -b github -u alice -u bob -t myorg/administrators
$ keyp collect -b github -u alice -u bob -t my-org/developers
ssh-rsa C1yc2EAAAADAQABAAACAQCmnCCt6PyH9jLZbPkMijSJYIu14nhxyFVw9M9eAkgcMQ3EsKf86GWlGPDfZcqcDqI+LP7LKQk4kAlmLOQQMavthrhGEURxxdX0Yk2A6pHjG3zrcW7X30ZBMwOzX/a6EWkPXPwPH6LcP3rM9yEIg95f2JntxO3z7l/8QjzJCoPIlqyoX4I7bxHus/rZVnRNh9C0PbUejbg/iWaTTxkNglSeEYpW+ID2k/4Absisa5XyY2zEOMw+6OyfRL9AlfGYv545J0g90qWS11iRSFnMR7A6FNUea/pVESIMmmBI56Ne+S8NmwR724u3d5kNJxuAKpmtThNPdxW/vmtuc5XBZgtPX/rzdAW0TQZvpVoLnoKaqYgfIpcrrkSAoPlcVxfq/NrpVlbIi6c9rZRZR4dcqmAK2eBGuDQZBiYJSESuPbE6i08GGnM8OblD8pshVeGMStztR+NuXywIXbRpyqNF2VNjil8r4qGNW9AB8ZVUB/1s6U8oxvtlbABoxXLrdNlKj3rl2YoPIZyCLAp9QDch8P1SnmQTEZK67YY5KNQrJZ2ql7pblo84JqsRbwuOrexTz6xrbBWFZMHWorFqF8ryX0LOw9TIaHbYqleynhqJ0a8VJHZMmwndYKjw3brtJ3SfCpXU0826LOExWXcjfBqHK65gM+MQ==
ssh-rsa AAADAQABAAACAQC8IEt4MqBed/yXQyjUTCZRdZoCUNhm0bEkOV8Ef5TduQvMIPDpBYyYIvFz7jxJyShPoiTMtIUnkkA2aDF0jhujFzqKmYm9H2tS7Tpf5iNwRJgJJJWv674tGUcu+6+ZadmDBQ//dwo8XWTHxmkWfgaybxs8/o0AlwZQ4pYFcky0q+/qP4cwPAmRW0rGCo0E5BhS/5eGssoLBXu4/Hcaz/93H8AtAe1UQrlCKma0rj0HIA9A9Q9EQtunw/zJTBtTyzE/TvxKcSMNulgdVmFSRmU6l84Ftc6tZPoiaCnxcvQUyjCEeQfy4DbtCWe1tEubyKeBLBTXTnpqWA3Gs9GryQA/bR7Ivan/03FshLFeVVnbvvO11sKNvkAJ8u417Q2/G9bcB1H30Xa9PSRE+2CbQ2maafhPVL17TJVBvkDCM5trmwxfM2tdlKA7R+mTj9nIrSLN4BYrge8IZ1fesC/sKMlMwhNEOrQYQQIZMIx8hfLAS37D8wbUPRodQFJsolrK6cHlNICR/TLcijNhCeHJkD8448EuJn1BCbYKglG7eUYKLbMXcVJAoTPlFTHPU80oaHJhmpLe0vFSxrhWVf/ha81zRefXOiye7Pbn/h+sa2qsKTnAMShpS1m+RP7QmHNmFAbHlPeTlnd0oJI/bt5Mysn5HHjX4vAJdQ==
[...]
Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ var (
teams []string
keepKeys []string
logTo string
create bool
)

var rootCmd = &cobra.Command{
Expand Down
27 changes: 23 additions & 4 deletions cmd/updateAuthorizedKeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,29 @@ var updateAuthorizedKeysCmd = &cobra.Command{
if u.HomeDir == "" {
return fmt.Errorf("'%s' does not have home directory", u.Name)
}
aKeys := filepath.Join(u.HomeDir, ".ssh", "authorized_keys")
dotSSHDir := filepath.Join(u.HomeDir, ".ssh")
aKeys := filepath.Join(dotSSHDir, "authorized_keys")
if _, err := os.Stat(aKeys); err != nil {
return err
}
if len(keepKeys) > 0 {
// [USER_HOME_DIR]/.ssh/autorized_keys
if !create {
return err
}
if _, err := os.Stat(u.HomeDir); err != nil {
return err
}
if _, err := os.Stat(dotSSHDir); err != nil {
if err := os.Mkdir(dotSSHDir, 0700); err != nil {
return err
}
}
f, err := os.OpenFile(aKeys, os.O_RDWR|os.O_CREATE, 0600)
if err != nil {
return err
}
if err := f.Close(); err != nil {
return err
}
} else if len(keepKeys) > 0 {
current, err := ioutil.ReadFile(filepath.Clean(aKeys))
if err != nil {
return err
Expand All @@ -96,6 +114,7 @@ func init() {
updateAuthorizedKeysCmd.Flags().StringSliceVarP(&teams, "team", "t", []string{}, "target org team")
updateAuthorizedKeysCmd.Flags().StringSliceVarP(&keepKeys, "keep-key", "k", []string{}, "substring of the key not to be overwritten on update")
updateAuthorizedKeysCmd.Flags().StringVarP(&logTo, "log", "l", "", "log")
updateAuthorizedKeysCmd.Flags().BoolVarP(&create, "create", "c", false, "create [USER_HOME_DIR]/.ssh/autorized_keys when it does not exist.")
if err := updateAuthorizedKeysCmd.MarkFlagRequired("backend"); err != nil {
updateAuthorizedKeysCmd.PrintErrln(err)
os.Exit(1)
Expand Down

0 comments on commit b330564

Please sign in to comment.