|
| 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