11package it .aboutbits .postgresql .core ;
22
3+ import com .fasterxml .jackson .annotation .JsonIgnoreProperties ;
34import com .fasterxml .jackson .databind .ObjectMapper ;
45import io .fabric8 .kubernetes .client .KubernetesClient ;
56import it .aboutbits .postgresql .crd .clusterconnection .ClusterConnection ;
1920@ RequiredArgsConstructor
2021@ NullMarked
2122public final class KubernetesService {
22- private final ObjectMapper objectMapper ;
23-
24- private record FileCredentials (
25- @ Nullable String username ,
26- @ Nullable String password
27- ) {
28- }
29-
3023 public static final String SECRET_TYPE_BASIC_AUTH = "kubernetes.io/basic-auth" ;
3124 public static final String SECRET_DATA_BASIC_AUTH_USERNAME_KEY = "username" ;
3225 public static final String SECRET_DATA_BASIC_AUTH_PASSWORD_KEY = "password" ;
3326
27+ private final ObjectMapper objectMapper ;
28+
3429 public Credentials getAdminCredentials (
3530 KubernetesClient kubernetesClient ,
3631 ClusterConnection clusterConnection
@@ -39,13 +34,15 @@ public Credentials getAdminCredentials(
3934 if (spec .getAdminSecretRef () != null ) {
4035 var secretRef = spec .getAdminSecretRef ();
4136 var defaultNamespace = clusterConnection .getMetadata ().getNamespace ();
37+
4238 var credentials = getSecretRefCredentials (kubernetesClient , secretRef , defaultNamespace );
4339 if (credentials .username () == null ) {
4440 var secretNamespace = getSecretNamespace (secretRef , defaultNamespace );
4541 throw new IllegalStateException (
4642 "The Secret reference is missing required data username [secret.namespace=%s, secret.name=%s]" .formatted (
4743 secretNamespace , secretRef .getName ()));
4844 }
45+
4946 return credentials ;
5047 } else if (spec .getAdminSecretFileRef () != null ) {
5148 return getSecretFileRefCredentials (spec .getAdminSecretFileRef ());
@@ -60,20 +57,17 @@ public Credentials getSecretFileRefCredentials(FileRef fileRef) {
6057 try (var in = Files .newInputStream (path )) {
6158 var file = objectMapper .readValue (in , FileCredentials .class );
6259 if (file .username () == null ) {
63- throw new IllegalStateException (
64- "Credentials file is missing required field 'username' [path=%s]" .formatted (path ));
60+ throw new IllegalStateException ("Credentials file is missing required field 'username' [path=%s]" .formatted (path ));
6561 }
6662 if (file .password () == null ) {
67- throw new IllegalStateException (
68- "Credentials file is missing required field 'password' [path=%s]" .formatted (path ));
63+ throw new IllegalStateException ("Credentials file is missing required field 'password' [path=%s]" .formatted (path ));
6964 }
65+
7066 return new Credentials (file .username (), file .password ());
7167 } catch (NoSuchFileException e ) {
72- throw new IllegalStateException (
73- "Credentials file not found [path=%s]" .formatted (path ), e );
68+ throw new IllegalStateException ("Credentials file not found [path=%s]" .formatted (path ), e );
7469 } catch (IOException e ) {
75- throw new IllegalStateException (
76- "Failed to read the credentials file [path=%s]" .formatted (path ), e );
70+ throw new IllegalStateException ("Failed to read the credentials file [path=%s]" .formatted (path ), e );
7771 }
7872 }
7973
@@ -141,9 +135,21 @@ public Credentials getSecretRefCredentials(
141135 );
142136 }
143137
144- private String getSecretNamespace (ResourceRef secretRef , String defaultNamespace ) {
138+ private String getSecretNamespace (
139+ ResourceRef secretRef ,
140+ String defaultNamespace
141+ ) {
145142 return secretRef .getNamespace () != null
146143 ? secretRef .getNamespace ()
147144 : defaultNamespace ;
148145 }
146+
147+ /// The JSON file may carry more keys than we need, for example, the AWS Secrets Manager
148+ /// format also has `engine`, `host`, `port` and `dbname`. Unknown keys are ignored.
149+ @ JsonIgnoreProperties (ignoreUnknown = true )
150+ private record FileCredentials (
151+ @ Nullable String username ,
152+ @ Nullable String password
153+ ) {
154+ }
149155}
0 commit comments