Yes, it  is legal. To begin with, you signed an agreement stating that you would repay them; since you contractually obligated yourself to do this, you have to reimburse the company if you leave in less than a year. Contracts which people voluntarily enter into are enforceable, and for  this purpose, it is voluntary if you chose to sign it to get the job--that is, you had the right and ability to decline the job if you considered this condition onerous or unfaiir.Second, there is nothing inherently illegal about agreeing to reimburse another's cost, which they incur due to your actions. In this case, they incur a "wasted" recruitment fee if you leave employment too soon; there is nothing illegal about  you covering that cost.