nisarg parekh
nisarg parekh

Reputation: 437

Can not attach Aws Iot Policy with Certificate

 AttachPrincipalPolicyRequest policyAttachRequest = new AttachPrincipalPolicyRequest();
 policyAttachRequest.setPolicyName(AWS_IOT_POLICY_NAME);                            

 policyAttachRequest.setPrincipal(createKeysAndCertificateResult.getCertificateArn());                 
 mIotAndroidClient.attachPrincipalPolicy(policyAttachRequest);

Here I want to attach a policy with aws certificate in aws iot. For this operation i'm using attachPrincipalPolicy() function. But this function is depreacted in 'com.amazonaws:aws-android-sdk-iot:2.6.29' sdk version. so each time after certificate created i need to manually attach policy with certificate. Is there any other method for attach policy with aws Certificate?

Upvotes: 2

Views: 1419

Answers (2)

Valeriy K.
Valeriy K.

Reputation: 2904

AWSIot awsIotClient = AWSIotClientBuilder.defaultClient();
awsIotClient.createPolicy(new CreatePolicyRequest().withPolicyDocument(policy).withPolicyName(policyName));
awsIotClient.attachPolicy(new AttachPolicyRequest().withPolicyName(policyName).withTarget("arn:aws:iot:<region>:<client_ID>:cert/<certificate_ID"

Upvotes: 1

Ashvin solanki
Ashvin solanki

Reputation: 4809

Ref : https://docs.aws.amazon.com/iot/latest/apireference/API_AttachPrincipalPolicy.html

AttachPrincipalPolicy

Attaches the specified policy to the specified principal (certificate or other credential).

Note: This API is deprecated. Please use AttachPolicy instead.

Edit 2

AttachPrincipalPolicyRequest attachPolicyReq = new AttachPrincipalPolicyRequest(); //in docs it called AttachPolicyRequest but it`s wrong
attachPolicyReq.setPolicyName("allAllowed"); //name of your IOTAWS policy
attachPolicyReq.setPrincipal(getIdRes.getIdentityId());
new AWSIotClient(credentialsProvider).attachPrincipalPolicy(attachPolicyReq);

For More Information AWS IoT Android application over MQTT throws MqttException (0) - java.io.IOException: Already connected

https://github.com/awslabs/aws-sdk-android-samples/issues/92

Upvotes: 0

Related Questions