كتاب الإمارة
33  
The Book on Government
(8)
Chapter: The obligation of obeying leaders in matters that do not involve sin, but it is forbidden to obey them in sinful matters
(8)
باب وُجُوبِ طَاعَةِ الأُمَرَاءِ فِي غَيْرِ مَعْصِيَةٍ وَتَحْرِيمِهَا فِي الْمَعْصِيَةِ ‏‏
Sahih Muslim 1709 e

It has been narrated on the authority of" Ubida who learnt the tradition from his father who, in turn, learnt it from his own father. 'Ubada's grandfather said:

The Messenger of Allah (ﷺ) took an oath of allegiance from us on our listening to and obeying the orders of our commander in adversity and prosperity, in pleasure and displeasure (and even) when somebody is given preference over us, on our avoiding to dispute the delegation of powers to a person deemed to be a fit recipient thereof (in the eye of one who delegates it) and on our telling the truth in whatever position we be without fearing in the matter ef Allah the reproach of the reproacher.
حَدَّثَنَا أَبُو بَكْرِ بْنُ أَبِي شَيْبَةَ، حَدَّثَنَا عَبْدُ اللَّهِ بْنُ إِدْرِيسَ، عَنْ يَحْيَى بْنِ سَعِيدٍ، وَعُبَيْدِ اللَّهِ بْنِ عُمَرَ عَنْ عُبَادَةَ بْنِ الْوَلِيدِ بْنِ عُبَادَةَ، عَنْ أَبِيهِ، عَنْ جَدِّهِ، قَالَ بَايَعْنَا رَسُولَ اللَّهِ صلى الله عليه وسلم عَلَى السَّمْعِ وَالطَّاعَةِ فِي الْعُسْرِ وَالْيُسْرِ وَالْمَنْشَطِ وَالْمَكْرَهِ وَعَلَى أَثَرَةٍ عَلَيْنَا وَعَلَى أَنْ لاَ نُنَازِعَ الأَمْرَ أَهْلَهُ وَعَلَى أَنْ نَقُولَ بِالْحَقِّ أَيْنَمَا كُنَّا لاَ نَخَافُ فِي اللَّهِ لَوْمَةَ لاَئِمٍ ‏.‏
Reference : Sahih Muslim 1709e
In-book reference : Book 33, Hadith 66
USC-MSA web (English) reference : Book 20, Hadith 4538
  (deprecated numbering scheme)