Yes this is legal and has always been the case, that the military can never make public statements.
{...
Indicative of the military’s special status, the Uniform Code of Military Justice prohibits military personnel from using “contemptuous speech” against the President and other leaders, from engaging in “conduct prejudicial to good order and discipline in the Armed Forces,” and from “conduct unbecoming an officer and a gentleman.”
...}
The only time any members of the military are allowed political expression, is if they are out of uniform and not identifiable as being members of the military.
Members of the military are subordinate to their political leaders, so are prohibited from being critical of them.