-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- introducting of ScanConfig, ScanConfigService and related parts - it is now possible to define a scan configuration inside a spring value which can define mappings for checkmarx - CheckmarxInstallSetup does now use ScanConfigService to resolve mappings for teamIds and also presetIds which is given to adapter - Missing: adapter must do rest call to update presetId when project has been created.
- Loading branch information
Showing
16 changed files
with
713 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
.../java/com/daimler/sechub/domain/scan/product/checkmarx/CheckmarxInstallSetupImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package com.daimler.sechub.domain.scan.product.checkmarx; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import static org.mockito.Mockito.*; | ||
|
||
import com.daimler.sechub.domain.scan.config.NamePatternIdprovider; | ||
import com.daimler.sechub.domain.scan.config.ScanConfigService; | ||
|
||
public class CheckmarxInstallSetupImplTest { | ||
|
||
private CheckmarxInstallSetupImpl setupImplToTest; | ||
private ScanConfigService scanConfigService; | ||
|
||
@Before | ||
public void before() { | ||
setupImplToTest = new CheckmarxInstallSetupImpl(); | ||
setupImplToTest.teamIdForNewProjects="A0"; | ||
scanConfigService = mock(ScanConfigService.class); | ||
setupImplToTest.scanConfigService = scanConfigService; | ||
} | ||
|
||
@Test | ||
public void teamId_found_name_by_provider_returns_team_id_from_provider() { | ||
/* prepare */ | ||
NamePatternIdprovider provider = mock(NamePatternIdprovider.class); | ||
when(provider.getIdForName("abc")).thenReturn("A1"); | ||
|
||
when(scanConfigService.getNamePatternIdProvider("checkmarx.newproject.teamid")).thenReturn(provider); | ||
|
||
/* execute */ | ||
String result = setupImplToTest.getTeamIdForNewProjects("abc"); | ||
/* test */ | ||
assertEquals("A1",result); | ||
} | ||
|
||
@Test | ||
public void teamId_not_found_name_by_provider_returns_default_teamId() { | ||
/* prepare */ | ||
NamePatternIdprovider provider = mock(NamePatternIdprovider.class); | ||
when(provider.getIdForName("abc")).thenReturn(null); | ||
|
||
when(scanConfigService.getNamePatternIdProvider("checkmarx.newproject.teamid")).thenReturn(provider); | ||
|
||
/* execute */ | ||
String result = setupImplToTest.getTeamIdForNewProjects("abc"); | ||
/* test */ | ||
assertEquals("A0",result); | ||
} | ||
|
||
@Test | ||
public void presetId_found_name_by_provider_returns_preset_id_from_provider() { | ||
/* prepare */ | ||
NamePatternIdprovider provider = mock(NamePatternIdprovider.class); | ||
when(provider.getIdForName("abc")).thenReturn("B1"); | ||
|
||
when(scanConfigService.getNamePatternIdProvider("checkmarx.newproject.presetid")).thenReturn(provider); | ||
|
||
/* execute */ | ||
String result = setupImplToTest.getPresetIdForNewProjects("abc"); | ||
/* test */ | ||
assertEquals("B1",result); | ||
} | ||
|
||
@Test | ||
public void presetId_not_found_name_by_provider_returns_null() { | ||
/* prepare */ | ||
NamePatternIdprovider provider = mock(NamePatternIdprovider.class); | ||
when(provider.getIdForName("abc")).thenReturn(null); | ||
|
||
when(scanConfigService.getNamePatternIdProvider("checkmarx.newproject.presetid")).thenReturn(provider); | ||
|
||
/* execute */ | ||
String result = setupImplToTest.getPresetIdForNewProjects("abc"); | ||
/* test */ | ||
assertEquals(null,result); | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
sechub-scan-product-checkmarx/src/test/resources/checkmarx_scan_config1.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"apiVersion": "1.0", | ||
"type" : "scanConfig", | ||
|
||
"namePatternIdProviders": { | ||
"checkmarx.newproject.presetid": [ | ||
{ | ||
"namePattern": "my-java-project-.*", | ||
"id": "100021" | ||
}, | ||
{ | ||
"namePattern": "a-go-project-.*", | ||
"id": "100031" | ||
}, | ||
{ | ||
"namePattern": ".*", | ||
"id": "100001" | ||
} | ||
], | ||
"checkmarx.newproject.teamid": [ | ||
{ | ||
"namePattern": "my-java-project-.*", | ||
"id": "e2e4e8f4-ba17-453b-b194-c736c605700d" | ||
}, | ||
{ | ||
"namePattern": "a-go-project-.*", | ||
"id": "e2e4e8f4-ba17-453b-b194-d836c605700d" | ||
}, | ||
{ | ||
"namePattern": ".*", | ||
"id": "f2c4g8f4-ba17-453b-b194-c746c605700d" | ||
} | ||
] | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
sechub-scan/src/main/java/com/daimler/sechub/domain/scan/config/NamePatternIdprovider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.daimler.sechub.domain.scan.config; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class NamePatternIdprovider { | ||
|
||
|
||
private static final Logger LOG = LoggerFactory.getLogger(NamePatternIdprovider.class); | ||
|
||
private List<NamePatternToIdEntry> entries = new ArrayList<>(); | ||
|
||
public void add(NamePatternToIdEntry entry) { | ||
if (entry==null) { | ||
LOG.warn("Ignoring null entry"); | ||
return; | ||
} | ||
entries.add(entry); | ||
} | ||
|
||
/** | ||
* Resolves id for given name or <code>null</code> when no matchers available | ||
* @param name | ||
* @return id or <code>null</code> | ||
*/ | ||
public String getIdForName(String name) { | ||
for (NamePatternToIdEntry entry: entries) { | ||
if (entry.isMatching(name)) { | ||
return entry.getId(); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
} |
67 changes: 67 additions & 0 deletions
67
sechub-scan/src/main/java/com/daimler/sechub/domain/scan/config/NamePatternToIdEntry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.daimler.sechub.domain.scan.config; | ||
|
||
import java.util.regex.Pattern; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.daimler.sechub.sharedkernel.util.JSONable; | ||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
public class NamePatternToIdEntry implements JSONable<NamePatternToIdEntry>{ | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(NamePatternToIdEntry.class); | ||
|
||
private String namePattern; | ||
private String id; | ||
|
||
private Pattern regexp; | ||
|
||
private static final NamePatternToIdEntry JSON_INITIALIZER = new NamePatternToIdEntry(); | ||
|
||
public static NamePatternToIdEntry createFromJSON(String json) { | ||
return JSON_INITIALIZER.fromJSON(json); | ||
} | ||
|
||
private NamePatternToIdEntry() { | ||
/* only internal used */ | ||
} | ||
|
||
@JsonCreator | ||
public NamePatternToIdEntry(@JsonProperty("namePattern")String namePattern,@JsonProperty("id") String id) { | ||
this.namePattern = namePattern; | ||
this.id = id; | ||
|
||
try { | ||
regexp=Pattern.compile(namePattern); | ||
}catch(RuntimeException e) { | ||
LOG.error("Was not able to compile regexp: {}",namePattern, e); | ||
} | ||
} | ||
|
||
Pattern getRegexp() { | ||
return regexp; | ||
} | ||
|
||
public String getNamePattern() { | ||
return namePattern; | ||
} | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
public boolean isMatching(String string) { | ||
if (string == null || regexp==null) { | ||
return false; | ||
} | ||
return regexp.matcher(string).matches(); | ||
} | ||
|
||
@Override | ||
public Class<NamePatternToIdEntry> getJSONTargetClass() { | ||
return NamePatternToIdEntry.class; | ||
} | ||
|
||
} |
Oops, something went wrong.