If the principal executes any mortgage or gives other security to the surety or endorser to indemnify him against loss by reason of his suretyship, the surety or endorser may proceed to foreclose such mortgage or enforce such other lien or security as soon as judgment shall be rendered against him on his contract.