https://www.history.navy.mil/research/library/online-reading-room/title-list-alphabetically/p/posse-comitatus-act-and-related-matters-a-sketch.html
The Posse Comitatus Act does not apply where Congress has expressly authorized use of the military to execute the law. Congress has done so in three ways, by giving a branch of the armed forces civilian law enforcement authority, by establishing general rules for certain types of assistance, and by addressing individual cases and circumstances with more narrowly crafted legislation. Thus it has vested the Coast Guard, a branch of the armed forces, with broad law enforcement responsibilities. Second, over the years it has passed a fairly extensive array of particularized statutes, like those permitting the President to call out the armed forces in times of insurrection and domestic violence, 10 U.S.C. §§ 331-335. Finally, it has enacted general legislation authorizing the armed forces to share information and equipment with civilian law enforcement agencies, 10 U.S.C. §§ 371-382.
The legislation contains both explicit grants of authority and restrictions on the use of that authority for military assistance to the police - federal, state and local - particularly in the form of information and equipment, 10 U.S.C. §§ 371-382. Section 371 specifically authorizes the armed forces to share information acquired during military operations and in fact encourages the armed forces to plan their activities with an eye to the production of incidental civilian benefits. The section allows the use of military undercover agents and the collection of intelligence concerning civilian activities only where there is a nexus to an underlying military purpose. Under sections 372 through 374, military equipment and facilities may be made available to civilian authorities; members of the armed forces may train civilian police on the operation and maintenance of equipment and may provide them with expert advice; and military personnel may be employed to maintain and operate the equipment supplied.
From Wikipedia article on the Insurrection Act of 1807:
There are Constitutional exceptions to Posse Comitatus restrictions rooted in the President's own constitutional authority. Defense Department guidelines describe "homeland defense" as a "constitutional exception" to Posse Comitatus restriction, meaning that measures necessary to guarantee National Security from external threats are not subject to the same limitations.
The 1807 Act has been modified twice. In 1861, a new section was added allowing the federal government to use the National Guard and armed forces against the will of the state government in the case of "rebellion against the authority of the government of the United States," in anticipation of continued unrest after the Civil War.[4] In 1871, the Third Enforcement Act revised this section (§ 253) to protect Black Americans from attacks by the Ku Klux Klan. The language added at that time allows the federal government to use the act to enforce the Equal Protection Clause of the Fourteenth Amendment to the United States Constitution.[3]:63–64