use local variable type inference where reasonable
Local variables with obvious type on both sides of their declaration use type inference now for more concise code. Some variable names are given a more precise name though.
This commit is contained in:
parent
587c6cde0a
commit
ce28b8eb60
@ -22,7 +22,6 @@ import de.stklcode.jvault.connector.exception.TlsException;
|
|||||||
import de.stklcode.jvault.connector.exception.VaultConnectorException;
|
import de.stklcode.jvault.connector.exception.VaultConnectorException;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
|
||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
@ -213,7 +212,7 @@ public final class HTTPVaultConnectorBuilder implements VaultConnectorBuilder {
|
|||||||
/* Parse URL from environment variable */
|
/* Parse URL from environment variable */
|
||||||
if (System.getenv(ENV_VAULT_ADDR) != null && !System.getenv(ENV_VAULT_ADDR).trim().isEmpty()) {
|
if (System.getenv(ENV_VAULT_ADDR) != null && !System.getenv(ENV_VAULT_ADDR).trim().isEmpty()) {
|
||||||
try {
|
try {
|
||||||
URL url = new URL(System.getenv(ENV_VAULT_ADDR));
|
var url = new URL(System.getenv(ENV_VAULT_ADDR));
|
||||||
this.host = url.getHost();
|
this.host = url.getHost();
|
||||||
this.port = url.getPort();
|
this.port = url.getPort();
|
||||||
this.tls = url.getProtocol().equals("https");
|
this.tls = url.getProtocol().equals("https");
|
||||||
@ -289,7 +288,7 @@ public final class HTTPVaultConnectorBuilder implements VaultConnectorBuilder {
|
|||||||
* @since 0.4.0
|
* @since 0.4.0
|
||||||
*/
|
*/
|
||||||
private X509Certificate certificateFromFile(final Path certFile) throws TlsException {
|
private X509Certificate certificateFromFile(final Path certFile) throws TlsException {
|
||||||
try (InputStream is = Files.newInputStream(certFile)) {
|
try (var is = Files.newInputStream(certFile)) {
|
||||||
return (X509Certificate) CertificateFactory.getInstance("X.509").generateCertificate(is);
|
return (X509Certificate) CertificateFactory.getInstance("X.509").generateCertificate(is);
|
||||||
} catch (IOException | CertificateException e) {
|
} catch (IOException | CertificateException e) {
|
||||||
throw new TlsException("Unable to read certificate.", e);
|
throw new TlsException("Unable to read certificate.", e);
|
||||||
|
@ -77,7 +77,7 @@ public final class RequestHelper implements Serializable {
|
|||||||
*/
|
*/
|
||||||
public String post(final String path, final Object payload, final String token) throws VaultConnectorException {
|
public String post(final String path, final Object payload, final String token) throws VaultConnectorException {
|
||||||
// Initialize POST.
|
// Initialize POST.
|
||||||
HttpRequest.Builder req = HttpRequest.newBuilder(URI.create(baseURL + path));
|
var req = HttpRequest.newBuilder(URI.create(baseURL + path));
|
||||||
|
|
||||||
// Generate JSON from payload.
|
// Generate JSON from payload.
|
||||||
try {
|
try {
|
||||||
@ -145,7 +145,7 @@ public final class RequestHelper implements Serializable {
|
|||||||
*/
|
*/
|
||||||
public String put(final String path, final Map<String, String> payload, final String token) throws VaultConnectorException {
|
public String put(final String path, final Map<String, String> payload, final String token) throws VaultConnectorException {
|
||||||
// Initialize PUT.
|
// Initialize PUT.
|
||||||
HttpRequest.Builder req = HttpRequest.newBuilder(URI.create(baseURL + path));
|
var req = HttpRequest.newBuilder(URI.create(baseURL + path));
|
||||||
|
|
||||||
// Generate JSON from payload.
|
// Generate JSON from payload.
|
||||||
try {
|
try {
|
||||||
@ -250,7 +250,7 @@ public final class RequestHelper implements Serializable {
|
|||||||
public String get(final String path, final Map<String, String> payload, final String token)
|
public String get(final String path, final Map<String, String> payload, final String token)
|
||||||
throws VaultConnectorException {
|
throws VaultConnectorException {
|
||||||
// Add parameters to URI.
|
// Add parameters to URI.
|
||||||
StringBuilder uriBuilder = new StringBuilder(baseURL + path);
|
var uriBuilder = new StringBuilder(baseURL + path);
|
||||||
|
|
||||||
if (!payload.isEmpty()) {
|
if (!payload.isEmpty()) {
|
||||||
uriBuilder.append("?").append(
|
uriBuilder.append("?").append(
|
||||||
@ -262,7 +262,7 @@ public final class RequestHelper implements Serializable {
|
|||||||
|
|
||||||
// Initialize GET.
|
// Initialize GET.
|
||||||
try {
|
try {
|
||||||
HttpRequest.Builder req = HttpRequest.newBuilder(new URI(uriBuilder.toString()));
|
var req = HttpRequest.newBuilder(new URI(uriBuilder.toString()));
|
||||||
|
|
||||||
// Set X-Vault-Token header.
|
// Set X-Vault-Token header.
|
||||||
if (token != null) {
|
if (token != null) {
|
||||||
@ -310,7 +310,7 @@ public final class RequestHelper implements Serializable {
|
|||||||
// Set JSON Header.
|
// Set JSON Header.
|
||||||
requestBuilder.setHeader("accept", "application/json");
|
requestBuilder.setHeader("accept", "application/json");
|
||||||
|
|
||||||
HttpClient.Builder clientBuilder = HttpClient.newBuilder();
|
var clientBuilder = HttpClient.newBuilder();
|
||||||
|
|
||||||
// Set custom timeout, if defined.
|
// Set custom timeout, if defined.
|
||||||
if (this.timeout != null) {
|
if (this.timeout != null) {
|
||||||
@ -369,23 +369,23 @@ public final class RequestHelper implements Serializable {
|
|||||||
private SSLContext createSSLContext() throws TlsException {
|
private SSLContext createSSLContext() throws TlsException {
|
||||||
try {
|
try {
|
||||||
// Create context.
|
// Create context.
|
||||||
SSLContext context = SSLContext.getInstance(tlsVersion);
|
var sslContext = SSLContext.getInstance(tlsVersion);
|
||||||
|
|
||||||
if (trustedCaCert != null) {
|
if (trustedCaCert != null) {
|
||||||
// Create Keystore with trusted certificate.
|
// Create Keystore with trusted certificate.
|
||||||
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
|
var keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
|
||||||
keyStore.load(null, null);
|
keyStore.load(null, null);
|
||||||
keyStore.setCertificateEntry("trustedCert", trustedCaCert);
|
keyStore.setCertificateEntry("trustedCert", trustedCaCert);
|
||||||
|
|
||||||
// Initialize TrustManager.
|
// Initialize TrustManager.
|
||||||
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
|
var tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
|
||||||
tmf.init(keyStore);
|
tmf.init(keyStore);
|
||||||
context.init(null, tmf.getTrustManagers(), null);
|
sslContext.init(null, tmf.getTrustManagers(), null);
|
||||||
} else {
|
} else {
|
||||||
context.init(null, null, null);
|
sslContext.init(null, null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
return context;
|
return sslContext;
|
||||||
} catch (CertificateException | NoSuchAlgorithmException | KeyStoreException | IOException | KeyManagementException e) {
|
} catch (CertificateException | NoSuchAlgorithmException | KeyStoreException | IOException | KeyManagementException e) {
|
||||||
throw new TlsException(Error.INIT_SSL_CONTEXT, e);
|
throw new TlsException(Error.INIT_SSL_CONTEXT, e);
|
||||||
}
|
}
|
||||||
@ -399,8 +399,8 @@ public final class RequestHelper implements Serializable {
|
|||||||
* @throws InvalidResponseException on reading errors
|
* @throws InvalidResponseException on reading errors
|
||||||
*/
|
*/
|
||||||
private String handleResult(final HttpResponse<InputStream> response) throws InvalidResponseException {
|
private String handleResult(final HttpResponse<InputStream> response) throws InvalidResponseException {
|
||||||
try (BufferedReader br = new BufferedReader(new InputStreamReader(response.body()))) {
|
try (var reader = new BufferedReader(new InputStreamReader(response.body()))) {
|
||||||
return br.lines().collect(Collectors.joining("\n"));
|
return reader.lines().collect(Collectors.joining("\n"));
|
||||||
} catch (IOException ignored) {
|
} catch (IOException ignored) {
|
||||||
throw new InvalidResponseException(Error.READ_RESPONSE, 200);
|
throw new InvalidResponseException(Error.READ_RESPONSE, 200);
|
||||||
}
|
}
|
||||||
@ -414,8 +414,8 @@ public final class RequestHelper implements Serializable {
|
|||||||
*/
|
*/
|
||||||
private void handleError(final HttpResponse<InputStream> response) throws VaultConnectorException {
|
private void handleError(final HttpResponse<InputStream> response) throws VaultConnectorException {
|
||||||
if (response.body() != null) {
|
if (response.body() != null) {
|
||||||
try (BufferedReader br = new BufferedReader(new InputStreamReader(response.body()))) {
|
try (var reader = new BufferedReader(new InputStreamReader(response.body()))) {
|
||||||
String responseString = br.lines().collect(Collectors.joining("\n"));
|
var responseString = reader.lines().collect(Collectors.joining("\n"));
|
||||||
ErrorResponse er = jsonMapper.readValue(responseString, ErrorResponse.class);
|
ErrorResponse er = jsonMapper.readValue(responseString, ErrorResponse.class);
|
||||||
/* Check for "permission denied" response */
|
/* Check for "permission denied" response */
|
||||||
if (!er.getErrors().isEmpty() && er.getErrors().get(0).equals("permission denied")) {
|
if (!er.getErrors().isEmpty() && er.getErrors().get(0).equals("permission denied")) {
|
||||||
|
@ -37,7 +37,7 @@ public final class AppRoleResponse extends VaultDataResponse {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setData(final Map<String, Object> data) throws InvalidResponseException {
|
public void setData(final Map<String, Object> data) throws InvalidResponseException {
|
||||||
ObjectMapper mapper = new ObjectMapper();
|
var mapper = new ObjectMapper();
|
||||||
try {
|
try {
|
||||||
/* null empty strings on list objects */
|
/* null empty strings on list objects */
|
||||||
Map<String, Object> filteredData = new HashMap<>(data.size(), 1);
|
Map<String, Object> filteredData = new HashMap<>(data.size(), 1);
|
||||||
|
@ -37,7 +37,7 @@ public final class AppRoleSecretResponse extends VaultDataResponse {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setData(final Map<String, Object> data) throws InvalidResponseException {
|
public void setData(final Map<String, Object> data) throws InvalidResponseException {
|
||||||
ObjectMapper mapper = new ObjectMapper();
|
var mapper = new ObjectMapper();
|
||||||
try {
|
try {
|
||||||
/* null empty strings on list objects */
|
/* null empty strings on list objects */
|
||||||
Map<String, Object> filteredData = new HashMap<>(data.size(), 1);
|
Map<String, Object> filteredData = new HashMap<>(data.size(), 1);
|
||||||
|
@ -44,7 +44,7 @@ public final class AuthMethodsResponse extends VaultDataResponse {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setData(final Map<String, Object> data) throws InvalidResponseException {
|
public void setData(final Map<String, Object> data) throws InvalidResponseException {
|
||||||
ObjectMapper mapper = new ObjectMapper();
|
var mapper = new ObjectMapper();
|
||||||
for (Map.Entry<String, Object> entry : data.entrySet()) {
|
for (Map.Entry<String, Object> entry : data.entrySet()) {
|
||||||
try {
|
try {
|
||||||
this.supportedMethods.put(entry.getKey(),
|
this.supportedMethods.put(entry.getKey(),
|
||||||
|
@ -45,7 +45,7 @@ public final class AuthResponse extends VaultDataResponse {
|
|||||||
*/
|
*/
|
||||||
@JsonProperty("auth")
|
@JsonProperty("auth")
|
||||||
public void setAuth(final Map<String, Object> auth) throws InvalidResponseException {
|
public void setAuth(final Map<String, Object> auth) throws InvalidResponseException {
|
||||||
ObjectMapper mapper = new ObjectMapper();
|
var mapper = new ObjectMapper();
|
||||||
try {
|
try {
|
||||||
this.auth = mapper.readValue(mapper.writeValueAsString(auth), AuthData.class);
|
this.auth = mapper.readValue(mapper.writeValueAsString(auth), AuthData.class);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
@ -37,7 +37,7 @@ public class MetadataResponse extends VaultDataResponse {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final void setData(final Map<String, Object> data) throws InvalidResponseException {
|
public final void setData(final Map<String, Object> data) throws InvalidResponseException {
|
||||||
ObjectMapper mapper = new ObjectMapper();
|
var mapper = new ObjectMapper();
|
||||||
try {
|
try {
|
||||||
this.metadata = mapper.readValue(mapper.writeValueAsString(data), SecretMetadata.class);
|
this.metadata = mapper.readValue(mapper.writeValueAsString(data), SecretMetadata.class);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
@ -44,7 +44,7 @@ public class SecretResponse extends VaultDataResponse {
|
|||||||
if (data.size() == 2
|
if (data.size() == 2
|
||||||
&& data.containsKey(KEY_DATA) && data.get(KEY_DATA) instanceof Map
|
&& data.containsKey(KEY_DATA) && data.get(KEY_DATA) instanceof Map
|
||||||
&& data.containsKey(KEY_METADATA) && data.get(KEY_METADATA) instanceof Map) {
|
&& data.containsKey(KEY_METADATA) && data.get(KEY_METADATA) instanceof Map) {
|
||||||
ObjectMapper mapper = new ObjectMapper();
|
var mapper = new ObjectMapper();
|
||||||
try {
|
try {
|
||||||
// This is apparently a KV v2 value.
|
// This is apparently a KV v2 value.
|
||||||
this.data = (Map<String, Object>) data.get(KEY_DATA);
|
this.data = (Map<String, Object>) data.get(KEY_DATA);
|
||||||
|
@ -37,7 +37,7 @@ public class SecretVersionResponse extends VaultDataResponse {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final void setData(final Map<String, Object> data) throws InvalidResponseException {
|
public final void setData(final Map<String, Object> data) throws InvalidResponseException {
|
||||||
ObjectMapper mapper = new ObjectMapper();
|
var mapper = new ObjectMapper();
|
||||||
try {
|
try {
|
||||||
this.metadata = mapper.readValue(mapper.writeValueAsString(data), VersionMetadata.class);
|
this.metadata = mapper.readValue(mapper.writeValueAsString(data), VersionMetadata.class);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
@ -46,7 +46,7 @@ public final class TokenResponse extends VaultDataResponse {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void setData(final Map<String, Object> data) throws InvalidResponseException {
|
public void setData(final Map<String, Object> data) throws InvalidResponseException {
|
||||||
ObjectMapper mapper = new ObjectMapper();
|
var mapper = new ObjectMapper();
|
||||||
try {
|
try {
|
||||||
this.data = mapper.readValue(mapper.writeValueAsString(data), TokenData.class);
|
this.data = mapper.readValue(mapper.writeValueAsString(data), TokenData.class);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
@ -43,7 +43,7 @@ public final class TokenRoleResponse extends VaultDataResponse {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void setData(final Map<String, Object> data) throws InvalidResponseException {
|
public void setData(final Map<String, Object> data) throws InvalidResponseException {
|
||||||
ObjectMapper mapper = new ObjectMapper();
|
var mapper = new ObjectMapper();
|
||||||
try {
|
try {
|
||||||
this.data = mapper.readValue(mapper.writeValueAsString(data), TokenRole.class);
|
this.data = mapper.readValue(mapper.writeValueAsString(data), TokenRole.class);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user