2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-05-16 11:32:00 -04:00
|
|
|
package ssh
|
2022-03-22 11:03:15 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/deploy/user"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/logger"
|
2022-06-28 10:51:30 -04:00
|
|
|
"go.uber.org/zap"
|
2022-03-22 11:03:15 -04:00
|
|
|
)
|
|
|
|
|
2022-05-16 11:32:00 -04:00
|
|
|
// UserKey describes an user that should be created with a corresponding public SSH key.
|
|
|
|
type UserKey struct {
|
2022-05-17 04:52:37 -04:00
|
|
|
Username string
|
|
|
|
PublicKey string
|
2022-05-16 11:32:00 -04:00
|
|
|
}
|
|
|
|
|
2022-06-13 10:23:19 -04:00
|
|
|
// Access reads SSH public keys from a channel, creates the specified users if required and writes the public keys to the users authorized_keys file.
|
|
|
|
type Access struct {
|
2022-06-28 10:51:30 -04:00
|
|
|
log *logger.Logger
|
2022-05-16 11:32:00 -04:00
|
|
|
userManager user.LinuxUserManager
|
2022-08-09 03:05:05 -04:00
|
|
|
authorized map[UserKey]bool
|
2022-03-22 11:03:15 -04:00
|
|
|
mux sync.Mutex
|
|
|
|
}
|
|
|
|
|
2022-06-13 10:23:19 -04:00
|
|
|
// NewAccess creates a new Access.
|
2022-06-28 10:51:30 -04:00
|
|
|
func NewAccess(log *logger.Logger, userManager user.LinuxUserManager) *Access {
|
2022-06-13 10:23:19 -04:00
|
|
|
return &Access{
|
2022-06-28 10:51:30 -04:00
|
|
|
log: log,
|
2022-05-16 11:32:00 -04:00
|
|
|
userManager: userManager,
|
2022-08-09 03:05:05 -04:00
|
|
|
authorized: map[UserKey]bool{},
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// alreadyAuthorized checks if key was written to authorized keys before.
|
2022-06-13 10:23:19 -04:00
|
|
|
func (s *Access) alreadyAuthorized(sshKey UserKey) bool {
|
2022-08-09 03:05:05 -04:00
|
|
|
_, ok := s.authorized[sshKey]
|
2022-03-22 11:03:15 -04:00
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// rememberAuthorized marks this key as already written to authorized keys..
|
2022-06-13 10:23:19 -04:00
|
|
|
func (s *Access) rememberAuthorized(sshKey UserKey) {
|
2022-08-09 03:05:05 -04:00
|
|
|
s.authorized[sshKey] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAuthorizedKeys returns a list of authorized keys for the specified user.
|
|
|
|
func (s *Access) GetAuthorizedKeys() []UserKey {
|
|
|
|
s.mux.Lock()
|
|
|
|
defer s.mux.Unlock()
|
|
|
|
|
|
|
|
var authorizedKeys []UserKey
|
|
|
|
for key := range s.authorized {
|
|
|
|
authorizedKeys = append(authorizedKeys, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
return authorizedKeys
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-06-13 10:23:19 -04:00
|
|
|
// DeployAuthorizedKey takes an user & public key pair, creates the user if required and deploy a SSH key for them.
|
|
|
|
func (s *Access) DeployAuthorizedKey(ctx context.Context, sshKey UserKey) error {
|
2022-03-22 11:03:15 -04:00
|
|
|
// allow only one thread to write to authorized keys, create users and update the authorized map at a time
|
|
|
|
s.mux.Lock()
|
|
|
|
defer s.mux.Unlock()
|
|
|
|
if s.alreadyAuthorized(sshKey) {
|
|
|
|
return nil
|
|
|
|
}
|
2022-06-28 10:51:30 -04:00
|
|
|
s.log.With(zap.String("username", sshKey.Username)).Infof("Trying to deploy ssh key for user")
|
2022-03-22 11:03:15 -04:00
|
|
|
user, err := s.userManager.EnsureLinuxUserExists(ctx, sshKey.Username)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// CoreOS uses https://github.com/coreos/ssh-key-dir to search for ssh keys in ~/.ssh/authorized_keys.d/*
|
|
|
|
sshFolder := fmt.Sprintf("%s/.ssh", user.Home)
|
2022-06-13 10:23:19 -04:00
|
|
|
authorizedKeysD := fmt.Sprintf("%s/authorized_keys.d", sshFolder)
|
|
|
|
if err := s.userManager.Fs.MkdirAll(authorizedKeysD, 0o700); err != nil {
|
2022-03-22 11:03:15 -04:00
|
|
|
return err
|
|
|
|
}
|
2022-06-13 10:23:19 -04:00
|
|
|
if err := s.userManager.Fs.Chown(sshFolder, user.UID, user.GID); err != nil {
|
2022-03-22 11:03:15 -04:00
|
|
|
return err
|
|
|
|
}
|
2022-06-13 10:23:19 -04:00
|
|
|
if err := s.userManager.Fs.Chown(authorizedKeysD, user.UID, user.GID); err != nil {
|
2022-03-22 11:03:15 -04:00
|
|
|
return err
|
|
|
|
}
|
2022-06-13 10:23:19 -04:00
|
|
|
authorizedKeysPath := fmt.Sprintf("%s/constellation-ssh-keys", authorizedKeysD)
|
2022-05-16 11:32:00 -04:00
|
|
|
authorizedKeysFile, err := s.userManager.Fs.OpenFile(authorizedKeysPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
|
2022-03-22 11:03:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-06-13 10:23:19 -04:00
|
|
|
_, err = authorizedKeysFile.WriteString(fmt.Sprintf("%s\n", sshKey.PublicKey))
|
2022-03-22 11:03:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := authorizedKeysFile.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-06-13 10:23:19 -04:00
|
|
|
if err := s.userManager.Fs.Chown(authorizedKeysPath, user.UID, user.GID); err != nil {
|
2022-03-22 11:03:15 -04:00
|
|
|
return err
|
|
|
|
}
|
2022-05-16 11:32:00 -04:00
|
|
|
if err := s.userManager.Fs.Chmod(authorizedKeysPath, 0o644); err != nil {
|
2022-03-22 11:03:15 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.rememberAuthorized(sshKey)
|
2022-06-28 10:51:30 -04:00
|
|
|
s.log.With(zap.String("username", sshKey.Username)).Infof("Successfully authorized user")
|
2022-03-22 11:03:15 -04:00
|
|
|
return nil
|
|
|
|
}
|