Actually, ninja were mostly from the bushi-class (samurai class). Espionage and subterfuge were not contrary to the bushido code, which is widely misunderstood and romanticised. I’ve written in length about that and the verifiable history of the ninja here.