(CommandLine cli)
| 163 | .addOption(AUTH_CONF_DIR_OPTION) |
| 164 | .addOption(CommonCLIOptions.CREDENTIALS_OPTION) |
| 165 | .addOptionGroup(getConnectionOptions()); |
| 166 | } |
| 167 | |
| 168 | private void ensureArgumentIsValidBooleanIfPresent(String optionName, String value) { |
| 169 | if (value != null && !"true".equalsIgnoreCase(value) && !"false".equalsIgnoreCase(value)) { |
| 170 | echo("Argument [" + optionName + "] must be either true or false, but was [" + value + "]"); |
| 171 | runtime.exit(1); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | private void handleCommand(String cmd, AuthParams params) throws Exception { |
| 176 | switch (cmd) { |
| 177 | case "enable" -> enableBasicAuth(params); |
| 178 | case "disable" -> disableBasicAuth(params); |
| 179 | default -> { |
| 180 | CLIO.out("Valid auth commands are: enable, disable."); |
| 181 | runtime.exit(1); |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | private void enableBasicAuth(AuthParams params) throws Exception { |
| 187 | if (!params.prompt() && params.credentials() == null) { |
| 188 | CLIO.out("Option --credentials or --prompt is required with enable."); |
| 189 | runtime.exit(1); |
| 190 | } else if (!params.prompt() && !params.credentials().contains(":")) { |
| 191 | CLIO.out("Option --credentials is not in correct format."); |
| 192 | runtime.exit(1); |
| 193 | } |
| 194 | |
| 195 | String zkHost = params.zkHost(); |
| 196 | |
| 197 | if (!params.updateIncludeFileOnly()) { |
| 198 | if (zkHost == null) { |
| 199 | printZkHostError(params.zkHostSpecified()); |
| 200 | runtime.exit(1); |
| 201 | } |
| 202 | |
| 203 | // check if security is already enabled or not |
| 204 | try (SolrZkClient zkClient = zkClient(zkHost)) { |
| 205 | checkSecurityJsonExists(zkClient); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | String username, password; |
| 210 | if (params.credentials() != null) { |
| 211 | username = params.credentials().split(":")[0]; |
| 212 | password = params.credentials().split(":")[1]; |
| 213 | } else { |
| 214 | Console console = System.console(); |
| 215 | // keep prompting until they've entered a non-empty username & password |
| 216 | do { |
| 217 | username = console.readLine("Enter username: "); |
| 218 | } while (username == null || username.trim().isEmpty()); |
| 219 | username = username.trim(); |
| 220 | |
| 221 | do { |
| 222 | password = new String(console.readPassword("Enter password: ")); |
no test coverage detected