aws-sdk-android icon indicating copy to clipboard operation
aws-sdk-android copied to clipboard

How to create userGroup in AWS ?

Open shabana0508 opened this issue 7 years ago • 11 comments

State your question Hi, I am trying to create UserGroup in AWS using below code,But getting Access Denied to this user error.

Which AWS Services are you utilizing? Cognito

Provide code snippets (if applicable) val stsClient = AWSSecurityTokenServiceClient(AWSMobileClient.getInstance().awsCredentials) stsClient.endpoint = "sts.us-east-2.amazonaws.com" val acc = AmazonCognitoIdentityClient(AWSMobileClient.getInstance().awsCredentials) val credentialsProvider = CognitoCachingCredentialsProvider( applicationContext, "614399667198", "us-east-2:efb7b136-ca4f-4ab5-9407-4e10701b9fb7", "arn:aws:iam::614399667198:role/Cognito_picapp_MOBILEHUB_108066410Unauth_Role", "arn:aws:iam::614399667198:role/picapp_auth_MOBILEHUB_108066410", acc, stsClient )

val groupReq = CreateGroupRequest() groupReq.groupName = "ABC" groupReq.userPoolId = "us-east-2_JUjVI2R8c" groupReq.description = "abx" val client = AmazonCognitoIdentityProviderClient(credentialsProvider ) client.createGroup(groupReq)

Environment(please complete the following information):

  • SDK Version: 2.7.0

Device Information (please complete the following information):

  • Device: Pixel XL, Simulator
  • Android Version: Pie
  • Specific to simulators: All devices

Error Info com.amazonaws.AmazonServiceException: User: arn:aws:sts::614399667198:assumed-role/picapp_auth_MOBILEHUB_108066410/CognitoIdentityCredentials is not authorized to perform: cognito-idp:CreateGroup on resource: arn:aws:cognito-idp:us-east-1:614399667198:userpool/us-east-2_JUjVI2R8c (Service: AmazonCognitoIdentityProvider; Status Code: 400; Error Code: AccessDeniedException; Request ID: 2960e306-3c26-11e9-b599-735aaedfd566)

If you need help with understanding how to implement something in particular then we suggest that you first look into our developer guide. You can also simplify your process of creating an application, as well as the associated backend setup by using the Amplify CLI.

shabana0508 avatar Mar 02 '19 14:03 shabana0508

@shabana0508 Thanks for reaching out. Can you use AWSMobileClient instance to initialize the client as val client = AmazonCognitoIdentityProviderClient(AWSMobileClient.getInstance()) and also possibly upgrade to the latest version of the SDK 2.12.2. and check if the issue persists?

desokroshan avatar Mar 02 '19 15:03 desokroshan

@shabana0508 maybe check the permissions for the auth role (picapp_auth_MOBILEHUB_108066410).

supreme96 avatar Mar 03 '19 05:03 supreme96

@desokroshan Thanks for reply, i have upgraded sdk to 2.12.2 and used AWSMobileClient instance to initialize the client but still issue is not solved.I have checked userpoolId it's same as AWS Console But still i am getting below error Caused by: com.amazonaws.services.cognitoidentityprovider.model.ResourceNotFoundException: User pool us-east-2_JUjVI2R8c does not exist.

shabana0508 avatar Mar 04 '19 05:03 shabana0508

Can you please check if a user pool with id us-east-2_JUjVI2R8c does exist in the backend?

desokroshan avatar Mar 04 '19 18:03 desokroshan

@shabana0508 Sorry for the inconvenience caused.

Thank you for the additional details.

  1. Can you check if the IAM role (Auth and Unauth role) of your Cognito Identity pool has a IAM policy for 'createGroup' ? This could be the reason of getting AccessDenied.

  2. Can you check the AWS region that is being passed through the code and the region where your resource exists? This could be the reason for getting ResourceNotFoundException.

  3. When you need to pass the credentials provider to the low-level service clients you are instantiating, you could follow one of the following ways:

3.1) Create a AWSCredentialsProvider object (in this case CognitoCachingCredentialsProvider) and use it in the constructor of the low-level service clients.

val credentialsProvider = CognitoCachingCredentialsProvider(
applicationContext,
"xxxxx",
"us-east-2:redacted",
"arn:aws:iam::redacted:role/redacted",
"arn:aws:iam::redacted:role/redacted",
acc, stsClient)

val stsClient = AWSSecurityTokenServiceClient(credentialsProvider)
stsClient.endpoint = "sts.us-east-2.amazonaws.com"

val acc = AmazonCognitoIdentityClient(credentialsProvider)
val groupReq = CreateGroupRequest()
groupReq.groupName = "ABC"
groupReq.userPoolId = "redacted"
groupReq.description = "abx"
val client = AmazonCognitoIdentityProviderClient(credentialsProvider)
client.createGroup(groupReq)

3.2) You can use AWSMobileClient which creates the CognitoCachingCredentialsProvider based on the information available in awsconfiguration.json. Beware of the fact that the CognitoCachingCredentialsProvider created by AWSMobileClient will be different from the object that you have constructed yourself.

Since you are integrating with STS, I would advice using 3.1. Just to confirm, can you describe your use-case that you are trying to accomplish with Cognito and STS?

rubyrose-alexis avatar Mar 04 '19 20:03 rubyrose-alexis

Can you please check if a user pool with id us-east-2_JUjVI2R8c does exist in the backend?

Yes it's available. i have used same user pool id for SignUp and SignIn User and it's working fine.

shabana0508 avatar Mar 05 '19 05:03 shabana0508

I have assigned policy for creatGroup To I AM Role. And not passing Region from my code still getting userpool doesn't exist error

shabana0508 avatar Mar 05 '19 06:03 shabana0508

@shabana0508 Since your identity-pool is in us-east-2, I would advice you to do the following:

val credentialsProvider = CognitoCachingCredentialsProvider(
applicationContext,
"identity-pool-id",
Regions.US_EAST_2)

val groupReq = CreateGroupRequest()
groupReq.groupName = "ABC"
groupReq.userPoolId = "redacted"
groupReq.description = "abx"

val client = AmazonCognitoIdentityProviderClient(credentialsProvider)
client.createGroup(groupReq)

Do you have a use-case for creating a STS client? If not, I would suggest to use the above constructor for CognitoCachingCredentialsProvider.

rubyrose-alexis avatar Mar 05 '19 06:03 rubyrose-alexis

@kvasukib While i am using CognitoCachingCredentialsProvider then getting AccessDeniedError.

shabana0508 avatar Mar 05 '19 07:03 shabana0508

@shabana0508 Thank you for the response. Can you give us the requestId, AWS region and the policy that you have attached in your IAM role?

rubyrose-alexis avatar Mar 07 '19 21:03 rubyrose-alexis

This issue has been automatically closed because of inactivity. Please open a new issue if are still encountering problems.

stale[bot] avatar Mar 25 '19 16:03 stale[bot]