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

v2 -> v4 Migration (#128) #129

Merged
merged 9 commits into from
Feb 6, 2024
Merged

v2 -> v4 Migration (#128) #129

merged 9 commits into from
Feb 6, 2024

Conversation

vishalhcl-5960
Copy link

v2 -> v4

@@ -223,7 +223,8 @@ protected String getReportStatus(String reportId) throws IOException, JSONExcept
return FAILED;
}

String request_url = authProvider.getServer() + String.format(API_REPORT_STATUS, reportId);
String request_url = authProvider.getServer() + API_REPORT_STATUS;
request_url += "?$top=100&$filter=Id eq " +String.format("%s",reportId)+ "&$count=false";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should just use a single string with String.format() rather than combining 3 separate strings.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay. Now, I am using String.format("?$top=100&$filter=Id eq %s&$count=false",reportId) instead of using multiple strings.

@@ -61,7 +61,7 @@ public void run() throws ScannerException, InvalidTargetException {

@Override
public String getType() {
return SAST;
return "Sast";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason we changed to a hardcoded string instead of a constant here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure Matt, Created a constant for this string.

@@ -125,7 +125,7 @@ protected void analyzeIR() throws IOException, ScannerException {
}

protected void submitScan() {
setScanId(getServiceProvider().createAndExecuteScan(STATIC_ANALYZER, getProperties()));
setScanId(getServiceProvider().createAndExecuteScan("Sast", getProperties()));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason we changed to a hardcoded string instead of a constant here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, Created a constant for this string.

* @param params A JSON of scan parameters.
* @return The id of the submitted scan, if successful. Otherwise, null.
*/
public String createAndExecuteScanWithJSONParameter(String type, JSONObject params);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This overload should keep the same name "createAndExecuteScan" and just change the type for the second parameter.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah Matt, I tried the same but getting the "Ambiguous Method call" error, as the JSONObject class extends the Java.util.HashMap class.
Ambiguous method call. Both
createAndExecuteScan(String, Map<String, String>) in IScanServiceProvider and
createAndExecuteScan(String, JSONObject) in IScanServiceProvider match

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Matt, Any suggestion, How should i handle this issue?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now, using the existing method as we can call the older method with JSONobject as argument.

@@ -19,7 +20,7 @@ public interface IScan {
* @throws ScannerException if a fatal error occurs in the scan.
* @throws InvalidTargetException if the target is invalid.
*/
public void run() throws ScannerException, InvalidTargetException;
public void run() throws ScannerException, InvalidTargetException, JSONException;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't modify the interface here because not all IScan implementations may even use JSON data. Instead, the concrete implementation that could potentially throw a JSONException should catch that exception and throw a ScannerException.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, sure.

@@ -142,6 +143,31 @@ public HttpResponse post(String url, Map<String, String> headerProperties, Map<S
return post(url, headerProperties, body);
}

public HttpResponse posts(String url, Map<String, String> headerProperties, JSONObject params)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new code is almost exactly the same as the other method that it's overloading. I suggest combining the common logic into a private method that both overloads can call rather than duplicating the code.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah Matt, I tried the same but getting the "Ambiguous Method call" error.
Ambiguous method call. Both
post (String, Map<String, String>, Map<String, String>) in HttpClient and
post (String, Map<String, String>, JSONObject) in HttpClient match
.

Any suggestion, How should i handle this error?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now, using the existing method as we can call the older method with JSONobject as argument.

if(loginExpired() || !verifyApplication(params.get(APP_ID).toString()))
return null;
} catch (JSONException e) {
throw new RuntimeException(e);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't want to throw a RuntimeException. We should handle any error conditions just as they are handled in the other overload of this method.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay Matt, To handle this condition, I am throwing a new ScannerException().

private boolean shouldUseV4Api(String type) {
return type.equals(CoreConstants.SCA);
@Override
public String createAndExecuteScanWithJSONParameter(String type, JSONObject params) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method has a lot of duplicated code from the existing createAndExecuteScan() method. The duplicated code should be moved to a private method that both of these methods can call, rather than having 2 copies of the same code.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I can do that. But, in above comments you suggested that "This overload should keep the same name "createAndExecuteScan" and just change the type for the second parameter."
After making the changes as suggested by you, I am getting the "ambiguous method call" error.

String API_REGIONS = "/api/v2/Utils/Regions"; //$NON-NLS-1$
String API_IS_VALID_URL = "/api/v2/Scans/IsValidUrl"; //$NON-NLS-1$
String API_AUTHENTICATION = "/api/V2/Account/IsAuthenticated"; //$NON-NLS-1$
String API_REPORT_STATUS = "/api/v4/Reports"; //$NON-NLS-1$

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason these constants need to hard code v4 instead of using API_ENV_LATEST?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't have any such reason. Even for the v2 APIs we were using the hard code strings only.
Now, Using the constant instead of the hard code values.

}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since there are no changes in this file (just whitespace) can you remove the file from the PR?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I will remove this file from the PR.

@@ -51,7 +51,7 @@ public CloudScanServiceProvider(IProgress progress, IAuthenticationProvider auth

@Override
public String createAndExecuteScan(String type, Map<String, String> params) {
if(loginExpired() || !verifyApplication(params.get(APP_ID)))
if(loginExpired() || !verifyApplication(params.get(APP_ID).toString()))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will result in a NullPointerException if the key APP_ID does not exist in the map. I suggest doing a check for params.has(APP_ID) to avoid this.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay Matt. Now, using the params.containsKey(APP_ID) check too.

@vishalhcl-5960 vishalhcl-5960 merged commit 13cc25a into master Feb 6, 2024
2 checks passed
@vishalhcl-5960 vishalhcl-5960 deleted the v2-->-v4 branch February 6, 2024 18:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants