harbor icon indicating copy to clipboard operation
harbor copied to clipboard

Using the robot account to call the vulnerability api.

Open derekcha opened this issue 2 years ago • 6 comments

We want to use the robot account to call the vulnerability api. So I created a system robot account with the following permission.

{
   "level":"system",
   "permissions": [
      {
         "kind":"system",
         "namespace":"/",
         "access":[
            {
               "resource":"*",
               "action":"list"
            }
         ]
      }
   ]
}

But when I call the api(/projects/{project_name}/repositories/{repository_name}/artifacts/{reference}/additions/vulnerabilities) I get a FORBIDDEN error.

What permission do I need to give so that the error doesn't occur?

Please answer. :)

derekcha avatar Oct 27 '22 07:10 derekcha

the RABC for this api

/projects/{project_name}/repositories/{repository_name}/artifacts/{reference}/additions/vulnerabilities

is here:

  • https://github.com/goharbor/harbor/blob/v2.6.1/src/server/v2.0/handler/artifact.go#L398

It seems this API requires rbac.ActionRead, while the permissions.access of this robot account is "action":"list".

zyyw avatar Oct 31 '22 02:10 zyyw

@zyyw hi. Thanks for the reply Because of your answer, I added a read action like below.

"permissions": [
        {
            "access": [
                {
                    "action": "list",
                    "resource": "*"
                },
                {
                    "action": "read",
                    "resource": "*"
                }
            ],
            "kind": "system",
            "namespace": "/"
        }
    ]

But still getting forbidden error. Am I setting something wrong?

derekcha avatar Oct 31 '22 05:10 derekcha

please refer to this to create the robot. https://github.com/goharbor/harbor/issues/14145#issuecomment-781006533

wy65701436 avatar Nov 01 '22 14:11 wy65701436

To fetch the cve scan results & additions over API (and with global system robot accounts) you should take a look at https://github.com/goharbor/harbor/blob/5cd5bcaee44e9f57c96ac8327009bcffb95ac7a5/src/common/rbac/const.go#L58 and https://github.com/goharbor/harbor/blob/v2.6.1/src/server/v2.0/handler/artifact.go#L398

Note: the required RBAC settings (snippet, see below) does not appear in the Harbor UI and can not be managed over the portal (version ?<=2.5.4). One of the options: use of harbor terraform provider, resource harbor_robot_account, kind : "project"., see https://github.com/goharbor/terraform-provider-harbor/blob/master/docs/resources/robot_account.md. Robot accounts with extended permissions on registry projects can then invoke Harbor API methods to fetch the list of vulnerabilities, see Harbor swagger.yml API spec and the method /projects/{project_name}/repositories/{repository_name}/artifacts/{reference}/additions/vulnerabilities

   ## kind: project, system robot permissions
    access {
      action   = "read"
      resource = "artifact-addition"
    }
    access {
      action   = "read"
      resource = "artifact-label"
    }

mwhcp avatar Nov 01 '22 15:11 mwhcp

@wy65701436 The account has already been created by calling the robots post api. But when I look up vulnerabilities, I get a permission error

derekcha avatar Nov 16 '22 11:11 derekcha

@mwhcp

access {
      action   = "read"
      resource = "artifact-addition"
    }
    access {
      action   = "read"
      resource = "artifact-label"
    }

Are you saying you need a robot account with this permission? I think it is the same whether it is created with terraform or created with api, and an error occurs even when I give permission as below.

"level": "system",
    "name": "r-sec-collector",
    "permissions": [
      {
        "access": [
          {
            "action": "read",
            "resource": "artifact-addition"
          },
          {
            "action": "read",
            "resource": "artifact-label"
          }
        ],
        "kind": "system",
        "namespace": "/"
      }

derekcha avatar Nov 16 '22 11:11 derekcha

I can confirm that adding READ for artifact-addition works.

I had to use the API: robots/{robot_id} to update the robot account permissions.

arielmorelli avatar Jan 02 '23 12:01 arielmorelli

This issue is being marked stale due to a period of inactivity. If this issue is still relevant, please comment or remove the stale label. Otherwise, this issue will close in 30 days.

github-actions[bot] avatar Mar 04 '23 09:03 github-actions[bot]

Hello all,

I'm having trouble with the robot account. Created in Harbor UI => covered all projects (read), permissions updated via API to export CVEs. Permissions see attached file: robot_permissions.txt

Got from python: requests.exceptions.HTTPError: 403 Client Error: Forbidden for url: https://fqdn-of-local-harbor/api/v2.0/export/cve

Does the robot still lack any authorization?

Regards, temirg. PS: Harbor v2.6.4

temirg avatar Mar 14 '23 09:03 temirg

This issue is being marked stale due to a period of inactivity. If this issue is still relevant, please comment or remove the stale label. Otherwise, this issue will close in 30 days.

github-actions[bot] avatar May 15 '23 09:05 github-actions[bot]

Not stale

heresie avatar Jun 05 '23 15:06 heresie

I can confirm that adding READ for artifact-addition works.

I had to use the API: robots/{robot_id} to update the robot account permissions.

This worked! Fetch the robot details with GET, then use the response json in PUT but with the added permission: { "resource": "artifact-addition", "action": "read" }

andreasbolstad avatar Jun 16 '23 12:06 andreasbolstad

I have a robot created over API:

{
  "disable": false,
  "name": "cve-export",
  "level": "system",
  "duration": 0,
  "description": "robot for cve export created over API",
  "permissions": [
    {
      "access": [
        {
          "action": "*",
          "resource": "*"
        }
      ],
      "kind": "system",
      "namespace": "/"
    },
      {
        "access": [
          {
            "action": "*",
            "resource": "*"
          }
        ],
        "kind": "project",
        "namespace": "proj1"
      }
  ]
}

According to https://goharbor.io/blog/harbor-2.6/.

Lets imagine that id of proj1 is 123 and secret from cve-export is qwerty1234567:

curl -X POST 'https://harbor.domain/api/v2.0/export/cve' -H 'content-type: application/json' -H 'x-scan-data-type: application/vnd.security.vulnerability.report; version=1.1' --data-raw '{"projects":[123],"labels":[],"repositories":null,"tags":null,"cveIds":null}' -u 'cve-export:qwerty1234567'

and i got:

{"errors":[{"code":"FORBIDDEN","message":"forbidden"}]}

where i was wrong?

I found one way which was accepted by harbor is authorize over cookies from my browser instead of basic auth header (flag -u at curl), but is useless in my case.

I use Harbor Version v2.8.0-89ef156d.

hardenchant avatar Jul 27 '23 13:07 hardenchant

harbor does not support * as a kind of resource or action, you have to specify it. @hardenchant

wy65701436 avatar Aug 03 '23 11:08 wy65701436

harbor does not support * as a kind of resource or action, you have to specify it. @hardenchant

Anyway, i tried specify a lot of combinations with permissions. I have harbor with oidc, and when i logged as admin user i can export cve csv at UI with cookie auth, but when i try to generate cve csv over api with user cli secret or robot token with system/project permissions – i got 403. Where i can find example set of permissions for cve export?

hardenchant avatar Aug 03 '23 13:08 hardenchant

@wy65701436 it could be easy: Developers write step-by-step instructions with examples and screenshots on how to create and authorize a robot account for the CVE export. This guide becomes part of the Harbor documentation.

This means that future issues / inquiries are no longer necessary.

Best Regards, temirg.

temirg avatar Aug 09 '23 07:08 temirg

Now I also ran into this issue - yeah its sub-optimal that there is no clear indication in swagger for the required permission nor is it usable that the permission for artifact-addition can't be set via UI

hoerup avatar Sep 27 '23 08:09 hoerup

are there any restrictions so that "artifact-addition" can only be added to system level robot accounts and not to project level ?

hoerup avatar Oct 01 '23 11:10 hoerup

This issue is being marked stale due to a period of inactivity. If this issue is still relevant, please comment or remove the stale label. Otherwise, this issue will close in 30 days.

github-actions[bot] avatar Dec 01 '23 09:12 github-actions[bot]

Still relevant

hoerup avatar Dec 01 '23 09:12 hoerup

The problem was resolved for me by adding the {"action":"read","resource":"artifact-addition"} permission to the robot account's permission list. But, is it possible to add this permission to the Harbor UI? Because it's a little hard to add or update the permission list through the API, and sometimes I don't know which permission needs to be added for a specific API call

mlkmhd avatar Dec 05 '23 03:12 mlkmhd

We've already add artifact-addition in the harbor UI of robot creation in v2.10.0. Please try with it, and reopen if it is still exist.

wy65701436 avatar Jan 08 '24 05:01 wy65701436