1515import io .fabric8 .kubernetes .api .model .rbac .ClusterRoleBinding ;
1616import io .fabric8 .kubernetes .client .CustomResource ;
1717import io .fabric8 .kubernetes .client .KubernetesClient ;
18- import io .strimzi .api .kafka .model .common .CertSecretSource ;
19- import io .strimzi .api .kafka .model .common .ClientTls ;
2018import io .strimzi .api .kafka .model .common .Condition ;
2119import io .strimzi .api .kafka .model .common .ConnectorState ;
22- import io .strimzi .api .kafka .model .common .authentication .KafkaClientAuthentication ;
2320import io .strimzi .api .kafka .model .common .authentication .KafkaClientAuthenticationOAuth ;
2421import io .strimzi .api .kafka .model .connect .AbstractKafkaConnectSpec ;
2522import io .strimzi .api .kafka .model .connect .KafkaConnectResources ;
7875import java .io .IOException ;
7976import java .util .ArrayList ;
8077import java .util .HashMap ;
81- import java .util .HashSet ;
8278import java .util .List ;
8379import java .util .Map ;
8480import java .util .Optional ;
85- import java .util .Set ;
8681import java .util .TreeMap ;
87- import java .util .concurrent .ConcurrentHashMap ;
8882import java .util .function .BiFunction ;
8983import java .util .function .Function ;
9084import java .util .stream .Collectors ;
@@ -284,38 +278,23 @@ protected Future<ReconcileResult<NetworkPolicy>> connectNetworkPolicy(Reconcilia
284278 * @return Future which completes when the reconciliation is done
285279 */
286280 protected Future <Void > tlsTrustedCertsSecret (Reconciliation reconciliation , String namespace , KafkaConnectCluster connect ) {
287- ClientTls tls = connect .getTls ();
288- Set <String > secretsToCopy = new HashSet <>();
289-
290- if (tls != null && tls .getTrustedCertificates () != null ) {
291- secretsToCopy .addAll (tls .getTrustedCertificates ().stream ().map (CertSecretSource ::getSecretName ).toList ());
292- }
293-
294- if (secretsToCopy .isEmpty ()) {
281+ if (connect .getTls () != null ) {
282+ return ReconcilerUtils .trustedCertificates (reconciliation , secretOperations , connect .getTls ().getTrustedCertificates ())
283+ .compose (certificates -> {
284+ if (certificates != null ) {
285+ return secretOperations .reconcile (
286+ reconciliation ,
287+ namespace ,
288+ KafkaConnectResources .internalTlsTrustedCertsSecretName (connect .getCluster ()),
289+ connect .generateTlsTrustedCertsSecret (Map .of ("ca.crt" , Util .encodeToBase64 (certificates )), KafkaConnectResources .internalTlsTrustedCertsSecretName (connect .getCluster ())))
290+ .mapEmpty ();
291+ } else {
292+ return Future .succeededFuture ();
293+ }
294+ });
295+ } else {
295296 return Future .succeededFuture ();
296297 }
297-
298- ConcurrentHashMap <String , String > secretData = new ConcurrentHashMap <>();
299- return Future .join (secretsToCopy .stream ()
300- .map (secretName -> secretOperations .getAsync (namespace , secretName )
301- .compose (secret -> {
302- if (secret == null ) {
303- return Future .failedFuture ("Secret " + secretName + " not found" );
304- } else {
305- secret .getData ().entrySet ().stream ()
306- .filter (e -> e .getKey ().contains (".crt" ))
307- // In case secrets contain the same key, append the secret name into the key
308- .forEach (e -> secretData .put (secretName + "-" + e .getKey (), e .getValue ()));
309- }
310- return Future .succeededFuture ();
311- }))
312- .collect (Collectors .toList ()))
313- .compose (ignore -> secretOperations .reconcile (
314- reconciliation ,
315- namespace ,
316- KafkaConnectResources .internalTlsTrustedCertsSecretName (connect .getCluster ()),
317- connect .generateTlsTrustedCertsSecret (secretData , KafkaConnectResources .internalTlsTrustedCertsSecretName (connect .getCluster ())))
318- .mapEmpty ());
319298 }
320299
321300 /**
@@ -327,52 +306,22 @@ protected Future<Void> tlsTrustedCertsSecret(Reconciliation reconciliation, Stri
327306 */
328307 @ SuppressWarnings ("deprecation" ) // OAuth authentication is deprecated
329308 protected Future <Void > oauthTrustedCertsSecret (Reconciliation reconciliation , String namespace , KafkaConnectCluster connect ) {
330- KafkaClientAuthentication authentication = connect .getAuthentication ();
331- Set <String > secretsToCopy = new HashSet <>();
332-
333- if (authentication instanceof KafkaClientAuthenticationOAuth oauth && oauth .getTlsTrustedCertificates () != null ) {
334- secretsToCopy .addAll (oauth .getTlsTrustedCertificates ().stream ().map (CertSecretSource ::getSecretName ).toList ());
335- }
336-
337- if (secretsToCopy .isEmpty ()) {
338- return Future .succeededFuture ();
339- }
340-
341- List <String > certs = new ArrayList <>();
342- String oauthSecret = KafkaConnectResources .internalOauthTrustedCertsSecretName (connect .getCluster ());
343- return Future .join (secretsToCopy .stream ()
344- .map (secretName -> secretOperations .getAsync (namespace , secretName )
345- .compose (secret -> {
346- if (secret == null ) {
347- return Future .failedFuture ("Secret " + secretName + " not found" );
348- } else {
349- secret .getData ().entrySet ().stream ()
350- .filter (e -> e .getKey ().contains (".crt" ))
351- // In case secrets contain the same key, append the secret name into the key
352- .forEach (e -> certs .add (e .getValue ()));
353- }
354- return Future .succeededFuture ();
355- }))
356- .collect (Collectors .toList ()))
357- .compose (ignore -> secretOperations .reconcile (
358- reconciliation ,
359- namespace ,
360- oauthSecret ,
361- connect .generateTlsTrustedCertsSecret (Map .of (oauthSecret + ".crt" , mergeAndEncodeCerts (certs )), oauthSecret ))
362- .mapEmpty ());
363- }
364-
365- private String mergeAndEncodeCerts (List <String > certs ) {
366- if (certs .size () > 1 ) {
367- String decodedAndMergedCerts = certs .stream ()
368- .map (Util ::decodeFromBase64 )
369- .collect (Collectors .joining ("\n " ));
370-
371- return Util .encodeToBase64 (decodedAndMergedCerts );
372- } else if (certs .size () < 1 ) {
373- return "" ;
309+ if (connect .getAuthentication () instanceof KafkaClientAuthenticationOAuth oauth ) {
310+ return ReconcilerUtils .trustedCertificates (reconciliation , secretOperations , oauth .getTlsTrustedCertificates ())
311+ .compose (certificates -> {
312+ if (certificates != null ) {
313+ return secretOperations .reconcile (
314+ reconciliation ,
315+ namespace ,
316+ KafkaConnectResources .internalOauthTrustedCertsSecretName (connect .getCluster ()),
317+ connect .generateTlsTrustedCertsSecret (Map .of ("ca.crt" , Util .encodeToBase64 (certificates )), KafkaConnectResources .internalOauthTrustedCertsSecretName (connect .getCluster ())))
318+ .mapEmpty ();
319+ } else {
320+ return Future .succeededFuture ();
321+ }
322+ });
374323 } else {
375- return certs . get ( 0 );
324+ return Future . succeededFuture ( );
376325 }
377326 }
378327
0 commit comments