Github user aledsage commented on a diff in the pull request:
https://github.com/apache/brooklyn-library/pull/33#discussion_r63860899
--- Diff:
software/database/src/main/java/org/apache/brooklyn/entity/database/postgresql/PostgreSqlSshDriver.java
---
@@ -328,9 +342,51 @@ private void initializeNewDatabase() {
" --command="+ createUserCommand),
sudoAsUser("postgres", getInstallDir() + "/bin/psql -p " +
entity.getAttribute(PostgreSqlNode.POSTGRESQL_PORT) +
" --command="+ createDatabaseCommand),
+ createRolesAdditionalCommand,
callPgctl("stop", true))
.failOnNonZeroResultCode().execute();
}
+
+ private String buildCreateRolesQuery() {
+ Map<String, Map> roles = entity.getConfig(PostgreSqlNode.ROLES);
+ StringBuilder builder = new StringBuilder("\"");
+
+ for (Map.Entry role: roles.entrySet()) {
+ builder.append(String.format("CREATE ROLE %s",
validateInput((String) role.getKey())));
+ if (((Map)
role.getValue()).containsKey(PostgreSqlNode.ROLE_PROPERTIES_KEY)) {
+ builder.append(String.format(" WITH %s; ",
validateInput((String) ((Map) role.getValue()).get("properties"))));
+ } else {
+ builder.append(";");
+ }
+
+ if (((Map)
role.getValue()).containsKey(PostgreSqlNode.ROLE_PRIVILEGES_KEY)) {
+ if ( ((Map)
role.getValue()).get(PostgreSqlNode.ROLE_PRIVILEGES_KEY) instanceof List) {
+ for (Object privilege: (List) ((Map)
role.getValue()).get(PostgreSqlNode.ROLE_PRIVILEGES_KEY)) {
+ builder.append(String.format("GRANT %s TO %s; ",
validateInput((String) privilege), validateInput((String) role.getKey())));
+ }
+ } else {
+ builder.append(String.format("GRANT %s TO %s; ",
+ validateInput((String) ((Map)
role.getValue()).get(PostgreSqlNode.ROLE_PRIVILEGES_KEY)),
validateInput((String) role.getKey())));
+ }
+ }
+ }
+
+ builder.append("\"");
+
+ return builder.toString();
+ }
+
+ private String validateInput(String input) {
+ String regex = "[A-Za-z_,\\s]+";
+ Pattern pattern = Pattern.compile(regex);
+ Matcher matcher = pattern.matcher(input);
+
+ if (matcher.find() && matcher.start() == 0 && matcher.end() ==
input.length()) {
+ return input;
+ }
+
+ return "";
--- End diff --
This seems like the wrong way to handle invalid input: we just return an
empty string without logging any problem? And presumably the subsequent roles
query would be invalid with missing role name etc, such as `CREATE ROLE WITH
...`?
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---