So far this century, Germany has been lucky compared with France, where Islamic extremist terror has claimed hundreds of lives – and of course the United States where 3,000 people were massacred on 9/11. But terror incidents in Germany have been isolated, perpetrated by lone wolves and with no victims or only a few.

Now this happy state has reached a bloody end. A taste of things to come was the shooting spree at a Munich McDonald’s last July, when a German-Iranian student took the lives of nine as well as his own. A psychiatric patient, he had no criminal record, nor an obvious political motive. Then, this week, “Nice” came to Berlin.

The similarities are striking. In Nice, on Bastille Day this year, a young French-Tunisian hijacked a truck and ploughed through the Promenade des Anglais; 86 were killed, and more than 400 injured. Later on, Isis claimed responsibility. In Berlin, a trailer-truck was driven into one of those Christmas markets that spring up all over Germany in December, wide open and impossible to protect. The toll: 12 dead and about 50 wounded. Ever cautious, the authorities have since said there was “no doubt” it was a terror attack. But at the time of writing there are doubts about whether the 23-year-old man from Pakistan, arrested soon after the incident last night, is indeed the culprit.

But whoever the culprits are, Angela Merkel has vowed that they will be punished, “as hard as our laws demand”. The nation is in shock; this was not supposed to happen in a country that has been able to foil more terror attacks than have actually occurred. And this, it must be said, with the help of the much-maligned NSA, plus the British GCHQ and the French DGSE.

In general, Germany has been lucky – and smart in ways that did not always please its allies. German troops stayed out of Iraq. Where fealty to an alliance left no other choice, as in Afghanistan, they picked areas where combat appeared unlikely. In the Middle East, the German air force flies reconnaissance only – no bombing. Germany has worked hard not to become a target for retaliation.

There is a scene in the fifth season of Homeland that says it all. Caught snooping in Germany, the CIA is ordered by the government to expel a high-ranking member of their Berlin team. Carrie, the show’s heroine, is perplexed, and asks her old mentor Saul: “What’s wrong with these new Germans?” Saul says: “I don’t know. They used to fight like hell.”

Angela Merkel visits the site of the attack. Photograph: Markus Schreiber/AP

But now the cocoon has burst for good. Ever protective of our privacy laws, Germans will soon come around to much-intensified surveillance, by our own intelligence services and those of our allies. Even before the Berlin bloodbath, the federal government had decided to beef up data gathering and cyber warfare capabilities in response to Russia’s far-flung efforts to manipulate western domestic politics.

Now, Germany will invest even more in security – and perhaps show a bit more gratitude to the NSA, GCHQ and DGSE. Pacifism, the nation’s traditional posture since the second world war, is losing its lustre as Putinist expansionism encroaches on Nato’s eastern borders, while Donald Trump dismisses the alliance as “obsolete”.

Above all, if the perpetrator does turn out to be a refugee, Merkel’s “open door” policy on refugees will get a decisive make-over. Recall last year when she flung the country’s doors wide open. Proclaiming “wir schaffen das” – we can do it – she essentially relinquished control over Germany’s borders. Some 800,000 people from the Middle East as well as North Africa arrived.

Merkel now says that if the perpetrator is indeed a refugee, “this would be extremely hard for us to bear,” and it “would be particularly repugnant for all those Germans, who toil daily to help refugees”.

Thus do good intentions come to a nasty end. For Merkel, the “open door” was a grand moral gesture stemming from Germany’s ugly past – an act of historical atonement. So is the ultra-liberal state that followed Nazi totalitarianism. “Never again!” explains why Germany, remembering the deadly fate of its Jews trying to escape extermination, opened its borders last summer.

Controls are now back, and they will be tightened – as will domestic surveillance. The noblest of intentions go awry when terror legitimates anti-migrant and isolationist parties on the right, and on the far left. Populism is always both left and right.

A few more terror assaults, and Alternative für Deutschland – a political party roughly comparable to Ukip – will make hay in the 2017 general elections when Merkel runs for a fourth term. She will be eager to present herself as protector of domestic security – but within the constraints of a society that is as nervous about terror as it is protective of its sacred liberties.

This story has just begun, and it is rife with speculation as to who did what and why. But one truth is inescapable: Germany, once lucky, has now joined the club of terror targets in the west, along with the US, Britain and France.