The obligation of obeying leaders in matters that do not involve sin, but it is forbidden to obey them in sinful matters Sahih Muslim 1835 a, b

Arabic hadith text
حَدَّثَنَا يَحْيَى بْنُ يَحْيَى، أَخْبَرَنَا الْمُغِيرَةُ بْنُ عَبْدِ الرَّحْمَنِ الْحِزَامِيُّ، عَنْ أَبِي الزِّنَادِ، عَنِ الأَعْرَجِ، عَنْ أَبِي هُرَيْرَةَ، عَنِ النَّبِيِّ صلى الله عليه وسلم قَالَ ‏"‏مَنْ أَطَاعَنِي فَقَدْ أَطَاعَ اللَّهَ وَمَنْ يَعْصِنِي فَقَدْ عَصَى اللَّهَ وَمَنْ يُطِعِ الأَمِيرَ فَقَدْ أَطَاعَنِي وَمَنْ يَعْصِ الأَمِيرَ فَقَدْ عَصَانِي ‏"‏ ‏.‏ وَحَدَّثَنِيهِ زُهَيْرُ بْنُ حَرْبٍ، حَدَّثَنَا ابْنُ عُيَيْنَةَ، عَنْ أَبِي الزِّنَادِ، بِهَذَا الإِسْنَادِ وَلَمْ يَذْكُر " وَمَنْ يَعْصِ الأَمِيرَ فَقَدْ عَصَانِي ‏"‏ ‏.‏
Translation
It has been narrated on the authority of Abu Huraira that the Holy prophet (ﷺ) said

Whoso obeys me obeys God, and whoso disobeys me disobeys God. Whoso obeys the commander (appointed by me) obeys me, and whoso disobeys the commander disobeys me. The same tradition transmitted by different persons omits the portion: And whose disobeys the commander disobeys me.