atlassian / go-artifactory

Go library for artifactory REST API
Apache License 2.0
23 stars 28 forks source link

panic on CreateToken #11

Closed universam1 closed 5 years ago

universam1 commented 5 years ago

Describe the bug calling CreateToken will panic because of body is nil. The test is wrong, it should return if body is nil, not if it is not nil. https://github.com/atlassian/go-artifactory/blob/v2/artifactory/client/client.go#L94

To Reproduce

tokenOpts := v1.AccessTokenOptions{
        Username: artifactory.String("admin"),
        Scope:    artifactory.String("member-of-groups:*"),
}
client.V1.Security.CreateToken(context.Background(), &tokenOpts)

it will panic: panic: runtime error: invalid memory address or nil pointer dereference

Expected behavior return the token

Log Output

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0x11e2d32]

goroutine 1 [running]:
strings.(*Reader).Len(...)
        /usr/local/Cellar/go/1.11.4/libexec/src/strings/reader.go:25
net/http.NewRequest(0x12acdd7, 0x4, 0xc000110420, 0x48, 0x12f19c0, 0x0, 0x0, 0xd0, 0x6)
        /usr/local/Cellar/go/1.11.4/libexec/src/net/http/request.go:839 +0x282
github.com/atlassian/go-artifactory/artifactory/client.(*Client).NewRequest(0xc00000c3c0, 0x12acdd7, 0x4, 0x12b024d, 0x13, 0x12f19c0, 0x0, 0xd0, 0x6, 0x0)
        /Users/sam/go/src/github.com/atlassian/go-artifactory/artifactory/client/client.go:69 +0x11c
github.com/atlassian/go-artifactory/artifactory/client.(*Client).NewURLEncodedRequest(0xc00000c3c0, 0x12acdd7, 0x4, 0x12b024d, 0x13, 0x1240ce0, 0xc00010c3f0, 0x1, 0x1, 0x6)
        /Users/sam/go/src/github.com/atlassian/go-artifactory/artifactory/client/client.go:128 +0xa4
github.com/atlassian/go-artifactory/artifactory/v1.(*SecurityService).CreateToken(0xc00009cd20, 0x12f3d80, 0xc0000180b8, 0xc00010c3f0, 0x0, 0x0, 0xc0001122c0, 0x0)
        /Users/sam/go/src/github.com/atlassian/go-artifactory/artifactory/v1/security.go:908 +0x75
exit status 2

Desktop (please complete the following information):

Additional context The bug is probably here: https://github.com/atlassian/go-artifactory/blob/v2/artifactory/client/client.go#L94 should be

    if body == nil {