-
Notifications
You must be signed in to change notification settings - Fork 574
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: sbom maven & gradle extra args support (#4819)
Add support for the following args for the snyk sbom command: maven: --maven-aggregate-project --scan-unmanaged --file=<JAR_FILE_NAME> --scan-all-unmanaged gradle: --sub-project=<NAME>, --gradle-sub-project=<NAME> --all-sub-projects --configuration-matching=<CONFIGURATION_REGEX> --configuration-attributes=<ATTRIBUTE>[,<ATTRIBUTE>]... --init-script=<FILE> Co-authored-by: Krzysztof <krzysztof.lesniewski@snyk.io>
- Loading branch information
1 parent
ff6575e
commit 3ebe046
Showing
17 changed files
with
390 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
plugins { | ||
id 'java-library' | ||
} | ||
|
||
group = 'com.example' | ||
version = '1.0' | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
def testAttribute = Attribute.of('test.snykattr', String) | ||
|
||
dependencies.attributesSchema { | ||
attribute(testAttribute) | ||
} | ||
|
||
configurations { | ||
api { | ||
attributes { | ||
attribute(testAttribute, 'api') | ||
} | ||
} | ||
} | ||
|
||
dependencies { | ||
api 'org.jooq:jooq:3.18.6' | ||
implementation 'com.google.guava:guava:23.0' | ||
testImplementation 'junit:junit:4.+' | ||
} |
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,7 @@ | ||
allprojects { | ||
plugins.withType(JavaPlugin) { | ||
dependencies { | ||
implementation 'org.apache.logging.log4j:log4j-api:2.20.0' | ||
} | ||
} | ||
} |
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,15 @@ | ||
plugins { | ||
id 'java' | ||
} | ||
|
||
group = 'com.example' | ||
version = '1.0' | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
implementation project(':lib') | ||
implementation 'com.fasterxml.jackson.core:jackson-core:2.15.2' | ||
} |
Empty file.
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,16 @@ | ||
plugins { | ||
id 'java-library' | ||
} | ||
|
||
group = 'com.example' | ||
version = '1.0' | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
api 'org.jooq:jooq:3.18.6' | ||
implementation 'com.google.guava:guava:23.0' | ||
testImplementation 'junit:junit:4.+' | ||
} |
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,3 @@ | ||
rootProject.name = 'example-multi-project' | ||
include 'app' | ||
include 'lib' |
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,29 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<artifactId>parent-project</artifactId> | ||
<groupId>io.snyk</groupId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<groupId>io.snyk</groupId> | ||
<artifactId>core</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
|
||
<name>core</name> | ||
<!-- FIXME change it to the project's website --> | ||
<url>http://www.example.com</url> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.google.guava</groupId> | ||
<artifactId>guava</artifactId> | ||
<version>29.0-jre</version> | ||
</dependency> | ||
</dependencies> | ||
</project> |
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,21 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<artifactId>parent-project</artifactId> | ||
<groupId>io.snyk</groupId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<groupId>io.snyk</groupId> | ||
<artifactId>extra</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
|
||
<name>extra</name> | ||
<!-- FIXME change it to the project's website --> | ||
<url>http://www.example.com</url> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
</project> |
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,23 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="no"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
|
||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>io.snyk</groupId> | ||
<artifactId>parent-project</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
<packaging>pom</packaging> | ||
<name>parent-project</name> | ||
|
||
<url>http://www.example.com</url> | ||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<maven.compiler.source>1.8</maven.compiler.source> | ||
<maven.compiler.target>1.8</maven.compiler.target> | ||
</properties> | ||
|
||
<modules> | ||
<module>service</module> | ||
<module>core</module> | ||
<module>extra</module> | ||
</modules> | ||
</project> |
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,21 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<artifactId>parent-project</artifactId> | ||
<groupId>io.snyk</groupId> | ||
<version>1.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<groupId>io.snyk</groupId> | ||
<artifactId>service</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
|
||
<name>service</name> | ||
<!-- FIXME change it to the project's website --> | ||
<url>http://www.example.com</url> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
</project> |
Binary file not shown.
Binary file not shown.
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,31 @@ | ||
import { createProjectFromFixture } from '../../util/createProject'; | ||
import { runSnykCLI } from '../../util/runSnykCLI'; | ||
|
||
export async function runSnykSbomCliCycloneDxJsonForFixture( | ||
fixtureName: string, | ||
options: string, | ||
env: Record<string, string>, | ||
): Promise<any> { | ||
const project = await createProjectFromFixture(fixtureName); | ||
|
||
const { code, stdout, stderr } = await runSnykCLI( | ||
`sbom --org=aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee --format=cyclonedx1.4+json --debug ${options}`, | ||
{ | ||
cwd: project.path(), | ||
env, | ||
}, | ||
); | ||
|
||
if (code) { | ||
console.log(stderr); | ||
} | ||
|
||
expect(code).toEqual(0); | ||
|
||
let sbom; | ||
expect(() => { | ||
sbom = JSON.parse(stdout); | ||
}).not.toThrow(); | ||
|
||
return sbom; | ||
} |
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,119 @@ | ||
import { fakeServer } from '../../../acceptance/fake-server'; | ||
import { runSnykSbomCliCycloneDxJsonForFixture } from './common'; | ||
|
||
jest.setTimeout(1000 * 60 * 5); | ||
|
||
describe('snyk sbom: gradle options (mocked server only)', () => { | ||
let server; | ||
let env: Record<string, string>; | ||
|
||
beforeAll((done) => { | ||
const port = process.env.PORT || process.env.SNYK_PORT || '58584'; | ||
const baseApi = '/api/v1'; | ||
env = { | ||
...process.env, | ||
SNYK_API: 'http://localhost:' + port + baseApi, | ||
SNYK_HOST: 'http://localhost:' + port, | ||
SNYK_TOKEN: '123456789', | ||
SNYK_DISABLE_ANALYTICS: '1', | ||
}; | ||
server = fakeServer(baseApi, env.SNYK_TOKEN); | ||
server.listen(port, () => { | ||
done(); | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
server.restore(); | ||
}); | ||
|
||
afterAll((done) => { | ||
server.close(() => { | ||
done(); | ||
}); | ||
}); | ||
|
||
test('`sbom --sub-project=<NAME>` generates an SBOM for selected sub-project', async () => { | ||
const sbom = await runSnykSbomCliCycloneDxJsonForFixture( | ||
'gradle-multi-project', | ||
'--sub-project=lib', | ||
env, | ||
); | ||
|
||
expect(sbom.metadata.component.name).toEqual('gradle-multi-project/lib'); | ||
expect(sbom.components.length).toBeGreaterThan(1); | ||
}); | ||
|
||
test('`sbom --gradle-sub-project=<NAME>` generates an SBOM for selected sub-project', async () => { | ||
const sbom = await runSnykSbomCliCycloneDxJsonForFixture( | ||
'gradle-multi-project', | ||
'--gradle-sub-project=lib', | ||
env, | ||
); | ||
|
||
expect(sbom.metadata.component.name).toEqual('gradle-multi-project/lib'); | ||
expect(sbom.components.length).toBeGreaterThan(1); | ||
}); | ||
|
||
test('`sbom --all-sub-projects` generates an SBOM for all sub-projects', async () => { | ||
const sbom = await runSnykSbomCliCycloneDxJsonForFixture( | ||
'gradle-multi-project', | ||
'--all-sub-projects', | ||
env, | ||
); | ||
|
||
expect(sbom.metadata.component.name).toEqual('gradle-multi-project'); | ||
const listedComponentNames = sbom.components.map((it) => it.name); | ||
expect(listedComponentNames).toContain('gradle-multi-project'); | ||
expect(listedComponentNames).toContain('gradle-multi-project/lib'); | ||
expect(listedComponentNames).toContain('gradle-multi-project/app'); | ||
}); | ||
|
||
test('`sbom --configuration-matching=<CONFIGURATION_REGEX>` generates an SBOM only for matching configuration', async () => { | ||
const sbom = await runSnykSbomCliCycloneDxJsonForFixture( | ||
'gradle-configurations', | ||
'--configuration-matching=^runtimeClasspath$', | ||
env, | ||
); | ||
|
||
const listedComponentNames = sbom.components.map((it) => it.name); | ||
expect(listedComponentNames).toContain('org.jooq:jooq'); | ||
expect(listedComponentNames).toContain('com.google.guava:guava'); | ||
expect(listedComponentNames).not.toContain('junit:junit'); | ||
}); | ||
|
||
test('`sbom --configuration-attributes=<ATTRIBUTE>[,<ATTRIBUTE>]...` generates an SBOM only for matching variant', async () => { | ||
const sbom = await runSnykSbomCliCycloneDxJsonForFixture( | ||
'gradle-configurations', | ||
'--configuration-attributes=snykattr:api', | ||
env, | ||
); | ||
|
||
// I failed to prepare an example where this works, | ||
// so I will just test here that CLI executes successfully, | ||
// meaning the command does not fail due to unrecognized config. | ||
// To prepare the example I tried to set attribute on a configuration and filter by that, | ||
// but I was always getting a full list of dependencies instead of a subset. | ||
// The same I was getting when trying `snyk test` command. | ||
// I was adding attributes as documented here: https://docs.gradle.org/current/userguide/variant_attributes.html | ||
// I also tried using existing attribute `usage:java-api`, but also no luck. | ||
expect(sbom.metadata.component.name).toEqual('gradle-configurations'); | ||
// const listedComponentNames = sbom.components.map((it) => it.name); | ||
// expect(listedComponentNames).toContain('org.jooq:jooq'); | ||
// expect(listedComponentNames).not.toContain('com.google.guava:guava'); | ||
}); | ||
|
||
test('`sbom --init-script=<FILE>` applies given init script', async () => { | ||
const sbom = await runSnykSbomCliCycloneDxJsonForFixture( | ||
'gradle-configurations', | ||
'--init-script=test-init.gradle', | ||
env, | ||
); | ||
|
||
const listedComponentNames = sbom.components.map((it) => it.name); | ||
expect(listedComponentNames).toContain( | ||
'org.apache.logging.log4j:log4j-api', | ||
); // Dep added by init script | ||
}); | ||
}); |
Oops, something went wrong.