1

I am developing an application in Java and it requires the user to have a policy document. The user enters the access key and secret key. I got AmazonIdentityManagementClient object using the credentials. My application requires "lambda:InvokeFunction". Can any one pls guide me how to check the user policy has lambdainvoke.

3
  • It's not a best practice to give out an AWS access key, secret key to end users of your application. What is the use case that requires this? Commented Jan 22, 2017 at 11:23
  • I want to start and stop instances using my application. Commented Jan 22, 2017 at 13:24
  • AmazonIdentityManagement iam = new AmazonIdentityManagementClient(credentials); ListAttachedUserPoliciesResult res = iam.listAttachedUserPolicies(req); is throwing Exception in thread "AWT-EventQueue-0" com.amazonaws.services.identitymanagement.model.AmazonIdentityManagementException: 1 validation error detected: Value null at 'userName' failed to satisfy constraint: Member must not be null (Service:AmazonIdentityManagement; Status Code: 400; Error Code: ValidationError; Request ID: 041cc807-e888-11e6-87da-1fcd46626f3e) at listAttachedUserPolicies Commented Feb 2, 2017 at 13:34

2 Answers 2

2

Try below code to get the attached policy as a string.

AmazonIdentityManagementAsync iam = AmazonIdentityManagementAsyncClientBuilder
                .standard()
                .withCredentials(new AWSStaticCredentialsProvider(
                        new BasicAWSCredentials("",
                                "")))
                .withRegion(Regions.fromName(""))
                .withClientConfiguration(getClientConfiguration()).build();

        ListAttachedUserPoliciesRequest pre = new ListAttachedUserPoliciesRequest();
        pre.setUserName(iam.getUser().getUser().getUserName());

        ListAttachedUserPoliciesResult re = iam.listAttachedUserPolicies(pre);
        re.getAttachedPolicies().forEach(p -> {
            GetPolicyRequest preq = new GetPolicyRequest();
            preq.setPolicyArn(p.getPolicyArn());
            GetPolicyResult r = iam.getPolicy(preq);
            GetPolicyVersionRequest req = new GetPolicyVersionRequest();
            req.setPolicyArn(p.getPolicyArn());
            req.setVersionId(r.getPolicy().getDefaultVersionId());
            GetPolicyVersionResult res = iam.getPolicyVersion(req);
            System.out.println(URLDecoder.decode(res.getPolicyVersion().getDocument()));
        });
Sign up to request clarification or add additional context in comments.

Comments

1

You can use AmazonIdentityManagementClient.listAttachedUserPolicies() to list the policies attached to a user. This will get you to a list of policy ARNs which you can pass to AmazonIdentityManagementClient.getPolicy().

1 Comment

AmazonIdentityManagement iam = new AmazonIdentityManagementClient(credentials); ListAttachedUserPoliciesResult res = iam.listAttachedUserPolicies(req); is throwing Exception in thread "AWT-EventQueue-0" com.amazonaws.services.identitymanagement.model.AmazonIdentityManagementException: 1 validation error detected: Value null at 'userName' failed to satisfy constraint: Member must not be null (Service:AmazonIdentityManagement; Status Code: 400; Error Code: ValidationError; Request ID: 041cc807-e888-11e6-87da-1fcd46626f3e) at listAttachedUserPolicies

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.