|
37 | 37 | import org.jooq.impl.DSL; |
38 | 38 | import org.jooq.lambda.tuple.Tuple; |
39 | 39 | import org.jooq.lambda.tuple.Tuple2; |
| 40 | +import org.jooq.lambda.tuple.Tuple3; |
40 | 41 |
|
41 | 42 | @Singleton |
42 | 43 | @OperatorVersionBinder |
@@ -199,40 +200,61 @@ private void setAuthenticatorCredentials( |
199 | 200 | StackGresClusterContext context, |
200 | 201 | Map<String, String> previousSecretData, |
201 | 202 | Map<String, String> data) { |
202 | | - final String authenticatorUsername = context.getAuthenticatorUsername() |
203 | | - .orElse(previousSecretData |
204 | | - .getOrDefault(AUTHENTICATOR_USERNAME_KEY, previousSecretData |
205 | | - .getOrDefault(AUTHENTICATOR_USERNAME_ENV, AUTHENTICATOR_USERNAME))); |
206 | | - data.put(AUTHENTICATOR_USERNAME_KEY, authenticatorUsername); |
207 | | - data.put(AUTHENTICATOR_USERNAME_ENV, authenticatorUsername); |
208 | | - final String authenticatorPasswordEnv = AUTHENTICATOR_PASSWORD_ENV |
209 | | - .replace(AUTHENTICATOR_USERNAME, authenticatorUsername); |
210 | | - final String authenticatorOptionsEnv = AUTHENTICATOR_OPTIONS_ENV |
211 | | - .replace(AUTHENTICATOR_USERNAME, authenticatorUsername); |
212 | | - final String authenticatorPassword = context.getAuthenticatorPassword() |
213 | | - .orElse(previousSecretData |
214 | | - .getOrDefault(AUTHENTICATOR_PASSWORD_KEY, previousSecretData |
215 | | - .getOrDefault(authenticatorPasswordEnv, |
216 | | - context.getGeneratedAuthenticatorPassword()))); |
217 | | - data.put(AUTHENTICATOR_PASSWORD_KEY, authenticatorPassword); |
218 | | - data.put(authenticatorPasswordEnv, context.getAuthenticatorPassword() |
| 203 | + var authenticatorCredentials = getAuthenticatorCredentials(context, previousSecretData); |
| 204 | + data.put(AUTHENTICATOR_USERNAME_KEY, authenticatorCredentials.v1); |
| 205 | + data.put(AUTHENTICATOR_USERNAME_ENV, authenticatorCredentials.v1); |
| 206 | + data.put(AUTHENTICATOR_PASSWORD_KEY, authenticatorCredentials.v2); |
| 207 | + data.put(authenticatorCredentials.v3, context.getAuthenticatorPassword() |
219 | 208 | .orElse(data.get(AUTHENTICATOR_PASSWORD_KEY))); |
| 209 | + final String authenticatorOptionsEnv = AUTHENTICATOR_OPTIONS_ENV |
| 210 | + .replace(AUTHENTICATOR_USERNAME, authenticatorCredentials.v1); |
220 | 211 | data.put(authenticatorOptionsEnv, "SUPERUSER"); |
221 | 212 | data.put( |
222 | 213 | ROLES_UPDATE_SQL_KEY, |
223 | 214 | Optional.ofNullable(data.get(ROLES_UPDATE_SQL_KEY)).orElse("") + "\n" |
224 | 215 | + "DO $$\n" |
225 | 216 | + "BEGIN\n" |
226 | | - + " IF NOT EXISTS (SELECT * FROM pg_roles WHERE rolname = " + DSL.inline(authenticatorUsername) + ") THEN\n" |
227 | | - + " CREATE USER " + DSL.quotedName(authenticatorUsername) |
228 | | - + " WITH SUPERUSER PASSWORD " + DSL.inline(authenticatorPassword) + ";\n" |
| 217 | + + " IF NOT EXISTS (SELECT * FROM pg_roles WHERE rolname = " |
| 218 | + + DSL.inline(authenticatorCredentials.v1) + ") THEN\n" |
| 219 | + + " CREATE USER " + DSL.quotedName(authenticatorCredentials.v1) |
| 220 | + + " WITH SUPERUSER PASSWORD " + DSL.inline(authenticatorCredentials.v2) + ";\n" |
229 | 221 | + " ELSE\n" |
230 | | - + " ALTER ROLE " + DSL.quotedName(authenticatorUsername) |
231 | | - + " WITH SUPERUSER PASSWORD " + DSL.inline(authenticatorPassword) + ";\n" |
| 222 | + + " ALTER ROLE " + DSL.quotedName(authenticatorCredentials.v1) |
| 223 | + + " WITH SUPERUSER PASSWORD " + DSL.inline(authenticatorCredentials.v2) + ";\n" |
232 | 224 | + " END IF;\n" |
233 | 225 | + "END$$;"); |
234 | 226 | } |
235 | 227 |
|
| 228 | + public static Tuple3<String, String, String> getAuthenticatorCredentials( |
| 229 | + StackGresClusterContext context) { |
| 230 | + final Map<String, String> previousSecretData = context.getDatabaseSecret() |
| 231 | + .map(Secret::getData) |
| 232 | + .map(ResourceUtil::decodeSecret) |
| 233 | + .orElse(Map.of()); |
| 234 | + |
| 235 | + return getAuthenticatorCredentials(context, previousSecretData); |
| 236 | + } |
| 237 | + |
| 238 | + private static Tuple3<String, String, String> getAuthenticatorCredentials( |
| 239 | + StackGresClusterContext context, |
| 240 | + Map<String, String> previousSecretData) { |
| 241 | + final String authenticatorUsername = context.getAuthenticatorUsername() |
| 242 | + .orElse(previousSecretData |
| 243 | + .getOrDefault(AUTHENTICATOR_USERNAME_KEY, previousSecretData |
| 244 | + .getOrDefault(AUTHENTICATOR_USERNAME_ENV, AUTHENTICATOR_USERNAME))); |
| 245 | + final String authenticatorPasswordEnv = AUTHENTICATOR_PASSWORD_ENV |
| 246 | + .replace(AUTHENTICATOR_USERNAME, authenticatorUsername); |
| 247 | + final String authenticatorPassword = context.getAuthenticatorPassword() |
| 248 | + .orElse(previousSecretData |
| 249 | + .getOrDefault(AUTHENTICATOR_PASSWORD_KEY, previousSecretData |
| 250 | + .getOrDefault(authenticatorPasswordEnv, |
| 251 | + context.getGeneratedAuthenticatorPassword()))); |
| 252 | + return Tuple.tuple( |
| 253 | + authenticatorUsername, |
| 254 | + authenticatorPassword, |
| 255 | + authenticatorPasswordEnv); |
| 256 | + } |
| 257 | + |
236 | 258 | private void setBabelfishCredentials( |
237 | 259 | StackGresClusterContext context, |
238 | 260 | final Map<String, String> previousSecretData, |
|
0 commit comments