Skip to content

Commit

Permalink
fix(server): add ssh commands for admin keys
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed May 2, 2023
1 parent 4b148fb commit 96ddc11
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions server/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,9 @@ func rootCommand(cfg *config.Config, s ssh.Session) *cobra.Command {
)

user, _ := cfg.Backend.UserByPublicKey(s.PublicKey())
if user != nil {
if user.IsAdmin() {
isAdmin := isPublicKeyAdmin(cfg, s.PublicKey()) || (user != nil && user.IsAdmin())
if user != nil || isAdmin {
if isAdmin {
rootCmd.AddCommand(
settingsCommand(),
userCommand(),
Expand Down Expand Up @@ -180,14 +181,21 @@ func checkIfReadable(cmd *cobra.Command, args []string) error {
return nil
}

func checkIfAdmin(cmd *cobra.Command, _ []string) error {
cfg, s := fromContext(cmd)
ak := backend.MarshalAuthorizedKey(s.PublicKey())
func isPublicKeyAdmin(cfg *config.Config, pk ssh.PublicKey) bool {
for _, k := range cfg.InitialAdminKeys {
if k == ak {
return nil
pk2, _, err := backend.ParseAuthorizedKey(k)
if err == nil && backend.KeysEqual(pk, pk2) {
return true
}
}
return false
}

func checkIfAdmin(cmd *cobra.Command, _ []string) error {
cfg, s := fromContext(cmd)
if isPublicKeyAdmin(cfg, s.PublicKey()) {
return nil
}

user, _ := cfg.Backend.UserByPublicKey(s.PublicKey())
if user == nil {
Expand Down

0 comments on commit 96ddc11

Please sign in to comment.