Skip to content

examples/upload_scan.py fails with 2022.x Black Duck servers #223

Open
@ghost

Description

As of 2022.x (?), uploading dryrun bdio files to Black Duck server /api/scans/data using this hub-rest-api-python examples/upload_scan.py fails in various ways, including 400 "The application has encountered an unknown error." errors client-side (visible as 415 UNSUPPORTED_MEDIA_TYPE "Unsupported media type" errors on Black Duck scan container logs--yeah, apparently a bug in there).

curl and postman can be tweaked to work fine relatively easily. But tweaking this script took a little longer.

Our understanding is that the /api/scans/data api endpoint now requires Content-Type multipart/form-data, with the "file" key. See the Black Duck api online doc.

But different clients handle Content-Type differently.

examples/upload_scan.py, via Scans.py, doesn't handle that properly for Black Duck 2022.x.

The python requests library, which is imported into this tool, handles the Content-Type and boundary properly--if you don't confuse it by manually adding your own Content-Type header.

The "headers=headers" in Scans.py, includes a somewhat hidden Content-Type "headers" dictionary key, so that needs to be removed, to avoid confusing the requests library.

To workaround, edit Scans.py. Use the header.pop command to remove that hidden Content-Type value from the "headers" variable, and tweak the requests.post call, like so.

    elif filename.endswith('.bdio'):
        #headers['Content-Type'] = 'application/vnd.blackducksoftware.bdio+zip'
        headers.pop('Content-Type')
        #with open(filename,"rb") as f:
            #response = requests.post(url, headers=headers, data=f, verify=not self.config['insecure'])
        f = {'file': open(filename,'rb')}
        response = requests.post(url, headers=headers, files=f, verify=not self.config['insecure'])

h/t Jeff C.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions