As the security situation in Afghanistan continues to slip, NATO has asked Canada to help with its Resolute Support mission by sending trainers to help build an Afghan military and police force capable of defending the country.

For the past three years, Canadian soldiers have been training and mentoring Kurdish Peshmerga fighters in Iraq with great success and only one fatality: Sgt. Andrew Doiron was killed in a "friendly fire" incident that all sides agree was a tragic but honest mistake.

The Canadians have developed a relationship of mutual trust and even affection with their Kurdish hosts, according to Canadian military commanders.

So the Afghan training and mentoring mission NATO is now asking Canada to take on, might also seem to be a relatively safe assignment.

But bitter experience has taught NATO that the Afghan frontline runs through every army base, police detachment and training classroom. Western soldiers have died not only on the battlefield, but on the parade ground, the firing range and even sitting down to dinner with their Afghan counterparts.

"Insider attacks" have been a constant of the Afghan war, and many have taken the form of so-called "green-on-blue" shootings, in which foreign instructors are deliberately killed by their own students.

In August 2012, an Afghan police commander called Asadullah sat down to a pre-dawn meal with three U.S. marines in Helmand province. The marines were there to train Afghan national police. Asadullah invited them, he told them, to discuss security arrangements.

Partway through the meal, Asadullah produced a pistol and shot the three marines dead. He then fled the base in the dark.

"He is with us now," Taliban spokesperson Qari Yusuk Ahmadi later confirmed to Agence France-Presse by phone.

Insider attacks

Sixty foreign advisers were killed by Afghan soldiers and police that same year, according to figures compiled by the International Security Assistance Force. The attacks prompted new measures by ISAF to protect trainers from their trainees.

The changes included strengthened identity vetting for Afghan security forces members.

That's no simple matter in a country where the typical recruit is an illiterate villager with no real birth certificate and often, no family name (many Afghans use only a first name).

The Afghan military is now using biometric scans, with equipment and supervision provided by ISAF, on all new recruits. Recruits must also provide references from two trusted elders from their home district.

Western trainers also instituted a system of "guardian angels," where a coalition soldier is assigned to stand guard over Afghan recruits at all times.

Afghan police demonstrate their skills during a graduation ceremony at a police training centre in the Adraskan district of Herat province, Afghanistan, in March, 2011. (Reza Shirmohammadi/Associated Press)

Not getting safer

On military bases where Afghan soldiers and police mix with foreign forces, the Afghans are required to be unarmed inside the gates, or have the firing pins removed from their rifles.

It was partly because of that rule that a recent Taliban attack on an Afghan army base in Mazar-e-Sharif was so deadly, says Bill Roggio, a former U.S. soldier who today edits the Long War Journal, published by the Washington think-tank the Foundation for the Defence of Democracies.

In that incident, about 10 attackers were recently able to kill about 140 mostly unarmed recruits.

"It tells you just how the Taliban strategy of conducting insider attacks has second and third order effects that people often don't consider," said Roggio. "They just aren't being trusted right now and trust is the first thing you need when you're partnering with foreign forces.

"You have cases where Afghan soldiers have gone through the vetting process, they didn't have any problems and then after a couple years in service, or a couple of months or weeks, they've been pressured or talked to by the Taliban and recruited to conduct attacks," he said.

Roggio says the countermeasures taken by NATO forces are not the main reason for the reduction in insider attacks.

"The primary reason for the reduction over the last several years is there's been less coalition forces partnered" with Afghan forces, he says, and fewer Westerners in Afghanistan generally.

Police forces clash with protesters during a demonstration in Kabul earlier this month. Hundreds of demonstrators demanded better security in the Afghan capital in the wake of a powerful truck bomb attack that killed scores of people. (Massoud Hossaini/Associated Press)

Yet three more U.S. soldiers fell victim to such an attack on June 12. The men of the 101st Airborne were killed in Nangarhar province by an Afghan National Army soldier who was immediately killed by return fire.

A week later, seven more Americans were shot by an Afghan soldier inside the Mazar-e-Sharif base. All seven survived.

An Ottawa cop remembers

Canada has been involved in training Afghan police before.

Ottawa police Sgt. Colin Stokes, who trained Afghan police in Kandahar from 2009 to 2010, operated mostly out of Camp Nathan Smith.

He says his experience was mostly positive, but remembers the precautions he and his fellow police officers took, particularly when their students were armed at the firing range.

Ottawa police Sgt. Colin Stokes oversees an Afghan police trainee during weapons training in Kandahar. Stokes was posted with the Canadian Armed Forces as a trainer in 2009 and 2010. (Colin Stokes )

"When we were on the range with them, we always ensured there were other police officers, basically, very hands on and close to all the Afghan soldiers and constables that were on the line," he says.

Stokes said his Afghan trainees were mostly keen to learn, but says it's impossible to prevent all green-on-blue killings when dealing with armed recruits.

"Mistakes are made, things slip through the cracks. Everybody tries to do the best we can to vet personnel, but it's a war zone and you can't be too surprised when people get killed."

He says he'd be willing to return under certain circumstances.

"If Canada does go back, if I was to go back, I'd want to mentor Afghans that have the capacity to absorb the type of police training that we have to give."