An Afghan army infiltrator opened fire inside the defence ministry in Kabul on Monday, killing two soldiers and wounding seven in an audacious strike on the heart of government claimed by the Taliban.
The attack, which the militants said was aimed at France's visiting defence minister, was the third major assault on Afghan security targets in four days and one of the worst security breaches in years.
"A person in Afghan army uniform opened fire on his comrades, killed two soldiers, injured seven others, then was targeted himself and was brought down," Afghan army spokesman General Mohammad Zahir Azimi told AFP.
After his death the gunman was found to be wearing a suicide vest, he said.
A military source speaking on condition of anonymity told AFP that three insurgents had managed to enter the building, which faces President Hamid Karzai's palace, and all were killed.
The ambush inside the tightly-secured compound is thought to be the most high-profile security breach since a failed attempt on Karzai's life in 2008.
French Defence Minister Gerard Longuet is currently on a visit to Afghanistan but was not in the building at the time.
An Afghan official, also speaking anonymously, said the building had been evacuated, the incident was over and an investigation was under way.
Taliban spokesman Zabihullah Mujahid told AFP that Longuet was the target.
"The reason for conducting this attack is the invasion of Afghanistan by the French military," he said, adding that it was not carried out over the controversial banning of the Islamic full-face veil in France.