Skip to content
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
5 changes: 3 additions & 2 deletions src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { BASEURL } from './constants'
import { GitPullError, HttpError, ValidationError } from './error'
import { Core } from './otomi-models'
import { FileMap, getFilePath, getResourceName, renderManifest, renderManifestForSecrets } from './repo'
import { getSanitizedErrorMessage, removeBlankAttributes } from './utils'
import { getSanitizedErrorMessage, removeBlankAttributes, sanitizeGitPassword } from './utils'

const debug = Debug('otomi:repo')

Expand Down Expand Up @@ -473,7 +473,8 @@ export class Git {
}
} catch (e) {
const sanitizedMessage = getSanitizedErrorMessage(e)
debug(`${sanitizedMessage} for command ${JSON.stringify(e.task?.commands).replace(env.GIT_PASSWORD, '****')}`)
const sanitizedCommands = sanitizeGitPassword(JSON.stringify(e.task?.commands))
debug(`${sanitizedMessage} for command ${sanitizedCommands}`)
debug('Git save error')
throw new GitPullError()
}
Expand Down
27 changes: 26 additions & 1 deletion src/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Cluster } from 'src/otomi-models'
import { getServiceUrl } from 'src/utils'
import { getSanitizedErrorMessage, getServiceUrl, sanitizeGitPassword } from 'src/utils'
import { cleanEnv, GIT_PASSWORD } from './validators'

describe('Utils', () => {
const cluster: Cluster = {
Expand Down Expand Up @@ -57,4 +58,28 @@ describe('Utils', () => {
expect(service.subdomain).toEqual('aa')
expect(service.domain).toEqual('bb.cc.dd.ee')
})

describe('sanitizeGitPassword should remove git credentials', () => {
const env = cleanEnv({
GIT_PASSWORD,
})
test('from strings', () => {
expect(sanitizeGitPassword('test string')).toBe('test string')
expect(sanitizeGitPassword(`${env.GIT_PASSWORD} test string ${env.GIT_PASSWORD}`)).toBe('**** test string ****')
})
test('from objects', () => {
expect(sanitizeGitPassword(JSON.stringify({ test: 'some string' }))).toEqual('{"test":"some string"}')
expect(sanitizeGitPassword(JSON.stringify({ test: `some string ${env.GIT_PASSWORD}` }))).toEqual(
'{"test":"some string ****"}',
)
})
test('return empty string on empty or undefined input', () => {
expect(sanitizeGitPassword('')).toEqual('')
expect(sanitizeGitPassword(undefined)).toEqual('')
})
test('extract message from exception', () => {
expect(getSanitizedErrorMessage(new Error('test error'))).toEqual('test error')
expect(getSanitizedErrorMessage(new Error(`test error ${env.GIT_PASSWORD}`))).toEqual('test error ****')
})
})
})
11 changes: 9 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,14 @@ export const objectToYaml = (obj: Record<string, any>, indent = 4, lineWidth = 2
return isEmpty(obj) ? '' : stringify(obj, { indent, lineWidth })
}

export function sanitizeGitPassword(str?: string) {
return str ? str.replaceAll(env.GIT_PASSWORD, '****') : ''
}

export function getSanitizedErrorMessage(error) {
const errorMessage = typeof error?.message === 'string' ? error.message.replace(env.GIT_PASSWORD, '****') : ''
return errorMessage
const message = error?.message
if (!message) {
return ''
}
return typeof message === 'string' ? sanitizeGitPassword(message) : `[unprocessable message type ${typeof message}]`
}
Loading