Skip to content

Commit

Permalink
dcmimport better UX if missing root cert
Browse files Browse the repository at this point in the history
  • Loading branch information
ThePrez committed Aug 7, 2022
1 parent c5e995f commit 211f29b
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ buildNumber.properties
.mvn/timing.properties
# https://github.com/takari/maven-wrapper#usage-without-binary-jar
.mvn/wrapper/maven-wrapper.jar
/bin/
20 changes: 19 additions & 1 deletion src/main/java/com/github/ibmioss/dcmtools/DcmImportCmd.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import java.util.List;

import com.github.ibmioss.dcmtools.CertFileImporter.ImportOptions;
import com.github.ibmioss.dcmtools.DcmUserOpts;
import com.github.ibmioss.dcmtools.utils.DcmChangeTracker;
import com.github.ibmioss.dcmtools.utils.DcmChangeTracker.NoChangesMadeException;
import com.github.ibmioss.dcmtools.utils.TempFileManager;
import com.github.theprez.jcmdutils.AppLogger;
import com.github.theprez.jcmdutils.ConsoleQuestionAsker;
Expand Down Expand Up @@ -133,11 +135,27 @@ public static void main(final String... _args) {
final DcmChangeTracker dcmTracker = new DcmChangeTracker(logger, opts);
final CertFileImporter off = new CertFileImporter(logger, files);
off.doImport(logger, opts, dcmTracker);
dcmTracker.printChanges(logger);
//@formatter:off
String noChangesResolution = "This failure may be due to not having imported the necessary root certificate(s). \n" +
"You can try running the following commands to import the Mozilla certificates bundle,\n" +
"which may include the needed root certificate:\n" +
" /QOpenSys/pkgs/bin/yum install ca-certificates-mozilla\n" +
" /QOpenSys/pkgs/bin/dcmimport --installed-certs\n" +
"Then, try this request again.";
//@formatter:on
dcmTracker.printChanges(logger, noChangesResolution);
logger.println_success("SUCCESS!!!");
} catch (final Exception e) {
logger.printExceptionStack_verbose(e);
logger.println_err(e.getLocalizedMessage());
if (e instanceof NoChangesMadeException) {
String resolutionText = ((NoChangesMadeException) e).getResolutionText();
if (StringUtils.isNonEmpty(resolutionText)) {
logger.println("\n");
logger.println(resolutionText);
logger.println("\n");
}
}
TempFileManager.cleanup();
System.exit(-1);
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public static void main(final String... _args) {
try (FileOutputStream fos = new FileOutputStream(opts.getDcmStore())) {
ks.store(fos, opts.getDcmPassword().toCharArray());
}
tracker.printChanges(logger);
tracker.printChanges(logger, null);
logger.println_success("SUCCESS!!!");
} catch (final Exception e) {
logger.printExceptionStack_verbose(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public static void main(final String... _args) {
ks.store(fos, opts.getDcmPassword().toCharArray());
}

tracker.printChanges(logger);
tracker.printChanges(logger, null);
logger.println_success("SUCCESS!!!");
} catch (final Exception e) {
logger.printExceptionStack_verbose(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,19 @@ public interface DcmChange {
public String getFormattedExplanation(String _linePrefix);
}

public static class NoChangesMadeException extends IOException {
private String m_resolutionText;

public NoChangesMadeException(final String _resolutionText) {
super("No changes were made to the DCM keystore!");
m_resolutionText = _resolutionText;
}

public String getResolutionText() {
return m_resolutionText;
}
}

private final DcmUserOpts m_opts;

private final KeyStoreInterrogator m_startingSnapshot;
Expand Down Expand Up @@ -134,10 +147,10 @@ public KeyStoreInterrogator getStartingSnapshot() {
return m_startingSnapshot;
}

public synchronized void printChanges(final AppLogger _logger) throws IOException, KeyStoreException, NoSuchAlgorithmException, CertificateException, PropertyVetoException, AS400SecurityException, ErrorCompletingRequestException, InterruptedException, ObjectDoesNotExistException {
public synchronized void printChanges(final AppLogger _logger, final String _resolutionText) throws IOException, KeyStoreException, NoSuchAlgorithmException, CertificateException, PropertyVetoException, AS400SecurityException, ErrorCompletingRequestException, InterruptedException, ObjectDoesNotExistException {
final List<DcmChange> changes = getChanges(_logger);
if (changes.isEmpty()) {
throw new IOException("No changes were made to the DCM keystore!");
throw new NoChangesMadeException(_resolutionText);
}
_logger.println("The following changes were made on the DCM keystore:");
for (final DcmChange change : changes) {
Expand Down

0 comments on commit 211f29b

Please sign in to comment.