Skip to content

Commit 269a8e1

Browse files
committed
Polishing.
Reorder methods. Reformat code. Add since tags. Convert revocation timestamp into instant. See: gh-477 Original pull request: gh-820
1 parent 8d7292c commit 269a8e1

File tree

6 files changed

+216
-152
lines changed

6 files changed

+216
-152
lines changed

spring-vault-core/src/main/java/org/springframework/vault/core/VaultPkiOperations.java

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2022 the original author or authors.
2+
* Copyright 2016-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -36,6 +36,7 @@
3636
* provide the verification functionality.
3737
*
3838
* @author Mark Paluch
39+
* @author Nanne Baars
3940
* @see <a href=
4041
* "https://www.vaultproject.io/docs/secrets/pki/index.html">https://www.vaultproject.io/docs/secrets/pki/index.html</a>
4142
*/
@@ -66,10 +67,10 @@ VaultCertificateResponse issueCertificate(String roleName, VaultCertificateReque
6667
* @param certificateRequest must not be {@literal null}.
6768
* @return the {@link VaultCertificateResponse} containing a
6869
* {@link org.springframework.vault.support.Certificate} .
69-
* @since 2.0
7070
* @see <a href=
7171
* "https://www.vaultproject.io/docs/secrets/pki/index.html#pki-issue">POST
7272
* /pki/sign/[role name]</a>
73+
* @since 2.0
7374
*/
7475
VaultSignCertificateRequestResponse signCertificateRequest(String roleName, String csr,
7576
VaultCertificateRequest certificateRequest) throws VaultException;
@@ -79,10 +80,10 @@ VaultSignCertificateRequestResponse signCertificateRequest(String roleName, Stri
7980
* standard method of revoking using Vault lease IDs. A successful revocation will
8081
* rotate the CRL
8182
* @param serialNumber must not be empty or {@literal null}.
82-
* @since 2.0
8383
* @see <a href=
8484
* "https://www.vaultproject.io/docs/secrets/pki/index.html#revoke-certificate">POST
8585
* /pki/revoke</a>
86+
* @since 2.0
8687
*/
8788
void revoke(String serialNumber) throws VaultException;
8889

@@ -96,43 +97,56 @@ VaultSignCertificateRequestResponse signCertificateRequest(String roleName, Stri
9697
* is {@literal null}.
9798
* @return {@link java.io.InputStream} containing the encoded CRL or {@literal null}
9899
* if Vault responds with 204 No Content.
99-
* @since 2.0
100100
* @see <a href="https://www.vaultproject.io/api/secret/pki/index.html#read-crl">GET
101101
* /pki/crl</a>
102+
* @since 2.0
102103
*/
103104
@Nullable
104105
InputStream getCrl(Encoding encoding) throws VaultException;
105106

106-
enum Encoding {
107-
108-
DER, PEM,
109-
110-
}
111-
112107
/**
113-
* Retrieves the specified issuer's certificate. Includes the full ca_chain of the
114-
* issuer.
108+
* Retrieves the specified issuer's certificate. Includes the full {@code ca_chain} of
109+
* the issuer.
115110
* @param issuer reference to an existing issuer, either by Vault-generated
116-
* identifier, or the name assigned to an issuer. Pass the literal string 'default' to
117-
* refer to the currently configured issuer.
111+
* identifier, or the name assigned to an issuer. Pass the literal string
112+
* {@code default} to refer to the currently configured issuer.
118113
* @return the {@link VaultIssuerCertificateRequestResponse} containing a
119114
* {@link org.springframework.vault.support.Certificate}
120115
* @see <a href=
121116
* "https://www.vaultproject.io/api/secret/pki/#read-issuer-certificate">GET *
122117
* /pki/issuer/:issuer_ref/json</a>
123-
*
118+
* @since 3.1
124119
*/
125120
VaultIssuerCertificateRequestResponse getIssuerCertificate(String issuer) throws VaultException;
126121

127122
/**
128-
* Retrieves the specified issuer's certificate. Includes the full ca_chain of the
129-
* issuer.
130-
* @return {@link java.io.InputStream} containing the encoded certificate or
131-
* {@literal null}
123+
* Retrieves the specified issuer's certificate. Includes the full {@code ca_chain} of
124+
* the issuer.
125+
* @param issuer reference to an existing issuer, either by Vault-generated
126+
* identifier, or the name assigned to an issuer. Pass the literal string
127+
* {@code default} to refer to the currently configured issuer.
128+
* @param encoding encoding to use.
129+
* @return {@link java.io.InputStream} containing the encoded certificate.
132130
* @see <a href=
133131
* "https://www.vaultproject.io/api/secret/pki/#read-issuer-certificate">GET
134132
* /pki/issuer/:issuer_ref/{der, pem}</a>
133+
* @since 3.1
135134
*/
136135
InputStream getIssuerCertificate(String issuer, Encoding encoding) throws VaultException;
137136

137+
enum Encoding {
138+
139+
/**
140+
* DER (Distinguished Encoding Rules) format in its binary representation, see
141+
* X.690.
142+
*/
143+
DER,
144+
145+
/**
146+
* Privacy-Enhanced Mail (PEM) format in base64.
147+
*/
148+
PEM;
149+
150+
}
151+
138152
}

spring-vault-core/src/main/java/org/springframework/vault/core/VaultPkiTemplate.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2022 the original author or authors.
2+
* Copyright 2016-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,9 +17,11 @@
1717

1818
import java.io.ByteArrayInputStream;
1919
import java.io.InputStream;
20+
import java.time.Instant;
2021
import java.time.temporal.ChronoUnit;
2122
import java.util.Collections;
2223
import java.util.HashMap;
24+
import java.util.Locale;
2325
import java.util.Map;
2426
import org.springframework.http.ResponseEntity;
2527
import org.springframework.util.Assert;
@@ -165,12 +167,14 @@ public VaultIssuerCertificateRequestResponse getIssuerCertificate(String issuer)
165167

166168
@Override
167169
public InputStream getIssuerCertificate(String issuer, Encoding encoding) throws VaultException {
170+
168171
Assert.hasText(issuer, "Issuer must not be empty");
169172
Assert.notNull(encoding, "Encoding must not be null");
170173

171174
return this.vaultOperations.doWithSession(restOperations -> {
172175

173-
String requestPath = encoding == Encoding.DER ? "{path}/issuer/{issuer}/der" : "{path}/issuer/{issuer}/pem";
176+
String requestPath = String.format("{path}/issuer/{issuer}/%s", encoding.name().toLowerCase(Locale.ROOT));
177+
174178
try {
175179
ResponseEntity<byte[]> response = restOperations.getForEntity(requestPath, byte[].class, this.path,
176180
issuer);
@@ -224,7 +228,7 @@ private static Map<String, Object> createIssueRequest(VaultCertificateRequest ce
224228
.to("exclude_cn_from_sans", request);
225229
mapper.from(certificateRequest::getFormat).whenHasText().to("format", request);
226230
mapper.from(certificateRequest::getPrivateKeyFormat).whenHasText().to("private_key_format", request);
227-
mapper.from(certificateRequest::getNotAfter).whenHasText().as(i -> i.toString()).to("not_after", request);
231+
mapper.from(certificateRequest::getNotAfter).whenHasText().as(Instant::toString).to("not_after", request);
228232
mapper.from(certificateRequest::getUserIds).whenHasText().to("user_ids", request);
229233

230234
return request;

spring-vault-core/src/main/java/org/springframework/vault/support/Certificate.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2022 the original author or authors.
2+
* Copyright 2017-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@
2020
import java.security.KeyStore;
2121
import java.security.cert.CertificateException;
2222
import java.security.cert.X509Certificate;
23+
import java.time.Instant;
2324
import java.util.ArrayList;
2425
import java.util.List;
2526

@@ -36,9 +37,9 @@
3637
* encoded. Certificates can be obtained as {@link X509Certificate}.
3738
*
3839
* @author Mark Paluch
39-
* @since 2.0
4040
* @see #getX509Certificate()
4141
* @see #getIssuingCaCertificate()
42+
* @since 2.0
4243
*/
4344
public class Certificate {
4445

@@ -50,17 +51,18 @@ public class Certificate {
5051

5152
private final List<String> caChain;
5253

53-
private final Long revocationTime;
54+
@Nullable
55+
private final Instant revocationTime;
5456

5557
Certificate(@JsonProperty("serial_number") String serialNumber, @JsonProperty("certificate") String certificate,
5658
@JsonProperty("issuing_ca") String issuingCaCertificate, @JsonProperty("ca_chain") List<String> caChain,
57-
@JsonProperty("revocation_time") Long revocationTime) {
59+
@Nullable @JsonProperty("revocation_time") Long revocationTime) {
5860

5961
this.serialNumber = serialNumber;
6062
this.certificate = certificate;
6163
this.issuingCaCertificate = issuingCaCertificate;
6264
this.caChain = caChain;
63-
this.revocationTime = revocationTime;
65+
this.revocationTime = revocationTime != null ? Instant.ofEpochMilli(revocationTime * 1000) : null;
6466
}
6567

6668
/**
@@ -87,7 +89,8 @@ public static Certificate of(String serialNumber, String certificate, String iss
8789
* @param certificate must not be empty or {@literal null}.
8890
* @param issuingCaCertificate must not be empty or {@literal null}.
8991
* @param caChain empty list allowed
90-
* @return the {@link Certificate}
92+
* @return the {@link Certificate}.
93+
* @since 3.1
9194
*/
9295
public static Certificate of(String serialNumber, String certificate, String issuingCaCertificate,
9396
List<String> caChain) {
@@ -107,8 +110,9 @@ public static Certificate of(String serialNumber, String certificate, String iss
107110
* @param certificate must not be empty or {@literal null}.
108111
* @param issuingCaCertificate must not be empty or {@literal null}.
109112
* @param caChain empty list allowed
110-
* @param revocationTime revocation time, must not be {@literal null}
111-
* @return the {@link Certificate}
113+
* @param revocationTime revocation time, must not be {@literal null}.
114+
* @return the {@link Certificate}.
115+
* @since 3.1
112116
*/
113117
public static Certificate of(String serialNumber, String certificate, String issuingCaCertificate,
114118
List<String> caChain, Long revocationTime) {
@@ -250,8 +254,13 @@ public List<X509Certificate> getX509IssuerCertificates() {
250254
return certificates;
251255
}
252256

253-
public @Nullable Long getRevocationTime() {
257+
@Nullable
258+
public Instant getRevocationTime() {
254259
return this.revocationTime;
255260
}
256261

262+
public boolean isRevoked() {
263+
return this.revocationTime != null;
264+
}
265+
257266
}

0 commit comments

Comments
 (0)