In the wake of the incessant terror attacks in 2013 and 2014, the relationship between the then National Intelligence Service (NIS) Director-General Major-General (Rtd) Michael Gichangi and President Uhuru Kenyatta was so strained that the President had grown tired of receiving intelligence briefs.
People in the know say the relationship was further tested by a confrontation between the spy chief and other security chiefs, among them the then military chief Julius Karangi and police boss David Kimaiyo, over the Mpeketoni killings in which al-Shabaab militants slaughtered more than 60 people.
The bad blood among the intelligence and security honchos resulted in the forced retirement of Maj-Gen Gichangi, who was blamed for not doing enough in gathering intelligence to avert the attacks.
Maj-Gen (Rtd) Philip Kameru, his successor, in a meeting with senior officers of the spy agency, promised to repair the public image of the intelligence agency and regain the confidence with the President.
Six years later, after the President openly rebuked the intelligence body, Maj-Gen Kameru has managed to restore the glory of the NIS and placed it as President Kenyatta's most powerful organ in the security sector.
"The intelligence agency has become the enforcer of government policy. Although it is difficult to place a yardstick, it has emerged as the most trusted entity in public service in the Uhuru era," says Edward Wanyonyi, a graduate of war studies from King's College, London.
Last week, as he announced Cabinet changes in which Agriculture Cabinet Secretary Mwangi Kiunjuri was fired, President Kenyatta -- in his most overt recognition of the importance of the spy agency -- directed it to deal with cartels in his government.
"I direct the NIS to undertake a rigorous review of all cartel groupings that have become leeches, sucking away the blood and sweat of hardworking Kenyans. I want the review to pay particular attention to cartels operating in the public systems of budgeting, procurement, regulation and the illegal rigging of markets," he said.
"It should also put the agriculture sector under the microscope. Once this review is completed, I further direct the DCI to take necessary action, working alongside the DPP, to confront these cartels with every instrument available."
Not only has the NIS become the most powerful security and intelligence agency in the country but Mr Kameru is also close to the President.
The NIS has now become heavily involved in the running of the country with its extended mandate including political intelligence, economic intelligence, active measures, counter-intelligence, political security and law enforcement through the multi-agency team.
"The NIS is now fully involved in national safety and security ranging from issues dealing with counter-terrorism to economic crimes. They are also involved in local governance and political issues, including giving birth to the "handshake" between ODM leader Raila Odinga and President Kenyatta and also on foreign policy matters," says Byron Odera, a security analyst and a former Special Forces commando.
Captain (Rtd) Odera argues that the NIS is like a shadow government. "Essentially, the decisions at strategic levels are intelligence-driven. The Big Four Agenda is also shaped around the same, albeit with interdependence on other moving parts."
The power of the NIS, whose staff numbers are not public, is enhanced by its unique legal status through the National Intelligence Service Act, 2012, which gives it the responsibility for "security intelligence and counter-intelligence to enhance national security in accordance with the Constitution".
Employing its wide mandate, the NIS is now involved in almost all facets of running the government, from dealing with corruption, terrorism to land issues.
The Ministry of Land has created a Sh490 million Land Information System in collaboration with the NIS.
The digital registry is expected to be operational in all the 61 land registries countrywide before end of April.
Already, NIS officers have embarked on testing the system in Nairobi. They are expected to have finished and operationalised the system by March 31.
In November last year, the agency flagged nine judges and three lawyers seeking appointments as Court of Appeal judges.
Adverse reports had been made against the candidates. Although, complaints were levelled against 21 of the 127 applicants, only the nine judges and three lawyers were blacklisted.
In 2018, the agency was involved in the lifestyle audit of more than 1,000 public officers.
It provided highly-secured houses in different posh estates from where the individuals were taken for questioning.
The NIS had compiled a report on each of the top government officials, beginning with the procurement and finance officers, which formed the basis of the interrogations.
Dozens of officers who failed the lifestyle audit tests have been sacked.
WORKING IN SECRECY
NIS officers have also been assisting the Kenya Wildlife Service in tracking suspected poachers.
However, its operations in the Kenyatta government have had challenges.
Unlike most of its peers worldwide, which have embraced openness, Kenya's NIS has remained an organisation shrouded in secrecy.
Other than a website, which is one of the dullest, with very little and mostly unhelpful information, the NIS has no other online presence. They don't have social media pages.
In 2015, the agency was on the spot after reports that it attempted to procure the Remote Control System Surveillance product of Italian company, Hacking Team, to intercept private communications and bring down websites deemed offensive to the government.
The agency was also alleged to have acquired intrusion malware from FinFisher.
According to a trove of e-mails released by whistle-blowing site, WikiLeaks, the spy system could enable NIS unfettered access to people's information, infect and monitor computers and smartphones.
The system is "designed to attack, infect and monitor target PCs and smartphones in a stealth way", according to an exchange of e-mails between a representative of the Hacking Team and a supposed NIS operative in Nairobi on April 27, 2015.
The National Security Intelligence Service and its predecessors, the Directorate of Security Intelligence (DSI) and the Special Branch, have come a long way.
The DSI and the Special Branch were part of the Kenya Police, and were extremely secretive about their work.
They used espionage, destabilisation and subversion to achieve their ends. The Special Branch was infamous for hunting down and torturing government critics.
The Special Branch was started in 1926 by the colonial government as a small department within the police force.
It was later made an independent department and, in 1969, renamed the Department of Security Intelligence.
In 1998, the National Security Intelligence Service was created with retired Brigadier Wilson Boinnet as the first director-general.
Brig Boinnet was succeeded by Maj-Gen Gichangi, who steered the agency through the transition to National Intelligence Service under the new Constitution.
Historically, the spy agency has played a key role in the running affairs of the country.
After the death of founding President Jomo Kenyatta, the then Special Branch director James Kanyotu "managed" events, ensuring that the then Vice-President Daniel Moi became acting president, overcoming efforts by powerful men in government who did not want Moi to ascend to power.