2222import io .minio .messages .LifecycleRule ;
2323import io .minio .messages .RuleFilter ;
2424import io .minio .messages .Status ;
25+ import okhttp3 .OkHttpClient ;
2526import org .slf4j .Logger ;
2627import org .slf4j .LoggerFactory ;
2728import org .springframework .boot .autoconfigure .condition .ConditionalOnExpression ;
3031import org .springframework .core .env .Environment ;
3132import org .springframework .stereotype .Service ;
3233
34+ import javax .net .ssl .SSLContext ;
35+ import javax .net .ssl .TrustManagerFactory ;
36+ import javax .net .ssl .X509TrustManager ;
3337import java .io .ByteArrayInputStream ;
3438import java .io .InputStream ;
39+ import java .security .KeyStore ;
40+ import java .security .cert .CertificateFactory ;
41+ import java .security .cert .X509Certificate ;
3542import java .time .ZonedDateTime ;
3643import java .util .LinkedList ;
3744import java .util .List ;
@@ -60,24 +67,17 @@ public S3DAO(final Environment environment) {
6067 public void init () throws Exception {
6168
6269 BUCKET_NAME = this .environment .getProperty ("sps.s3.bucketName" , "sps.s3.defaultBucketName" );
70+ this .minioClient = createMinioClient ();
6371
64- this .minioClient =
65- MinioClient .builder ()
66- .endpoint (this .environment .getRequiredProperty ("sps.s3.endpointUrl" ))
67- .credentials (
68- this .environment .getRequiredProperty ("sps.s3.accessKey" ),
69- this .environment .getRequiredProperty ("sps.s3.secretKey" )
70- )
71- .build ();
72+ printAllBucketsInService ();
7273
7374 if (!isBucketExisting ()){
7475 createBucket ();
75- setBucketLifecycle ();
76+ // setBucketLifecycle();
7677// getBucketLifecycle();
7778 }
7879 }
7980
80-
8181 public Result <InputStream > getItem (final String sessionUUID , final Long pk ) {
8282 return Result .tryCatch (() ->
8383 this .minioClient .getObject (
@@ -209,4 +209,51 @@ private void getBucketLifecycle(){
209209 log .error ("" );
210210 }
211211 }
212+
213+ private MinioClient createMinioClient () throws Exception {
214+ if (this .environment .getProperty ("sps.s3.tls.cert" ) == null ){
215+ return MinioClient .builder ()
216+ .endpoint (this .environment .getRequiredProperty ("sps.s3.endpointUrl" ))
217+ .credentials (
218+ this .environment .getRequiredProperty ("sps.s3.accessKey" ),
219+ this .environment .getRequiredProperty ("sps.s3.secretKey" )
220+ )
221+ .build ();
222+ }
223+
224+ return MinioClient .builder ()
225+ .endpoint (this .environment .getRequiredProperty ("sps.s3.endpointUrl" ))
226+ .credentials (
227+ this .environment .getRequiredProperty ("sps.s3.accessKey" ),
228+ this .environment .getRequiredProperty ("sps.s3.secretKey" )
229+ )
230+ .httpClient (createOkHttpClientWithCert ())
231+ .build ();
232+ }
233+
234+ private OkHttpClient createOkHttpClientWithCert () throws Exception {
235+ String pemCert = this .environment .getRequiredProperty ("sps.s3.tls.cert" );
236+
237+ // Convert PEM string to X509Certificate
238+ CertificateFactory cf = CertificateFactory .getInstance ("X.509" );
239+ X509Certificate cert = (X509Certificate ) cf .generateCertificate (new ByteArrayInputStream (pemCert .getBytes ()));
240+
241+ // Create a KeyStore and load the certificate
242+ KeyStore keyStore = KeyStore .getInstance (KeyStore .getDefaultType ());
243+ keyStore .load (null , null );
244+ keyStore .setCertificateEntry ("custom-cert" , cert );
245+
246+ // Initialize TrustManager with the KeyStore
247+ TrustManagerFactory tmf = TrustManagerFactory .getInstance (TrustManagerFactory .getDefaultAlgorithm ());
248+ tmf .init (keyStore );
249+
250+ // Set up SSLContext using the TrustManager
251+ SSLContext sslContext = SSLContext .getInstance ("TLS" );
252+ sslContext .init (null , tmf .getTrustManagers (), new java .security .SecureRandom ());
253+
254+ // Return the OkHttpClient with SSLContext configured
255+ return new OkHttpClient .Builder ()
256+ .sslSocketFactory (sslContext .getSocketFactory (), (X509TrustManager ) tmf .getTrustManagers ()[0 ])
257+ .build ();
258+ }
212259}
0 commit comments