diff --git a/src/main/java/com/venafi/vcert/sdk/connectors/cloud/CloudConnectorUtils.java b/src/main/java/com/venafi/vcert/sdk/connectors/cloud/CloudConnectorUtils.java index 7862fd0..db7dc36 100644 --- a/src/main/java/com/venafi/vcert/sdk/connectors/cloud/CloudConnectorUtils.java +++ b/src/main/java/com/venafi/vcert/sdk/connectors/cloud/CloudConnectorUtils.java @@ -174,28 +174,38 @@ private static void addCitToApp(CertificateIssuingTemplate cit, Application appl application.certificateIssuingTemplateAliasIdMap( citAliasIdMap ); } + boolean updateApplication = false; + //if the App doesn't contain the relation to the cit if ( !citAliasIdMap.containsKey(cit.name()) ) { //adding the reference to the cit citAliasIdMap.put(cit.name(), cit.id()); + updateApplication = true; + } - //getting the appId because it will be used to invoke the API to update the related Application - String appId = application.id(); + // Updating the owners list of the Application + List ownersList = CloudConnectorUtils.resolveUsersToCloudOwners(usersList, apiKey, cloud); + if (ownersList.size() > 0){ +// application.ownerIdsAndTypes().addAll(ownersList); + List newList = mergeOwnersList(application.ownerIdsAndTypes(), ownersList); + application.ownerIdsAndTypes(newList); + updateApplication = true; + } - //The id, companyId, fqDns and internalFqDns needs to be null in the request to update the Application, - //therefore these attributes are set to null - application.id(null); - application.companyId(null); - application.fqDns(null); - application.internalFqDns(null); + if (updateApplication) { + //getting the appId because it will be used to invoke the API to update the related Application + String appId = application.id(); - // Updating the owners list - List ownersList = CloudConnectorUtils.resolveUsersToCloudOwners(usersList, apiKey, cloud); - application.ownerIdsAndTypes(ownersList); + //The id, companyId, fqDns and internalFqDns needs to be null in the request to update the Application, + //therefore these attributes are set to null + application.id(null); + application.companyId(null); + application.fqDns(null); + application.internalFqDns(null); - cloud.updateApplication(application, appId, apiKey); - } - } + cloud.updateApplication(application, appId, apiKey); + } + } private static List resolveUsersToCloudOwners(String[] usersList, String apiKey, Cloud cloud) { List ownersList = new ArrayList<>(); @@ -289,6 +299,19 @@ private static String[] resolveCloudOwnersNames(String policyName, String apiKey return usersList.toArray(new String[0]); } + private static List mergeOwnersList(List sourceList, List incomingList) { + sourceList.addAll(incomingList); + Map ownerMap = new HashMap<>(); + + for(Application.OwnerIdsAndType owner : sourceList) { + if (!ownerMap.containsKey(owner.ownerId())){ + ownerMap.put(owner.ownerId(), owner); + } + } + + return new ArrayList<>(ownerMap.values()); + } + private static CertificateIssuingTemplate getPolicy(String policyName, String apiKey, Cloud cloud) throws VCertException { CloudZone zone = new CloudZone(policyName); diff --git a/src/test/java/com/venafi/vcert/sdk/connectors/cloud/CloudConnectorPolicyAT.java b/src/test/java/com/venafi/vcert/sdk/connectors/cloud/CloudConnectorPolicyAT.java index 6972eda..b008930 100644 --- a/src/test/java/com/venafi/vcert/sdk/connectors/cloud/CloudConnectorPolicyAT.java +++ b/src/test/java/com/venafi/vcert/sdk/connectors/cloud/CloudConnectorPolicyAT.java @@ -121,4 +121,30 @@ public void createAndGetPolicyContacts() throws VCertException { Assertions.assertEquals("pki-admin@opensource.qa.venafi.io", psReturned.users()[0]); Assertions.assertEquals("resource-owner@opensource.qa.venafi.io", psReturned.users()[1]); } + + @Test + @DisplayName("Cloud - Testing setting contacts that are duplicated on VaaS") + public void testPolicyContactsDuplicated() throws VCertException { + CloudConnector connector = connectorResource.connector(); + String policyName = CloudTestUtils.getRandomZone(); + PolicySpecification policySpecification = CloudTestUtils.getPolicySpecification(); + policySpecification.users(new String[]{"pki-admin@opensource.qa.venafi.io","resource-owner@opensource.qa.venafi.io"}); + connector.setPolicy(policyName, policySpecification); + PolicySpecification psReturned = connector.getPolicy(policyName); + + Assertions.assertEquals(2, psReturned.users().length); + Assertions.assertEquals("pki-admin@opensource.qa.venafi.io", psReturned.users()[0]); + Assertions.assertEquals("resource-owner@opensource.qa.venafi.io", psReturned.users()[1]); + + //Updating the Policy Specification to include the duplicated contacts + + PolicySpecification ps2 = CloudTestUtils.getPolicySpecification(); + ps2.users(new String[]{"pki-admin@opensource.qa.venafi.io"}); + connector.setPolicy(policyName, policySpecification); + PolicySpecification psReturned2 = connector.getPolicy(policyName); + + Assertions.assertEquals(2, psReturned2.users().length); + Assertions.assertEquals("pki-admin@opensource.qa.venafi.io", psReturned.users()[0]); + Assertions.assertEquals("resource-owner@opensource.qa.venafi.io", psReturned.users()[1]); + } }