How to leave a war in which you are not the main attacker/defender?

Solution 1:

You can't.

You can minimise losses by keeping your fleet out of the way, but there's not much you can do if the enemy decides to attack your territory or, worse, cede your planets.

In future, you can avoid this by not making defensive pacts that you don't want to have to honour.