【问题标题】:Setting IAM Policy on a function using projects.locations.functions.setIamPolicy Gives error使用 projects.locations.functions.setIamPolicy 对函数设置 IAM 策略会出现错误
【发布时间】:2020-11-30 20:33:09
【问题描述】:

我正在尝试使用 projects.locations.functions.setIamPolicy 在我创建和部署的函数上设置 IAM 政策,但我不断收到以下错误

GaxiosError: Permission 'cloudfunctions.functions.setIamPolicy' denied on resource '

这是我在 Node.js 中的代码

    const { auth } = require('google-auth-library');
    const { google } = require('googleapis');
    const fs = require('fs');
    const path = require('path');

    function getCredentials() {
    
        const filePath = path.join(__dirname, 'mykeyfile.json');
        console.log(filePath);
        if (fs.existsSync(filePath)) {
            let rawdata = fs.readFileSync(filePath);
            let jsonData = JSON.parse(rawdata);
            return jsonData;
        }
        return null;
    
    }
    async function setPolicy() {
    
    
        try {
    
            const credentials = getCredentials();
            if (!credentials)
                return;
    
            const client = auth.fromJSON(credentials);
            client.scopes = ['https://www.googleapis.com/auth/cloud-platform'];
    
            const cloudfunctions = await google.cloudfunctions({
                version: 'v1',
                auth: client
            });
    
            const request = {
                // REQUIRED: The resource for which the policy is being specified.
                // See the operation documentation for the appropriate value for this field.
                resource_: "projects/{myprorjectid}/locations/{zone}/functions/function-1",
    
                resource: {
                    "policy": {
                        "etag": "BwWP3fXnMuQ=",
                        "version": 1,
                        "bindings": [
                            {
                                "members": [
                                    "allUsers"
                                ],
                                "role": "roles/cloudfunctions.invoker"
                            }
                        ]
                    }
                },
    
                auth: client,
            };
    
                const response = (await cloudfunctions.projects.locations.functions.setIamPolicy(request)).data;
               
                console.log(JSON.stringify(response, null, 2));
         
    
            
        } catch (err) {
    
            console.log(err);
        }
    }
    setIamPolicy();

非常感谢任何帮助。对选项之类的命令不感兴趣。必须在 Node 中完成。

【问题讨论】:

    标签: node.js google-api google-cloud-functions google-authentication google-iam


    【解决方案1】:

    您在代码上使用的服务帐户似乎没有正确的权限。该错误与doc 类似,解决方案是将Project OwnerCloud Functions Admin 角色添加到您的服务帐户,因为两者都包含cloudfunctions.functions.setIamPolicy 权限。

    【讨论】:

      猜你喜欢
      • 2017-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-26
      • 2021-05-13
      • 2020-10-22
      • 2021-12-16
      • 2020-12-15
      相关资源
      最近更新 更多