Skip to content

Commit

Permalink
Create CertificatePrivateKey class instead of combining into Certific…
Browse files Browse the repository at this point in the history
…ateBundle (#171)

Fixes #118 

In this PR:
- Extract the `privateKey` attribute from `CertificateBundle` into its own class
- Return a `CertificatePrivateKey` response in the `Certificates.getCertificatePrivateKey` endpoint
  • Loading branch information
ggalmazor authored Mar 12, 2024
1 parent 6402b6a commit c21b726
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 29 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## main

- BREAKING: Remove the `privateKey` property of `CertificateBundle`. Use `Certificates.getCertificatePrivateKey()` instead. See https://github.com/dnsimple/dnsimple-java/issues/118

## 0.14.0

ENHANCEMENTS:
Expand Down
8 changes: 1 addition & 7 deletions src/main/java/com/dnsimple/data/CertificateBundle.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,16 @@
import java.util.List;

public class CertificateBundle {
private final String privateKey;
private final String server;
private final String root;
private final List<String> chain;

public CertificateBundle(String privateKey, String server, String root, List<String> chain) {
this.privateKey = privateKey;
public CertificateBundle(String server, String root, List<String> chain) {
this.server = server;
this.root = root;
this.chain = chain;
}

public String getPrivateKey() {
return privateKey;
}

public String getServerCertificate() {
return server;
}
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/dnsimple/data/CertificatePrivateKey.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.dnsimple.data;

public class CertificatePrivateKey {
private final String privateKey;

public CertificatePrivateKey(String privateKey) {
this.privateKey = privateKey;
}

public String getPrivateKey() {
return privateKey;
}
}
9 changes: 3 additions & 6 deletions src/main/java/com/dnsimple/endpoints/Certificates.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.dnsimple.endpoints;

import com.dnsimple.data.Certificate;
import com.dnsimple.data.CertificateBundle;
import com.dnsimple.data.CertificatePurchase;
import com.dnsimple.data.CertificateRenewal;
import com.dnsimple.data.*;
import com.dnsimple.http.HttpEndpointClient;
import com.dnsimple.request.CertificatePurchaseOptions;
import com.dnsimple.request.CertificateRenewalPurchaseOptions;
Expand Down Expand Up @@ -86,8 +83,8 @@ public SimpleResponse<CertificateBundle> downloadCertificate(Number account, Str
* @return The get certificate private key response
* @see <a href="https://developer.dnsimple.com/v2/certificates/#getCertificatePrivateKey">https://developer.dnsimple.com/v2/certificates/#getCertificatePrivateKey</a>
*/
public SimpleResponse<CertificateBundle> getCertificatePrivateKey(Number account, String domain, Number certificateId) {
return client.simple(GET, account + "/domains/" + domain + "/certificates/" + certificateId + "/private_key", ListOptions.empty(), null, CertificateBundle.class);
public SimpleResponse<CertificatePrivateKey> getCertificatePrivateKey(Number account, String domain, Number certificateId) {
return client.simple(GET, account + "/domains/" + domain + "/certificates/" + certificateId + "/private_key", ListOptions.empty(), null, CertificatePrivateKey.class);
}

/**
Expand Down
23 changes: 7 additions & 16 deletions src/test/java/com/dnsimple/endpoints/CertificatesTest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.dnsimple.endpoints;

import com.dnsimple.data.Certificate;
import com.dnsimple.data.CertificateBundle;
import com.dnsimple.data.CertificatePurchase;
import com.dnsimple.data.CertificateRenewal;
import com.dnsimple.data.*;
import com.dnsimple.exception.ResourceNotFoundException;
import com.dnsimple.request.CertificatePurchaseOptions;
import com.dnsimple.request.CertificateRenewalPurchaseOptions;
Expand All @@ -20,7 +17,6 @@

import static com.dnsimple.http.HttpMethod.GET;
import static com.dnsimple.http.HttpMethod.POST;
import static com.dnsimple.tools.CustomMatchers.number;
import static com.dnsimple.tools.CustomMatchers.thrownException;
import static java.time.ZoneOffset.UTC;
import static org.hamcrest.MatcherAssert.assertThat;
Expand Down Expand Up @@ -81,8 +77,7 @@ public void testGetCertificate() {
assertThat(certificate.getCreatedAt(), is(OffsetDateTime.of(2020, 6, 18, 18, 54, 17, 0, UTC)));
assertThat(certificate.getUpdatedAt(), is(OffsetDateTime.of(2020, 6, 18, 19, 10, 14, 0, UTC)));
assertThat(certificate.getExpiresAt(), is(OffsetDateTime.of(2020, 9, 16, 18, 10, 13, 0, UTC)));
assertThat(certificate.getCertificateRequest(), is("" +
"-----BEGIN CERTIFICATE REQUEST-----\n" +
assertThat(certificate.getCertificateRequest(), is("-----BEGIN CERTIFICATE REQUEST-----\n" +
"MIICmTCCAYECAQAwGjEYMBYGA1UEAwwPd3d3LmJpbmdvLnBpenphMIIBIjANBgkq\n" +
"hkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAw4+KoZ9IDCK2o5qAQpi+Icu5kksmjQzx\n" +
"5o5g4B6XhRxhsfHlK/i3iU5hc8CONjyVv8j82835RNsiKrflnxGa9SH68vbQfcn4\n" +
Expand Down Expand Up @@ -114,9 +109,7 @@ public void testDownloadCertificate() {
assertThat(server.getRecordedRequest().getMethod(), is(GET));
assertThat(server.getRecordedRequest().getPath(), is("/v2/1010/domains/weppos.net/certificates/1/download"));
CertificateBundle certificateBundle = response.getData();
assertThat(certificateBundle.getPrivateKey(), is(nullValue()));
assertThat(certificateBundle.getServerCertificate(), is("" +
"-----BEGIN CERTIFICATE-----\n" +
assertThat(certificateBundle.getServerCertificate(), is("-----BEGIN CERTIFICATE-----\n" +
"MIIE7TCCA9WgAwIBAgITAPpTe4O3vjuQ9L4gLsogi/ukujANBgkqhkiG9w0BAQsF\n" +
"ADAiMSAwHgYDVQQDDBdGYWtlIExFIEludGVybWVkaWF0ZSBYMTAeFw0xNjA2MTEx\n" +
"NzQ4MDBaFw0xNjA5MDkxNzQ4MDBaMBkxFzAVBgNVBAMTDnd3dy53ZXBwb3MubmV0\n" +
Expand Down Expand Up @@ -147,8 +140,7 @@ public void testDownloadCertificate() {
"-----END CERTIFICATE-----\n"));
assertThat(certificateBundle.getRootCertificate(), is(isEmptyOrNullString()));
assertThat(certificateBundle.getIntermediateCertificates(), hasSize(1));
assertThat(certificateBundle.getIntermediateCertificates().get(0), is("" +
"-----BEGIN CERTIFICATE-----\n" +
assertThat(certificateBundle.getIntermediateCertificates().get(0), is("-----BEGIN CERTIFICATE-----\n" +
"MIIEqzCCApOgAwIBAgIRAIvhKg5ZRO08VGQx8JdhT+UwDQYJKoZIhvcNAQELBQAw\n" +
"GjEYMBYGA1UEAwwPRmFrZSBMRSBSb290IFgxMB4XDTE2MDUyMzIyMDc1OVoXDTM2\n" +
"MDUyMzIyMDc1OVowIjEgMB4GA1UEAwwXRmFrZSBMRSBJbnRlcm1lZGlhdGUgWDEw\n" +
Expand Down Expand Up @@ -180,12 +172,11 @@ public void testDownloadCertificate() {
@Test
public void testCertificatePrivateKey() {
server.stubFixtureAt("getCertificatePrivateKey/success.http");
SimpleResponse<CertificateBundle> response = client.certificates.getCertificatePrivateKey(1010, "weppos.net", 1);
SimpleResponse<CertificatePrivateKey> response = client.certificates.getCertificatePrivateKey(1010, "weppos.net", 1);
assertThat(server.getRecordedRequest().getMethod(), is(GET));
assertThat(server.getRecordedRequest().getPath(), is("/v2/1010/domains/weppos.net/certificates/1/private_key"));
CertificateBundle certificateBundle = response.getData();
assertThat(certificateBundle.getPrivateKey(), is("" +
"-----BEGIN RSA PRIVATE KEY-----\n" +
CertificatePrivateKey certificatePrivateKey = response.getData();
assertThat(certificatePrivateKey.getPrivateKey(), is("-----BEGIN RSA PRIVATE KEY-----\n" +
"MIIEowIBAAKCAQEAtzCcMfWoQRt5AMEY0HUb2GaraL1GsWOo6YXdPfe+YDvtnmDw\n" +
"23NcoTX7VSeCgU9M3RKs19AsCJcRNTLJ2dmDrAuyCTud9YTAaXQcTOLUhtO8T8+9\n" +
"AFVIva2OmAlKCR5saBW3JaRxW7V2aHEd/d1ss1CvNOO7jNppc9NwGSnDHcn3rqNv\n" +
Expand Down

0 comments on commit c21b726

Please sign in to comment.