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

Commit ce9b976

Browse files
committed
Added support for Teams on VaaS.
Added validation to not add api Key user as owner when a user list has been declared in the Policy Specification
1 parent 30a85d0 commit ce9b976

8 files changed

Lines changed: 99 additions & 36 deletions

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ Response retrieveCertificate(@Param("id") String id, @Param("apiKey") String api
114114
@RequestLine("GET /v1/users/{id}")
115115
User retrieveUserById(@Param("id") String id, @Param("apiKey") String apiKey);
116116

117+
@Headers({"tppl-api-key: {apiKey}", "Content-Type: application/json"})
118+
@RequestLine("GET /v1/teams")
119+
Teams retrieveTeams(@Param("apiKey") String apiKey);
120+
117121
static Cloud connect() {
118122
return connect((Config)null);
119123
}

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

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -199,16 +199,19 @@ private static void addCitToApp(CertificateIssuingTemplate cit, Application appl
199199
private static List<Application.OwnerIdsAndType> resolveOwners(String[] usersList, String apiKey, Cloud cloud) {
200200
List<Application.OwnerIdsAndType> ownersList = new ArrayList<>();
201201

202-
// Adding the current user to the owners list
203-
UserDetails userDetails = cloud.authorize(apiKey);
204-
String userId = userDetails.user().id();
205-
Application.OwnerIdsAndType currentOwner = new Application.OwnerIdsAndType();
206-
currentOwner.ownerId(userId);
207-
currentOwner.ownerType(CloudConstants.OWNER_TYPE_USER);
208-
ownersList.add(currentOwner);
209-
210-
// Resolving the usernames list
211-
if (usersList != null) {
202+
if (usersList == null) {
203+
// When no user is provided on the list, adds the current one as owner
204+
UserDetails userDetails = cloud.authorize(apiKey);
205+
String userId = userDetails.user().id();
206+
Application.OwnerIdsAndType currentOwner = new Application.OwnerIdsAndType();
207+
currentOwner.ownerId(userId);
208+
currentOwner.ownerType(CloudConstants.OWNER_TYPE_USER);
209+
ownersList.add(currentOwner);
210+
}
211+
else {
212+
// Resolving the usernames list
213+
// Creating a higher level Teams object to cache the response.
214+
Teams tResponse = null;
212215
for (String username: usersList) {
213216
UserResponse response = cloud.retrieveUser(username, apiKey);
214217
// If the name matches a user, create the entry
@@ -218,11 +221,24 @@ private static List<Application.OwnerIdsAndType> resolveOwners(String[] usersLis
218221
owner.ownerType(CloudConstants.OWNER_TYPE_USER);
219222
ownersList.add(owner);
220223
}else{
221-
// TODO: Logic to find Teams by name is not available at VaaS. Update when ready.
224+
if (tResponse == null) {
225+
tResponse = cloud.retrieveTeams(apiKey);
226+
}
227+
if (tResponse != null) {
228+
for (Team t : tResponse.teams()) {
229+
if (t.name().equals(username)) {
230+
Application.OwnerIdsAndType owner = new Application.OwnerIdsAndType();
231+
owner.ownerId(t.id());
232+
owner.ownerType(CloudConstants.OWNER_TYPE_TEAM);
233+
ownersList.add(owner);
234+
break;
235+
}
236+
}
237+
}
238+
222239
}
223240
}
224241
}
225-
226242
return ownersList;
227243
}
228244

@@ -235,12 +251,25 @@ public static CloudPolicy getCloudPolicy( String policyName, String apiKey, Clou
235251
throw new VCertException("Application "+ zone.appName() + " could not be found");
236252
}
237253
List<String> usersList = new ArrayList<>();
254+
Teams tResponse = null;
238255
for (Application.OwnerIdsAndType owner: app.ownerIdsAndTypes()) {
239256
if (owner.ownerType().equals(CloudConstants.OWNER_TYPE_USER)) {
240257
User user = cloud.retrieveUserById(owner.ownerId(), apiKey);
241258
usersList.add(user.username());
242259
}else if (owner.ownerType().equals(CloudConstants.OWNER_TYPE_TEAM)) {
243-
// TODO: Include Teams logic here when supported by VaaS API.
260+
if (tResponse == null){
261+
// This validation caches the teams list, so we don't have to call
262+
// the teams' endpoint multiple times when iterating owners of type TEAM
263+
tResponse = cloud.retrieveTeams(apiKey);
264+
}
265+
if (tResponse != null){
266+
for (Team t : tResponse.teams()) {
267+
if (t.id().equals(owner.ownerId())){
268+
usersList.add(t.name());
269+
break;
270+
}
271+
}
272+
}
244273
}
245274
}
246275
cloudPolicy.owners(usersList.toArray(new String[0]));
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.venafi.vcert.sdk.connectors.cloud.domain;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
6+
@Data
7+
@AllArgsConstructor
8+
public class Team {
9+
10+
private String id;
11+
private String name;
12+
private String role;
13+
private String companyId;
14+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.venafi.vcert.sdk.connectors.cloud.domain;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Data;
6+
7+
import java.util.List;
8+
9+
@Data
10+
@AllArgsConstructor
11+
public class Teams {
12+
13+
@SerializedName("teams")
14+
private List<Team> teams;
15+
}

src/main/java/com/venafi/vcert/sdk/connectors/tpp/AbstractTppConnector.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,11 @@ public TPPPolicy getTPPPolicy(String policyName) throws VCertException {
125125

126126
protected String[] resolveTPPContacts(String[] contacts) throws VCertException{
127127
List<String> identitiesIdList = new ArrayList<>();
128-
for (String contact: contacts) {
129-
IdentityEntry identity = this.getTPPIdentity(contact);
130-
identitiesIdList.add(identity.prefixedUniversal());
128+
if (contacts != null){
129+
for (String contact: contacts) {
130+
IdentityEntry identity = this.getTPPIdentity(contact);
131+
identitiesIdList.add(identity.prefixedUniversal());
132+
}
131133
}
132134
return identitiesIdList.toArray(new String[0]);
133135
}

src/main/java/com/venafi/vcert/sdk/connectors/tpp/TppConnectorUtils.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,12 +330,12 @@ private static String[] retrieveUsernamesFromTPPContacts(String policyName, TppA
330330
throw new VCertException(contactResponse.error());
331331
}
332332
if (contactResponse.values() != null) {
333-
String[] contacts = (String[]) contactResponse.values();
334-
for (String prefixedUniversal : contacts) {
333+
Object[] contacts = contactResponse.values();
334+
for (Object prefixedUniversal : contacts) {
335335
try{
336336
ValidateIdentityResponse response = tppAPI.validateIdentity(
337337
new ValidateIdentityRequest(
338-
new IdentityInformation(prefixedUniversal)
338+
new IdentityInformation((String)prefixedUniversal)
339339
)
340340
);
341341
String username = response.id().name();

src/main/java/com/venafi/vcert/sdk/connectors/tpp/TppTokenConnector.java

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,11 @@
22

33
import static org.apache.commons.lang3.StringUtils.isBlank;
44

5-
import java.util.Map;
6-
7-
import com.google.common.io.CharStreams;
85
import com.venafi.vcert.sdk.VCertException;
9-
import com.venafi.vcert.sdk.certificate.ImportRequest;
10-
import com.venafi.vcert.sdk.certificate.ImportResponse;
116
import com.venafi.vcert.sdk.connectors.ConnectorException.FailedToRevokeTokenException;
127
import com.venafi.vcert.sdk.connectors.ConnectorException.MissingAccessTokenException;
138
import com.venafi.vcert.sdk.connectors.ConnectorException.MissingRefreshTokenException;
149
import com.venafi.vcert.sdk.connectors.TokenConnector;
15-
import com.venafi.vcert.sdk.connectors.tpp.Tpp.CertificateRenewalResponse;
16-
import com.venafi.vcert.sdk.connectors.tpp.Tpp.CertificateRequestResponse;
17-
import com.venafi.vcert.sdk.connectors.tpp.Tpp.CertificateRetrieveResponse;
18-
import com.venafi.vcert.sdk.connectors.tpp.Tpp.CertificateRevokeResponse;
19-
import com.venafi.vcert.sdk.connectors.tpp.Tpp.CertificateSearchResponse;
20-
import com.venafi.vcert.sdk.connectors.tpp.endpoint.*;
21-
import com.venafi.vcert.sdk.connectors.tpp.endpoint.ssh.TppSshCaTemplateRequest;
22-
import com.venafi.vcert.sdk.connectors.tpp.endpoint.ssh.TppSshCaTemplateResponse;
23-
import com.venafi.vcert.sdk.connectors.tpp.endpoint.ssh.TppSshCertRequest;
24-
import com.venafi.vcert.sdk.connectors.tpp.endpoint.ssh.TppSshCertRequestResponse;
25-
import com.venafi.vcert.sdk.connectors.tpp.endpoint.ssh.TppSshCertRetrieveRequest;
26-
import com.venafi.vcert.sdk.connectors.tpp.endpoint.ssh.TppSshCertRetrieveResponse;
2710
import com.venafi.vcert.sdk.endpoint.Authentication;
2811
import com.venafi.vcert.sdk.endpoint.ConnectorType;
2912

src/test/java/com/venafi/vcert/sdk/connectors/tpp/TppTokenConnectorPolicyAT.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.venafi.vcert.sdk.connectors.tpp.endpoint.IdentityInformation;
77
import com.venafi.vcert.sdk.connectors.tpp.endpoint.ValidateIdentityRequest;
88
import com.venafi.vcert.sdk.connectors.tpp.endpoint.ValidateIdentityResponse;
9+
import com.venafi.vcert.sdk.policy.domain.PolicySpecification;
910
import org.junit.jupiter.api.Assertions;
1011
import org.junit.jupiter.api.DisplayName;
1112
import org.junit.jupiter.api.Test;
@@ -45,4 +46,19 @@ public void validateIdentity() throws VCertException {
4546
Assertions.assertNotNull(response);
4647
Assertions.assertEquals(username, response.id().name());
4748
}
49+
50+
@Test
51+
@DisplayName("TPP - Create a policy with contacts and retrieve it")
52+
public void createAndGetPolicyContacts() throws VCertException {
53+
TppTokenConnector connector = connectorResource.connector();
54+
55+
PolicySpecification policySpecification = TppTestUtils.getPolicySpecification();
56+
policySpecification.users(new String[]{"osstestuser"});
57+
String zone = TppTestUtils.getRandomZone();
58+
connector.setPolicy(zone, policySpecification);
59+
PolicySpecification psReturned = connector.getPolicy(zone);
60+
61+
Assertions.assertEquals(1, psReturned.users().length);
62+
Assertions.assertEquals("osstestuser", psReturned.users()[0]);
63+
}
4864
}

0 commit comments

Comments
 (0)