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

Commit 939eaf9

Browse files
committed
Fixed policy creation with Teams on VaaS
Fixed an issue where a policy specification with a VaaS Team as user would not be created.
1 parent 6a8d71e commit 939eaf9

3 files changed

Lines changed: 70 additions & 23 deletions

File tree

src/main/java/com/venafi/vcert/sdk/connectors/ConnectorException.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,4 +543,13 @@ public TppContactException(String policy, String error) {
543543
}
544544
}
545545

546+
public static class VaaSUsernameNotFoundException extends ConnectorException {
547+
548+
private static final long serialVersionUID = 1L;
549+
private static final String message = "Username %s is not an existing VaaS user or team";
550+
551+
public VaaSUsernameNotFoundException(String username){
552+
super(format(message, username));
553+
}
554+
}
546555
}

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

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -201,45 +201,69 @@ private static void addCitToApp(CertificateIssuingTemplate cit, Application appl
201201

202202
}
203203

204-
private static List<Application.OwnerIdsAndType> resolveUsersToCloudOwners(String[] usersList, String apiKey, Cloud cloud) {
204+
private static List<Application.OwnerIdsAndType> resolveUsersToCloudOwners(String[] usersList, String apiKey, Cloud cloud) throws VCertException {
205205
List<Application.OwnerIdsAndType> ownersList = new ArrayList<>();
206206

207207
if (usersList == null) {
208-
// When no user is provided on the list, adds the current one as owner
209-
UserDetails userDetails = cloud.authorize(apiKey);
210-
Application.OwnerIdsAndType owner = createOwner(CloudConstants.OWNER_TYPE_USER, userDetails.user().id());
211-
ownersList.add(owner);
208+
// When no users are provided on the list, adds the api key user as owner
209+
Application.OwnerIdsAndType tokenOwner = resolveApiKeyOwner(apiKey, cloud);
210+
ownersList.add(tokenOwner);
212211
}
213212
else {
214213
// Resolving the usernames list
215214
// Creating a higher level Teams object to cache the response.
216-
Teams tResponse = null;
215+
Teams teams = null;
217216
for (String username: usersList) {
218-
UserResponse response = cloud.retrieveUser(username, apiKey);
219-
// If the name matches a user, create the entry
220-
if (response != null) {
221-
Application.OwnerIdsAndType owner = createOwner(CloudConstants.OWNER_TYPE_USER, response.users().get(0).id());
217+
try{
218+
Application.OwnerIdsAndType owner = resolveUserToCloudOwner(username, apiKey, cloud);
222219
ownersList.add(owner);
223-
} else {
224-
if (tResponse == null) {
225-
tResponse = cloud.retrieveTeams(apiKey);
220+
} catch(FeignException fe) {
221+
// When no user is found, the framework throws an exception.
222+
// Exception status must be 404 Not Found.
223+
// Otherwise, a different error occurred and the exception must be thrown.
224+
if (fe.status() != 404){
225+
throw VCertException.fromFeignException(fe);
226226
}
227-
if (tResponse != null) {
228-
for (Team t : tResponse.teams()) {
229-
if (t.name().equals(username)) {
230-
Application.OwnerIdsAndType owner = createOwner(CloudConstants.OWNER_TYPE_TEAM, t.id());
231-
ownersList.add(owner);
232-
break;
233-
}
234-
}
227+
if (teams == null) {
228+
teams = cloud.retrieveTeams(apiKey);
235229
}
236-
230+
Application.OwnerIdsAndType teamOwner = resolveUserToCloudTeam(teams, username, apiKey, cloud);
231+
if (teamOwner == null){
232+
throw new ConnectorException.VaaSUsernameNotFoundException(username);
233+
}
234+
ownersList.add(teamOwner);
237235
}
238236
}
239237
}
240238
return ownersList;
241239
}
242240

241+
private static Application.OwnerIdsAndType resolveApiKeyOwner(String apiKey, Cloud cloud){
242+
// When no user is provided on the list, adds the current one as owner
243+
UserDetails userDetails = cloud.authorize(apiKey);
244+
Application.OwnerIdsAndType owner = createOwner(CloudConstants.OWNER_TYPE_USER, userDetails.user().id());
245+
return owner;
246+
}
247+
248+
private static Application.OwnerIdsAndType resolveUserToCloudOwner(String username, String apiKey, Cloud cloud){
249+
UserResponse response = cloud.retrieveUser(username, apiKey);
250+
Application.OwnerIdsAndType owner = createOwner(CloudConstants.OWNER_TYPE_USER, response.users().get(0).id());
251+
return owner;
252+
}
253+
254+
private static Application.OwnerIdsAndType resolveUserToCloudTeam(Teams teams, String username, String apiKey, Cloud cloud){
255+
if (teams == null) {
256+
teams = cloud.retrieveTeams(apiKey);
257+
}
258+
for (Team t : teams.teams()) {
259+
if (t.name().equals(username)) {
260+
Application.OwnerIdsAndType owner = createOwner(CloudConstants.OWNER_TYPE_TEAM, t.id());
261+
return owner;
262+
}
263+
}
264+
return null;
265+
}
266+
243267
public static Application.OwnerIdsAndType createOwner(String type, String id) {
244268
Application.OwnerIdsAndType owner = new Application.OwnerIdsAndType();
245269
owner.ownerType(type);

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public void createPolicyWithUsers() throws VCertException {
137137
CloudConnector connector = connectorResource.connector();
138138
String policyName = CloudTestUtils.getRandomZone();
139139
PolicySpecification policySpecification = CloudTestUtils.getPolicySpecification();
140-
policySpecification.users(new String[]{"pki-admin@opensource.qa.venafi.io","resource-owner@opensource.qa.venafi.io"});
140+
policySpecification.users(new String[]{"RVelaTeam"});
141141
connector.setPolicy(policyName, policySpecification);
142142
PolicySpecification psReturned = connector.getPolicy(policyName);
143143

@@ -194,4 +194,18 @@ public void updatePolicyWithUsers() throws VCertException {
194194
Assertions.assertEquals(2, psReturned2.users().length);
195195
Assertions.assertEquals("pki-admin@opensource.qa.venafi.io", psReturned2.users()[0]);
196196
Assertions.assertEquals("resource-owner@opensource.qa.venafi.io", psReturned2.users()[1]); }
197+
198+
@Test
199+
@DisplayName("Cloud - Testing policy creation with a team in the users list")
200+
public void createPolicyWithTeam() throws VCertException {
201+
CloudConnector connector = connectorResource.connector();
202+
String policyName = CloudTestUtils.getRandomZone();
203+
PolicySpecification policySpecification = CloudTestUtils.getPolicySpecification();
204+
policySpecification.users(new String[]{"DevOpsTeam"});
205+
connector.setPolicy(policyName, policySpecification);
206+
PolicySpecification psReturned = connector.getPolicy(policyName);
207+
208+
Assertions.assertEquals(1, psReturned.users().length);
209+
Assertions.assertEquals("DevOpsTeam", psReturned.users()[0]);
210+
}
197211
}

0 commit comments

Comments
 (0)