It’s dangerous to assume that just because something is legal or illegal, it is therefore right or wrong.
There is a distinction between what is legal and what is right. After all, the institution of slavery, lack of women’s voting rights, and Nazi treatment of minorities were all perfectly legal, but were also abject moral travesties.
There are certainly many laws which coincide with morality (like laws against initiating violence), and acting in accordance with such laws is moral. However following a law simply because it’s a law demonstrates nothing about one’s virtue. In fact, the moral thing to do is often in complete opposition to the law. For example, when an officer refuses to comply with an order which would victimize an innocent person, the officer is acting outside of his legal duties but acting morally.
When we do the right thing, we don’t require a law forcing us to do it. If a law is forcing us to take an action we otherwise wouldn’t take based on our own moral compass, there’s a good chance the law is not based in morality.