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

KSM-475 Added 'find by title' and 'prefix/no prefix' handling #126

Merged
merged 2 commits into from
Mar 13, 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
36 changes: 33 additions & 3 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import {expect, test} from '@jest/globals'
import {getRecordUids, parseSecretsInputs} from '../src/main'

test('Input parsing Ok', () => {
test('Input parsing OK', () => {
const parsedInputs = parseSecretsInputs(['BediNKCMG21ztm5xGYgNww/field/login > username'])
expect(parsedInputs[0].notation).toBe('BediNKCMG21ztm5xGYgNww/field/login')
expect(parsedInputs[0].destination).toBe('username')
})

test('Record uid extraction Ok', () => {
test('Record uid extraction OK', () => {
const recordUids = getRecordUids(parseSecretsInputs(['BediNKCMG21ztm5xGYgNww/field/login > username', 'BediNKCMG21ztm5xGYgNww/field/password > password']))
expect(recordUids).toStrictEqual(['BediNKCMG21ztm5xGYgNww'])
})

test('Input and Destination Splitting Ok', () => {
test('Record title extraction OK', () => {
const recordUids = getRecordUids(parseSecretsInputs(['My Secret Ttile/field/login > username', 'My Secret Ttile/field/password > password']))
expect(recordUids).toStrictEqual(['My Secret Ttile'])
})

test('Input and Destination splitting OK', () => {
const parsedInputs = parseSecretsInputs([
'BediNKCMG21ztm5xGYgNww/field/a b>ab',
'BediNKCMG21ztm5xGYgNww/field/a b >ab',
Expand All @@ -28,3 +33,28 @@ test('Input and Destination Splitting Ok', () => {
expect(parsedInput.destinationType).toBe(0)
})
})

test('Record Title and Destination splitting OK', () => {
const parsedInputs = parseSecretsInputs([
`Title w\/ special chars > and delims/field/a b>>ab`,
`Title w\/ special chars > and delims/field/a b> >ab`,
`Title w\/ special chars > and delims/field/a b> > ab`
])

parsedInputs.forEach((parsedInput, index) => {
expect(parsedInput.notation).toBe(`Title w\/ special chars > and delims/field/a b>`)
expect(parsedInput.destination).toBe('ab')
expect(parsedInput.destinationType).toBe(0)
})
})

test('Notation prefix and no prefix are OK', () => {
const parsedInputs = parseSecretsInputs(['keeper://Title1/field/a b > ab', 'Title1/field/a b > ab'])

parsedInputs.forEach((parsedInput, index) => {
let notations = ['keeper://Title1/field/a b', 'Title1/field/a b']
expect(notations).toContain(parsedInput.notation)
expect(parsedInput.destination).toBe('ab')
expect(parsedInput.destinationType).toBe(0)
})
})
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 41 additions & 6 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as core from '@actions/core'
import * as fs from 'fs'
import {KeeperFile, downloadFile, getSecrets, getValue, loadJsonConfig} from '@keeper-security/secrets-manager-core'
import {KeeperFile, downloadFile, getSecrets, getValue, loadJsonConfig, parseNotation} from '@keeper-security/secrets-manager-core'

enum DestinationType {
output,
Expand All @@ -9,17 +9,35 @@ enum DestinationType {
}

type SecretsInput = {
uid: string
selector: string
notation: string
destination: string
destinationType: DestinationType
}

const splitInput = (text: string): string[] => {
const n = text.lastIndexOf('>')
if (n < 0) return [text, '']
const notation = text.substring(0, n)
const destination = text.substring(n + 1)
return [notation.trimEnd(), destination.trimStart()]
}

export const parseSecretsInputs = (inputs: string[]): SecretsInput[] => {
const results: SecretsInput[] = []

for (const input of inputs) {
core.debug(`inputParts=[${input}]`)
const inputParts = input.split(/\s*>\s*/)
const inputParts = splitInput(input)
let [uid, selector] = ['', '']
try {
const notation = parseNotation(inputParts[0])
uid = notation[1].text ? notation[1].text[0] : ''
selector = notation[2].text ? notation[2].text[0] : ''
} catch (error) {
core.error(`Failed to parse KSM Notation: ${error instanceof Error ? error.message : ''}`)
}
let destinationType: DestinationType = DestinationType.output
let destination = inputParts[1]
core.debug(`destination=[${destination}]`)
Expand All @@ -30,13 +48,15 @@ export const parseSecretsInputs = (inputs: string[]): SecretsInput[] => {
destinationType = DestinationType.file
destination = destination.slice(5)
}
if (inputParts[0].split('/')[1] === 'file') {
if (selector === 'file') {
destinationType = DestinationType.file
}

core.debug(`notation=[${inputParts[0]}], destinationType=[${destinationType}], destination=[${destination}]`)
core.debug(`notation=[${inputParts[0]}], destinationType=[${destinationType}], destination=[${destination}], secret=[${uid}]`)

results.push({
uid,
selector,
notation: inputParts[0],
destination,
destinationType
Expand All @@ -48,7 +68,7 @@ export const parseSecretsInputs = (inputs: string[]): SecretsInput[] => {
export const getRecordUids = (inputs: SecretsInput[]): string[] => {
const set = new Set<string>()
for (const input of inputs) {
set.add(input.notation.split('/')[0])
set.add(input.uid)
}
return Array.from(set)
}
Expand All @@ -72,7 +92,22 @@ const run = async (): Promise<void> => {

core.debug('Retrieving Secrets from KSM...')
const options = {storage: loadJsonConfig(config)}
const secrets = await getSecrets(options, getRecordUids(inputs))

const rxUid = new RegExp('^[A-Za-z0-9_-]{22}$')
const recordUids = getRecordUids(inputs)
const hasTitles = recordUids.some(function (e) {
return !rxUid.test(e)
})
let uidFilter = recordUids && !hasTitles ? recordUids : undefined

let secrets = await getSecrets(options, uidFilter)
// there's a slight chance a valid title to match a recordUID (22 url-safe base64 chars)
// or a missing record or record not shared to the KSM App - we need to pull all records
if (uidFilter && secrets.records.length < recordUids.length) {
uidFilter = undefined
core.debug(`KSM Didn't get expected num records - requesting all (search by title or missing UID /not shared to the app/)`)
secrets = await getSecrets(options, uidFilter)
}
core.debug(`Retrieved [${secrets.records.length}] secrets`)

if (secrets.warnings) {
Expand Down
Loading