Official cover is a term used in state-sponsored espionage (particularly used by the CIA) that refers to operatives (usually intelligence officers, but sometimes intelligence agents) who assume positions in organizations with diplomatic ties to the government for which they work, generally the diplomatic service.
Official cover has three main functions. First, it provides a reason for operatives to be living in the country that they are operating in. Second, it provides an excuse for operatives to be in contact with sources connected to other governments due to diplomatic duties, which creates many opportunities to recruit agents. Third, it allows the operative's state to extend legal protection for the operative should that operative be discovered, a safety net that is not afforded to operatives under non-official cover.
There are several disadvantages of official cover. One disadvantage is that diplomats and those with diplomatic ties are automatically under suspicion of being an operative, and because of this will often be under surveillance. This surveillance limits an operative's actions. For example, an operative under official cover would generally not be able to perform a lengthy clandestine operation, as his absence from work would be a suspicious action that would immediately alert the nation he is operating in.
In the event that an operative under official cover is publicly caught, the operative's state will almost always extend diplomatic protection to the operative through diplomatic immunity. This protection prevents the operative from being arrested or apprehended in any way by the nation he is operating in, so that nation's only option is to declare the operative persona non grata and send him back to his home country with his tail between his legs but no worse for the wear. On the other hand, if, when the operative is caught, the operative's network of agents is compromised, these agents are generally not legally protected in any way and will be "rolled up", i.e. apprehended and then dealt with in all manner of nasty ways, publicly or secretly.
It is worth noting that it is the state's right to extend diplomatic protection to the operative. As such, the state can choose to not extend such protection, although there are very few instances of this happening, and these instances are mostly limited to people who had severely betrayed their nations' trust (not typically the case in espionage). In addition, it is not the right of the operative to refuse diplomatic protection (with the limited exception of defection), as it is a state right and not a human right that is being exercised.
In many cases, if an operative is quietly discovered without his or his state's knowledge, the government he is spying on may choose not to act against him at all, knowing that he will be protected, and that another operative, this one unknown, will be sent to replace him when he is gone. Instead, the country may prefer the known enemy to the unknown one and not act against the operative and instead keep surveillance on him and engage in counter-espionage by feeding him misinformation and acting against his agents.