JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.
If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.
If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.
| CodeSonar® 9.2p0 | CONFIDENTIAL | CodeSecure Inc |
You can use a script to delete an existing project from the command line.
Deleting a project from the hub removes all the information associated with the project:
Because removing a project deletes all this information, it is generally not recommended. Exceptions are the following cases.
This task provides a Python script for deleting a specified project, along with some suggestions for modifying the script to suit your needs.
This page does not describe how to automate script deployment: we strongly advise against deleting projects automatically.
For other scripting options, see:
Note: The Python script in this task is structured to be as similar as possible to those in the other download script tasks listed above.The script requires that special user Anonymous has the following permissions for the project P and its analyses.
If you will be extending the script to derive the project ID from an analysis directory for some analysis A of the project, you will also need the following permissions for A.
See Modifying the Script for information on modifying the script to specify credentials for a non-Anonymous user with the required permissions.
The example script imports standard Python modules argparse, os, and subprocess.
You will need a Python installation to run the script. If you do not have a local installation, you can use the cspython shipped with CodeSonar:
Use the cURL shipped with CodeSonar: $CSONAR/third-party/curl/inst/bin/curl, where $CSONAR is the CodeSonar installation directory. Either:
The following script will delete
import argparse import os import subprocess # Delete the specified project from the specified hub def del_project(hubaddr, project_id): curl_cmd = ['curl'] delete_args = ['-L', '--cookie-jar','cookies.txt', '-d', f'remove_project_id={project_id}', hubaddr] subprocess.check_call(curl_cmd + delete_args) # Set up. def go(): parser = argparse.ArgumentParser( description=('Delete a project from a CodeSonar hub,' + 'as specified by the command-line arguments.')) parser.add_argument("hub", help="The hub URL.") parser.add_argument("pid", help="The project ID for the project to delete.") args = parser.parse_args() allargs = (args.hub, args.pid) if not any([a is None for a in allargs]): del_project(*allargs) go()
This Python script uses an HTTP POST request to perform the deletion: the POST request is for the hub Home page and must specify remove_project_id=pid, where pid is the project ID of the project to be deleted. It outputs the HTML landing page that reports successful deletion.
The hub functionality for deleting an analysis entails several HTTP redirects across a single session. When you are using an anonymous session, as here, it must be tracked using cookies. The script uses cURL options to ensure that the redirects and cookies are properly accounted for:
Inspect the HTML source for a Project page to see the remove_project_form element and how its attributes and sub-elements correspond to the HTTP POST request constructed by the script.
To use this script with your hub, do the following.
Make sure you have full file permissions in rundir, and that using file rundir/cookies.txt to manage cookies for this task will not cause conflicts with other uses of rundir. If there is a problem, either:
| protocol | is the protocol for your hub: http or https. |
|---|---|
| host:port | is the location of your hub. |
| pid |
is the project ID
for the project whose name you wish to change. You can find the project ID:
|
Using the hub at http://[::1]:7341, delete the project with ID 3:
| Get more verbose output |
For more verbose curl output,
edit delete_project.py so that
curl is invoked with the
-v flag. For example:
curl_cmd=['curl', '-v'] |
|---|---|
| No files downloaded |
If there is no HTML output, this indicates that cURL did not
download anything. This can occur if you have an HTTPS-enabled
hub with a self-signed hub server
certificate. To instruct curl to accept self-signed certificates,
edit delete_project.py so that
curl is invoked with the
-k flag. For example:
curl_cmd=['curl', '-k'] |
| Downloaded files contain "Permission Denied" messages | If there is an output HTML file but it contains a "Permission Denied" message rather than a page reporting successful deletion, this indicates that Anonymous does not have the required permissions. You will need to specify credentials for a user with these permissions. |
You may wish to make one or more of the following modifications.
If you don't want to see the final HTML file notifying you of successful deletion as part of your script's output, you can redirect it to the null location by modifying your curl command. Note that if you make further changes that you need to debug later, you may wish to lift this suppression at least temporarily.
delete_args = ['-o', os.devnull, '-L', '--cookie-jar', 'cookies.txt', '-d', f'remove_project_id={project_id}', hubaddr]
Instead of specifying the project ID on the command line, you can change the script to read the analysis directory from the command line and derive the project ID by reading the most recent analysis ID from the analysis directory, requesting the corresponding Analysis page from the hub, then reading the project ID from the Analysis page.
Because this approach requires the Analysis page for the analysis A in question, you must have the following permissions for A in addition to the required project permissions.
Once you have verified that you have these permissions, proceed as follows.
parser.add_argument("pid", help="The project ID for the project to delete.")
parser.add_argument("adir", help="The analysis .prj_files directory.")
allargs = (args.hub, args.adir)
def del_project(hubaddr, analysis_dir):
with open(os.path.join(analysis_dir,'aid.txt'),'r') as aidfile:
analysis_id=aidfile.read()
analysis_html_url = f'{hubaddr}/analysis/{analysis_id}.html'
# Download and scrape Analysis HTML page for a project ID,
# which will be that of the analyzed project.
analysis_dl=subprocess.Popen(curl_cmd + [analysis_html_url],
stdout=subprocess.PIPE)
analysis_page=analysis_dl.communicate()[0]
pid_match=re.search(r'<a href="/project/(\d+).html">',
analysis_page)
if pid_match is None:
print 'Could not get analysis page', analysis_html_url
print 'Make sure you have ANALYSIS_READ permission for this analysis.'
exit(1)
project_id = pid_match.group(1)
For example: using the hub at http://[::1]:7341, delete the analysis whose analysis directory is /myprojects/projectX.prj_files/.
If your hub is configured so that special user Anonymous does not have the required permissions, you will need to edit the script to submit credentials for a suitable hub user account.
We recommend using bearer authentication. Alternative mechanisms are described in the table below.
For bearer authentication, do the following.
with open('path/to/bearerfile','r') as bearerfile: bearer_token = bearerfile.read().strip()
| path/to/bearerfile | is the path to the file containing the bearer token you want to use. |
|---|
curl_cmd=['curl', '-H', f"Authorization: Bearer {bearer_token}"]
delete_args=['-L', f'remove_project_id={project_id}', '-d', f'remove_analysis_id={analysis_id}' hubaddr]
For more information about bearer authentication in CodeSonar, see User Sessions and Anonymous Sessions: Bearer Authentication.
| Certificate |
If the hub is configured for certificate-based
authentication, you can edit the script to
specify a suitable user
certificate.
|
||||
|---|---|---|---|---|---|
| Hard-Coded Username/Password |
If you will be running the Python script under secure
conditions, you may be willing to specify the account username
and password
directly in the script invocation.
For example, if your hub location is http://[::1]:7340 and the hub user account has username jean and password xyz123, the first argument to the script would be http://jean:xyz123@[::1]:7340. Example: Use the hub user account with username jean and password xyz123 to authorize deleting the analysis with ID 3 on the hub at http://[::1]:7340:
python delete_project.py http://jean:xyz123@[::1]:7340 3
Username and password must both be URL-encoded.
|
||||
| Username/Password: Other | See the curl man page for alternative username/password authentication mechanisms. |
See CodeSonar HTTP API: Authentication for more information on authentication strategies.
You can follow the overall structure of this script to create Python scripts that download other kinds of file from the hub.
In general, the process for constructing a script will be along the following lines.
Note. This page contains references to HTTP API documentation, which is served directly by the hub and cannot be accessed via a file:// URL. For active HTTP API documentation links, start a hub (if one is not already running), then open the manual from the hub.
To report problems with this documentation, please visit https://support.codesecure.com/.