|
11 | 11 | import io.fabric8.kubernetes.api.model.apps.Deployment;
|
12 | 12 |
|
13 | 13 | import java.util.Map;
|
| 14 | +import java.util.Optional; |
14 | 15 |
|
| 16 | +import static io.apicurio.registry.operator.EnvironmentVariables.*; |
15 | 17 | import static io.apicurio.registry.operator.resource.app.AppDeploymentResource.addEnvVar;
|
16 | 18 | import static java.util.Optional.ofNullable;
|
17 | 19 |
|
18 | 20 | public class KafkaSqlTLS {
|
19 | 21 |
|
20 |
| - public static final String ENV_KAFKASQL_SECURITY_PROTOCOL = "APICURIO_KAFKA_COMMON_SECURITY_PROTOCOL"; |
21 |
| - |
22 |
| - public static final String ENV_KAFKASQL_SSL_KEYSTORE_TYPE = "APICURIO_KAFKA_COMMON_SSL_KEYSTORE_TYPE"; |
23 |
| - public static final String ENV_KAFKASQL_SSL_KEYSTORE_LOCATION = "APICURIO_KAFKA_COMMON_SSL_KEYSTORE_LOCATION"; |
24 |
| - public static final String ENV_KAFKASQL_SSL_KEYSTORE_PASSWORD = "APICURIO_KAFKA_COMMON_SSL_KEYSTORE_PASSWORD"; |
25 |
| - |
26 |
| - public static final String ENV_KAFKASQL_SSL_TRUSTSTORE_TYPE = "APICURIO_KAFKA_COMMON_SSL_TRUSTSTORE_TYPE"; |
27 |
| - public static final String ENV_KAFKASQL_SSL_TRUSTSTORE_LOCATION = "APICURIO_KAFKA_COMMON_SSL_TRUSTSTORE_LOCATION"; |
28 |
| - public static final String ENV_KAFKASQL_SSL_TRUSTSTORE_PASSWORD = "APICURIO_KAFKA_COMMON_SSL_TRUSTSTORE_PASSWORD"; |
29 |
| - |
30 | 22 | /**
|
31 | 23 | * Plain KafkaSQL must be already configured.
|
32 | 24 | */
|
33 | 25 | public static boolean configureKafkaSQLTLS(ApicurioRegistry3 primary, Deployment deployment,
|
34 | 26 | String containerName, Map<String, EnvVar> env) {
|
35 | 27 |
|
36 | 28 | // spotless:off
|
37 |
| - var keystore = new SecretKeyRefTool(ofNullable(primary) |
38 |
| - .map(ApicurioRegistry3::getSpec) |
39 |
| - .map(ApicurioRegistry3Spec::getApp) |
40 |
| - .map(AppSpec::getStorage) |
41 |
| - .map(StorageSpec::getKafkasql) |
42 |
| - .map(KafkaSqlSpec::getTls) |
| 29 | + var keystore = new SecretKeyRefTool(getKafkaSqlTLSSpec(primary) |
43 | 30 | .map(KafkaSqlTLSSpec::getKeystoreSecretRef)
|
44 | 31 | .orElse(null), "user.p12");
|
45 | 32 |
|
46 |
| - var keystorePassword = new SecretKeyRefTool(ofNullable(primary) |
47 |
| - .map(ApicurioRegistry3::getSpec) |
48 |
| - .map(ApicurioRegistry3Spec::getApp) |
49 |
| - .map(AppSpec::getStorage) |
50 |
| - .map(StorageSpec::getKafkasql) |
51 |
| - .map(KafkaSqlSpec::getTls) |
| 33 | + var keystorePassword = new SecretKeyRefTool(getKafkaSqlTLSSpec(primary) |
52 | 34 | .map(KafkaSqlTLSSpec::getKeystorePasswordSecretRef)
|
53 | 35 | .orElse(null), "user.password");
|
54 | 36 |
|
55 |
| - var truststore = new SecretKeyRefTool(ofNullable(primary) |
56 |
| - .map(ApicurioRegistry3::getSpec) |
57 |
| - .map(ApicurioRegistry3Spec::getApp) |
58 |
| - .map(AppSpec::getStorage) |
59 |
| - .map(StorageSpec::getKafkasql) |
60 |
| - .map(KafkaSqlSpec::getTls) |
| 37 | + var truststore = new SecretKeyRefTool(getKafkaSqlTLSSpec(primary) |
61 | 38 | .map(KafkaSqlTLSSpec::getTruststoreSecretRef)
|
62 | 39 | .orElse(null), "ca.p12");
|
63 | 40 |
|
64 |
| - var truststorePassword = new SecretKeyRefTool(ofNullable(primary) |
65 |
| - .map(ApicurioRegistry3::getSpec) |
66 |
| - .map(ApicurioRegistry3Spec::getApp) |
67 |
| - .map(AppSpec::getStorage) |
68 |
| - .map(StorageSpec::getKafkasql) |
69 |
| - .map(KafkaSqlSpec::getTls) |
| 41 | + var truststorePassword = new SecretKeyRefTool(getKafkaSqlTLSSpec(primary) |
70 | 42 | .map(KafkaSqlTLSSpec::getTruststorePasswordSecretRef)
|
71 | 43 | .orElse(null), "ca.password");
|
72 | 44 | // spotless:on
|
73 | 45 |
|
74 | 46 | if (truststore.isValid() && truststorePassword.isValid() && keystore.isValid()
|
75 | 47 | && keystorePassword.isValid()) {
|
76 | 48 |
|
77 |
| - addEnvVar(env, ENV_KAFKASQL_SECURITY_PROTOCOL, "SSL"); |
| 49 | + addEnvVar(env, KAFKASQL_SECURITY_PROTOCOL, "SSL"); |
78 | 50 |
|
79 | 51 | // ===== Keystore
|
80 | 52 |
|
81 |
| - addEnvVar(env, ENV_KAFKASQL_SSL_KEYSTORE_TYPE, "PKCS12"); |
| 53 | + addEnvVar(env, KAFKASQL_SSL_KEYSTORE_TYPE, "PKCS12"); |
82 | 54 | keystore.applySecretVolume(deployment, containerName);
|
83 |
| - addEnvVar(env, ENV_KAFKASQL_SSL_KEYSTORE_LOCATION, keystore.getSecretVolumeKeyPath()); |
84 |
| - keystorePassword.applySecretEnvVar(env, ENV_KAFKASQL_SSL_KEYSTORE_PASSWORD); |
| 55 | + addEnvVar(env, KAFKASQL_SSL_KEYSTORE_LOCATION, keystore.getSecretVolumeKeyPath()); |
| 56 | + keystorePassword.applySecretEnvVar(env, KAFKASQL_SSL_KEYSTORE_PASSWORD); |
85 | 57 |
|
86 | 58 | // ===== Truststore
|
87 | 59 |
|
88 |
| - addEnvVar(env, ENV_KAFKASQL_SSL_TRUSTSTORE_TYPE, "PKCS12"); |
| 60 | + addEnvVar(env, KAFKASQL_SSL_TRUSTSTORE_TYPE, "PKCS12"); |
89 | 61 | truststore.applySecretVolume(deployment, containerName);
|
90 |
| - addEnvVar(env, ENV_KAFKASQL_SSL_TRUSTSTORE_LOCATION, truststore.getSecretVolumeKeyPath()); |
91 |
| - truststorePassword.applySecretEnvVar(env, ENV_KAFKASQL_SSL_TRUSTSTORE_PASSWORD); |
| 62 | + addEnvVar(env, KAFKASQL_SSL_TRUSTSTORE_LOCATION, truststore.getSecretVolumeKeyPath()); |
| 63 | + truststorePassword.applySecretEnvVar(env, KAFKASQL_SSL_TRUSTSTORE_PASSWORD); |
92 | 64 |
|
93 | 65 | return true;
|
94 | 66 | }
|
95 | 67 | return false;
|
96 | 68 | }
|
| 69 | + |
| 70 | + private static Optional<KafkaSqlTLSSpec> getKafkaSqlTLSSpec(ApicurioRegistry3 primary) { |
| 71 | + // spotless:off |
| 72 | + return ofNullable(primary) |
| 73 | + .map(ApicurioRegistry3::getSpec) |
| 74 | + .map(ApicurioRegistry3Spec::getApp) |
| 75 | + .map(AppSpec::getStorage) |
| 76 | + .map(StorageSpec::getKafkasql) |
| 77 | + .map(KafkaSqlSpec::getTls); |
| 78 | + // spotless:on |
| 79 | + } |
97 | 80 | }
|
0 commit comments