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

Unblock dev #816

Merged
merged 3 commits into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/lib/audit-logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const fetch = require('node-fetch')
const fs = require('fs')
const path = require('path')
const chalk = require('chalk')
const { getCliEnv, PROD_ENV } = require('@adobe/aio-lib-env')
const aioLogger = require('@adobe/aio-lib-core-logging')('@adobe/aio-cli-plugin-app:lib-audit-logger', { provider: 'debug' })

const OPERATIONS = {
AB_APP_DEPLOY: 'ab_app_deploy',
Expand All @@ -33,6 +35,11 @@ const AUDIT_SERVICE_ENPOINTS = {
* @param {string} env valid env stage|prod
*/
async function sendAuditLogs (accessToken, logEvent, env = 'prod') {
// TODO: this is blocked by the audit service only being available in stage
// remove this check once the service is available in prod
if (env !== 'stage') {
return
}
const url = AUDIT_SERVICE_ENPOINTS[env]
const payload = {
event: logEvent
Expand Down Expand Up @@ -60,6 +67,11 @@ async function sendAuditLogs (accessToken, logEvent, env = 'prod') {
* @returns {object} logEvent
*/
function getAuditLogEvent (flags, project, event) {
if (getCliEnv() === PROD_ENV) {
aioLogger.debug('Audit logging is currently disabled in production environment')
return null
}

let logEvent, logStrMsg
if (project && project.org && project.workspace) {
if (event === 'AB_APP_DEPLOY') {
Expand Down
1 change: 1 addition & 0 deletions test/BaseCommand.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ test('getLaunchUrlPrefix() warns on older url', async () => {
test('getLaunchUrlPrefix() uses stage launch prefix', async () => {
const cmd = new TheCommand()
libEnv.getCliEnv.mockReturnValue('stage')
mockAioConfig.get.mockReturnValue(0)
expect(cmd.getLaunchUrlPrefix()).toBe('https://experience-stage.adobe.com/?devMode=true#/custom-apps/?localDevUrl=')
})

Expand Down
19 changes: 19 additions & 0 deletions test/__mocks__/@adobe/aio-lib-env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
Copyright 2024 Adobe. All rights reserved.
This file is licensed to you 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 http://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 REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/

module.exports = {
getCliEnv: jest.fn(() => {
return 'stage'
}),
PROD_ENV: 'prod',
STAGE_ENV: 'stage'
}
15 changes: 12 additions & 3 deletions test/commands/lib/audit-logger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const chalk = require('chalk')
const fetch = require('node-fetch')
jest.mock('node-fetch', () => jest.fn())
const auditLogger = require('../../../src/lib/audit-logger')
const { getCliEnv } = require('@adobe/aio-lib-env')

jest.mock('fs')
jest.mock('chalk', () => ({
Expand Down Expand Up @@ -75,6 +76,7 @@ test('sendAuditLogs with valid params', async () => {
expect(fetch).toHaveBeenCalledWith(auditLogger.AUDIT_SERVICE_ENPOINTS[mockEnv], options)
})

// NOTE: this test is blocked until the audit service is available in prod
test('sendAuditLogs with default params', async () => {
fetch.mockReturnValue(mockResponse)
const options = {
Expand All @@ -86,8 +88,8 @@ test('sendAuditLogs with default params', async () => {
body: JSON.stringify({ event: mockLogEvent })
}
await auditLogger.sendAuditLogs(mockToken, mockLogEvent)
expect(fetch).toHaveBeenCalledTimes(1)
expect(fetch).toHaveBeenCalledWith(auditLogger.AUDIT_SERVICE_ENPOINTS.prod, options)
expect(fetch).toHaveBeenCalledTimes(0)
// expect(fetch).toHaveBeenCalledWith(auditLogger.AUDIT_SERVICE_ENPOINTS.prod, options)
})

test('sendAuditLogs error response', async () => {
Expand Down Expand Up @@ -192,7 +194,14 @@ describe('getAuditLogEvent', () => {
const event = 'AB_APP_DEPLOY'
const result = auditLogger.getAuditLogEvent(flags, {}, event)

expect(result).toBeUndefined()
expect(result).toBeFalsy()
})

test('should return undefined in PROD (for now)', () => {
getCliEnv.mockReturnValueOnce('prod')
const event = 'AB_APP_DEPLOY'
const result = auditLogger.getAuditLogEvent(flags, project, event)
expect(result).toBeFalsy()
})

test('should default operation to APP_TEST if event is not found in OPERATIONS', () => {
Expand Down