Skip to content

Commit

Permalink
Merge pull request #2834 from GoogleCloudPlatform/nodejs-containerana…
Browse files Browse the repository at this point in the history
…lysis-migration

migrate code from googleapis/nodejs-containeranalysis
  • Loading branch information
NimJay authored Nov 11, 2022
2 parents 7fca47f + bdcb86a commit 2668a58
Show file tree
Hide file tree
Showing 22 changed files with 1,444 additions and 0 deletions.
67 changes: 67 additions & 0 deletions .github/workflows/container-analysis-snippets.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
name: container-analysis-snippets
on:
push:
branches:
- main
paths:
- 'container-analysis/snippets/**'
pull_request:
paths:
- 'container-analysis/snippets/**'
pull_request_target:
types: [labeled]
schedule:
- cron: '0 0 * * 0'
jobs:
test:
if: ${{ github.event.action != 'labeled' || github.event.label.name == 'actions:force-run' }}
runs-on: ubuntu-latest
timeout-minutes: 60
permissions:
contents: 'write'
pull-requests: 'write'
id-token: 'write'
steps:
- uses: actions/[email protected]
with:
ref: ${{github.event.pull_request.head.ref}}
repository: ${{github.event.pull_request.head.repo.full_name}}
- uses: 'google-github-actions/[email protected]'
with:
workload_identity_provider: 'projects/1046198160504/locations/global/workloadIdentityPools/github-actions-pool/providers/github-actions-provider'
service_account: '[email protected]'
create_credentials_file: 'true'
access_token_lifetime: 600s
- uses: actions/[email protected]
with:
node-version: 16
- run: npm install
working-directory: container-analysis/snippets
- run: npm test
working-directory: container-analysis/snippets
env:
MOCHA_REPORTER_SUITENAME: container_analysis_snippets
MOCHA_REPORTER_OUTPUT: container_analysis_snippets_sponge_log.xml
MOCHA_REPORTER: xunit
- if: ${{ github.event.action == 'labeled' && github.event.label.name == 'actions:force-run' }}
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
try {
await github.rest.issues.removeLabel({
name: 'actions:force-run',
owner: 'GoogleCloudPlatform',
repo: 'nodejs-docs-samples',
issue_number: context.payload.pull_request.number
});
} catch (e) {
if (!e.message.includes('Label does not exist')) {
throw e;
}
}
- if: ${{ github.event_name == 'schedule'}}
run: |
curl https://github.com/googleapis/repo-automation-bots/releases/download/flakybot-1.1.0/flakybot -o flakybot -s -L
chmod +x ./flakybot
./flakybot --repo GoogleCloudPlatform/nodejs-docs-samples --commit_hash ${{github.sha}} --build_url https://github.com/${{github.repository}}/actions/runs/${{github.run_id}}
1 change: 1 addition & 0 deletions .github/workflows/workflows.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"cloud-tasks/tutorial-gcf/function",
"composer",
"composer/functions/composer-storage-trigger",
"container-analysis/snippets",
"containerengine/hello-world",
"datacatalog/cloud-client",
"datalabeling",
Expand Down
3 changes: 3 additions & 0 deletions container-analysis/snippets/.eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
rules:
no-console: off
68 changes: 68 additions & 0 deletions container-analysis/snippets/createNote.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

// sample-metadata:
// title: Create Note
// description: Creates a Note with specified ID
// usage: node createNote.js "project-id" "note-id"
async function main(
projectId = 'your-project-id', // Your GCP Project ID
noteId = 'my-note-id' // Id of the note
) {
// [START containeranalysis_create_note]
/**
* TODO(developer): Uncomment these variables before running the sample
*/
// const projectId = 'your-project-id', // Your GCP Project ID
// const noteId = 'my-note-id' // Id of the note

// Import the library and create a client
const {ContainerAnalysisClient} = require('@google-cloud/containeranalysis');
const client = new ContainerAnalysisClient();

// Construct request
// Associate the Note with a metadata type
// https://cloud.google.com/container-registry/docs/container-analysis#supported_metadata_types
// Here, we use the type "vulnerabiltity"
const formattedParent = client.getGrafeasClient().projectPath(projectId);

// Creates and returns a new Note
const [note] = await client.getGrafeasClient().createNote({
parent: formattedParent,
noteId: noteId,
note: {
vulnerability: {
details: [
{
affectedCpeUri: 'foo.uri',
affectedPackage: 'foo',
affectedVersionStart: {
kind: 'MINIMUM',
},
affectedVersionEnd: {
kind: 'MAXIMUM',
},
},
],
},
},
});

console.log(`Note ${note.name} created.`);
// [END containeranalysis_create_note]
}

main(...process.argv.slice(2));
81 changes: 81 additions & 0 deletions container-analysis/snippets/createOccurrence.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

// sample-metadata:
// title: Create Occurrence
// description: Creates an Occurrence of a Note and attaches it as a metadata to an image
// usage: node createOccurrence.js "note-project-id" "note-id" "occurrence-project-id" "image url"
async function main(
noteProjectId = 'your-project-id', // Your GCP Project Id
noteId = 'my-note-id', // Id of the note
occurrenceProjectId = 'your-project-id', // GCP Project Id of Occurrence
// If you are using Google Container Registry
imageUrl = 'https://gcr.io/my-project/my-repo/my-image:123' // Image to attach metadata to
// If you are using Google Artifact Registry
// imageUrl = 'https://LOCATION-docker.pkg.dev/my-project/my-repo/my-image:123' // Image to attach metadata to
) {
// [START containeranalysis_create_occurrence]
/**
* TODO(developer): Uncomment these variables before running the sample
*/
// const noteProjectId = 'your-project-id', // Your GCP Project Id
// const noteId = 'my-note-id', // Id of the note
// const occurrenceProjectId = 'your-project-id', // GCP Project Id of Occurrence
// If you are using Google Container Registry
// const imageUrl = 'https://gcr.io/my-project/my-repo/my-image:123' // Image to attach metadata to
// If you are using Google Artifact Registry
// const imageUrl = 'https://LOCATION-docker.pkg.dev/my-project/my-repo/my-image:123' // Image to attach metadata to

// Import the library and create a client
const {ContainerAnalysisClient} = require('@google-cloud/containeranalysis');
const client = new ContainerAnalysisClient();

// Construct request
const formattedParent = client
.getGrafeasClient()
.projectPath(occurrenceProjectId);
const formattedNote = client
.getGrafeasClient()
.notePath(noteProjectId, noteId);

// Creates and returns a new Occurrence associated with an existing Note
const [occurrence] = await client.getGrafeasClient().createOccurrence({
parent: formattedParent,
occurrence: {
noteName: formattedNote,
resourceUri: imageUrl,
vulnerability: {
packageIssue: [
{
affectedCpeUri: 'foo.uri',
affectedPackage: 'foo',
affectedVersion: {
kind: 'MINIMUM',
},
fixedVersion: {
kind: 'MAXIMUM',
},
},
],
},
},
});
console.log(`Occurrence created ${occurrence.name}.`);
return occurrence;
// [END containeranalysis_create_occurrence]
}

main(...process.argv.slice(2));
45 changes: 45 additions & 0 deletions container-analysis/snippets/deleteNote.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

// sample-metadata:
// title: Delete Note
// description: Deletes a specified Note
// usage: node deleteNote.js "project-id" "note-id"
async function main(
projectId = 'your-project-id', // Your GCP Project Id
noteId = 'my-note-id' // Id of the note
) {
// [START containeranalysis_delete_note]
/**
* TODO(developer): Uncomment these variables before running the sample
*/
// const projectId = 'your-project-id', // Your GCP Project Id
// const noteId = 'my-note-id' // Id of the note

// Import the library and create a client
const {ContainerAnalysisClient} = require('@google-cloud/containeranalysis');
const client = new ContainerAnalysisClient();

// Get the full path to the note
const formattedName = client.notePath(projectId, noteId);

// Delete the note
await client.getGrafeasClient().deleteNote({name: formattedName});
console.log(`Note ${formattedName} deleted.`);
// [END containeranalysis_delete_note]
}

main(...process.argv.slice(2));
50 changes: 50 additions & 0 deletions container-analysis/snippets/deleteOccurrence.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

// sample-metadata:
// title: Delete Occurrence
// description: Deletes a specified Occurrence
// usage: node deleteOccurrence.js "project-id" "occurrence-id"
async function main(
projectId = 'your-project-id', // Your GCP Project ID
occurrenceId = 'my-occurrence' // The API-generated identifier associated with the occurrence
) {
// [START containeranalysis_delete_occurrence]
/**
* TODO(developer): Uncomment these variables before running the sample
*/
// const projectId = 'your-project-id', // Your GCP Project ID
// const occurrenceId = 'my-occurrence' // The API-generated identifier associated with the occurrence

// Import the library and create a client
const {ContainerAnalysisClient} = require('@google-cloud/containeranalysis');
const client = new ContainerAnalysisClient();

// Get full path to occurrence
const formattedName = client
.getGrafeasClient()
.occurrencePath(projectId, occurrenceId);

// Deletes an existing Occurrence from the server
await client.getGrafeasClient().deleteOccurrence({
name: formattedName,
});

console.log(`Occurrence deleted: ${formattedName}`);
// [END containeranalysis_delete_occurrence]
}

main(...process.argv.slice(2));
61 changes: 61 additions & 0 deletions container-analysis/snippets/getDiscoveryInfo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

// sample-metadata:
// title: Get Discovery Info
// description: Gets all Discovery Occurrences attached to specified image
// usage: node getDiscoveryInfo.js "project-id" "image-url"
async function main(
projectId = 'your-project-id', // Your GCP Project ID
// If you are using Google Container Registry
imageUrl = 'https://gcr.io/my-project/my-repo/my-image:123' // Image to attach metadata to
// If you are using Google Artifact Registry
// imageUrl = 'https://LOCATION-docker.pkg.dev/my-project/my-repo/my-image:123' // Image to attach metadata to
) {
// [START containeranalysis_discovery_info]
/**
* TODO(developer): Uncomment these variables before running the sample
*/
// const projectId = 'your-project-id', // Your GCP Project ID
// If you are using Google Container Registry
// const imageUrl = 'https://gcr.io/my-project/my-repo/my-image:123' // Image to attach metadata to
// If you are using Google Artifact Registry
// const imageUrl = 'https://LOCATION-docker.pkg.dev/my-project/my-repo/my-image:123' // Image to attach metadata to

// Import the library and create a client
const {ContainerAnalysisClient} = require('@google-cloud/containeranalysis');
const client = new ContainerAnalysisClient();

const formattedParent = client.getGrafeasClient().projectPath(projectId);
// Retrieves and prints the Discovery Occurrence created for a specified image
// The Discovery Occurrence contains information about the initial scan on the image
const [occurrences] = await client.getGrafeasClient().listOccurrences({
parent: formattedParent,
filter: `kind = "DISCOVERY" AND resourceUrl = "${imageUrl}"`,
});

if (occurrences.length > 0) {
console.log(`Discovery Occurrences for ${imageUrl}`);
occurrences.forEach(occurrence => {
console.log(`${occurrence.name}:`);
});
} else {
console.log('No occurrences found.');
}
// [END containeranalysis_discovery_info]
}

main(...process.argv.slice(2));
Loading

0 comments on commit 2668a58

Please sign in to comment.