Skip to content

[NOTE] API "GET /projects" with an ambiguous parameter --public #18

@moooofly

Description

@moooofly

登录状态问题

  • 如果执行该 API 前,未进行 login 登录,则只能获取到 "public": "true" 的 project 信息;
  • 如果执行该 API 前,进行了 login 登录,则除了能获取到 "public": "true" 的 project 信息外,还能获取到属于该 login 用户的 "public": "false" 的 project 信息(即 private project);

二值语义问题

在 swagger file 中,将 public 参数定义为 boolean 类型,仅具有 true 和 false 两种值,分别对应了 public 和 private ;然而,在实际使用中,还存在第三种情况,即在一次 API 调用中同时获取 public 和 private 类型的 project ;

基于上述考虑,在具体实现中,将 public 参数类型从 boolean 调整成了 string 类型:

  1. 若为 true 或 TRUE 则仅返回 public 项目;
  2. 若为 false 或 FALSE 则仅返回 private 项目;
  3. 若不指定 public 参数,则同时返回 public 和 private 项目;

image

public 类型问题

在 swagger file 中,public 参数的类型为 boolean ,但实际测试时发现,可以使用 1 和 0 分别对应 true 和 false 成功执行;

实验说明

帮助信息

[#322#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$./harborctl project list -h
This endpoint returns all projects created by Harbor, and can be filtered by project name.

Usage:
  harborctl project list [flags]

Flags:
  -h, --help              help for list
  -n, --name string       The name of project.
  -o, --owner string      The name of project owner.
  -p, --page int32        The page nubmer, default is 1. (default 1)
  -s, --page_size int32   The size of per page, default is 10, maximum is 100. (default 10)
  -k, --public string     The project is public or private.

Global Flags:
      --config string   config file (default is $HOME/, working dir (.), and ./conf dir)
[#323#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$

未登录

此时无法获取到 private project

[#320#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$./harborctl logout
==> GET https://localhost/log_out
<== Rsp Cookies: [beegosessionID=; Path=/; Expires=Tue, 30 Oct 2018 05:37:42 GMT; Max-Age=0; HttpOnly; Secure]
<== Rsp Status: 200 OK
<== Rsp Body:
[#321#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$
[#321#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$
[#321#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$./harborctl project list -s 50 -k false
==> GET https://localhost/api/projects?name=&public=false&owner=&page=1&page_size=50
Error: open conf/.cookie.yaml: no such file or directory
[#322#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$

已登录

仅获取 private project

[#323#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$./harborctl login -u admin
Password:
==> POST https://localhost/login
<== Cookies: [beegosessionID=94da0f3f5556b1b96b7f874aad213677; Path=/; HttpOnly; Secure]
<== Rsp Status: 200 OK
<== Rsp Body:
[#324#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$
[#324#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$./harborctl project list -s 50 -k false
==> GET https://localhost/api/projects?name=&public=false&owner=&page=1&page_size=50
<==
<== Rsp Status: 200 OK
<== Rsp Body: [
  {
    "project_id": 96,
    "owner_id": 1,
    "name": "ppp",
    "creation_time": "2018-10-30T03:53:00Z",
    "update_time": "2018-10-30T03:53:00Z",
    "deleted": 0,
    "owner_name": "",
    "togglable": true,
    "current_user_role_id": 1,
    "repo_count": 0,
    "metadata": {
      "public": "false"
    }
  }
]
[#325#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$

不使用 false ,改为使用 0 同样可以仅获取 private project;

[#325#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$./harborctl project list -s 50 -k 0
==> GET https://localhost/api/projects?name=&public=0&owner=&page=1&page_size=50
<==
<== Rsp Status: 200 OK
<== Rsp Body: [
  {
    "project_id": 96,
    "owner_id": 1,
    "name": "ppp",
    "creation_time": "2018-10-30T03:53:00Z",
    "update_time": "2018-10-30T03:53:00Z",
    "deleted": 0,
    "owner_name": "",
    "togglable": true,
    "current_user_role_id": 1,
    "repo_count": 0,
    "metadata": {
      "public": "false"
    }
  }
]
[#326#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$

使用 true 和 1 均可以用于仅获取 public project

[#327#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$./harborctl project list -s 5 -k 1
==> GET https://localhost/api/projects?name=&public=1&owner=&page=1&page_size=5
<==
<== Rsp Status: 200 OK
<== Rsp Body: [
  {
    "project_id": 1,
    "owner_id": 1,
    "name": "library",
    "creation_time": "2018-06-10T03:14:43Z",
    "update_time": "2018-06-10T03:14:43Z",
    "deleted": 0,
    "owner_name": "",
    "togglable": true,
    "current_user_role_id": 1,
    "repo_count": 0,
    "metadata": {
      "public": "true"
    }
  },
  {
    "project_id": 95,
    "owner_id": 1,
    "name": "moooofly",
    "creation_time": "2018-10-29T12:05:31Z",
    "update_time": "2018-10-29T12:05:31Z",
    "deleted": 0,
    "owner_name": "",
    "togglable": true,
    "current_user_role_id": 1,
    "repo_count": 0,
    "metadata": {
      "public": "true"
    }
  },
  {
    "project_id": 92,
    "owner_id": 1,
    "name": "t1",
    "creation_time": "2018-08-12T14:03:10Z",
    "update_time": "2018-08-12T14:03:10Z",
    "deleted": 0,
    "owner_name": "",
    "togglable": true,
    "current_user_role_id": 1,
    "repo_count": 0,
    "metadata": {
      "public": "true"
    }
  },
  {
    "project_id": 82,
    "owner_id": 1,
    "name": "temp_1",
    "creation_time": "2018-08-11T03:09:43Z",
    "update_time": "2018-08-11T03:09:43Z",
    "deleted": 0,
    "owner_name": "",
    "togglable": true,
    "current_user_role_id": 1,
    "repo_count": 1,
    "metadata": {
      "public": "true"
    }
  },
  {
    "project_id": 91,
    "owner_id": 1,
    "name": "temp_10",
    "creation_time": "2018-08-11T03:10:28Z",
    "update_time": "2018-08-11T03:10:28Z",
    "deleted": 0,
    "owner_name": "",
    "togglable": true,
    "current_user_role_id": 1,
    "repo_count": 1,
    "metadata": {
      "public": "true"
    }
  }
]
[#328#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$
[#328#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$./harborctl project list -s 5 -k true
==> GET https://localhost/api/projects?name=&public=true&owner=&page=1&page_size=5
<==
<== Rsp Status: 200 OK
<== Rsp Body: [
  {
    "project_id": 1,
    "owner_id": 1,
    "name": "library",
    "creation_time": "2018-06-10T03:14:43Z",
    "update_time": "2018-06-10T03:14:43Z",
    "deleted": 0,
    "owner_name": "",
    "togglable": true,
    "current_user_role_id": 1,
    "repo_count": 0,
    "metadata": {
      "public": "true"
    }
  },
  {
    "project_id": 95,
    "owner_id": 1,
    "name": "moooofly",
    "creation_time": "2018-10-29T12:05:31Z",
    "update_time": "2018-10-29T12:05:31Z",
    "deleted": 0,
    "owner_name": "",
    "togglable": true,
    "current_user_role_id": 1,
    "repo_count": 0,
    "metadata": {
      "public": "true"
    }
  },
  {
    "project_id": 92,
    "owner_id": 1,
    "name": "t1",
    "creation_time": "2018-08-12T14:03:10Z",
    "update_time": "2018-08-12T14:03:10Z",
    "deleted": 0,
    "owner_name": "",
    "togglable": true,
    "current_user_role_id": 1,
    "repo_count": 0,
    "metadata": {
      "public": "true"
    }
  },
  {
    "project_id": 82,
    "owner_id": 1,
    "name": "temp_1",
    "creation_time": "2018-08-11T03:09:43Z",
    "update_time": "2018-08-11T03:09:43Z",
    "deleted": 0,
    "owner_name": "",
    "togglable": true,
    "current_user_role_id": 1,
    "repo_count": 1,
    "metadata": {
      "public": "true"
    }
  },
  {
    "project_id": 91,
    "owner_id": 1,
    "name": "temp_10",
    "creation_time": "2018-08-11T03:10:28Z",
    "update_time": "2018-08-11T03:10:28Z",
    "deleted": 0,
    "owner_name": "",
    "togglable": true,
    "current_user_role_id": 1,
    "repo_count": 1,
    "metadata": {
      "public": "true"
    }
  }
]
[#329#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$

在未指定 public 值的情况下,同时获取 public 和 private project 信息;

[#339#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$./harborctl project list -s 50
==> GET https://localhost/api/projects?name=&public=&owner=&page=1&page_size=50
<==
<== Rsp Status: 200 OK
<== Rsp Body: [
  {
    "project_id": 1,
    "owner_id": 1,
    "name": "library",
    "creation_time": "2018-06-10T03:14:43Z",
    "update_time": "2018-06-10T03:14:43Z",
    "deleted": 0,
    "owner_name": "",
    "togglable": true,
    "current_user_role_id": 1,
    "repo_count": 0,
    "metadata": {
      "public": "true"
    }
  },
  {
    "project_id": 95,
    "owner_id": 1,
    "name": "moooofly",
    "creation_time": "2018-10-29T12:05:31Z",
    "update_time": "2018-10-29T12:05:31Z",
    "deleted": 0,
    "owner_name": "",
    "togglable": true,
    "current_user_role_id": 1,
    "repo_count": 0,
    "metadata": {
      "public": "true"
    }
  },
  {
    "project_id": 96,
    "owner_id": 1,
    "name": "ppp",
    "creation_time": "2018-10-30T03:53:00Z",
    "update_time": "2018-10-30T03:53:00Z",
    "deleted": 0,
    "owner_name": "",
    "togglable": true,
    "current_user_role_id": 1,
    "repo_count": 0,
    "metadata": {
      "public": "false"
    }
  },
  {
    "project_id": 92,
    "owner_id": 1,
    "name": "t1",
    "creation_time": "2018-08-12T14:03:10Z",
    "update_time": "2018-08-12T14:03:10Z",
    "deleted": 0,
    "owner_name": "",
    "togglable": true,
    "current_user_role_id": 1,
    "repo_count": 0,
    "metadata": {
      "public": "true"
    }
  },
  {
    "project_id": 82,
    "owner_id": 1,
    "name": "temp_1",
    "creation_time": "2018-08-11T03:09:43Z",
    "update_time": "2018-08-11T03:09:43Z",
    "deleted": 0,
    "owner_name": "",
    "togglable": true,
    "current_user_role_id": 1,
    "repo_count": 1,
    "metadata": {
      "public": "true"
    }
  },
  {
    "project_id": 91,
    "owner_id": 1,
    "name": "temp_10",
    "creation_time": "2018-08-11T03:10:28Z",
    "update_time": "2018-08-11T03:10:28Z",
    "deleted": 0,
    "owner_name": "",
    "togglable": true,
    "current_user_role_id": 1,
    "repo_count": 1,
    "metadata": {
      "public": "true"
    }
  },
  {
    "project_id": 83,
    "owner_id": 1,
    "name": "temp_2",
    "creation_time": "2018-08-11T03:09:45Z",
    "update_time": "2018-08-11T03:09:45Z",
    "deleted": 0,
    "owner_name": "",
    "togglable": true,
    "current_user_role_id": 1,
    "repo_count": 0,
    "metadata": {
      "public": "true"
    }
  },
  {
    "project_id": 84,
    "owner_id": 1,
    "name": "temp_3",
    "creation_time": "2018-08-11T03:09:52Z",
    "update_time": "2018-08-11T03:09:52Z",
    "deleted": 0,
    "owner_name": "",
    "togglable": true,
    "current_user_role_id": 1,
    "repo_count": 1,
    "metadata": {
      "public": "true"
    }
  },
  {
    "project_id": 86,
    "owner_id": 1,
    "name": "temp_5",
    "creation_time": "2018-08-11T03:09:57Z",
    "update_time": "2018-08-11T03:09:57Z",
    "deleted": 0,
    "owner_name": "",
    "togglable": true,
    "current_user_role_id": 0,
    "repo_count": 0,
    "metadata": {
      "public": "true"
    }
  },
  {
    "project_id": 87,
    "owner_id": 1,
    "name": "temp_6",
    "creation_time": "2018-08-11T03:10:05Z",
    "update_time": "2018-08-11T03:10:05Z",
    "deleted": 0,
    "owner_name": "",
    "togglable": true,
    "current_user_role_id": 1,
    "repo_count": 1,
    "metadata": {
      "public": "true"
    }
  },
  {
    "project_id": 88,
    "owner_id": 1,
    "name": "temp_7",
    "creation_time": "2018-08-11T03:10:11Z",
    "update_time": "2018-08-11T03:10:11Z",
    "deleted": 0,
    "owner_name": "",
    "togglable": true,
    "current_user_role_id": 1,
    "repo_count": 1,
    "metadata": {
      "public": "true"
    }
  },
  {
    "project_id": 89,
    "owner_id": 1,
    "name": "temp_8",
    "creation_time": "2018-08-11T03:10:19Z",
    "update_time": "2018-08-11T03:10:19Z",
    "deleted": 0,
    "owner_name": "",
    "togglable": true,
    "current_user_role_id": 1,
    "repo_count": 0,
    "metadata": {
      "public": "true"
    }
  },
  {
    "project_id": 90,
    "owner_id": 1,
    "name": "temp_9",
    "creation_time": "2018-08-11T03:10:27Z",
    "update_time": "2018-08-11T03:10:27Z",
    "deleted": 0,
    "owner_name": "",
    "togglable": true,
    "current_user_role_id": 1,
    "repo_count": 1,
    "metadata": {
      "public": "true"
    }
  }
]
[#340#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions