ssh_util: extract_authorized_keys raises IndexError when sshd_config cannot be read

Aperta Adatta ai principianti
#7,229 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
76/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
backend

Direzione di ricerca

Inizia da cloudinit/ssh_util.py, vicino al codice collegato extract_authorized_keys(), e segui come vengono impostati key_paths, strictmodes e auth_key_fns in entrambi i percorsi. Aggiungi un test unitario mirato che faccia generare un OSError a parse_ssh_config_map(). Poi verifica che venga usato il ~/.ssh/authorized_keys predefinito e che le chiavi dell’utente vengano scritte senza eccezioni.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Bug report

In ssh_util.extract_authorized_keys(), the fallback path used when sshd_config cannot be read is broken and raises IndexError instead of falling back to ~/.ssh/authorized_keys.

https://github.com/canonical/cloud-init/blob/b452c2097/cloudinit/ssh_util.py#L412-L434

    auth_key_fns = []
    with util.SeLinuxGuard(ssh_dir, recursive=True):
        try:
            ssh_cfg = parse_ssh_config_map(sshd_cfg_file)
            ...
        except (IOError, OSError):
            # Give up and use a default key filename
            auth_key_fns[0] = default_authorizedkeys_file   # <-- list is empty

auth_key_fns is still an empty list at that point, so the assignment raises IndexError: list assignment index out of range. Even if it succeeded, key_paths and strictmodes would be unbound in the for key_path, auth_key_fn in zip(key_paths.split(), ...) loop right after, raising UnboundLocalError.

Expected: if sshd_config cannot be read, log the error and fall back to the default ~/.ssh/authorized_keys (which is what the log message says happens).
Actual: IndexError, and the SSH keys for the user are not written.

Steps to reproduce the problem

from unittest import mock
from cloudinit import ssh_util

with mock.patch.object(ssh_util, "parse_ssh_config_map",
                       side_effect=PermissionError("denied")):
    ssh_util.extract_authorized_keys("root")
# IndexError: list assignment index out of range

In practice this happens whenever reading /etc/ssh/sshd_config raises OSError (e.g. permission/SELinux denial, I/O error).

Environment details

  • Cloud-init version: main @ b452c2097 (26.2)
  • Operating System Distribution: any
  • Cloud provider, platform or installer type: any

cloud-init logs

N/A – reproduced in a unit-level test (above).

Possible fix

Initialise key_paths/strictmodes with defaults before the try and use auth_key_fns = [default_authorizedkeys_file] in the except branch.


Contributor note: I'm happy to raise a PR with the fix and unit tests once a maintainer confirms this is a bug and the proposed approach is acceptable.

Lingua principale
Python
Stelle
3.9k
Fork
1.2k
Merge medio
2g 26m
PR unite (30g)
16

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di canonical/cloud-init

Tutte le issue di canonical/cloud-init

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.