/
add.go
executable file
·131 lines (104 loc) · 4.4 KB
/
add.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
Copyright © 2019 ServerAuth.com <info@serverauth.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package cmd
import (
"log"
"os"
"os/user"
"strconv"
"github.com/spf13/viper"
"github.com/fatih/color"
"github.com/spf13/cobra"
)
var username string
var apikey string
// addCmd represents the add command
var addCmd = &cobra.Command{
Use: "add",
Short: "Add a system account to ServerAuth",
Long: `Add a new system account (e.g root) to ServerAuth to have it's SSH Keys automatically managed.`,
Run: func(cmd *cobra.Command, args []string) {
// Check the user exists on the server
u, err := user.Lookup(username)
if err != nil {
color.Red("Unable to find user `%s`. Please check the username, and re-create the user on ServerAuth.", username)
os.Exit(1)
}
color.Green("Found system user: %s\nSetting up ServerAuth for the account.", u.Username)
// Read in the existing accounts and get ready for adding another user
viper.ReadInConfig()
var accounts []Account
configErr := viper.UnmarshalKey("accounts", &accounts)
if configErr != nil {
panic("There was a problem setting up the user account. Please try again or contact ServerAuth for assistance")
}
for _, data := range accounts {
if data.Username == username {
color.Green("The user %s is already configured - no changes needed.", data.Username)
os.Exit(1)
}
}
// Append the new account and update the config
accounts = append(accounts, Account{username, apikey})
viper.Set("accounts", accounts)
viper.WriteConfig()
// Work out the uid and gid for chowning
uid, _ := strconv.Atoi(u.Uid)
gid, _ := strconv.Atoi(u.Gid)
// Set up our path and file vars
homeDir := u.HomeDir
keysDir := homeDir + "/.ssh"
keysFile := keysDir + "/authorized_keys"
backupKeysFile := keysDir + "/authorized_keys.bak"
// If the .ssh directory doesnt exist, create it and set it to be owned by the user
if _, keysDirErr := os.Stat(keysDir); os.IsNotExist(keysDirErr) {
color.Yellow("It looks like " + keysDir + " does not yet exist. Lets create it now.")
os.MkdirAll(keysDir, 0700)
os.Chown(keysDir, uid, gid)
}
// If we find an authorized_keys file, move it to a backup location so we dont overwrite it.
if _, keysFileErr := os.Stat(keysFile); !os.IsNotExist(keysFileErr) {
// File exists, move it to its backup location
backupFileErr := os.Rename(keysFile, backupKeysFile)
if backupFileErr != nil {
log.Fatal(backupFileErr)
}
color.Yellow("An existing authorized_keys file was found. This has been moved to %s", backupKeysFile)
}
// Create our authorized_keys file
file, fileError := os.Create(keysFile)
if fileError != nil {
panic("Unable to create authorized_keys file. Please check that the user you are running the agent as has the correct privieges.")
}
// Write the template to the file
file.Write(keysFileTemplate)
// Ensure the file is owned by the correct user
os.Chown(keysFile, uid, gid)
color.Green("The user was successfully configured and is now managed by ServerAuth.")
},
}
func init() {
rootCmd.AddCommand(addCmd)
// User flag
addCmd.Flags().StringVarP(&username, "username", "u", "", "The username of the system account to add to ServerAuth")
addCmd.MarkFlagRequired("user")
// API Key Flag
addCmd.Flags().StringVarP(&apikey, "apikey", "k", "", "The unique API Key for the system account, provided when adding the account via your ServerAuth control panel.")
addCmd.MarkFlagRequired("api-key")
}