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 find the most recent analysis of a named project.
This task provides a batch file that finds the most recent analysis of a project (specified by name), along with some suggestions for modifying the script to suit your needs.
For other scripting options, see:
The file requires that special user Anonymous has the following permissions for the project P of interest and its most recent analysis A.
See Modifying the Batch File for information on modifying the file 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 file will download the CSV project search results for the project (or projects) on the hub specified in the first argument to the file whose name matches the second argument. It will use these results to generate a summary CSV document that lists, for each project found:
Both CSV files are stored in the directory specified in the third argument to the file.
@ECHO off
set CALL_DIR=%CD%
set HUB=%1
set PROJECT_NAME=%2
set SAVEDIR=%3
set PROJECT_SEARCH_CSV="%HUB%/project_search.csv?query=project%%3D"%PROJECT_NAME%"&scope=all"
set CURL_CMD=curl
set RESULT_FNAME=overall_results.csv
set SEARCH_FNAME=project_search_results.csv
cd /D %SAVEDIR%
rm -fr *
echo project id, most recent analysis id, notes >%RESULT_FNAME%
(%CURL_CMD% %PROJECT_SEARCH_CSV% -o %SEARCH_FNAME%)
set /a LINECOUNT=0
FOR /f %%j in ('find "" /v /c ^<%SEARCH_FNAME%') DO set /A LINECOUNT=%%j
:: If no results, report and exit.
if %LINECOUNT% == 1 (
echo No projects with URL-encoded name %PROJECT_NAME% were found.
echo This may indicate one or more of the following.
echo - The project name was not specified correctly (remember to URL-encode^).
echo - You do not have PROJECT_EXISTS permission for the specified project.
cd /D %CALL_DIR%
exit /b
)
:: If multiple results, report and keep going.
if %LINECOUNT% GTR 2 (
echo Found multiple projects with URL-encoded name '%PROJECT_NAME%'.
echo The script will attempt to report the most recent analysis of each.
)
for /F "skip=1 tokens=*" %%L in (%SEARCH_FNAME%) do (
call :handle_csv_line "%RESULT_FNAME%" "%%L"
)
cd /D %CALL_DIR%
exit /b
:handle_csv_line
set OUTFILE=%1
set LINE=%2
:: obtain last column of line
set LASTCOLUMN=
set /A COUNTER=0
:LOOPBEGIN
set /A COUNTER+=1
for /F "tokens=%COUNTER% delims=," %%A in (%LINE%) do (
if "%%A"=="" goto :LOOPEND
set LASTCOLUMN=%%A
goto :LOOPBEGIN)
:LOOPEND
:: extract URL type and ID from last column
set LASTFIELD=
set SECONDLASTFIELD=
set /A COUNTER=0
:LOOPBEGIN
set /A COUNTER+=1
for /F "tokens=%COUNTER%* delims=/" %%A in (%LINE%) do (
if "%%B"=="" goto :LOOPEND
set SECONDLASTFIELD=%%A
set LASTFIELD=%%B
goto :LOOPBEGIN)
:LOOPEND
set RESULT_TYPE=%SECONDLASTFIELD%
set RESULT_ID=%LASTFIELD:~0,-4%
if "%RESULT_TYPE%"=="analysis" (
echo -,%RESULT_ID%,- >> %OUTFILE%
) else (
if "%RESULT_TYPE%"=="project" (
SETLOCAL ENABLEDELAYEDEXPANSION
set "NO_ANALYSIS=No most recent analysis found. Either: a) project %RESULT_ID% has"
set "NO_ANALYSIS=!NO_ANALYSIS! no analyses or b) you do not have ANALYSIS_EXISTS"
set "NO_ANALYSIS=!NO_ANALYSIS! permission for the most recent analysis."
echo %RESULT_ID%,-,!NO_ANALYSIS! >> %OUTFILE%
ENDLOCAL
) else (
echo Unexpected result type %RESULT_TYPE% in search results.
)
)
)
exit /b
This batch file works as follows.
This is the same file you would download if you did the following.
To use this file with your hub, do the following.
| protocol | is the protocol for your hub: http or https. |
|---|---|
| host:port | is the location of your hub. |
| projname | is the name of the project whose last analysis you want to find. It must be URL-encoded. |
| savepath | is the path to the savedir directory you created in the first step. |
Using the hub at http://[::1]:7341, find the most recent analysis of the project named "My Favorite Project", saving the results in directory c:\tmp\csvout:
| Get more verbose output |
For more verbose curl output,
edit find_project_analysis.bat so that
curl is invoked with the
-v flag. For example:
set CURL_CMD=curl -v |
|---|---|
| No project_search_results.csv file |
If your output directory does not contain file project_search_results.csv, there are two
possible reasons.
|
| project_search_results.csv lists no results |
If your output directory contains file project_search_results.csv but it does not
list any results, there are two possibilities.
Similarly, if project_search_results.csv lists fewer results than expected, it is likely to be because Anonymous does not have PROJECT_EXISTS permission for all matching projects. |
| overall_results.csv lists project IDs rather than analysis IDs | This indicates that Anonymous has PROJECT_EXISTS permission for the project(s) of interest, but does not have ANALYSIS_EXISTS permission for the most recent analysis of each. You will need to specify credentials for a user with the required permissions. |
You may wish to make one or more of the following modifications.
To change a search to perform substring matching rather than exact
matching, we need to edit the corresponding field-condition
to change its operator from
= to :.
Because of URL-encoding, this becomes a change from %3D to %3A in the query string constructed
by the file.
set PROJECT_SEARCH_CSV="%HUB%/project_search.csv?query=project%%3A"%PROJECT_NAME%"&scope=all"
If your hub is configured so that special user Anonymous does not have the required permissions, you will need to edit the file 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.
set /p BEARER_TOKEN=<path\to\bearerfile
| path\to\bearerfile | is the path to the file containing the bearer token you want to use. |
|---|
CURL_CMD=curl -H "Authorization: Bearer %BEARER_TOKEN%"
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 file to
specify a suitable user
certificate.
|
||||||||
|---|---|---|---|---|---|---|---|---|---|
| Hard-Coded Username/Password |
If you will be running the batch file under secure
conditions, you may be willing to specify the account username
and password
directly in the batch file 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 batch file would be http://jean:xyz123@[::1]:7340. Example: Use the hub user account with username jean and password xyz123 to authorize finding the most recent analysis of the project named "Project X " on the hub at http://[::1]:7340, saving the results in directoryc:\tmp\csvout:
find_project_analysis http://jean:xyz123@[::1]:7340 PROJECT%20X
c:\tmp\csvout
Both username and password must also 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 file to create batch files that download other kinds of file from the hub.
In general, the process for constructing a file 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/.