func DeleteKeys(user string, keyIds ...string) error {
	keysMutex.Lock()
	defer keysMutex.Unlock()
	existingKeyData, err := readAuthorisedKeys(user)
	if err != nil {
		return err
	}
	// Build up a map of keys indexed by fingerprint, and fingerprints indexed by comment
	// so we can easily get the key represented by each keyId, which may be either a fingerprint
	// or comment.
	var keysToWrite []string
	var sshKeys = make(map[string]string)
	var keyComments = make(map[string]string)
	for _, key := range existingKeyData {
		fingerprint, comment, err := KeyFingerprint(key)
		if err != nil {
			logger.Debugf("keeping unrecognised existing ssh key %q: %v", key, err)
			keysToWrite = append(keysToWrite, key)
			continue
		}
		sshKeys[fingerprint] = key
		if comment != "" {
			keyComments[comment] = fingerprint
		}
	}
	for _, keyId := range keyIds {
		// assume keyId may be a fingerprint
		fingerprint := keyId
		_, ok := sshKeys[keyId]
		if !ok {
			// keyId is a comment
			fingerprint, ok = keyComments[keyId]
		}
		if !ok {
			return errors.Errorf("cannot delete non existent key: %v", keyId)
		}
		delete(sshKeys, fingerprint)
	}
	for _, key := range sshKeys {
		keysToWrite = append(keysToWrite, key)
	}
	if len(keysToWrite) == 0 {
		return errors.Errorf("cannot delete all keys")
	}
	return writeAuthorisedKeys(user, keysToWrite)
}
// DeleteKeys removes the specified ssh keys from the authorized ssh keys file for user.
// keyIds may be either key comments or fingerprints.
// Returns an error if there is an issue with *any* of the keys to delete.
DeleteKeys removes the specified ssh keys from the authorized ssh keys file for user.
keyIds may be either key comments or fingerprints.
Returns an error if there is an issue with *any* of the keys to delete.
// DeleteKeys removes the specified ssh keys from the authorized ssh keys file for user . // keyIds may be either key comments or fingerprints . // Returns an error if there is an issue with * any * of the keys to delete .
func DeleteKeys(user string, keyIds ...string) error {
	keysMutex.Lock()
	defer keysMutex.Unlock()
	existingKeyData, err := readAuthorisedKeys(user)
	if err != nil {
		return err
	}
	var keysToWrite []string
	var sshKeys = make(map[string]string)
	var keyComments = make(map[string]string)
	for _, key := range existingKeyData {
		fingerprint, comment, err := KeyFingerprint(key)
		if err != nil {
			logger.Debugf("keeping unrecognised existing ssh key %q: %v", key, err)
			keysToWrite = append(keysToWrite, key)
			continue
		}
		sshKeys[fingerprint] = key
		if comment != "" {
			keyComments[comment] = fingerprint
		}
	}
	for _, keyId := range keyIds {
		fingerprint := keyId
		_, ok := sshKeys[keyId]
		if !ok {
			fingerprint, ok = keyComments[keyId]
		}
		if !ok {
			return errors.Errorf("cannot delete non existent key: %v", keyId)
		}
		delete(sshKeys, fingerprint)
	}
	for _, key := range sshKeys {
		keysToWrite = append(keysToWrite, key)
	}
	if len(keysToWrite) == 0 {
		return errors.Errorf("cannot delete all keys")
	}
	return writeAuthorisedKeys(user, keysToWrite)
}
func DeleteKeys ( user string , keyIds ... string ) error { keysMutex . Lock ( ) defer keysMutex . Unlock ( ) existingKeyData , err := readAuthorisedKeys ( user ) if err != nil { return err } var keysToWrite [ ] string var sshKeys = make ( map [ string ] string ) var keyComments = make ( map [ string ] string ) for _ , key := range existingKeyData { fingerprint , comment , err := KeyFingerprint ( key ) if err != nil { logger . Debugf ( " " , key , err ) keysToWrite = append ( keysToWrite , key ) continue } sshKeys [ fingerprint ] = key if comment != " " { keyComments [ comment ] = fingerprint } } for _ , keyId := range keyIds { fingerprint := keyId _ , ok := sshKeys [ keyId ] if ! ok { fingerprint , ok = keyComments [ keyId ] } if ! ok { return errors . Errorf ( " " , keyId ) } delete ( sshKeys , fingerprint ) } for _ , key := range sshKeys { keysToWrite = append ( keysToWrite , key ) } if len ( keysToWrite ) == 0 { return errors . Errorf ( " " ) } return writeAuthorisedKeys ( user , keysToWrite ) }
DeleteKeys removes the specified ssh keys from the authorized ssh keys file for user.
DeleteKeys removes the specified ssh keys from the authorized ssh keys file for user .
// Build up a map of keys indexed by fingerprint, and fingerprints indexed by comment // so we can easily get the key represented by each keyId, which may be either a fingerprint // or comment. // assume keyId may be a fingerprint // keyId is a comment