Yes, it is illegal to get someone's iCloud password and misuse the iCloud account data such as leaking them in public. As long as the person is close to you and you use the password for monitoring purpose and keep the data secret, it should not be a problem. But it is always recommended to get someone's consent to monitor someone's iCloud account.