Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SCANNPM-2 Validation fixes #140

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 0 additions & 142 deletions .npmignore

This file was deleted.

Empty file removed ca.pem
Empty file.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
"arrowParens": "avoid"
},
"files": [
"build/**"
"build/**",
"bin/**"
]
}
9 changes: 8 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import path from 'path';
import { ScannerProperty } from './types';

export const SCANNER_BOOTSTRAPPER_NAME = 'ScannerNpm';
Expand Down Expand Up @@ -65,3 +64,11 @@ export const SCANNER_CLI_MIRROR =
export const SCANNER_CLI_INSTALL_PATH = 'native-sonar-scanner';

export const WINDOWS_WHERE_EXE_PATH = 'C:\\Windows\\System32\\where.exe';

export const SCANNER_DEPRECATED_PROPERTIES: ScannerProperty[][] = [
[ScannerProperty.SonarWsTimeout, ScannerProperty.SonarScannerResponseTimeout],
[ScannerProperty.HttpProxyHost, ScannerProperty.SonarScannerProxyHost],
[ScannerProperty.HttpProxyPort, ScannerProperty.SonarScannerProxyPort],
[ScannerProperty.HttpProxyUser, ScannerProperty.SonarScannerProxyUser],
[ScannerProperty.HttpProxyPassword, ScannerProperty.SonarScannerProxyPassword],
];
14 changes: 9 additions & 5 deletions src/java.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

import fsExtra from 'fs-extra';
import path from 'path';
import semver, { SemVer } from 'semver';
import {
Expand Down Expand Up @@ -88,8 +88,7 @@ export async function serverSupportsJREProvisioning(
properties: ScannerProperties,
): Promise<boolean> {
if (properties[ScannerProperty.SonarScannerInternalIsSonarCloud] === 'true') {
//TODO: return to true once SC has the new provisioning mechanism in place
return false;
lucas-paulger-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
return true;
}

// SonarQube
Expand All @@ -107,7 +106,7 @@ export async function serverSupportsJREProvisioning(
export async function fetchJRE(properties: ScannerProperties): Promise<string> {
log(LogLevel.DEBUG, 'Detecting latest version of JRE');
const jreMetaData = await fetchLatestSupportedJRE(properties);
log(LogLevel.INFO, 'Latest Supported JRE: ', jreMetaData);
log(LogLevel.DEBUG, 'Latest Supported JRE: ', jreMetaData);

log(LogLevel.DEBUG, 'Looking for Cached JRE');
const cachedJrePath = await getCacheFileLocation(properties, {
Expand All @@ -132,7 +131,12 @@ export async function fetchJRE(properties: ScannerProperties): Promise<string> {
const url = jreMetaData.downloadUrl ?? `${API_V2_JRE_ENDPOINT}/${jreMetaData.id}`;

await download(url, archivePath);
await validateChecksum(archivePath, jreMetaData.sha256);
try {
await validateChecksum(archivePath, jreMetaData.sha256);
} catch (error) {
await fsExtra.remove(archivePath);
throw error;
}
await extractArchive(archivePath, jreDirPath);
return path.join(jreDirPath, jreMetaData.javaPath);
}
Expand Down
27 changes: 25 additions & 2 deletions src/properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
ENV_VAR_PREFIX,
NPM_CONFIG_ENV_VAR_PREFIX,
SCANNER_BOOTSTRAPPER_NAME,
SCANNER_DEPRECATED_PROPERTIES,
SONARCLOUD_API_BASE_URL,
SONARCLOUD_URL,
SONARCLOUD_URL_REGEX,
Expand Down Expand Up @@ -363,6 +364,28 @@ function getHttpProxyEnvProperties(serverUrl: string): ScannerProperties {
return properties;
}

function hotfixDeprecatedProperties(properties: ScannerProperties): ScannerProperties {
for (const [oldProp, newProp] of SCANNER_DEPRECATED_PROPERTIES) {
if (typeof properties[oldProp] !== 'undefined') {
if (typeof properties[newProp] === 'undefined') {
log(
LogLevel.WARN,
`Property "${oldProp}" is deprecated and will be removed in a future version. Please use "${newProp}" instead.`,
);
properties[newProp] = properties[oldProp];
} else {
log(
LogLevel.WARN,
`Both properties "${oldProp}" and "${newProp}" are set. "${oldProp}" is deprecated and will be removed in a future version. Value of deprecated property "${oldProp}" will be ignored.`,
);
properties[oldProp] = properties[newProp];
}
}
}

return properties;
}

export function getProperties(
scanOptions: ScanOptions,
startTimestampMs: number,
Expand Down Expand Up @@ -416,11 +439,11 @@ export function getProperties(
// Hotfix host properties with custom SonarCloud URL
const hostProperties = getHostProperties(properties);

return {
return hotfixDeprecatedProperties({
...properties,
// Can't be overridden:
...hostProperties,
...getBootstrapperProperties(startTimestampMs),
'sonar.projectBaseDir': projectBaseDir,
};
});
}
3 changes: 3 additions & 0 deletions src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ export async function download(url: string, destPath: string, overrides?: AxiosR
url,
method: 'GET',
responseType: 'stream',
headers: {
Accept: 'application/octet-stream',
},
lucas-paulger-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
...overrides,
});

Expand Down
36 changes: 20 additions & 16 deletions src/scanner-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,11 @@
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import fsExtra from 'fs-extra';
import { spawn } from 'child_process';
import fs from 'fs';
import { API_V2_SCANNER_ENGINE_ENDPOINT } from './constants';
import {
extractArchive,
getCacheDirectories,
getCacheFileLocation,
validateChecksum,
} from './file';
import { getCacheDirectories, getCacheFileLocation, validateChecksum } from './file';
import { LogLevel, log, logWithPrefix } from './logging';
import { proxyUrlToJavaOptions } from './proxy';
import { download, fetch } from './request';
Expand Down Expand Up @@ -54,30 +50,33 @@ export async function fetchScannerEngine(properties: ScannerProperties) {

properties[ScannerProperty.SonarScannerWasEngineCacheHit] = 'false';

const { archivePath, unarchivePath: scannerEnginePath } = await getCacheDirectories(properties, {
const { archivePath } = await getCacheDirectories(properties, {
checksum,
filename,
});
const url = downloadUrl ?? API_V2_SCANNER_ENGINE_ENDPOINT;
log(LogLevel.DEBUG, `Starting download of Scanner Engine`);
await download(url, archivePath);
log(LogLevel.INFO, `Downloaded Scanner Engine to ${scannerEnginePath}`);
log(LogLevel.INFO, `Downloaded Scanner Engine to ${archivePath}`);

await validateChecksum(archivePath, checksum);
try {
await validateChecksum(archivePath, checksum);
} catch (error) {
await fsExtra.remove(archivePath);
lucas-paulger-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
throw error;
}

log(LogLevel.INFO, `Extracting Scanner Engine to ${scannerEnginePath}`);
await extractArchive(archivePath, scannerEnginePath);
return scannerEnginePath;
return archivePath;
}

async function logOutput(message: string) {
try {
// Try and assume the log comes from the scanner engine
const parsed = JSON.parse(message) as ScannerLogEntry;
logWithPrefix(parsed.level, 'ScannerEngine', parsed.formattedMessage);
if (parsed.throwable) {
logWithPrefix(parsed.level, 'ScannerEngine', parsed.message);
if (parsed.stacktrace) {
// Console.log without newline
process.stdout.write(parsed.throwable);
process.stdout.write(parsed.stacktrace);
}
} catch (e) {
process.stdout.write(message);
Expand All @@ -93,7 +92,12 @@ export function runScannerEngine(
log(LogLevel.INFO, 'Running the Scanner Engine');

// The scanner engine expects a JSON object of properties attached to a key name "scannerProperties"
const propertiesJSON = JSON.stringify({ scannerProperties: properties });
const propertiesJSON = JSON.stringify({
scannerProperties: Object.entries(properties).map(([key, value]) => ({
key,
value,
})),
});

// Run the scanner-engine
const args = [
Expand Down
10 changes: 8 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export type CacheFileData = { checksum: string; filename: string };

export type ScannerLogEntry = {
level: LogLevel;
formattedMessage: string;
throwable?: string;
message: string;
stacktrace?: string;
};

export enum ScannerProperty {
Expand Down Expand Up @@ -58,6 +58,12 @@ export enum ScannerProperty {
SonarScannerInternalSqVersion = 'sonar.scanner.internal.sqVersion',
SonarScannerCliVersion = 'sonar.scanner.version',
SonarScannerCliMirror = 'sonar.scanner.mirror',
// Deprecated properties:
SonarWsTimeout = 'sonar.ws.timeout',
HttpProxyHost = 'http.proxyHost',
HttpProxyPort = 'http.proxyPort',
HttpProxyUser = 'http.proxyUser',
HttpProxyPassword = 'http.proxyPassword',
}

export type ScannerProperties = {
Expand Down
Loading