Skip to content

Commit

Permalink
Merge pull request #7178 from breakponchito/FISH-10458-fixing-code-sm…
Browse files Browse the repository at this point in the history
…ells-for-clustercli

FISH-10458: fixing code smells for cluster-cli
  • Loading branch information
breakponchito authored Feb 4, 2025
2 parents 45dff37 + ed9212f commit 015acd9
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2016-2021] [Payara Foundation and/or its affiliates]
// Portions Copyright [2016-2025] [Payara Foundation and/or its affiliates]

package com.sun.enterprise.admin.cli.cluster;

Expand All @@ -47,6 +47,7 @@

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
Expand Down Expand Up @@ -87,7 +88,7 @@ public class ChangeNodeMasterPasswordCommand extends LocalInstanceCommand {
protected static final String NEW_PASSWORD_ALIAS = "AS_ADMIN_NEWMASTERPASSWORD";

@Param(name = "node", primary = true)
protected String node;
protected String passwordNode;

@Param(name = "savemasterpassword", optional = true)
private boolean saveMasterPassword;
Expand All @@ -100,7 +101,7 @@ public class ChangeNodeMasterPasswordCommand extends LocalInstanceCommand {
protected void inject() throws CommandException {
super.inject();

selectedNodeDir = new File(nodeDir, node);
selectedNodeDir = new File(nodeDir, passwordNode);
}

@Override
Expand Down Expand Up @@ -157,7 +158,7 @@ protected int executeCommand() throws CommandException {
try {
// Write the master password file
PasswordAdapter p = new PasswordAdapter(pwdFile.getAbsolutePath(), MASTERPASSWORD_FILE.toCharArray());
p.setPasswordForAlias(MASTERPASSWORD_FILE, newPassword.getBytes());
p.setPasswordForAlias(MASTERPASSWORD_FILE, newPassword.getBytes(StandardCharsets.UTF_8));
FileProtectionUtility.chmod0600(pwdFile);
return 0;
} catch (Exception ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
* holder.
*/

// Portions Copyright [2016-2020] [Payara Foundation and/or its affiliates]
// Portions Copyright [2016-2025] [Payara Foundation and/or its affiliates]


package com.sun.enterprise.admin.cli.cluster;
Expand All @@ -58,6 +58,7 @@
import java.io.File;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.logging.Level;
import org.glassfish.api.ActionReport;
Expand Down Expand Up @@ -356,7 +357,7 @@ protected void createMasterPasswordFile(String masterPassword) throws CommandExc
final File pwdFile = new File(this.getServerDirs().getAgentDir(), MASTERPASSWORD_FILE);
try {
PasswordAdapter p = new PasswordAdapter(pwdFile.getAbsolutePath(), MASTERPASSWORD_FILE.toCharArray());
p.setPasswordForAlias(MASTERPASSWORD_FILE, masterPassword.getBytes());
p.setPasswordForAlias(MASTERPASSWORD_FILE, masterPassword.getBytes(StandardCharsets.UTF_8));
FileProtectionUtility.chmod0600(pwdFile);
} catch (Exception ex) {
throw new CommandException(Strings.get("masterPasswordFileNotCreated", pwdFile),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2025] [Payara Foundation and/or its affiliates]
package com.sun.enterprise.admin.cli.cluster;

import com.sun.enterprise.universal.io.SmartFile;
Expand All @@ -45,6 +46,7 @@
import com.sun.enterprise.util.io.FileUtils;
import com.sun.enterprise.util.zip.ZipFileException;
import com.sun.enterprise.util.zip.ZipWriter;
import java.nio.charset.StandardCharsets;
import org.glassfish.api.Param;
import org.glassfish.api.admin.CommandException;
import org.glassfish.hk2.api.PerLookup;
Expand Down Expand Up @@ -224,7 +226,7 @@ private static boolean isFileWithinBinDirectory(String file) {

public static String toString(InputStream ins) throws IOException {
StringWriter sw = new StringWriter();
InputStreamReader reader = new InputStreamReader(ins);
InputStreamReader reader = new InputStreamReader(ins, StandardCharsets.UTF_8);

char[] buffer = new char[4096];
int n;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2019-2021] Payara Foundation and/or affiliates
// Portions Copyright [2019-2025] Payara Foundation and/or affiliates

package com.sun.enterprise.admin.cli.cluster;

import java.nio.charset.StandardCharsets;
import java.util.logging.Level;
import com.sun.enterprise.util.SystemPropertyConstants;
import com.sun.enterprise.util.io.FileUtils;
Expand Down Expand Up @@ -200,14 +201,15 @@ private void copyToHostsInternal(File zipFile, ArrayList<String> binDirFiles) th
String unzipCommand = "cd '" + sshInstallDir + "'; jar -xvf " + getArchiveName();
int status = sshLauncher.runCommand(unzipCommand, outStream);
if (status != 0) {
logger.info(Strings.get("jar.failed", host, outStream.toString()));
throw new CommandException("Remote command output: " + outStream.toString());
String outStreamToString = outStream.toString(StandardCharsets.UTF_8);
logger.info(Strings.get("jar.failed", host, outStreamToString));
throw new CommandException("Remote command output: " + outStreamToString);
}
if (logger.isLoggable(Level.FINER))
logger.log(Level.FINER, "Installed {0} into {1}:{2}", new Object[]{getArchiveName(), host, sshInstallDir});
}
catch (IOException ioe) {
logger.info(Strings.get("jar.failed", host, outStream.toString()));
logger.info(Strings.get("jar.failed", host, outStream.toString(StandardCharsets.UTF_8)));
throw new IOException(ioe);
}

Expand Down Expand Up @@ -311,11 +313,11 @@ private void checkIfAlreadyInstalled(String host, String sshInstallDir) throws C
int status = sshLauncher.runCommand(cmd, outStream);
if (status == 0) {
if (logger.isLoggable(Level.FINER))
logger.log(Level.FINER, "{0}:''{1}'' returned [{2}]", new Object[]{host, cmd, outStream.toString()});
logger.log(Level.FINER, "{0}:''{1}'' returned [{2}]", new Object[]{host, cmd, outStream.toString(StandardCharsets.UTF_8)});
throw new CommandException(Strings.get("install.dir.exists", sshInstallDir));
} else {
if (logger.isLoggable(Level.FINER))
logger.log(Level.FINER, "{0}:''{1}'' failed [{2}]", new Object[]{host, cmd, outStream.toString()});
logger.log(Level.FINER, "{0}:''{1}'' failed [{2}]", new Object[]{host, cmd, outStream.toString(StandardCharsets.UTF_8)});
}
}
catch (IOException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portios Copyright [2019] [Payara Foundation and/or its affiliates]
// Portios Copyright [2019-2025] [Payara Foundation and/or its affiliates]

package com.sun.enterprise.admin.cli.cluster;

Expand Down Expand Up @@ -407,9 +407,13 @@ protected final void whackFilesystem() throws CommandException {
StringBuilder sb = new StringBuilder();
sb.append("whackee=").append(whackee.toString());
sb.append(", files in parent:");
files = parent.listFiles();
for (File f : files) {
sb.append(f.toString()).append(", ");
if (parent != null) {
files = parent.listFiles();
if (files != null) {
for (File f : files) {
sb.append(f.toString()).append(", ");
}
}
}
File f1 = new File(whackee.toString());
sb.append(", new wackee.exists=").append(f1.exists());
Expand All @@ -419,15 +423,17 @@ protected final void whackFilesystem() throws CommandException {
// now see if the parent dir is empty. If so wipe it out.
// Don't be too picky with throwin errors here...
try {
files = parent.listFiles();
if (parent != null) {
files = parent.listFiles();

if (noInstancesRemain(files)) {
File tmpwhackee = File.createTempFile("oldnode", null, grandParent);
if (!tmpwhackee.delete()) {
throw new IOException(Strings.get("cantdelete", tmpwhackee));
if (noInstancesRemain(files)) {
File tmpwhackee = File.createTempFile("oldnode", null, grandParent);
if (!tmpwhackee.delete()) {
throw new IOException(Strings.get("cantdelete", tmpwhackee));
}
FileUtils.renameFile(parent, tmpwhackee);
FileUtils.whack(tmpwhackee);
}
FileUtils.renameFile(parent, tmpwhackee);
FileUtils.whack(tmpwhackee);
}
} catch (IOException ioe) {
// we tried!!!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2018-2019] [Payara Foundation and/or its affiliates]
// Portions Copyright [2018-2025] [Payara Foundation and/or its affiliates]

package com.sun.enterprise.admin.cli.cluster;

Expand Down Expand Up @@ -296,55 +296,58 @@ String expandPasswordAlias(String host, String alias, boolean verifyConn) {

try {
File domainsDirFile = DomainDirs.getDefaultDomainsDir();

//get the list of domains
File[] files = domainsDirFile.listFiles(new FileFilter() {
@Override
public boolean accept(File f) {
return f.isDirectory();
}
});

for (File f : files) {
//the following property is required for initializing the password helper
System.setProperty(SystemPropertyConstants.INSTANCE_ROOT_PROPERTY, f.getAbsolutePath());
try {
final PasswordAdapter pa = new PasswordAdapter(null);
final boolean exists = pa.aliasExists(alias);
if (exists) {
String mPass = getMasterPassword(f.getName());
expandedPassword = new PasswordAdapter(mPass.toCharArray()).getPasswordForAlias(alias);
}
} catch (Exception e) {
if (logger.isLoggable(Level.FINER)) {
logger.finer(StringUtils.cat(": ", alias, e.getMessage()));
if (domainsDirFile != null) {
//get the list of domains
//if(domainsDirFile != null) {
File[] files = domainsDirFile.listFiles(new FileFilter() {
@Override
public boolean accept(File f) {
return f.isDirectory();
}
logger.warning(Strings.get("GetPasswordFailure", f.getName()));
continue;
}

if (expandedPassword != null) {
SSHLauncher sshL = new SSHLauncher();
if (host != null) {
sshpassword = expandedPassword;
sshL.init(getRemoteUser(), host, getRemotePort(), sshpassword, null, null, logger);
connStatus = sshL.checkPasswordAuth();
if (!connStatus) {
logger.warning(Strings.get("PasswordAuthFailure", f.getName()));
}
} else {
sshkeypassphrase = expandedPassword;
if (verifyConn) {
sshL.init(getRemoteUser(), hosts[0], getRemotePort(), sshpassword, getSshKeyFile(), sshkeypassphrase, logger);
connStatus = sshL.checkConnection();
if (!connStatus) {
logger.warning(Strings.get("PasswordAuthFailure", f.getName()));
});
if (files != null) {
for (File f : files) {
//the following property is required for initializing the password helper
System.setProperty(SystemPropertyConstants.INSTANCE_ROOT_PROPERTY, f.getAbsolutePath());
try {
final PasswordAdapter pa = new PasswordAdapter(null);
final boolean exists = pa.aliasExists(alias);
if (exists) {
String mPass = getMasterPassword(f.getName());
expandedPassword = new PasswordAdapter(mPass.toCharArray()).getPasswordForAlias(alias);
}
} catch (Exception e) {
if (logger.isLoggable(Level.FINER)) {
logger.finer(StringUtils.cat(": ", alias, e.getMessage()));
}
logger.warning(Strings.get("GetPasswordFailure", f.getName()));
continue;
}
}

if (connStatus) {
break;
if (expandedPassword != null) {
SSHLauncher sshL = new SSHLauncher();
if (host != null) {
sshpassword = expandedPassword;
sshL.init(getRemoteUser(), host, getRemotePort(), sshpassword, null, null, logger);
connStatus = sshL.checkPasswordAuth();
if (!connStatus) {
logger.warning(Strings.get("PasswordAuthFailure", f.getName()));
}
} else {
sshkeypassphrase = expandedPassword;
if (verifyConn) {
sshL.init(getRemoteUser(), hosts[0], getRemotePort(), sshpassword, getSshKeyFile(), sshkeypassphrase, logger);
connStatus = sshL.checkConnection();
if (!connStatus) {
logger.warning(Strings.get("PasswordAuthFailure", f.getName()));
}
}
}

if (connStatus) {
break;
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2019-2021] Payara Foundation and/or affiliates
// Portions Copyright [2019-2025] Payara Foundation and/or affiliates

package com.sun.enterprise.admin.cli.cluster;

Expand Down Expand Up @@ -286,10 +286,12 @@ protected boolean synchronizeInstance() throws CommandException {
sr.instance = instanceName;
sr.dir = "config-specific";
File configDir = new File(instanceDir, "config");
for (File f : configDir.listFiles()) {
if (!f.isDirectory())
continue;
getFileModTimes(f, configDir, sr, SyncLevel.DIRECTORY);
if(configDir.exists()) {
for (File f : Objects.requireNonNull(configDir.listFiles())) {
if (!f.isDirectory())
continue;
getFileModTimes(f, configDir, sr, SyncLevel.DIRECTORY);
}
}
/*
* Before sending the last sync request revert to using the original
Expand Down Expand Up @@ -372,7 +374,7 @@ private void getFileModTimes(File dir, File baseDir, SyncRequest sr,
sr.files.add(mt);
return;
}
for (String file : dir.list()) {
for (String file : Objects.requireNonNull(dir.list())) {
File f = new File(dir, file);
long time = f.lastModified();
if (time == 0)
Expand Down

0 comments on commit 015acd9

Please sign in to comment.