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.comIAMlatestAPIReferenceAPI_PutUserPolicy.html。
你可能有一个大小写敏感度的问题。Statement
必须大写。
补充说明:避免使用内联策略被认为是良好的做法。你可以创建和附加管理策略来代替。此外,根据 CIS AWS基础基准在此,建议IAM策略直接应用于组和角色,而不是用户。
这样做的理由是,随着用户数量的增加,在组或角色层面分配特权可以降低访问管理的复杂性。降低访问管理的复杂度,可以减少负责人无意中获得或保留过度特权的机会。
'statement'应该是'Statement',你还应该有 "Version"。"2012-10-17 "与 "声明 "处于同一层次上