|
26 | 26 | */ |
27 | 27 | public class VSecMHttpClient { |
28 | 28 | private static final Logger LOGGER = Logger.getLogger(VSecMHttpClient.class.getName()); |
| 29 | + |
| 30 | + /** |
| 31 | + * The path to the SPIFFE socket used to communicate with the SPIFFE Workload API. |
| 32 | + * This socket provides access to the X.509 SVID (SPIFFE Verifiable Identity Document) |
| 33 | + * used for mutual TLS authentication. |
| 34 | + */ |
29 | 35 | private static final String SPIFFE_SOCKET_PATH = "unix:///spire-agent-socket/spire-agent.sock"; |
30 | 36 |
|
31 | 37 | /** |
32 | | - * Creates an instance of {@link HttpClient} with SPIFFE-based SSL context. |
33 | | - * This client can be used for secure HTTP communication. |
| 38 | + * Creates and configures an {@link HttpClient} instance with SPIFFE-based mutual TLS. |
| 39 | + * This client is used for secure communication with SPIFFE-enabled services. |
| 40 | + * |
| 41 | + * @return An instance of {@link HttpClient} configured with mutual TLS using SPIFFE credentials. |
34 | 42 | * |
35 | | - * @return A configured {@link HttpClient} instance ready for secure communication. |
36 | | - * @throws RuntimeException if there's an issue configuring the SSL context, |
37 | | - * encapsulating any underlying exceptions. |
| 43 | + * @throws VSecMHttpClientException.SocketPathError If the SPIFFE socket path is inaccessible. |
| 44 | + * @throws VSecMHttpClientException.X509FetchError If fetching X.509 SVIDs fails. |
| 45 | + * @throws VSecMHttpClientException.SSLContextError If SSLContext configuration fails. |
| 46 | + * @throws RuntimeException If an unknown error occurs during client initialization. |
| 47 | + * |
| 48 | + * @see #configureSSLContext() |
38 | 49 | */ |
39 | 50 | public HttpClient client() { |
40 | 51 | try { |
41 | 52 | SSLContext sslContext = configureSSLContext(); |
42 | 53 | return HttpClient.newBuilder().sslContext(sslContext).build(); |
43 | 54 | } catch (Exception e) { |
44 | 55 | LOGGER.log(Level.SEVERE, "Failed to fetch secrets", e); |
45 | | - throw new RuntimeException(e); |
| 56 | + |
| 57 | + if (e instanceof SocketEndpointAddressException) { |
| 58 | + throw VSecMHttpClientException.socketPathError("SPIFFE socket path is inaccessible: " + e.getMessage()); |
| 59 | + } else if (e instanceof X509SourceException) { |
| 60 | + throw VSecMHttpClientException.x509FetchError("Failed to fetch X.509 SVIDs: " + e.getMessage()); |
| 61 | + } else if (e instanceof NoSuchAlgorithmException || e instanceof KeyManagementException) { |
| 62 | + throw VSecMHttpClientException.sslContextError("SSLContext configuration failed: " + e.getMessage()); |
| 63 | + } else { |
| 64 | + throw new RuntimeException("Unknown error occurred: " + e.getMessage(), e); |
| 65 | + } |
46 | 66 | } |
47 | 67 | } |
48 | 68 |
|
49 | 69 | /** |
50 | | - * Configures and returns an {@link SSLContext} suitable for SPIFFE based secure communication. |
| 70 | + * Configures and returns an {@link SSLContext} instance using SPIFFE credentials. |
| 71 | + * This method creates a secure SSL context that is configured with X.509 SVIDs |
| 72 | + * obtained from the SPIFFE Workload API to enable mutual TLS for communication. |
51 | 73 | * |
52 | | - * @return An {@link SSLContext} configured with SPIFFE X.509 SVIDs for mutual TLS. |
53 | | - * @throws SocketEndpointAddressException If the SPIFFE Workload API socket endpoint address is incorrect. |
54 | | - * @throws X509SourceException If there's an issue fetching or processing the X.509 SVIDs. |
55 | | - * @throws NoSuchAlgorithmException If the SSL context cannot be instantiated due to a missing algorithm. |
56 | | - * @throws KeyManagementException If there's an issue initializing the {@link SSLContext} with SPIFFE SVIDs. |
| 74 | + * @return A configured {@link SSLContext} that supports mutual TLS using SPIFFE identities. |
| 75 | + * @throws SocketEndpointAddressException If there is an issue with the SPIFFE socket path, |
| 76 | + * typically indicating that the SPIFFE Workload API is inaccessible. |
| 77 | + * @throws X509SourceException If there is an error fetching or processing the X.509 SVIDs from the SPIFFE Workload API. |
| 78 | + * @throws NoSuchAlgorithmException If the SSLContext cannot be instantiated due to a missing or unsupported algorithm. |
| 79 | + * @throws KeyManagementException If there is an issue initializing the SSLContext, typically indicating a problem |
| 80 | + * with key management or protocol setup. |
| 81 | + * @see DefaultX509Source |
| 82 | + * @see SpiffeSslContextFactory |
57 | 83 | */ |
| 84 | + |
58 | 85 | private SSLContext configureSSLContext() throws SocketEndpointAddressException, X509SourceException, NoSuchAlgorithmException, KeyManagementException { |
59 | 86 | DefaultX509Source.X509SourceOptions sourceOptions = DefaultX509Source.X509SourceOptions |
60 | 87 | .builder() |
|
0 commit comments