|
| 1 | +/* |
| 2 | + * MinIO Java SDK for Amazon S3 Compatible Cloud Storage, (C) 2021 MinIO, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import io.minio.MinioClient; |
| 18 | +import io.minio.StatObjectArgs; |
| 19 | +import io.minio.StatObjectResponse; |
| 20 | +import io.minio.credentials.CertificateIdentityProvider; |
| 21 | +import io.minio.credentials.Provider; |
| 22 | +import javax.net.ssl.SSLSocketFactory; |
| 23 | +import javax.net.ssl.X509TrustManager; |
| 24 | + |
| 25 | +public class MinioClientWithCertificateIdentityProvider { |
| 26 | + public static void main(String[] args) throws Exception { |
| 27 | + // STS endpoint usually point to MinIO server. |
| 28 | + String stsEndpoint = "https://STS-HOST:STS-PORT/"; |
| 29 | + |
| 30 | + // SSL socket factory. |
| 31 | + SSLSocketFactory sslSocketFactory = null; |
| 32 | + |
| 33 | + // Trust manager. |
| 34 | + X509TrustManager trustManager = null; |
| 35 | + |
| 36 | + // Below is a sample code to construct sslSocketFactory and trustManager for self-signed |
| 37 | + // certificates (server and client) used in a MinIO server setup. |
| 38 | + // |
| 39 | + // CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509"); |
| 40 | + // |
| 41 | + // Certificate serverCertificate = null; |
| 42 | + // try (FileInputStream fis = new FileInputStream("/home/bala/.minio/certs/public.crt")) { |
| 43 | + // serverCertificate = certificateFactory.generateCertificate(fis); |
| 44 | + // } |
| 45 | + // |
| 46 | + // KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); |
| 47 | + // trustStore.load(null, "secret".toCharArray()); |
| 48 | + // |
| 49 | + // trustStore.setCertificateEntry("server-certificate", serverCertificate); |
| 50 | + // |
| 51 | + // String privateKeyString = |
| 52 | + // new String( |
| 53 | + // Files.readAllBytes(Paths.get("/home/bala/.minio/certs/CAs/client1.key")), |
| 54 | + // Charset.defaultCharset()) |
| 55 | + // .replace("-----BEGIN PRIVATE KEY-----", "") |
| 56 | + // .replaceAll(System.lineSeparator(), "") |
| 57 | + // .replace("-----END PRIVATE KEY-----", ""); |
| 58 | + // |
| 59 | + // byte[] privateKey = Base64.getDecoder().decode(privateKeyString); |
| 60 | + // KeyFactory keyFactory = KeyFactory.getInstance("RSA"); |
| 61 | + // PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKey); |
| 62 | + // |
| 63 | + // Certificate certificateChain = null; |
| 64 | + // try (FileInputStream fis = new FileInputStream("/home/bala/.minio/certs/CAs/client1.crt")) { |
| 65 | + // certificateChain = certificateFactory.generateCertificate(fis); |
| 66 | + // } |
| 67 | + // |
| 68 | + // KeyStore identityStore = KeyStore.getInstance(KeyStore.getDefaultType()); |
| 69 | + // identityStore.load(null, "secret".toCharArray()); |
| 70 | + // identityStore.setKeyEntry( |
| 71 | + // "client", |
| 72 | + // keyFactory.generatePrivate(keySpec), |
| 73 | + // "secret".toCharArray(), |
| 74 | + // new Certificate[] {certificateChain}); |
| 75 | + // |
| 76 | + // TrustManagerFactory trustManagerFactory = |
| 77 | + // TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); |
| 78 | + // trustManagerFactory.init(trustStore); |
| 79 | + // TrustManager[] trustManagers = trustManagerFactory.getTrustManagers(); |
| 80 | + // |
| 81 | + // KeyManagerFactory keyManagerFactory = |
| 82 | + // KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); |
| 83 | + // keyManagerFactory.init(identityStore, "secret".toCharArray()); |
| 84 | + // KeyManager[] keyManagers = keyManagerFactory.getKeyManagers(); |
| 85 | + // |
| 86 | + // SSLContext sslContext = SSLContext.getInstance("TLS"); |
| 87 | + // sslContext.init(keyManagers, trustManagers, null); |
| 88 | + // |
| 89 | + // SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory(); |
| 90 | + // X509TrustManager trustManager = (X509TrustManager) trustManagers[0]; |
| 91 | + // |
| 92 | + |
| 93 | + Provider provider = |
| 94 | + new CertificateIdentityProvider(stsEndpoint, sslSocketFactory, trustManager, null, null); |
| 95 | + |
| 96 | + MinioClient minioClient = |
| 97 | + MinioClient.builder() |
| 98 | + .endpoint("https://MINIO-HOST:MINIO-PORT") |
| 99 | + .credentialsProvider(provider) |
| 100 | + .build(); |
| 101 | + |
| 102 | + // Get information of an object. |
| 103 | + StatObjectResponse stat = |
| 104 | + minioClient.statObject( |
| 105 | + StatObjectArgs.builder().bucket("my-bucketname").object("my-objectname").build()); |
| 106 | + System.out.println(stat); |
| 107 | + } |
| 108 | +} |
0 commit comments