diff --git a/model-armor/snippets/getFolderFloorSettings.js b/model-armor/snippets/getFolderFloorSettings.js index bacb9570de..6e66376d2c 100644 --- a/model-armor/snippets/getFolderFloorSettings.js +++ b/model-armor/snippets/getFolderFloorSettings.js @@ -19,7 +19,7 @@ * * @param {string} folderId - The ID of the Google Cloud folder for which to retrieve floor settings. */ -async function getFolderFloorSettings(folderId) { +async function main(folderId) { // [START modelarmor_get_folder_floor_settings] /** * TODO(developer): Uncomment these variables before running the sample. @@ -34,14 +34,30 @@ async function getFolderFloorSettings(folderId) { // Instantiates a client const modelarmorClient = new ModelArmorClient(); - // Construct request - const request = { - name, - }; + async function getFolderFloorSettings() { + // Construct request + const request = { + name, + }; - const [response] = await modelarmorClient.getFloorSetting(request); - return response; + const [response] = await modelarmorClient.getFloorSetting(request); + return response; + } + + return await getFolderFloorSettings(); // [END modelarmor_get_folder_floor_settings] } -module.exports = getFolderFloorSettings; +module.exports.main = main; + +/* c8 ignore next 10 */ +if (require.main === module) { + main(...process.argv.slice(2)).catch(err => { + console.error(err.message); + process.exitCode = 1; + }); + process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; + }); +} diff --git a/model-armor/snippets/getOrganizationFloorSettings.js b/model-armor/snippets/getOrganizationFloorSettings.js index 10a0186777..016f82e03a 100644 --- a/model-armor/snippets/getOrganizationFloorSettings.js +++ b/model-armor/snippets/getOrganizationFloorSettings.js @@ -20,13 +20,12 @@ * @param {string} organizationId - The ID of the Google Cloud organization for which to retrieve * floor settings. */ -async function getOrganizationFloorSettings(organizationId) { +async function main(organizationId) { // [START modelarmor_get_organization_floor_settings] /** * TODO(developer): Uncomment these variables before running the sample. */ // const organizationId = 'your-organization-id'; - const name = `organizations/${organizationId}/locations/global/floorSetting`; // Imports the Modelarmor library @@ -35,15 +34,31 @@ async function getOrganizationFloorSettings(organizationId) { // Instantiates a client const modelarmorClient = new ModelArmorClient(); - // Construct request - const request = { - name, - }; + async function getOrganizationFloorSettings() { + // Construct request + const request = { + name, + }; + + // Run request + const [response] = await modelarmorClient.getFloorSetting(request); + return response; + } - // Run request - const [response] = await modelarmorClient.getFloorSetting(request); - return response; + return await getOrganizationFloorSettings(); // [END modelarmor_get_organization_floor_settings] } -module.exports = getOrganizationFloorSettings; +module.exports.main = main; + +/* c8 ignore next 10 */ +if (require.main === module) { + main(...process.argv.slice(2)).catch(err => { + console.error(err.message); + process.exitCode = 1; + }); + process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; + }); +} diff --git a/model-armor/snippets/getProjectFloorSettings.js b/model-armor/snippets/getProjectFloorSettings.js index 9a37d96a6c..2b6b0bf23f 100644 --- a/model-armor/snippets/getProjectFloorSettings.js +++ b/model-armor/snippets/getProjectFloorSettings.js @@ -20,7 +20,7 @@ * @param {string} projectId - The ID of the Google Cloud project for which to retrieve * floor settings. */ -async function getProjectFloorSettings(projectId) { +async function main(projectId) { // [START modelarmor_get_project_floor_settings] /** * TODO(developer): Uncomment these variables before running the sample. @@ -35,15 +35,31 @@ async function getProjectFloorSettings(projectId) { // Instantiates a client const modelarmorClient = new ModelArmorClient(); - // Construct request - const request = { - name, - }; + async function getProjectFloorSettings() { + // Construct request + const request = { + name, + }; - // Run request - const [response] = await modelarmorClient.getFloorSetting(request); - return response; + // Run request + const [response] = await modelarmorClient.getFloorSetting(request); + return response; + } + + return await getProjectFloorSettings(); // [END modelarmor_get_project_floor_settings] } -module.exports = getProjectFloorSettings; +module.exports.main = main; + +/* c8 ignore next 10 */ +if (require.main === module) { + main(...process.argv.slice(2)).catch(err => { + console.error(err.message); + process.exitCode = 1; + }); + process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; + }); +} diff --git a/model-armor/snippets/updateFolderFloorSettings.js b/model-armor/snippets/updateFolderFloorSettings.js index 1b59c6562f..d34415b67d 100644 --- a/model-armor/snippets/updateFolderFloorSettings.js +++ b/model-armor/snippets/updateFolderFloorSettings.js @@ -19,7 +19,7 @@ * * @param {string} folderId - Google Cloud folder ID for which floor settings need to be updated. */ -async function updateFolderFloorSettings(folderId) { +async function main(folderId) { // [START modelarmor_update_folder_floor_settings] /** * TODO(developer): Uncomment these variables before running the sample. @@ -34,42 +34,59 @@ async function updateFolderFloorSettings(folderId) { // Instantiates a client const client = new ModelArmorClient(); - const floorSettingsName = `folders/${folderId}/locations/global/floorSetting`; + async function updateFolderFloorSettings() { + const floorSettingsName = `folders/${folderId}/locations/global/floorSetting`; - // Build the floor settings with your preferred filters - // For more details on filters, please refer to the following doc: - // https://cloud.google.com/security-command-center/docs/key-concepts-model-armor#ma-filters - const floorSetting = { - name: floorSettingsName, - filterConfig: { - raiSettings: { - raiFilters: [ - { - filterType: - protos.google.cloud.modelarmor.v1.RaiFilterType.HARASSMENT, - confidenceLevel: - protos.google.cloud.modelarmor.v1.DetectionConfidenceLevel - .LOW_AND_ABOVE, - }, - { - filterType: - protos.google.cloud.modelarmor.v1.RaiFilterType.SEXUALLY_EXPLICIT, - confidenceLevel: - protos.google.cloud.modelarmor.v1.DetectionConfidenceLevel.HIGH, - }, - ], + // Build the floor settings with your preferred filters + // For more details on filters, please refer to the following doc: + // https://cloud.google.com/security-command-center/docs/key-concepts-model-armor#ma-filters + const floorSetting = { + name: floorSettingsName, + filterConfig: { + raiSettings: { + raiFilters: [ + { + filterType: + protos.google.cloud.modelarmor.v1.RaiFilterType.HARASSMENT, + confidenceLevel: + protos.google.cloud.modelarmor.v1.DetectionConfidenceLevel + .LOW_AND_ABOVE, + }, + { + filterType: + protos.google.cloud.modelarmor.v1.RaiFilterType + .SEXUALLY_EXPLICIT, + confidenceLevel: + protos.google.cloud.modelarmor.v1.DetectionConfidenceLevel.HIGH, + }, + ], + }, }, - }, - enableFloorSettingEnforcement: true, - }; + enableFloorSettingEnforcement: true, + }; - const request = { - floorSetting: floorSetting, - }; + const request = { + floorSetting: floorSetting, + }; - const [response] = await client.updateFloorSetting(request); - return response; + const [response] = await client.updateFloorSetting(request); + return response; + } + + return await updateFolderFloorSettings(); // [END modelarmor_update_folder_floor_settings] } -module.exports = updateFolderFloorSettings; +module.exports.main = main; + +/* c8 ignore next 10 */ +if (require.main === module) { + main(...process.argv.slice(2)).catch(err => { + console.error(err.message); + process.exitCode = 1; + }); + process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; + }); +} diff --git a/model-armor/snippets/updateOrganizationFloorSettings.js b/model-armor/snippets/updateOrganizationFloorSettings.js index f4ce121394..d78ef47d0c 100644 --- a/model-armor/snippets/updateOrganizationFloorSettings.js +++ b/model-armor/snippets/updateOrganizationFloorSettings.js @@ -19,7 +19,7 @@ * * @param {string} organizationId - Google Cloud organization ID for which floor settings need to be updated. */ -async function updateOrganizationFloorSettings(organizationId) { +async function main(organizationId) { // [START modelarmor_update_organization_floor_settings] /** * TODO(developer): Uncomment these variables before running the sample. @@ -32,42 +32,59 @@ async function updateOrganizationFloorSettings(organizationId) { const client = new ModelArmorClient(); - const floorSettingsName = `organizations/${organizationId}/locations/global/floorSetting`; + async function updateOrganizationFloorSettings() { + const floorSettingsName = `organizations/${organizationId}/locations/global/floorSetting`; - // Build the floor settings with your preferred filters - // For more details on filters, please refer to the following doc: - // https://cloud.google.com/security-command-center/docs/key-concepts-model-armor#ma-filters - const floorSetting = { - name: floorSettingsName, - filterConfig: { - raiSettings: { - raiFilters: [ - { - filterType: - protos.google.cloud.modelarmor.v1.RaiFilterType.HARASSMENT, - confidenceLevel: - protos.google.cloud.modelarmor.v1.DetectionConfidenceLevel - .LOW_AND_ABOVE, - }, - { - filterType: - protos.google.cloud.modelarmor.v1.RaiFilterType.SEXUALLY_EXPLICIT, - confidenceLevel: - protos.google.cloud.modelarmor.v1.DetectionConfidenceLevel.HIGH, - }, - ], + // Build the floor settings with your preferred filters + // For more details on filters, please refer to the following doc: + // https://cloud.google.com/security-command-center/docs/key-concepts-model-armor#ma-filters + const floorSetting = { + name: floorSettingsName, + filterConfig: { + raiSettings: { + raiFilters: [ + { + filterType: + protos.google.cloud.modelarmor.v1.RaiFilterType.HARASSMENT, + confidenceLevel: + protos.google.cloud.modelarmor.v1.DetectionConfidenceLevel + .LOW_AND_ABOVE, + }, + { + filterType: + protos.google.cloud.modelarmor.v1.RaiFilterType + .SEXUALLY_EXPLICIT, + confidenceLevel: + protos.google.cloud.modelarmor.v1.DetectionConfidenceLevel.HIGH, + }, + ], + }, }, - }, - enableFloorSettingEnforcement: true, - }; + enableFloorSettingEnforcement: true, + }; - const request = { - floorSetting: floorSetting, - }; + const request = { + floorSetting: floorSetting, + }; - const [response] = await client.updateFloorSetting(request); - return response; + const [response] = await client.updateFloorSetting(request); + return response; + } + + return await updateOrganizationFloorSettings(); // [END modelarmor_update_organization_floor_settings] } -module.exports = updateOrganizationFloorSettings; +module.exports.main = main; + +/* c8 ignore next 10 */ +if (require.main === module) { + main(...process.argv.slice(2)).catch(err => { + console.error(err.message); + process.exitCode = 1; + }); + process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; + }); +} diff --git a/model-armor/snippets/updateProjectFloorSettings.js b/model-armor/snippets/updateProjectFloorSettings.js index c740836553..73307187bb 100644 --- a/model-armor/snippets/updateProjectFloorSettings.js +++ b/model-armor/snippets/updateProjectFloorSettings.js @@ -19,7 +19,7 @@ * * @param {string} projectId - Google Cloud project ID for which floor settings need to be updated. */ -async function updateProjectFloorSettings(projectId) { +async function main(projectId) { // [START modelarmor_update_project_floor_settings] /** * TODO(developer): Uncomment these variables before running the sample. @@ -30,44 +30,62 @@ async function updateProjectFloorSettings(projectId) { const {ModelArmorClient} = modelarmor.v1; const {protos} = modelarmor; + // Initiate client const client = new ModelArmorClient(); - const floorSettingsName = `projects/${projectId}/locations/global/floorSetting`; + async function updateProjectFloorSettings() { + const floorSettingsName = `projects/${projectId}/locations/global/floorSetting`; - // Build the floor settings with your preferred filters - // For more details on filters, please refer to the following doc: - // https://cloud.google.com/security-command-center/docs/key-concepts-model-armor#ma-filters - const floorSetting = { - name: floorSettingsName, - filterConfig: { - raiSettings: { - raiFilters: [ - { - filterType: - protos.google.cloud.modelarmor.v1.RaiFilterType.HARASSMENT, - confidenceLevel: - protos.google.cloud.modelarmor.v1.DetectionConfidenceLevel - .LOW_AND_ABOVE, - }, - { - filterType: - protos.google.cloud.modelarmor.v1.RaiFilterType.SEXUALLY_EXPLICIT, - confidenceLevel: - protos.google.cloud.modelarmor.v1.DetectionConfidenceLevel.HIGH, - }, - ], + // Build the floor settings with your preferred filters + // For more details on filters, please refer to the following doc: + // https://cloud.google.com/security-command-center/docs/key-concepts-model-armor#ma-filters + const floorSetting = { + name: floorSettingsName, + filterConfig: { + raiSettings: { + raiFilters: [ + { + filterType: + protos.google.cloud.modelarmor.v1.RaiFilterType.HARASSMENT, + confidenceLevel: + protos.google.cloud.modelarmor.v1.DetectionConfidenceLevel + .LOW_AND_ABOVE, + }, + { + filterType: + protos.google.cloud.modelarmor.v1.RaiFilterType + .SEXUALLY_EXPLICIT, + confidenceLevel: + protos.google.cloud.modelarmor.v1.DetectionConfidenceLevel.HIGH, + }, + ], + }, }, - }, - enableFloorSettingEnforcement: true, - }; + enableFloorSettingEnforcement: true, + }; - const request = { - floorSetting: floorSetting, - }; + const request = { + floorSetting: floorSetting, + }; - const [response] = await client.updateFloorSetting(request); - return response; + const [response] = await client.updateFloorSetting(request); + return response; + } + + return await updateProjectFloorSettings(); // [END modelarmor_update_project_floor_settings] } -module.exports = updateProjectFloorSettings; +module.exports.main = main; + +/* c8 ignore next 10 */ +if (require.main === module) { + main(...process.argv.slice(2)).catch(err => { + console.error(err.message); + process.exitCode = 1; + }); + process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; + }); +} diff --git a/model-armor/test/modelarmor.test.js b/model-armor/test/modelarmor.test.js index 2f813b1f4f..1a176dab4f 100644 --- a/model-armor/test/modelarmor.test.js +++ b/model-armor/test/modelarmor.test.js @@ -21,6 +21,8 @@ const {DlpServiceClient} = require('@google-cloud/dlp'); let projectId; const locationId = process.env.GCLOUD_LOCATION || 'us-central1'; +const folderId = process.env.MA_FOLDER_ID; +const organizationId = process.env.MA_ORG_ID; const options = { apiEndpoint: `modelarmor.${locationId}.rep.googleapis.com`, }; @@ -75,6 +77,83 @@ async function deleteTemplate(templateName) { } } +// eslint-disable-next-line no-unused-vars +async function disableFloorSettings() { + try { + // Create global client + const global_client = new ModelArmorClient(); + + // Disable project floor settings + const [projectFloorSettings] = await global_client.getFloorSetting({ + name: `projects/${projectId}/locations/global/floorSetting`, + }); + + if (projectFloorSettings.enableFloorSettingEnforcement) { + const [updatedProjectSettings] = await global_client.updateFloorSetting({ + floorSetting: { + name: `projects/${projectId}/locations/global/floorSetting`, + enableFloorSettingEnforcement: false, + }, + updateMask: { + paths: ['enable_floor_setting_enforcement'], + }, + }); + console.log( + 'Disabled project floor settings:', + updatedProjectSettings.name + ); + } + + // Disable folder floor settings if folderId is available + if (folderId) { + const [folderFloorSettings] = await global_client.getFloorSetting({ + name: `folders/${folderId}/locations/global/floorSetting`, + }); + + if (folderFloorSettings.enableFloorSettingEnforcement) { + const [updatedFolderSettings] = await global_client.updateFloorSetting({ + floorSetting: { + name: `folders/${folderId}/locations/global/floorSetting`, + enableFloorSettingEnforcement: false, + }, + updateMask: { + paths: ['enable_floor_setting_enforcement'], + }, + }); + console.log( + 'Disabled folder floor settings:', + updatedFolderSettings.name + ); + } + } + + // Disable organization floor settings if organizationId is available + if (organizationId) { + const [orgFloorSettings] = await global_client.getFloorSetting({ + name: `organizations/${organizationId}/locations/global/floorSetting`, + }); + + if (orgFloorSettings.enableFloorSettingEnforcement) { + const [updatedOrgSettings] = await global_client.updateFloorSetting({ + floorSetting: { + name: `organizations/${organizationId}/locations/global/floorSetting`, + enableFloorSettingEnforcement: false, + }, + updateMask: { + paths: ['enable_floor_setting_enforcement'], + }, + }); + console.log( + 'Disabled organization floor settings:', + updatedOrgSettings.name + ); + } + } + } catch (error) { + console.error('Error disabling floor settings:', error); + } +} + // Helper function to create DLP template. async function createDlpTemplates() { try { @@ -292,16 +371,16 @@ describe('Model Armor tests', () => { after(async () => { for (const templateName of templatesToDelete) { + // TODO(b/424365799): Uncomment below code once the mentioned issue is resolved + // Disable floor settings to restore original state + // await disableFloorSettings(); + await deleteTemplate(templateName); } await deleteDlpTemplates(); }); - // =================== Floor Settings Tests =================== - - // TODO: Add tests for floor settings once the floor setting API issues are resolved. - // =================== Template Creation Tests =================== it('should create a basic template', async () => { @@ -858,4 +937,57 @@ describe('Model Armor tests', () => { 'NO_MATCH_FOUND' ); }); + + // =================== Floor Settings Tests =================== + + // TODO(b/424365799): Enable below tests once the mentioned issue is resolved + + it.skip('should get organization floor settings', async () => { + const getOrganizationFloorSettings = require('../snippets/getOrganizationFloorSettings'); + + const output = await getOrganizationFloorSettings.main(organizationId); + + const expectedName = `organizations/${organizationId}/locations/global/floorSetting`; + assert.equal(output.name, expectedName); + }); + + it.skip('should get folder floor settings', async () => { + const getFolderFloorSettings = require('../snippets/getFolderFloorSettings'); + + const output = await getFolderFloorSettings.main(folderId); + + // Check for expected name format in output + const expectedName = `folders/${folderId}/locations/global/floorSetting`; + assert.equal(output.name, expectedName); + }); + + it.skip('should get project floor settings', async () => { + const getProjectFloorSettings = require('../snippets/getProjectFloorSettings'); + + const output = await getProjectFloorSettings.main(projectId); + // Check for expected name format in output + const expectedName = `projects/${projectId}/locations/global/floorSetting`; + assert.equal(output.name, expectedName); + }); + + it.skip('should update organization floor settings', async () => { + const updateOrganizationFloorSettings = require('../snippets/updateOrganizationFloorSettings'); + const output = await updateOrganizationFloorSettings.main(organizationId); + // Check that the enableFloorSettingEnforcement=true + assert.equal(output.enableFloorSettingEnforcement, true); + }); + + it.skip('should update folder floor settings', async () => { + const updateFolderFloorSettings = require('../snippets/updateFolderFloorSettings'); + const output = await updateFolderFloorSettings.main(folderId); + // Check that the enableFloorSettingEnforcement=true + assert.equal(output.enableFloorSettingEnforcement, true); + }); + + it.skip('should update project floor settings', async () => { + const updateProjectFloorSettings = require('../snippets/updateProjectFloorSettings'); + const output = await updateProjectFloorSettings.main(projectId); + // Check that the enableFloorSettingEnforcement=true + assert.equal(output.enableFloorSettingEnforcement, true); + }); });