In my view, this question is based on a very questionable premise. It is based on the idea that the business of intelligence is inherently immoral. I do not accept this particular premise. I believe that intelligence is not inherently immoral and I therefore think that this question is not valid.
I can see where a person could believe that intelligence is fundamentally immoral. At its most basic, the business is based on some degree of deception and dishonesty. A person who is actually spying on others has to lie to them to get them to believe that he or she should be trusted. It is possible to argue that it is always immoral to lie.
I can also see where a person would say that intelligence is fundamentally immoral given the sorts of things that are detailed in the Senate report on torture conducted by the CIA after the 9/11 attacks. Many of the things that are described in that report are shocking and it is hard to see how a person could morally agree to take part in them.
However, I would reject the idea that intelligence is an inherently immoral job. With respect to the torture, we have to realize that individuals can act immorally in any line of work. Many teachers have engaged in sexual relationships with students that were, in my mind at least, immoral. This does not mean that teaching is an immoral profession. We cannot judge an entire line of work because of the ways in which some of its practitioners have behaved.
I also do not accept the idea that the deceptiveness involved in intelligence makes it immoral. It is true that many kinds of spies (though not ones who do things like listening to wiretaps) are lying to those with whom they interact. However, I do not think that this is necessarily immoral. We would not typically say that it is immoral for a police officer to go undercover to get evidence to apprehend and convict criminals. I would argue that spying for one’s country is very similar to being an undercover police officer. In both cases, you are engaging in deception in order to help (at least in your mind) make the world a better place.
To be sure, spies and other intelligence agents have more opportunities to commit immoral acts than most of us do. Most of us are never in the position where we could be able to torture another person. Most of us are never in position to have to decide when it is right to deceive someone badly enough that the person might get in trouble with their government because of our actions. However, I believe that it is still possible to have a code of ethics in such an occupation. I believe that intelligence agents have to weigh the good that can come from their actions against the bad. They have to think hard about when it is worth it to deceive others. They must also make sure that they have the moral fiber to refuse to act inhumanely towards others. This will be hard for them, but in many ways it is no harder than what people like police officers and prison guards have to do.
In short, I do not agree that intelligence is an inherently immoral line of work. I think that intelligence agents can have codes of ethics just like the rest of us. It is not easy for them to do so and it is not possible to describe how they can have such codes other than to say that they simply have to think about what they are doing and they have to try as hard as they can to act ethically.