Soon after I became a senator in 2014, the Abbott government decided the threat of terrorism required urgent legislation. The powers of the security agencies, particularly the AFP and ASIO, were insufficient it said.

Each subsequent year there were more bills, all deemed urgent. At one stage the government wanted to announce a new national security measure every week.

Among the measures were preventive detention orders, which allow people to be detained without charge; control orders, which impose bail-like conditions on people without charge; snooping on our metadata; and hacking our encrypted smartphones. And of course, limits on what we can say.

I opposed them all. My view was that terrorism was already a crime, as was conspiracy and incitement, and that if security agencies did their job properly, they did not require additional powers. It was a long, lonely and ultimately fruitless battle. Each bill passed easily with the support of Labor.

The media was among the casualties. In the 2014 bill, for example, there were serious limits on what journalists could report about ASIO’s activities, with the prospect of imprisonment for up to 10 years. There was a similar provision in a bill increasing the powers of the AFP.

I railed against these provisions and warned the media of the danger, to little effect. Fortunately, a year or so later the Independent National Security Legislation Monitor found the provisions of the ASIO bill to be disproportionate and recommended winding them back. The government relented.

I lobbied to have the AFP bill changed too, as it was not reviewed by the INSLM. Eventually, the government agreed to that too. It made a bad law slightly less bad.

What became clear from all this is that the agencies involved in national security wanted to operate without scrutiny or challenge.

When I told the Attorney-General that I was concerned at the loss of liberty, he replied that the legislation was nothing compared to what the agencies had sought.

I particularly recall one bill which included a provision protecting ASIO agents from prosecution if they engaged in torture. When I raised concerns, I was ridiculed and told no Australian government agency would ever do such a thing. It took a lot of noise by me, with others chiming in, before the government agreed to delete it.

I was once offered a briefing by the security agencies on the need for a particular bill. In the secure cabinet room with phones left outside, all I got was broad assertions and information I had already seen in newspapers. I left with the impression the government gets no more.

Thus it was no great surprise to me that search warrants were executed on the home of a News Corp journalist and the ABC head office, at the behest of the Departments of Home Affairs and Defence. Their purpose was to identify a whistleblower in the first case and to gather information about a whistleblower in the second. The departments had been embarrassed by the information they had revealed.

What this shows, as if it was needed, is that government agencies act in their own interests irrespective of whether it is consistent with the values of a liberal democracy. If the legislation permits it, pursuing those who embarrass them is never likely to come second to a free media and the public interest.

My hope is that the controversy will result in legislation that protects press freedom. It should be the responsibility of agencies to keep their own secrets, not the media or anybody else. And unless there is a clear and present danger, which there wasn’t in either of these cases, there should be no thought given to hunting down whistleblowers.

If it was up to me, I would abolish several agencies and downsize the rest. An agency that doesn’t exist can’t ask the AFP to conduct raids because it is embarrassed. What some of them contribute to national security is minimal and doesn’t stack up financially either.

At the very least, each agency should have its purpose precisely defined in legislation, be subject to constant oversight, and have its discretion severely restricted. Judicial oversight at a senior level, genuine parliamentary scrutiny and actual ministerial responsibility are all vital. And when they get it wrong, heads must roll.

A free society requires a free media. This is even more important than national security.

David Leyonhjelm is former Australian federal senator.