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

Commit ec55a1a

Browse files
Transition from Username/Password to Token Auth for TPP (#19)
* adding support for getting access token on tpp server * VEN-60642 Provide support for Token Authentication on implemented operations. * Added a new high level class to invoke the api methods using the token mechanism. This new class VCertTknClient does contain the same methods as VCertClient but the low level implementation changes to make use of the token. It also includes methods for token specific operations (refresh, revoke). * Reverted changes in example classes that are no longer valid. * Updated code according to @ttonchev review. Co-authored-by: angelmoo <angel.moo@venafi.com>
1 parent a253200 commit ec55a1a

21 files changed

Lines changed: 2321 additions & 183 deletions

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,8 @@ hs_err_pid*
2525

2626
.idea/
2727
*.iml
28-
target/
28+
target/
29+
/.settings/
30+
/.classpath
31+
/.factorypath
32+
/.project

src/main/java/com/venafi/vcert/sdk/VCertClient.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
import com.venafi.vcert.sdk.connectors.tpp.TppConnector;
2020
import com.venafi.vcert.sdk.endpoint.Authentication;
2121
import com.venafi.vcert.sdk.endpoint.ConnectorType;
22+
import com.venafi.vcert.sdk.utils.VCertConstants;
2223

2324
public class VCertClient implements Connector {
2425

2526
private Connector connector;
26-
private static final String defaultVendorAndProductName = "Venafi VCert-Java";
2727

2828
public VCertClient(Config config) throws VCertException {
2929
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
@@ -42,7 +42,8 @@ public VCertClient(Config config) throws VCertException {
4242
throw new VCertException("ConnectorType is not defined");
4343
}
4444

45-
connector.setVendorAndProductName(isBlank(config.appInfo()) ? defaultVendorAndProductName : config.appInfo());
45+
connector.setVendorAndProductName(isBlank(config.appInfo()) ? VCertConstants.DEFAULT_VENDOR_AND_PRODUCT_NAME :
46+
config.appInfo());
4647
}
4748

4849
@VisibleForTesting
@@ -59,7 +60,10 @@ public ConnectorType getType() {
5960
}
6061

6162
/**
62-
* {@inheritDoc}
63+
* Method not implemented yet.
64+
* Guaranteed to throw an exception.
65+
*
66+
* @throws UnsupportedOperationException always
6367
*/
6468
@Override
6569
public void setBaseUrl(String url) throws VCertException {
@@ -207,13 +211,12 @@ public void revokeCertificate(RevocationRequest request) throws VCertException {
207211
@Override
208212
public String renewCertificate(RenewalRequest request) throws VCertException {
209213
try {
210-
connector.renewCertificate(request);
214+
return connector.renewCertificate(request);
211215
} catch (FeignException e) {
212216
throw VCertException.fromFeignException(e);
213217
} catch (Exception e) {
214218
throw new VCertException("Unexpected exception", e);
215219
}
216-
return null;
217220
}
218221

219222
/**
@@ -222,13 +225,12 @@ public String renewCertificate(RenewalRequest request) throws VCertException {
222225
@Override
223226
public ImportResponse importCertificate(ImportRequest request) throws VCertException {
224227
try {
225-
connector.importCertificate(request);
228+
return connector.importCertificate(request);
226229
} catch (FeignException e) {
227230
throw VCertException.fromFeignException(e);
228231
} catch (Exception e) {
229232
throw new VCertException("Unexpected exception", e);
230233
}
231-
return null;
232234
}
233235

234236
/**
@@ -237,13 +239,11 @@ public ImportResponse importCertificate(ImportRequest request) throws VCertExcep
237239
@Override
238240
public Policy readPolicyConfiguration(String zone) throws VCertException {
239241
try {
240-
connector.readPolicyConfiguration(zone);
242+
return connector.readPolicyConfiguration(zone);
241243
} catch (FeignException e) {
242244
throw VCertException.fromFeignException(e);
243245
} catch (Exception e) {
244246
throw new VCertException("Unexpected exception", e);
245247
}
246-
return null;
247248
}
248-
249249
}
Lines changed: 252 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,252 @@
1+
package com.venafi.vcert.sdk;
2+
3+
import static org.apache.commons.lang3.StringUtils.isBlank;
4+
import java.security.Security;
5+
import com.google.common.annotations.VisibleForTesting;
6+
import feign.FeignException;
7+
import com.venafi.vcert.sdk.certificate.CertificateRequest;
8+
import com.venafi.vcert.sdk.certificate.ImportRequest;
9+
import com.venafi.vcert.sdk.certificate.ImportResponse;
10+
import com.venafi.vcert.sdk.certificate.PEMCollection;
11+
import com.venafi.vcert.sdk.certificate.RenewalRequest;
12+
import com.venafi.vcert.sdk.certificate.RevocationRequest;
13+
import com.venafi.vcert.sdk.connectors.Policy;
14+
import com.venafi.vcert.sdk.connectors.TokenConnector;
15+
import com.venafi.vcert.sdk.connectors.ZoneConfiguration;
16+
import com.venafi.vcert.sdk.connectors.tpp.TokenInfo;
17+
import com.venafi.vcert.sdk.connectors.tpp.Tpp;
18+
import com.venafi.vcert.sdk.connectors.tpp.TppTokenConnector;
19+
import com.venafi.vcert.sdk.endpoint.Authentication;
20+
import com.venafi.vcert.sdk.endpoint.ConnectorType;
21+
import com.venafi.vcert.sdk.utils.VCertConstants;
22+
23+
24+
public class VCertTknClient implements TokenConnector {
25+
26+
private TokenConnector connector;
27+
28+
public VCertTknClient(Config config) throws VCertException {
29+
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
30+
switch (config.connectorType()) {
31+
case TPP_TOKEN:
32+
connector = new TppTokenConnector(Tpp.connect(config));
33+
break;
34+
default:
35+
throw new VCertException("ConnectorType is not defined");
36+
}
37+
connector.setVendorAndProductName(isBlank(config.appInfo()) ? VCertConstants.DEFAULT_VENDOR_AND_PRODUCT_NAME :
38+
config.appInfo());
39+
}
40+
41+
@VisibleForTesting
42+
VCertTknClient(TokenConnector connector) {
43+
this.connector = connector;
44+
}
45+
46+
/**
47+
* {@inheritDoc}
48+
*/
49+
@Override
50+
public ConnectorType getType() {
51+
return connector.getType();
52+
}
53+
54+
/**
55+
* Method not implemented yet.
56+
* Guaranteed to throw an exception.
57+
*
58+
* @throws UnsupportedOperationException always
59+
*/
60+
@Override
61+
public void setBaseUrl(String url) throws VCertException {
62+
connector.setBaseUrl(url);
63+
}
64+
65+
/**
66+
* {@inheritDoc}
67+
*/
68+
@Override
69+
public void setZone(String zone) {
70+
connector.setZone(zone);
71+
}
72+
73+
/**
74+
* {@inheritDoc}
75+
*/
76+
@Override
77+
public void setVendorAndProductName(String vendorAndProductName) {
78+
connector.setVendorAndProductName(vendorAndProductName);
79+
}
80+
81+
/**
82+
* {@inheritDoc}
83+
*/
84+
@Override
85+
public String getVendorAndProductName() {
86+
return connector.getVendorAndProductName();
87+
}
88+
89+
//=========================================================================================\\
90+
//=============================== VENAFI 20.2 OAUTH METHODS ===============================\\
91+
//=========================================================================================\\
92+
93+
@Override
94+
public TokenInfo getAccessToken(Authentication auth) throws VCertException{
95+
try {
96+
return connector.getAccessToken(auth);
97+
} catch (FeignException e) {
98+
throw VCertException.fromFeignException(e);
99+
} catch (Exception e) {
100+
throw new VCertException("Unexpected exception", e);
101+
}
102+
}
103+
104+
@Override
105+
public TokenInfo refreshAccessToken(String refreshToken, String applicationId) throws VCertException{
106+
return connector.refreshAccessToken(refreshToken, applicationId);
107+
}
108+
109+
@Override
110+
public int revokeAccessToken(String accessToken) throws VCertException {
111+
return connector.revokeAccessToken(accessToken);
112+
}
113+
114+
/**
115+
* {@inheritDoc}
116+
*/
117+
@Override
118+
public void ping(String accessToken) throws VCertException {
119+
try {
120+
connector.ping(accessToken);
121+
} catch (FeignException e) {
122+
throw VCertException.fromFeignException(e);
123+
} catch (Exception e) {
124+
throw new VCertException("Unexpected exception", e);
125+
}
126+
}
127+
128+
/**
129+
* {@inheritDoc}
130+
*/
131+
@Override
132+
public ZoneConfiguration readZoneConfiguration(String zone, String accessToken) throws VCertException {
133+
try {
134+
return connector.readZoneConfiguration(zone, accessToken);
135+
} catch (FeignException e) {
136+
throw VCertException.fromFeignException(e);
137+
} catch (Exception e) {
138+
throw new VCertException("Unexpected exception", e);
139+
}
140+
}
141+
142+
/**
143+
* {@inheritDoc}
144+
*/
145+
@Override
146+
public CertificateRequest generateRequest(ZoneConfiguration config, CertificateRequest request, String accessToken)
147+
throws VCertException {
148+
try {
149+
return connector.generateRequest(config, request, accessToken);
150+
} catch (FeignException e) {
151+
throw VCertException.fromFeignException(e);
152+
} catch (Exception e) {
153+
throw new VCertException("Unexpected exception", e);
154+
}
155+
}
156+
157+
@Override
158+
public String requestCertificate(CertificateRequest request, String zone, String accessToken) throws VCertException {
159+
try {
160+
return connector.requestCertificate(request, zone, accessToken);
161+
} catch (FeignException e) {
162+
throw VCertException.fromFeignException(e);
163+
} catch (Exception e) {
164+
throw new VCertException("Unexpected exception", e);
165+
}
166+
}
167+
168+
/**
169+
* {@inheritDoc}
170+
*/
171+
@Override
172+
public String requestCertificate(CertificateRequest request, ZoneConfiguration zoneConfiguration, String accessToken)
173+
throws VCertException {
174+
try {
175+
return connector.requestCertificate(request, zoneConfiguration, accessToken);
176+
} catch (FeignException e) {
177+
throw VCertException.fromFeignException(e);
178+
} catch (Exception e) {
179+
throw new VCertException("Unexpected exception", e);
180+
}
181+
}
182+
183+
/**
184+
* {@inheritDoc}
185+
*/
186+
@Override
187+
public PEMCollection retrieveCertificate(CertificateRequest request, String accessToken) throws VCertException {
188+
try {
189+
return connector.retrieveCertificate(request, accessToken);
190+
} catch (FeignException e) {
191+
throw VCertException.fromFeignException(e);
192+
} catch (Exception e) {
193+
throw new VCertException("Unexpected exception", e);
194+
}
195+
}
196+
197+
/**
198+
* {@inheritDoc}
199+
*/
200+
@Override
201+
public void revokeCertificate(RevocationRequest request, String accessToken) throws VCertException {
202+
try {
203+
connector.revokeCertificate(request, accessToken);
204+
} catch (FeignException e) {
205+
throw VCertException.fromFeignException(e);
206+
} catch (Exception e) {
207+
throw new VCertException("Unexpected exception", e);
208+
}
209+
}
210+
211+
/**
212+
* {@inheritDoc}
213+
*/
214+
@Override
215+
public String renewCertificate(RenewalRequest request, String accessToken) throws VCertException {
216+
try {
217+
return connector.renewCertificate(request, accessToken);
218+
} catch (FeignException e) {
219+
throw VCertException.fromFeignException(e);
220+
} catch (Exception e) {
221+
throw new VCertException("Unexpected exception", e);
222+
}
223+
}
224+
225+
/**
226+
* {@inheritDoc}
227+
*/
228+
@Override
229+
public ImportResponse importCertificate(ImportRequest request, String accessToken) throws VCertException {
230+
try {
231+
return connector.importCertificate(request, accessToken);
232+
} catch (FeignException e) {
233+
throw VCertException.fromFeignException(e);
234+
} catch (Exception e) {
235+
throw new VCertException("Unexpected exception", e);
236+
}
237+
}
238+
239+
/**
240+
* {@inheritDoc}
241+
*/
242+
@Override
243+
public Policy readPolicyConfiguration(String zone, String accessToken) throws VCertException {
244+
try {
245+
return connector.readPolicyConfiguration(zone, accessToken);
246+
} catch (FeignException e) {
247+
throw VCertException.fromFeignException(e);
248+
} catch (Exception e) {
249+
throw new VCertException("Unexpected exception", e);
250+
}
251+
}
252+
}

0 commit comments

Comments
 (0)