Skip to content
This repository was archived by the owner on Jun 5, 2025. It is now read-only.

Commit 55f5577

Browse files
committed
Adding support for duplicated contacts
Adding implementation to filter out duplicated contacts when updating VaaS Applications.
1 parent 5abd6b1 commit 55f5577

2 files changed

Lines changed: 63 additions & 14 deletions

File tree

src/main/java/com/venafi/vcert/sdk/connectors/cloud/CloudConnectorUtils.java

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -174,28 +174,38 @@ private static void addCitToApp(CertificateIssuingTemplate cit, Application appl
174174
application.certificateIssuingTemplateAliasIdMap( citAliasIdMap );
175175
}
176176

177+
boolean updateApplication = false;
178+
177179
//if the App doesn't contain the relation to the cit
178180
if ( !citAliasIdMap.containsKey(cit.name()) ) {
179181
//adding the reference to the cit
180182
citAliasIdMap.put(cit.name(), cit.id());
183+
updateApplication = true;
184+
}
181185

182-
//getting the appId because it will be used to invoke the API to update the related Application
183-
String appId = application.id();
186+
// Updating the owners list of the Application
187+
List<Application.OwnerIdsAndType> ownersList = CloudConnectorUtils.resolveUsersToCloudOwners(usersList, apiKey, cloud);
188+
if (ownersList.size() > 0){
189+
// application.ownerIdsAndTypes().addAll(ownersList);
190+
List<Application.OwnerIdsAndType> newList = mergeOwnersList(application.ownerIdsAndTypes(), ownersList);
191+
application.ownerIdsAndTypes(newList);
192+
updateApplication = true;
193+
}
184194

185-
//The id, companyId, fqDns and internalFqDns needs to be null in the request to update the Application,
186-
//therefore these attributes are set to null
187-
application.id(null);
188-
application.companyId(null);
189-
application.fqDns(null);
190-
application.internalFqDns(null);
195+
if (updateApplication) {
196+
//getting the appId because it will be used to invoke the API to update the related Application
197+
String appId = application.id();
191198

192-
// Updating the owners list
193-
List<Application.OwnerIdsAndType> ownersList = CloudConnectorUtils.resolveUsersToCloudOwners(usersList, apiKey, cloud);
194-
application.ownerIdsAndTypes(ownersList);
199+
//The id, companyId, fqDns and internalFqDns needs to be null in the request to update the Application,
200+
//therefore these attributes are set to null
201+
application.id(null);
202+
application.companyId(null);
203+
application.fqDns(null);
204+
application.internalFqDns(null);
195205

196-
cloud.updateApplication(application, appId, apiKey);
197-
}
198-
}
206+
cloud.updateApplication(application, appId, apiKey);
207+
}
208+
}
199209

200210
private static List<Application.OwnerIdsAndType> resolveUsersToCloudOwners(String[] usersList, String apiKey, Cloud cloud) {
201211
List<Application.OwnerIdsAndType> ownersList = new ArrayList<>();
@@ -289,6 +299,19 @@ private static String[] resolveCloudOwnersNames(String policyName, String apiKey
289299
return usersList.toArray(new String[0]);
290300
}
291301

302+
private static List<Application.OwnerIdsAndType> mergeOwnersList(List<Application.OwnerIdsAndType> sourceList, List<Application.OwnerIdsAndType> incomingList) {
303+
sourceList.addAll(incomingList);
304+
Map<String, Application.OwnerIdsAndType> ownerMap = new HashMap<>();
305+
306+
for(Application.OwnerIdsAndType owner : sourceList) {
307+
if (!ownerMap.containsKey(owner.ownerId())){
308+
ownerMap.put(owner.ownerId(), owner);
309+
}
310+
}
311+
312+
return new ArrayList<>(ownerMap.values());
313+
}
314+
292315
private static CertificateIssuingTemplate getPolicy(String policyName, String apiKey, Cloud cloud) throws VCertException {
293316

294317
CloudZone zone = new CloudZone(policyName);

src/test/java/com/venafi/vcert/sdk/connectors/cloud/CloudConnectorPolicyAT.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,30 @@ public void createAndGetPolicyContacts() throws VCertException {
121121
Assertions.assertEquals("pki-admin@opensource.qa.venafi.io", psReturned.users()[0]);
122122
Assertions.assertEquals("resource-owner@opensource.qa.venafi.io", psReturned.users()[1]);
123123
}
124+
125+
@Test
126+
@DisplayName("Cloud - Testing setting contacts that are duplicated on VaaS")
127+
public void testPolicyContactsDuplicated() throws VCertException {
128+
CloudConnector connector = connectorResource.connector();
129+
String policyName = CloudTestUtils.getRandomZone();
130+
PolicySpecification policySpecification = CloudTestUtils.getPolicySpecification();
131+
policySpecification.users(new String[]{"pki-admin@opensource.qa.venafi.io","resource-owner@opensource.qa.venafi.io"});
132+
connector.setPolicy(policyName, policySpecification);
133+
PolicySpecification psReturned = connector.getPolicy(policyName);
134+
135+
Assertions.assertEquals(2, psReturned.users().length);
136+
Assertions.assertEquals("pki-admin@opensource.qa.venafi.io", psReturned.users()[0]);
137+
Assertions.assertEquals("resource-owner@opensource.qa.venafi.io", psReturned.users()[1]);
138+
139+
//Updating the Policy Specification to include the duplicated contacts
140+
141+
PolicySpecification ps2 = CloudTestUtils.getPolicySpecification();
142+
ps2.users(new String[]{"pki-admin@opensource.qa.venafi.io"});
143+
connector.setPolicy(policyName, policySpecification);
144+
PolicySpecification psReturned2 = connector.getPolicy(policyName);
145+
146+
Assertions.assertEquals(2, psReturned2.users().length);
147+
Assertions.assertEquals("pki-admin@opensource.qa.venafi.io", psReturned.users()[0]);
148+
Assertions.assertEquals("resource-owner@opensource.qa.venafi.io", psReturned.users()[1]);
149+
}
124150
}

0 commit comments

Comments
 (0)