Search in sources :

Example 1 with CommitableFile

use of com.aws.greengrass.util.CommitableFile in project aws-greengrass-nucleus by aws-greengrass.

the class DeviceProvisioningHelper method updateKernelConfigWithIotConfiguration.

/**
 * Update the kernel config with iot thing info, in specific CA, private Key and cert path.
 *
 * @param kernel        Kernel instance
 * @param thing         thing info
 * @param awsRegion     aws region
 * @param roleAliasName role alias for using IoT credentials endpoint
 * @throws IOException                  Exception while reading root CA from file
 * @throws DeviceConfigurationException when the configuration parameters are not valid
 */
public void updateKernelConfigWithIotConfiguration(Kernel kernel, ThingInfo thing, String awsRegion, String roleAliasName) throws IOException, DeviceConfigurationException {
    Path rootDir = kernel.getNucleusPaths().rootPath();
    Path caFilePath = rootDir.resolve("rootCA.pem");
    Path privKeyFilePath = rootDir.resolve("privKey.key");
    Path certFilePath = rootDir.resolve("thingCert.crt");
    downloadRootCAToFile(caFilePath.toFile());
    try (CommitableFile cf = CommitableFile.of(privKeyFilePath, true)) {
        cf.write(thing.keyPair.privateKey().getBytes(StandardCharsets.UTF_8));
    }
    try (CommitableFile cf = CommitableFile.of(certFilePath, true)) {
        cf.write(thing.certificatePem.getBytes(StandardCharsets.UTF_8));
    }
    new DeviceConfiguration(kernel, thing.thingName, thing.dataEndpoint, thing.credEndpoint, privKeyFilePath.toString(), certFilePath.toString(), caFilePath.toString(), awsRegion, roleAliasName);
    // Make sure tlog persists the device configuration
    kernel.getContext().waitForPublishQueueToClear();
    outStream.println("Created device configuration");
}
Also used : Path(java.nio.file.Path) CommitableFile(com.aws.greengrass.util.CommitableFile) DeviceConfiguration(com.aws.greengrass.deployment.DeviceConfiguration)

Aggregations

DeviceConfiguration (com.aws.greengrass.deployment.DeviceConfiguration)1 CommitableFile (com.aws.greengrass.util.CommitableFile)1 Path (java.nio.file.Path)1