0

AWS 新手。我正在尝试使用 putUserPolicy API 向 IAM 用户添加内联策略,如下所示。运行到 Malformedpolicydocument 作为错误代码和策略中的语法错误作为错误消息

// Attaches the policy document to the IAM user userName in the account
    async putUserPolicy(userName: string, roleArn: string) {
        let userPolicyData: any = null;

        try {
            // creates the policy document
            const policyDocumentForUser = this.createUserPolicyDocument(roleArn);
            const trustPolicyParamsForUser = {
                PolicyDocument: JSON.stringify(policyDocumentForUser),
                PolicyName: 'userPolciy',
                UserName: userName
        };

        // attaching the policy document to the IAM user
        userPolicyData = await this.iam.putUserPolicy(trustPolicyParamsForUser).promise();
        this.logger.info(`Successfully created user policy for '${userName}'`);

        } catch (error) {
            this.logger.error(`Unable to create user policy role`, error);
            throw error;
        }

    }

    private createUserPolicyDocument(roleArn: string) {

        const policyDocument = {
            'statement': [
                {
                    'Action': 'sts:AssumeRole',
                    'Resource': roleArn,
                    'Effect': 'Allow'
                }
            ]
        };
        this.logger.debug('policyDocument:', policyDocument);

        return policyDocument;
    }

也尝试将版本提供给策略,但观察到相同的错误。我一直在为我的代码库中的所有策略文档使用单引号。

添加参考文档:https ://docs.aws.amazon.com/IAM/latest/APIReference/API_PutUserPolicy.html

4

2 回答 2

1

您可能有区分大小写的问题。Statement必须大写。

作为旁注:它考虑了避免内联策略的良好做法。您可以改为创建和附加托管策略。此外,根据CIS AWS Foundations Benchmark,建议将 IAM 策略直接应用于组和角色,而不是用户。

这背后的基本原理是,随着用户数量的增长,在组或角色级别分配权限会降低访问管理的复杂性。降低访问管理复杂性可以反过来减少主体无意接收或保留过多特权的机会。

于 2020-05-15T11:01:25.820 回答
0

'statement' 应该是 'Statement' 并且您还应该有 "Version": "2012-10-17" 与 statement 处于同一级别

于 2020-05-15T11:00:22.647 回答