InstanceProfileHelper { boolean policyChanged(String localPolicyJSON, com.amazonaws.services.identitymanagement.model.InstanceProfile remoteInstanceProfile) { String instanceProfileName = remoteInstanceProfile.getInstanceProfileName(); List<Role> roles = remoteInstanceProfile.getRoles(); Asserts.isFalse(roles.isEmpty(), "instance profile does not not have role, please check whether the role failed to add to instance profile, instanceProfileName={}", instanceProfileName); Asserts.equals(roles.size(), 1, "instance profile should only have one role, check whether it's modified not by cmn, instanceProfileName={}, roles={}", instanceProfileName, roles); Role role = roles.get(0); Optional<Policy> remotePolicy = AWS.getIam().findRolePolicy(role.getRoleName(), role.getRoleName()); if (!remotePolicy.isPresent()) { logger.warn("role policy doesn't exist, it could be due to failure of last sync, it will try to create this time, instanceProfileName={}", instanceProfileName); return true; } Policy localPolicy = Policy.fromJson(localPolicyJSON); return policyChanged(localPolicy, remotePolicy.get()); }    }
@Test void policyChangedWithSamePolicy() { Policy policy1 = Policy.fromJson(ClasspathResources.text("iam-test/policy1.json")); Policy policy2 = Policy.fromJson(ClasspathResources.text("iam-test/policy1.json")); boolean changed = instanceProfileHelper.policyChanged(policy1, policy2); assertFalse(changed); }
@Test void policyChangedWithDifferentPolicies() { Policy policy1 = Policy.fromJson(ClasspathResources.text("iam-test/policy1.json")); Policy policy2 = Policy.fromJson(ClasspathResources.text("iam-test/policy2.json")); boolean changed = instanceProfileHelper.policyChanged(policy1, policy2); assertTrue(changed); }