Search in sources :

Example 1 with CreateKeysAndCertificateResponse

use of software.amazon.awssdk.services.iot.model.CreateKeysAndCertificateResponse in project aws-greengrass-provisioner by awslabs.

the class BasicIotHelper method createKeysAndCertificate.

@Override
public KeysAndCertificate createKeysAndCertificate(GreengrassGroupName greengrassGroupName, String deviceName) {
    ioHelper.createDirectoryIfNecessary(getCredentialsDirectoryForGroupName(greengrassGroupName));
    // Let them know that they'll need to re-run the bootstrap script because the core's keys changed
    boolean isCore = CORE_DEVICE_NAME.equals(deviceName);
    String supplementalMessage = isCore ? "  If you have an existing deployment for this group you'll need to re-run the bootstrap script since the core certificate ARN will change." : "";
    log.info(String.join("", "- Creating new keys.", supplementalMessage));
    CreateKeysAndCertificateRequest createKeysAndCertificateRequest = CreateKeysAndCertificateRequest.builder().setAsActive(true).build();
    CreateKeysAndCertificateResponse createKeysAndCertificateResponse = iotClient.createKeysAndCertificate(createKeysAndCertificateRequest);
    KeysAndCertificate keysAndCertificate = KeysAndCertificate.from(createKeysAndCertificateResponse);
    writeKeysAndCertificateFile(keysAndCertificate, greengrassGroupName, deviceName);
    return keysAndCertificate;
}
Also used : CreateKeysAndCertificateRequest(software.amazon.awssdk.services.iot.model.CreateKeysAndCertificateRequest) CreateKeysAndCertificateResponse(software.amazon.awssdk.services.iot.model.CreateKeysAndCertificateResponse) KeysAndCertificate(com.awslabs.aws.greengrass.provisioner.data.KeysAndCertificate)

Example 2 with CreateKeysAndCertificateResponse

use of software.amazon.awssdk.services.iot.model.CreateKeysAndCertificateResponse in project aws-greengrass-nucleus by aws-greengrass.

the class DeviceProvisioningHelper method createThing.

/**
 * Create a thing with provided configuration.
 *
 * @param client     iotClient to use
 * @param policyName policyName
 * @param thingName  thingName
 * @return created thing info
 */
public ThingInfo createThing(IotClient client, String policyName, String thingName) {
    // Find or create IoT policy
    try {
        client.getPolicy(GetPolicyRequest.builder().policyName(policyName).build());
        outStream.printf("Found IoT policy \"%s\", reusing it%n", policyName);
    } catch (ResourceNotFoundException e) {
        outStream.printf("Creating new IoT policy \"%s\"%n", policyName);
        client.createPolicy(CreatePolicyRequest.builder().policyName(policyName).policyDocument("{\n  \"Version\": \"2012-10-17\",\n  \"Statement\": [\n    {\n" + "      \"Effect\": \"Allow\",\n      \"Action\": [\n" + "                \"iot:Connect\",\n                \"iot:Publish\",\n" + "                \"iot:Subscribe\",\n                \"iot:Receive\",\n" + "                \"greengrass:*\"\n],\n" + "      \"Resource\": \"*\"\n    }\n  ]\n}").build());
    }
    // Create cert
    outStream.println("Creating keys and certificate...");
    CreateKeysAndCertificateResponse keyResponse = client.createKeysAndCertificate(CreateKeysAndCertificateRequest.builder().setAsActive(true).build());
    // Attach policy to cert
    outStream.println("Attaching policy to certificate...");
    client.attachPolicy(AttachPolicyRequest.builder().policyName(policyName).target(keyResponse.certificateArn()).build());
    // Create the thing and attach the cert to it
    outStream.printf("Creating IoT Thing \"%s\"...%n", thingName);
    String thingArn = client.createThing(CreateThingRequest.builder().thingName(thingName).build()).thingArn();
    outStream.println("Attaching certificate to IoT thing...");
    client.attachThingPrincipal(AttachThingPrincipalRequest.builder().thingName(thingName).principal(keyResponse.certificateArn()).build());
    return new ThingInfo(thingArn, thingName, keyResponse.certificateArn(), keyResponse.certificateId(), keyResponse.certificatePem(), keyResponse.keyPair(), client.describeEndpoint(DescribeEndpointRequest.builder().endpointType("iot:Data-ATS").build()).endpointAddress(), client.describeEndpoint(DescribeEndpointRequest.builder().endpointType("iot:CredentialProvider").build()).endpointAddress());
}
Also used : CreateKeysAndCertificateResponse(software.amazon.awssdk.services.iot.model.CreateKeysAndCertificateResponse) ResourceNotFoundException(software.amazon.awssdk.services.iot.model.ResourceNotFoundException)

Example 3 with CreateKeysAndCertificateResponse

use of software.amazon.awssdk.services.iot.model.CreateKeysAndCertificateResponse in project aws-sdk-java-v2 by aws.

the class IotControlPlaneIntegrationTest method createCertificate_Returns_success.

@Test
public void createCertificate_Returns_success() {
    final CreateKeysAndCertificateRequest createReq = CreateKeysAndCertificateRequest.builder().setAsActive(true).build();
    CreateKeysAndCertificateResponse createResult = client.createKeysAndCertificate(createReq);
    Assert.assertNotNull(createResult.certificateArn());
    Assert.assertNotNull(createResult.certificateId());
    Assert.assertNotNull(createResult.certificatePem());
    Assert.assertNotNull(createResult.keyPair());
    certificateId = createResult.certificateId();
    client.updateCertificate(UpdateCertificateRequest.builder().certificateId(certificateId).newStatus(CertificateStatus.REVOKED).build());
}
Also used : CreateKeysAndCertificateRequest(software.amazon.awssdk.services.iot.model.CreateKeysAndCertificateRequest) CreateKeysAndCertificateResponse(software.amazon.awssdk.services.iot.model.CreateKeysAndCertificateResponse) Test(org.junit.Test)

Aggregations

CreateKeysAndCertificateResponse (software.amazon.awssdk.services.iot.model.CreateKeysAndCertificateResponse)3 CreateKeysAndCertificateRequest (software.amazon.awssdk.services.iot.model.CreateKeysAndCertificateRequest)2 KeysAndCertificate (com.awslabs.aws.greengrass.provisioner.data.KeysAndCertificate)1 Test (org.junit.Test)1 ResourceNotFoundException (software.amazon.awssdk.services.iot.model.ResourceNotFoundException)1