The February 1903 lease agreement for Guantanamo states that the United States is allowed "generally to do any and all things necessary to fit the premises for use as coaling or naval stations only, and for no other purpose."
The prison was built at Guantanamo, not because of the excellent harbor, but because it was thought to be a legal black hole, out of reach of the courts, because of the Supreme Court rulings in the Insular Cases, that the Constitution does not "follow the flag". The flag, they say, is sometimes only a sign of imperial domination, of unchecked military power.
Since the siting of the prison was motivated by this rationale, it violates the terms of the lease, which only allows the site generally to be fitted to the purpose of a naval station, only, and not any additional purpose.
The permanent arbitrary detention of people, in violation of the rights of man, the Constitution, and international conventions, is not generally held to be a necessary function of a military installation, despite its frequency.