96ddc11f470aca96894eeb540e30f57bb587f227

Author
Ayman Bagabas <ayman.bagabas@gmail.com>
Committer
Ayman Bagabas <ayman.bagabas@gmail.com>
Date

Message

fix(server): add ssh commands for admin keys

Diff

 1diff --git a/server/cmd/cmd.go b/server/cmd/cmd.go
 2index 436f5ea8526c7ec0809898e6f28aa41f53a6b0a6..333861ca3cbd223a562c6d299acca19bc0c89325 100644
 3--- a/server/cmd/cmd.go
 4+++ b/server/cmd/cmd.go
 5@@ -141,8 +141,9 @@ func rootCommand(cfg *config.Config, s ssh.Session) *cobra.Command {
 6 	)
 7 
 8 	user, _ := cfg.Backend.UserByPublicKey(s.PublicKey())
 9-	if user != nil {
10-		if user.IsAdmin() {
11+	isAdmin := isPublicKeyAdmin(cfg, s.PublicKey()) || (user != nil && user.IsAdmin())
12+	if user != nil || isAdmin {
13+		if isAdmin {
14 			rootCmd.AddCommand(
15 				settingsCommand(),
16 				userCommand(),
17@@ -180,14 +181,21 @@ func checkIfReadable(cmd *cobra.Command, args []string) error {
18 	return nil
19 }
20 
21-func checkIfAdmin(cmd *cobra.Command, _ []string) error {
22-	cfg, s := fromContext(cmd)
23-	ak := backend.MarshalAuthorizedKey(s.PublicKey())
24+func isPublicKeyAdmin(cfg *config.Config, pk ssh.PublicKey) bool {
25 	for _, k := range cfg.InitialAdminKeys {
26-		if k == ak {
27-			return nil
28+		pk2, _, err := backend.ParseAuthorizedKey(k)
29+		if err == nil && backend.KeysEqual(pk, pk2) {
30+			return true
31 		}
32 	}
33+	return false
34+}
35+
36+func checkIfAdmin(cmd *cobra.Command, _ []string) error {
37+	cfg, s := fromContext(cmd)
38+	if isPublicKeyAdmin(cfg, s.PublicKey()) {
39+		return nil
40+	}
41 
42 	user, _ := cfg.Backend.UserByPublicKey(s.PublicKey())
43 	if user == nil {