Browse Source

import

master
Vladimir Smagin 4 months ago
commit
6e61ef51ab
4 changed files with 141 additions and 0 deletions
  1. 23
    0
      README.md
  2. 61
    0
      cmd/import.go
  3. 47
    0
      cmd/root.go
  4. 10
    0
      main.go

+ 23
- 0
README.md View File

@@ -0,0 +1,23 @@
# Sample

```
vlad@turtle:/opt/dev/-go/ssh-vault$ go build && ./ssh-vault import --username=nickd --key-public=keys/nick_diamond.pem.pub --key-secret=keys/nick_diamond.pem --name-first=Nick
Error: required flag(s) "name-last" not set
Usage:
ssh-vault import [flags]

Flags:
-h, --help help for import
-p, --key-public string Public key file
-s, --key-secret string Secret key file
-f, --name-first string First name
-l, --name-last string Last name
-u, --username string User's login

required flag(s) "name-last" not set
vlad@turtle:/opt/dev/-go/ssh-vault$ go build && ./ssh-vault import --username=nickd --key-public=keys/nick_diamond.pem.pub --key-secret=keys/nick_diamond.pem --name-first=Nick --name-last=Diamond
2019/07/03 02:59:52 Importing key pair
2019/07/03 02:59:53 <nil>
vlad@turtle:/opt/dev/-go/ssh-vault$
```


+ 61
- 0
cmd/import.go View File

@@ -0,0 +1,61 @@
package cmd

import (
"log"

"github.com/spf13/cobra"
)

type userData struct {
nameFirst string
nameLast string
username string
keySecret string
keyPublic string
}

var userinfo userData

var importCmd = &cobra.Command{
Use: "import",
Short: "imports key pair and user info",
Long: `This command helps you to import key pair, user's login and real name.`,
Run: func(cmd *cobra.Command, args []string) {

log.Println("Importing key pair")

err := InitVault(VaultToken)
if err != nil {
log.Println(err)
}
c := VClient.Logical()
nameFull := userinfo.username
if userinfo.nameFirst != "" && userinfo.nameLast != "" {
nameFull = userinfo.nameFirst + " " + userinfo.nameLast
}
secret, err := c.Write("ssh-keys/"+userinfo.username,
map[string]interface{}{
"name-first": userinfo.nameFirst,
"name-last": userinfo.nameLast,
"name": nameFull,
"username": userinfo.username,
"key-secret": userinfo.keySecret,
"key-public": userinfo.keyPublic,
})
if err != nil {
log.Println(err)
}
log.Println(secret)
},
}

func init() {
importCmd.Flags().StringVarP(&userinfo.username, "username", "u", "", "User's login, required")
importCmd.MarkFlagRequired("username")
importCmd.Flags().StringVarP(&userinfo.keyPublic, "key-public", "p", "", "Public key file, required")
importCmd.MarkFlagRequired("key-public")
importCmd.Flags().StringVarP(&userinfo.keySecret, "key-secret", "s", "", "Secret key file")
importCmd.Flags().StringVarP(&userinfo.nameFirst, "name-first", "f", "", "First name")
importCmd.Flags().StringVarP(&userinfo.nameLast, "name-last", "l", "", "Last name")
rootCmd.AddCommand(importCmd)
}

+ 47
- 0
cmd/root.go View File

@@ -0,0 +1,47 @@
package cmd

import (
"fmt"
"os"

"github.com/hashicorp/vault/api"
"github.com/spf13/cobra"
)

var VClient *api.Client // global variable

var VaultToken = "a.352sdflgslfh3lkj4h53lk4j"

func InitVault(token string) error {
conf := &api.Config{
Address: "https://vault.server.ru",
}

client, err := api.NewClient(conf)
if err != nil {
return err
}
VClient = client

VClient.SetToken(token)
return nil
}

var rootCmd = &cobra.Command{
Use: "ssh-vault",
Short: "ssh-vault is a simple ssh access control application",
Long: `Requres Vault KV storage and some luck.`,
Run: func(cmd *cobra.Command, args []string) {
// Do Stuff Here
},
}

func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func init() {
cobra.OnInitialize()
}

+ 10
- 0
main.go View File

@@ -0,0 +1,10 @@
package main

import (
"./cmd"
)

func main() {
cmd.Execute()

}

Loading…
Cancel
Save