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 shell 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:
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.
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
#! /bin/sh -e
if [ $# -ne 2 ]; then
echo "Usage: $0 HUB PROJECT_ID"
exit 1
fi
HUB=$1
PROJECT_ID=$2
CURL_CMD=curl
del_project(){
"$CURL_CMD" -L --cookie-jar cookies.txt -d remove_project_id="$1" "$2"
}
del_project "${PROJECT_ID}" "${HUB}"
This shell 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.sh so that
curl is invoked with the
-v flag. For example:
del_project(){
"$CURL_CMD" -v -L --cookie-jar cookies.txt -d remove_project_id="$1" "$2"
}
|
|---|---|
| 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.sh so that
curl is invoked with the
-k flag. For example:
del_project(){
"$CURL_CMD" -k -L --cookie-jar cookies.txt -d remove_project_id="$1" "$2"
}
|
| 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.
del_project(){
"$CURL_CMD" -o /dev/null -L --cookie-jar cookies.txt -d remove_project_id="$1" "$2"
}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.
ANALYSIS_DIR=$2
CSONAR_CMD=codesonar
ANALYSIS_ID=$("$CSONAR_CMD" analysis_id.py "$ANALYSIS_DIR" | tr -d '\r')
ANALYSIS_HTML_URL="${HUB}/analysis/${ANALYSIS_ID}.html"
get_project_id(){
PROJ_ID=$("$CURL_CMD" "$2" \
| sed -n '/href="\/project\//p' \
| head -n1 \
| sed 's/.* href="\/project\/\([0-9]*\)\.html".*/\1/')
# exit with an error if no Project ID found
if test "${PROJ_ID}" = ""
then
echo "No Project ID found."
echo "This probably means you do not have ANALYSIS_READ permission"
echo "for the analysis and so cannot access $2."
exit 1
else
eval "$1='${PROJ_ID}'"
fi
}
get_project_id PROJECT_ID "${ANALYSIS_HTML_URL}" 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.
BEARER_TOKEN=$(cat path/to/bearerfile)
| path/to/bearerfile | is the path to the file containing the bearer token you want to use. |
|---|
del_project(){
"$CURL_CMD" -H "Authorization: Bearer ${BEARER_TOKEN}" -L -d remove_project_id="$1" "$2"
}
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 shell 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:
./delete_project.sh 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 shell 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/.