Ultimately, framing this as a legal question rather than as a political one is only marginally useful. There is no court which could hold North Korea accountable for a violation of an international legal obligation or a treaty violation. And, seeking recourse from the domestic courts of North Korea in the context of its current totalitarian regime would be futile.
- Protocol I to the Geneva Conventions (to which North Korea is a party) bans the used of nuclear weapons on civilian targets.
None of the other treaties to which North Korea is a party, other than its membership in the United Nations which calls for peaceable resolution of disputes and the involvement of the U.N. Security Council, are plausibly relevant to this situation.
Firing an ICBM over a nation is a violation of its airspace under general principles of international law. Both Japan and the U.S. would be aggrieved.
Directing an ICBM at or over another sovereign nation's territory, even if it is intended as a warning shot or a way of demonstrating a credible threat, could be determined by the offended countries to be an act of war that would justify declaring all out war on the offending country. The U.S., Japan, and any of their respective allies could join this cause if they saw fit based upon a threat directed at any one of them.
In practice, since any recourse for violating international law or a treaty would involve action organized by the aggrieved country rather than legal action, the real question is whether Russia or China would stand in the way of actions by the U.S., Japan and their allies to retaliate against North Korea. And, the main relevance of international law and treaties would be to provide China and Russia with justification for not intervening and to provide allies with a justification for intervening.
China accounts for 75% of North Korea's trade, borders North Korea, and has stronger relations with it than any other country, while North Korea is currently fairly peripheral in Russia's political calculus. Therefore, the primary issue, which is really only tangentially related to any argument related to legality, is whether China would take any action to interfere with or object to military action and/or sanctions directed by the U.S. and/or Japan and/or their allies against North Korea, and whether an argument based upon international law would influence China's opinion on the subject.
If for whatever reason, China (which has nuclear weapons of its own, strong trade ties with the U.S., and lots of importance in world affairs especially in this region) strenuously objected, retaliating militarily would be much riskier for the U.S., Japan and/or their allies.
If China did not strenuously object, even if it raised token objections, retaliating militarily could probably proceed in a scenario in which North Korea was an international orphan with no allies, and the U.S. and/or Japan had myriad allies willing to offer any necessary assistance if requested.