-
Notifications
You must be signed in to change notification settings - Fork 5
feat: Support usage of VCAP-SERVICES-FILE-PATH #171
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
Open
simeongelovski
wants to merge
13
commits into
main
Choose a base branch
from
ALS-8139
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f13c282
[ALS-8139] Support usage of VCAP-SERVICES-FILE-PATH
simeongelovski 22a3435
[ALS-8139] Add unit tests
simeongelovski 757e0c0
[ALS-8139] Fix formating
simeongelovski 5c04228
[ALS-8139] Fix formating
simeongelovski 06b0f27
[ALS-8139] Replace err message assertion with regex
simeongelovski 172a3e2
[ALS-8139] Add changelog and bump version
simeongelovski 3be23a0
don't disable rule no-console
sjvans b64774c
eslint . --max-warnings=0
sjvans 67881b2
formatting
sjvans d71df80
Merge branch 'main' into ALS-8139
sjvans 577f462
changelog formatting
sjvans 5310503
[ALS-8139] Remove console logs
simeongelovski 4ed2a16
[ALS-8139] Add logger
simeongelovski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,4 +1,6 @@ | ||
| const credentials = JSON.parse(process.env.VCAP_SERVICES) || {} | ||
| const isV3 = credentials['user-provided']?.some(obj => obj.tags.includes('auditlog-ng')) | ||
| const { loadVCAPServices } = require('../lib/utils') | ||
|
|
||
| const vcapServices = loadVCAPServices() | ||
| const isV3 = vcapServices['user-provided']?.some(obj => obj.tags.includes('auditlog-ng')) | ||
|
|
||
| module.exports = isV3 ? require('./log2alsng') : require('./log2restv2') |
This file contains hidden or 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 hidden or 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,112 @@ | ||
| const path = require('path') | ||
| const { loadVCAPServices } = require('../../lib/utils') | ||
| const cds = require('@sap/cds') | ||
| const LOG = cds.log('audit-log') | ||
|
|
||
| jest.mock('fs', () => ({ | ||
| readFileSync: jest.fn() | ||
| })) | ||
|
|
||
| const fs = require('fs') | ||
|
|
||
| describe('Test loadVCAPServices', () => { | ||
| const ORIGINAL_ENV = process.env | ||
| const FAKE_VCAP = { 'user-provided': [{ name: 'test', credentials: { token: 'abc' } }] } | ||
| const INVALID_JSON = 'invalid json' | ||
| const FAKE_PATH = '/path/to/vcap.json' | ||
|
|
||
| let logSpy | ||
|
|
||
| beforeEach(() => { | ||
| delete process.env.VCAP_SERVICES_FILE_PATH | ||
| delete process.env.VCAP_SERVICES | ||
|
|
||
| jest.clearAllMocks() | ||
| logSpy = jest.spyOn(LOG, 'debug').mockImplementation(() => {}) | ||
| }) | ||
|
|
||
| afterAll(() => { | ||
| process.env = ORIGINAL_ENV | ||
| }) | ||
|
|
||
| test('loads and parses VCAP_SERVICES from VCAP_SERVICES_FILE_PATH', () => { | ||
| process.env.VCAP_SERVICES_FILE_PATH = FAKE_PATH | ||
|
|
||
| fs.readFileSync.mockReturnValueOnce(JSON.stringify(FAKE_VCAP)) | ||
|
|
||
| const result = loadVCAPServices() | ||
|
|
||
| expect(fs.readFileSync).toHaveBeenCalledWith(path.resolve(FAKE_PATH), 'utf8') | ||
| expect(result).toEqual(FAKE_VCAP) | ||
| expect(logSpy).toHaveBeenCalledWith(`VCAP_SERVICES loaded from file at ${FAKE_PATH}`) | ||
| }) | ||
|
|
||
| test('throws error when reading VCAP_SERVICES_FILE_PATH fails', () => { | ||
| const errorMessage = 'ENOENT: no such file or directory' | ||
| process.env.VCAP_SERVICES_FILE_PATH = FAKE_PATH | ||
|
|
||
| fs.readFileSync.mockImplementationOnce(() => { | ||
| const err = new Error(errorMessage) | ||
| err.code = 'ENOENT' | ||
| throw err | ||
| }) | ||
|
|
||
| expect(() => loadVCAPServices()).toThrow( | ||
| `Failed to read or parse VCAP_SERVICES from file at ${FAKE_PATH}: ${errorMessage}` | ||
| ) | ||
| expect(logSpy).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| test('throws error when JSON in VCAP_SERVICES_FILE_PATH is invalid', () => { | ||
| process.env.VCAP_SERVICES_FILE_PATH = FAKE_PATH | ||
|
|
||
| fs.readFileSync.mockReturnValueOnce(INVALID_JSON) | ||
|
|
||
| expect(() => loadVCAPServices()).toThrow( | ||
| new RegExp(`^Failed to read or parse VCAP_SERVICES from file at ${FAKE_PATH}:`) | ||
| ) | ||
| expect(logSpy).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| test('loads and parses VCAP_SERVICES from environment variable', () => { | ||
| process.env.VCAP_SERVICES = JSON.stringify(FAKE_VCAP) | ||
|
|
||
| const result = loadVCAPServices() | ||
|
|
||
| expect(result).toEqual(FAKE_VCAP) | ||
| expect(logSpy).toHaveBeenCalledWith('VCAP_SERVICES loaded from environment variable') | ||
| expect(fs.readFileSync).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| test('throws error when VCAP_SERVICES env var JSON is invalid', () => { | ||
| process.env.VCAP_SERVICES = INVALID_JSON | ||
|
|
||
| expect(() => loadVCAPServices()).toThrow(new RegExp(`^Failed to parse VCAP_SERVICES from environment variable:`)) | ||
| expect(logSpy).not.toHaveBeenCalled() | ||
| expect(fs.readFileSync).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| test('returns empty object when neither VCAP_SERVICES_FILE_PATH nor VCAP_SERVICES is set', () => { | ||
| const result = loadVCAPServices() | ||
|
|
||
| expect(result).toEqual({}) | ||
| expect(logSpy).not.toHaveBeenCalled() | ||
| expect(fs.readFileSync).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| test('VCAP_SERVICES_FILE_PATH takes precedence over VCAP_SERVICES', () => { | ||
| const fromFile = { from: 'file' } | ||
| const fromEnv = { from: 'env' } | ||
|
|
||
| process.env.VCAP_SERVICES_FILE_PATH = FAKE_PATH | ||
| process.env.VCAP_SERVICES = JSON.stringify(fromEnv) | ||
|
|
||
| fs.readFileSync.mockReturnValueOnce(JSON.stringify(fromFile)) | ||
|
|
||
| const result = loadVCAPServices() | ||
|
|
||
| expect(result).toEqual(fromFile) | ||
| expect(fs.readFileSync).toHaveBeenCalledTimes(1) | ||
| expect(logSpy).toHaveBeenCalledWith(`VCAP_SERVICES loaded from file at ${FAKE_PATH}`) | ||
| }) | ||
| }) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.