Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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, sanitizeString } 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 = sanitizeString(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, sanitizeString } 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('should remove git credentials', () => {
const env = cleanEnv({
GIT_PASSWORD,
})
test('from strings', () => {
expect(sanitizeString('test string')).toBe('test string')
expect(sanitizeString(`test string ${env.GIT_PASSWORD}`)).toBe('test string ****')
})
test('from objects', () => {
expect(sanitizeString(JSON.stringify({ test: 'some string' }))).toEqual('{"test":"some string"}')
expect(sanitizeString(JSON.stringify({ test: `some string ${env.GIT_PASSWORD}` }))).toEqual(
'{"test":"some string ****"}',
)
})
test('return empty string on empty or undefined input', () => {
expect(sanitizeString('')).toEqual('')
expect(sanitizeString(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 sanitizeString(str?: string) {
Comment thread
merll marked this conversation as resolved.
Outdated
return str ? str.replace(env.GIT_PASSWORD, '****') : ''
Comment thread
merll marked this conversation as resolved.
Outdated
}

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' ? sanitizeString(message) : `[unprocessable message type ${typeof message}]`
}
Loading