I won't go over whether or not it was the prudent thing to do, but it's not illegal. Discharge of a firearm is illegal, but it's assumed that any otherwise justified used of a firearm would be legal, such as in self defense against a person or an animal. The biggest problem would be that there are some locations in the park (usually indoors) where bringing in a firearm is illegal.
Firearms
Federal law allows people who can legally possess firearms under applicable federal, state, and local laws, to legally possess firearms in Yellowstone. Hunting and the discharge of firearms remain prohibited in Yellowstone.
It is your responsibility to understand and comply with all applicable state, local, and federal firearms laws before entering the park. Yellowstone encompasses parts of
Wyoming,
Montana, and
Idaho, and each state has different regulations: follow the links provided here to learn more.
Federal law prohibits firearms in certain facilities in this park (such as visitor centers and government offices): those places are marked with signs at all public entrances.
Firearms should not be considered a wildlife protection strategy. Bear spray and other safety precautions are the proven methods for preventing bear and other wildlife interactions.
Bear spray is probably more effective and easier to use.