harborctl icon indicating copy to clipboard operation
harborctl copied to clipboard

[NOTE] API "POST /labels" not work as expect

Open moooofly opened this issue 5 years ago • 4 comments

by swagger file

image

image

This API has the same issue as #7 , #20 and #21

moooofly avatar Nov 06 '18 08:11 moooofly

a wired case by swagger UI

create a global label by swagger UI

image

image

but you will find nothing in harbor UI

image

run it again, you will get

image

make the body content more reasonable, and try again

image

image

still get nothing

image

and

image

moooofly avatar Nov 06 '18 08:11 moooofly

Test by harborctl

HELP info

[#454#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$./harborctl label create -h
This endpoint let user creates a label.

Usage:
  harborctl label create [flags]

Flags:
  -c, --color string           The color code of this label. (e.g. Format: #A9B6BE)
      --creation_time string   The creation time of this label.
      --deleted                The label is deleted or not.
  -d, --description string     The description of this label.
  -h, --help                   help for create
  -i, --id int                 The ID of the label.
  -n, --name string            The name of this label.
  -j, --project_id int         The project ID if the label is a project label. ('0' indicates global label, others indicate specific project)
  -s, --scope string           The scope of this label. ('p' for project scope, 'g' for global scope)
      --update_time string     The update time of this label.

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

case 1: set nothing

[#455#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$./harborctl label create
==> POST https://localhost/api/labels
<==
<== Rsp Status: 400 Bad Request
<== Rsp Body: name cannot be empty
scope invalid:

[#456#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$

From the response we know that parameters name and scope are required.

case2: set only name and scope

after updating the codes

[#460#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$./harborctl label create
Error: required flag(s) "name", "scope" not set
Usage:
  harborctl label create [flags]

Flags:
  -c, --color string           The color code of this label. (e.g. Format: #A9B6BE)
      --creation_time string   The creation time of this label.
      --deleted                The label is deleted or not.
  -d, --description string     The description of this label.
  -h, --help                   help for create
  -i, --id int                 The ID of the label.
  -n, --name string            (REQUIRED) The name of this label.
  -j, --project_id int         The project ID if the label is a project label. ('0' indicates global label, others indicate specific project)
  -s, --scope string           (REQUIRED) The scope of this label. ('p' for project scope, 'g' for global scope)
      --update_time string     The update time of this label.

Global Flags:
      --config string   config file (default is $HOME/, working dir (.), and ./conf dir)

required flag(s) "name", "scope" not set
[#461#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$
[#461#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$
[#461#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$
[#461#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$./harborctl label create -n label-1 -s g
==> POST https://localhost/api/labels
<==
<== Rsp Status: 500 Internal Server Error
<== Rsp Body:

[#462#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$

it will trigger "500 | Internal Server Error", and you can get the same result from swagger UI

image

image

case 3: some more tests

[#461#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$./harborctl label create -n label-1 -s g
==> POST https://localhost/api/labels
<==
<== Rsp Status: 500 Internal Server Error
<== Rsp Body:

[#462#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$
[#462#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$
[#462#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$
[#462#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$./harborctl label create -n "label-1" -s "g"
==> POST https://localhost/api/labels
<==
<== Rsp Status: 500 Internal Server Error
<== Rsp Body:

[#463#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$./harborctl label create -n "label" -s "g"
==> POST https://localhost/api/labels
<==
<== Rsp Status: 201 Created
<== Rsp Body:
[#464#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$
[#464#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$./harborctl label create -n label_one -s g
==> POST https://localhost/api/labels
<==
<== Rsp Status: 201 Created
<== Rsp Body:
[#465#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$./harborctl label create -n label_1 -s g
==> POST https://localhost/api/labels
<==
<== Rsp Status: 201 Created
<== Rsp Body:
[#466#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$
[#466#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$./harborctl label create -n label_@ -s g
==> POST https://localhost/api/labels
<==
<== Rsp Status: 201 Created
<== Rsp Body:
[#467#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$

[#468#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$./harborctl label create -n "label_@#$%^&*()" -s g
==> POST https://localhost/api/labels
<==
<== Rsp Status: 201 Created
<== Rsp Body:
[#469#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$

from harbor UI you will see

image

case 4: the name of label must not be "string"

[#481#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$./harborctl label create -n string -s g
==> POST https://localhost/api/labels
<==
<== Rsp Status: 500 Internal Server Error
<== Rsp Body:

[#483#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$./harborctl label create -n string1 -s g
==> POST https://localhost/api/labels
<==
<== Rsp Status: 201 Created
<== Rsp Body:
[#484#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$./harborctl label create -n string1 -s g
==> POST https://localhost/api/labels
<==
<== Rsp Status: 409 Conflict
<== Rsp Body:

[#485#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$./harborctl label create -n int -s g
==> POST https://localhost/api/labels
<==
<== Rsp Status: 201 Created
<== Rsp Body:
[#486#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$./harborctl label create -n bool -s g
==> POST https://localhost/api/labels
<==
<== Rsp Status: 201 Created
<== Rsp Body:
[#487#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$./harborctl label create -n integer -s g
==> POST https://localhost/api/labels
<==
<== Rsp Status: 201 Created
<== Rsp Body:
[#488#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$
[#488#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$./harborctl label create -n func -s g
==> POST https://localhost/api/labels
<==
<== Rsp Status: 201 Created
<== Rsp Body:
[#489#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$./harborctl label create -n interface -s g
==> POST https://localhost/api/labels
<==
<== Rsp Status: 201 Created
<== Rsp Body:
[#490#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$./harborctl label create -n string -s g
==> POST https://localhost/api/labels
<==
<== Rsp Status: 500 Internal Server Error
<== Rsp Body:

[#491#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$

image

CONCLUSION

  • The name of label must not include "-", or you will get wired "500 Internal Server Error"
  • the name of label must not be "string"

moooofly avatar Nov 06 '18 08:11 moooofly

another issue

  • create a label by harborctl, show success
[#544#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$./harborctl label create -h
This endpoint let user creates a label.

Usage:
  harborctl label create [flags]

Flags:
  -c, --color string           The color code of this label. (e.g. Format: #A9B6BE)
      --creation_time string   The creation time of this label.
      --deleted                The label is deleted or not.
  -d, --description string     The description of this label.
  -h, --help                   help for create
  -i, --id int                 The ID of the label.
  -n, --name string            (REQUIRED) The name of this label.
  -j, --project_id int         The project ID if the label is a project label. ('0' indicates global label, others indicate specific project)
  -s, --scope string           (REQUIRED) The scope of this label. ('p' for project scope, 'g' for global scope)
      --update_time string     The update time of this label.

Global Flags:
      --config string   config file (default is $HOME/, working dir (.), and ./conf dir)
[#545#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$
[#545#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$./harborctl label create -n fly -s g -d "the color of fly" -c "#F52F52" -j 0 -i 2000 --deleted
==> POST https://localhost/api/labels
<==
<== Rsp Status: 201 Created
<== Rsp Body:
[#546#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$
  • but you can not find it from harbror UI

image

  • create again with the same parameters, you will get "500 Internal Server Error" not "409 Conflict"
[#546#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$./harborctl label create -n fly -s g -d "the color of fly" -c "#F52F52" -j 0 -i 2000 --deleted
==> POST https://localhost/api/labels
<==
<== Rsp Status: 500 Internal Server Error
<== Rsp Body:

[#547#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$
  • you can not get this label either
[#547#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$./harborctl label get -i 2000
==> GET https://localhost/api/labels/2000
<==
<== Rsp Status: 404 Not Found
<== Rsp Body: label 2000 not found

[#548#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$

another one

[#548#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$./harborctl label create -n flyAgain -s g -d "the color of flyAgain" -c "#F52F52" -j 0 -i 3000 --deleted
==> POST https://localhost/api/labels
<==
<== Rsp Status: 201 Created
<== Rsp Body:
[#549#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$
[#549#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$./harborctl label get -i 3000
==> GET https://localhost/api/labels/3000
<==
<== Rsp Status: 404 Not Found
<== Rsp Body: label 3000 not found

[#550#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$

Conclusion

This issue must be related with --deleted parameter which means "Mark the label is deleted or not."

If you call this API with --deleted, you actually create a label that has been marked "Deleted", and you can get it by any means.

This behavior will cause a obscure problem:

  • create a label with --deleted and succeed.
  • some day after, you want to get it back, but only get "404 Not Found" (you will keep doubting yourself).
  • try to create this label with the same parameters (actually any composition will fail). You can do nothing, even make a deletion on it.
  • you can only create another label with totally different parameters composition (without --deleted this time).

Here is the question: What is the role of --deleted in fact?

moooofly avatar Nov 06 '18 09:11 moooofly

a good case

[#554#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$./harborctl label create -h
This endpoint let user creates a label.

Usage:
  harborctl label create [flags]

Flags:
  -c, --color string           The color code of this label. (e.g. Format: #A9B6BE)
      --creation_time string   The creation time of this label.
      --deleted                The label is deleted or not.
  -d, --description string     The description of this label.
  -h, --help                   help for create
  -i, --id int                 The ID of the label.
  -n, --name string            (REQUIRED) The name of this label.
  -j, --project_id int         The project ID if the label is a project label. ('0' indicates global label, others indicate specific project)
  -s, --scope string           (REQUIRED) The scope of this label. ('p' for project scope, 'g' for global scope)
      --update_time string     The update time of this label.

Global Flags:
      --config string   config file (default is $HOME/, working dir (.), and ./conf dir)
[#555#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$
[#555#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$
[#555#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$./harborctl label create -n flyAgainAgain -s g -d "the color of flyAgainAgain" -c "#F52F52" -j 0 -i 5000
==> POST https://localhost/api/labels
<==
<== Rsp Status: 201 Created
<== Rsp Body:
[#556#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$
[#556#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$./harborctl label get -i 5000
==> GET https://localhost/api/labels/5000
<==
<== Rsp Status: 200 OK
<== Rsp Body: {
  "id": 5000,
  "name": "flyAgainAgain",
  "description": "the color of flyAgainAgain",
  "color": "#F52F52",
  "scope": "g",
  "project_id": 0,
  "creation_time": "2018-11-06T09:59:19.097633Z",
  "update_time": "2018-11-06T09:59:19.097633Z",
  "deleted": false
}
[#557#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$
[#557#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$./harborctl label create -n flyAgainAgain -s g -d "the color of flyAgainAgain" -c "#F52F52" -j 0 -i 5000
==> POST https://localhost/api/labels
<==
<== Rsp Status: 409 Conflict
<== Rsp Body:

[#558#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$

you can get it from harbor UI

image

[#559#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$./harborctl label delete -i 5000
==> DELETE https://localhost/api/labels/5000
<==
<== Rsp Status: 200 OK
<== Rsp Body:
[#560#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$
[#560#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$./harborctl label get -i 5000
==> GET https://localhost/api/labels/5000
<==
<== Rsp Status: 404 Not Found
<== Rsp Body: label 5000 not found

[#561#root@ubuntu-1604 /go/src/github.com/moooofly/harborctl]$

moooofly avatar Nov 06 '18 10:11 moooofly