I have read in a quora answer that rahu and ketu desired to eat surya and chandra as revenge for what happened to them. They chased surya dev and chandra dev who took surrender in lord shiva.
Seeing this rahu and ketu did penances for lord shiva and when he was pleased, they asked a boon about staying in lord shiva's body as surya and chandra did. But just touching lord, made them realise their mistake and made them satwic.
Because of this they got position among navgrahas.
Also, for their satisfaction lord shiva allowed them to consume surya and chandra for rahu and ketu respectively. But that actually causes no harm to neither suryadev nor chandra dev.
Is this story valid as per scriptures? If yes, then which scripture?