func AddKeys(user string, newKeys ...string) error {
	keysMutex.Lock()
	defer keysMutex.Unlock()
	existingKeys, err := readAuthorisedKeys(user)
	if err != nil {
		return err
	}
	for _, newKey := range newKeys {
		fingerprint, comment, err := KeyFingerprint(newKey)
		if err != nil {
			return err
		}
		if comment == "" {
			return errors.Errorf("cannot add ssh key without comment")
		}
		for _, key := range existingKeys {
			existingFingerprint, existingComment, err := KeyFingerprint(key)
			if err != nil {
				// Only log a warning if the unrecognised key line is not a comment.
				if key[0] != '#' {
					logger.Warningf("invalid existing ssh key %q: %v", key, err)
				}
				continue
			}
			if existingFingerprint == fingerprint {
				return errors.Errorf("cannot add duplicate ssh key: %v", fingerprint)
			}
			if existingComment == comment {
				return errors.Errorf("cannot add ssh key with duplicate comment: %v", comment)
			}
		}
	}
	sshKeys := append(existingKeys, newKeys...)
	return writeAuthorisedKeys(user, sshKeys)
}
// AddKeys adds the specified ssh keys to the authorized_keys file for user.
// Returns an error if there is an issue with *any* of the supplied keys.
AddKeys adds the specified ssh keys to the authorized_keys file for user.
Returns an error if there is an issue with *any* of the supplied keys.
// AddKeys adds the specified ssh keys to the authorized_keys file for user . // Returns an error if there is an issue with * any * of the supplied keys .
func AddKeys(user string, newKeys ...string) error {
	keysMutex.Lock()
	defer keysMutex.Unlock()
	existingKeys, err := readAuthorisedKeys(user)
	if err != nil {
		return err
	}
	for _, newKey := range newKeys {
		fingerprint, comment, err := KeyFingerprint(newKey)
		if err != nil {
			return err
		}
		if comment == "" {
			return errors.Errorf("cannot add ssh key without comment")
		}
		for _, key := range existingKeys {
			existingFingerprint, existingComment, err := KeyFingerprint(key)
			if err != nil {
				if key[0] != '#' {
					logger.Warningf("invalid existing ssh key %q: %v", key, err)
				}
				continue
			}
			if existingFingerprint == fingerprint {
				return errors.Errorf("cannot add duplicate ssh key: %v", fingerprint)
			}
			if existingComment == comment {
				return errors.Errorf("cannot add ssh key with duplicate comment: %v", comment)
			}
		}
	}
	sshKeys := append(existingKeys, newKeys...)
	return writeAuthorisedKeys(user, sshKeys)
}
func AddKeys ( user string , newKeys ... string ) error { keysMutex . Lock ( ) defer keysMutex . Unlock ( ) existingKeys , err := readAuthorisedKeys ( user ) if err != nil { return err } for _ , newKey := range newKeys { fingerprint , comment , err := KeyFingerprint ( newKey ) if err != nil { return err } if comment == " " { return errors . Errorf ( " " ) } for _ , key := range existingKeys { existingFingerprint , existingComment , err := KeyFingerprint ( key ) if err != nil { if key [ 0 ] != '#' { logger . Warningf ( " " , key , err ) } continue } if existingFingerprint == fingerprint { return errors . Errorf ( " " , fingerprint ) } if existingComment == comment { return errors . Errorf ( " " , comment ) } } } sshKeys := append ( existingKeys , newKeys ... ) return writeAuthorisedKeys ( user , sshKeys ) }
AddKeys adds the specified ssh keys to the authorized_keys file for user.
AddKeys adds the specified ssh keys to the authorized_keys file for user .
// Only log a warning if the unrecognised key line is not a comment.