aws-amplify / aws-sdk-android

AWS SDK for Android. For more information, see our web site:
https://docs.amplify.aws
Other
1.03k stars 548 forks source link

AttachPolicyRequest using programmatically not working in android #1812

Closed amarpulli1994 closed 4 years ago

amarpulli1994 commented 4 years ago

Hi All,

I am trying to do attaching policy request for cognito identity user using programmatically instead using aws cli. Through aws cli attaching successfully and also working fine. But programmatically not working. It gives exception like Cognito Identity not configured . This is my sample code

  CognitoUserSession cognitoUserSession = AppHelper.getCurrSession();

                String idToken = cognitoUserSession.getIdToken().getJWTToken();

                Map<String, String> logins = new HashMap<String, String>();

                logins.put("cognito-idp.ap-south-1.amazonaws.com/xx-xxxx-x_xxxxxxxxx", idToken);

                credentialsProvider = new CognitoCachingCredentialsProvider(
                        getApplicationContext(),
                        "ap-south-1:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxdxxx", // Identity pool ID
                        Regions.AP_SOUTH_1 // Region
                );

                credentialsProvider.setLogins(logins);
                AttachPolicyRequest attachPolicyReq = new AttachPolicyRequest();
                attachPolicyReq.withPolicyName("MyIotPolicy");
                attachPolicyReq.withTarget(credentialsProvider.getIdentityId());

                AWSIotClient mIotAndroidClient = new AWSIotClient(AWSMobileClient.getInstance());
                mIotAndroidClient.setRegion(Region.getRegion(Regions.AP_SOUTH_1)); 
                mIotAndroidClient.attachPolicy(attachPolicyReq);

I am getting below exception

Exception : com.amazonaws.AmazonClientException: Cognito Identity not configured

By using below command is working fine.

aws iot attach-principal-policy --policy-name <iot-policy-name> --principal <cognito-identity-id> Please guide me how to attach policy request through programmatically.

Thanks in Advance.

kirantpatil commented 4 years ago

Please check this code https://github.com/aws-amplify/aws-sdk-android/issues/600#issuecomment-444793515


import com.amazonaws.services.iot.AWSIotClient;
import com.amazonaws.services.iot.model.AttachPolicyRequest;

AttachPolicyRequest attachPolicyReq = new AttachPolicyRequest();
attachPolicyReq.setPolicyName("myIOTPolicy"); // name of your IoT AWS policy
attachPolicyReq.setTarget(AWSMobileClient.getInstance().getIdentityId());
AWSIotClient mIotAndroidClient = new AWSIotClient(AWSMobileClient.getInstance());
mIotAndroidClient.setRegion(Region.getRegion("MyRegion")); // name of your aws region such as "us-east-1"
mIotAndroidClient.attachPolicy(attachPolicyReq);
amarpulli1994 commented 4 years ago

@kirantpatil I tried its not working

kirantpatil commented 4 years ago

Please try this https://github.com/aws-amplify/aws-sdk-android/issues/1552#issuecomment-612866584

amarpulli1994 commented 4 years ago

@kirantpatil There is no attachpolicyrequest.

kirantpatil commented 4 years ago

@rjuliano, Any updates on this issue ?

Why no one is answering to this question, we even went and purchased aws developer subscription to my surprise they are again asking us to go back to github.

What should we do, since we are struck here ?

Thank you.

kirantpatil commented 4 years ago

Dear @anuragdce,

Did you face this issue ?

Thanks.

kirantpatil commented 4 years ago

I think you need to add below code. Found at https://stackoverflow.com/questions/44244375/aws-iot-android-application-over-mqtt-throws-mqttexception-0-java-io-ioexcep

AmazonCognitoIdentity cognitoIdentity = new AmazonCognitoIdentityClient(credentialsProvider);
GetIdRequest getIdReq = new GetIdRequest();
getIdReq.setLogins(logins); //or if you have already set provider logins just use credentialsProvider.getLogins()
getIdReq.setIdentityPoolId(COGNITO_POOL_ID);
GetIdResult getIdRes = cognitoIdentity.getId(getIdReq);
amarpulli1994 commented 4 years ago

Hi all, After spent lot of time..found mistake. In my case the attach policy request api by default it will take us-east-1. So here we have to set end point and as well as region also. Then only it will work. Here is the sample code.

 AttachPrincipalPolicyRequest principalPolicyRequest = new AttachPrincipalPolicyRequest();
                principalPolicyRequest.setPolicyName("iotPolicy");
                principalPolicyRequest.setPrincipal(credentialsProvider.getIdentityId());

                AWSIotClient awsIotClient = new AWSIotClient(credentialsProvider);
                awsIotClient.setEndpoint(CUSTOMER_SPECIFIC_ENDPOINT);
                awsIotClient.setRegion(Region.getRegion(Regions.AP_SOUTH_1));
                awsIotClient.attachPrincipalPolicy(principalPolicyRequest);
xueyangp commented 3 months ago

Hi all, After spent lot of time..found mistake. In my case the attach policy request api by default it will take us-east-1. So here we have to set end point and as well as region also. Then only it will work. Here is the sample code.

 AttachPrincipalPolicyRequest principalPolicyRequest = new AttachPrincipalPolicyRequest();
                principalPolicyRequest.setPolicyName("iotPolicy");
                principalPolicyRequest.setPrincipal(credentialsProvider.getIdentityId());

                AWSIotClient awsIotClient = new AWSIotClient(credentialsProvider);
                awsIotClient.setEndpoint(CUSTOMER_SPECIFIC_ENDPOINT);
                awsIotClient.setRegion(Region.getRegion(Regions.AP_SOUTH_1));
                awsIotClient.attachPrincipalPolicy(principalPolicyRequest);

I have been trying to fix my issue for days. You are awesome. Thank you!