in src/main/java/com/awslabs/aws/greengrass/provisioner/implementations/helpers/BasicHsiBootstrapHelper.java [50:112]
public void execute(HsiBootstrapArguments hsiBootstrapArguments) {
log.info("Copying bootstrap script to host via scp...");
// Get an SSH session to the target
Session session = sshHelper.getSshSession(hsiBootstrapArguments.targetHost, hsiBootstrapArguments.targetUser);
final String bootstrapVendorPath = (String.join("", BOOTSTRAP_VENDOR_PREFIX, hsiBootstrapArguments.hsiVendor.name(), ".sh")).toLowerCase();
final String bootstrapVendorResourcePath = String.join("/", GREENGRASS_HSI, bootstrapVendorPath);
// Get input streams for the resources
InputStream bootstrapCommonShStream = javaResourceHelper.getResourceAsStream(BOOTSTRAP_COMMON_SH_RESOURCE_PATH);
// Get the bootstrap script for this vendor as a string, replace the PKCS11 URL into it
String bootstrapVendorShString = javaResourceHelper.resourceToString(bootstrapVendorResourcePath);
bootstrapVendorShString = bootstrapVendorShString.replaceAll(PKCS_11_URL, hsiBootstrapArguments.hsiVendor.getPkcs11Url());
// Turn it back into an input stream
InputStream bootstrapVendorShStream = new ByteArrayInputStream(bootstrapVendorShString.getBytes());
// Copy the files
Try.run(() -> ioHelper.sendFile(session, bootstrapCommonShStream, BOOTSTRAP_COMMON_SH_RESOURCE_PATH, BOOTSTRAP_COMMON_SH)).get();
Try.run(() -> ioHelper.sendFile(session, bootstrapVendorShStream, bootstrapVendorResourcePath, bootstrapVendorPath)).get();
// Make them executable
makeExecutable(session, bootstrapVendorPath);
makeExecutable(session, BOOTSTRAP_COMMON_SH);
String temporaryConfiguration = getTemporaryConfiguration();
// Run the HSI script on the remote host
log.info("Running HSI bootstrap script");
String command = String.join("", "$SHELL -l -c \"", temporaryConfiguration, "./", bootstrapVendorPath, "\"");
List<String> output = Try.of(() -> ioHelper.runCommand(session, command, Optional.of(log::info))).get();
log.info("Finished running HSI bootstrap script");
// Disconnect SSH so GGP can exit
session.disconnect();
Optional<String> optionalSuccess = output.stream()
.filter(string -> string.contains(SUCCESS))
.filter(string -> string.contains(ARN_AWS_IOT))
.filter(string -> string.contains(CERT))
.findFirst();
if (optionalSuccess.isPresent()) {
String successString = optionalSuccess.get();
String arn = successString.substring(successString.indexOf(ARN_AWS_IOT));
log.info(String.join("", "HSI successfully bootstrapped. ARN for the certificate is: ", arn));
return;
}
Optional<String> optionalError = output.stream()
.filter(string -> string.contains(ERROR))
.findFirst();
if (optionalError.isPresent()) {
log.error(String.join("", "HSI bootstrap failed for the following reason [", optionalError.get(), "]"));
} else {
log.error("HSI bootstrap failed for an unknown reason");
}
}