Under international law, a state (Israel) cannot invoke the right of self-defense against territory it occupies (Gaza). The occupied territory can resist the occupation, although killing civilians is illegal. From Ian Sinclair at scheerpost.com:

ISRAEL has a right to defend herself.” This supposed moral truism has been repeated incessantly, by public figures on the right and left, since the Hamas terror attacks on October 7.
Marjorie Cohn, professor emerita at Thomas Jefferson School of Law in San Diego and a former president of the US National Lawyers Guild, has a different view.
There is a consensus across most of the political spectrum in the US and Britain that, in the words of British Labour Party leader Keir Starmer, “Israel has a right to defend herself” following the Hamas attacks on October 7. What does international law say about this?
The UN charter requires all states to settle their international disputes peacefully so as not to endanger international peace and security. That doesn’t just apply to states, but also to the settlement of any international disputes.
The charter says no state can use military force against the territorial integrity or political independence of another state “or in any other manner inconsistent with the Purposes of the UN.”
Although Israel denies that Palestine is a state, the Israeli Supreme Court in the Targeted Killings case recognised the dispute between Israel and the Palestinians to be of an international character. Israel cannot use the Palestinians’ lack of statehood to justify its use of military force.