-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: end-to-end run logic (feat/commander) (#81)
* Add commander for CLI invocation * Add logic to invoke scopes based on config file * Start adding anonymization logic
- Loading branch information
1 parent
71654c7
commit 3042616
Showing
42 changed files
with
1,041 additions
and
536 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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,55 @@ | ||
/* | ||
* Copyright IBM Corp. 2023, 2023 | ||
* | ||
* This source code is licensed under the Apache-2.0 license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
import { type Attributes } from '@opentelemetry/api' | ||
import { createHash } from 'crypto' | ||
|
||
type RequireAtLeastOne<T> = { | ||
[K in keyof T]-?: Required<Pick<T, K>> & Partial<Pick<T, Exclude<keyof T, K>>> | ||
}[keyof T] | ||
|
||
interface AnonymizeConfig<T extends Attributes> { | ||
hash?: Array<keyof T> | ||
substitute?: Array<keyof T> | ||
} | ||
|
||
/** | ||
* Anonymizes incoming raw data. The keys to be anonymized are specified in the config object | ||
* under either the `hash` key or the `substitute` key. | ||
* | ||
* @param raw - The attributes to anonymize. | ||
* @param config - The keys to either hash or substitute. | ||
* @returns The raw object will all specified keys replaced with anonymized versions of their | ||
* values. | ||
*/ | ||
export function anonymize<T extends Attributes>( | ||
raw: T, | ||
config: RequireAtLeastOne<AnonymizeConfig<T>> | ||
) { | ||
const anonymizedEntries = Object.entries(raw).map(([key, value]) => { | ||
if (typeof value !== 'string') { | ||
return { key, value } | ||
} | ||
|
||
if (config.hash?.includes(key) ?? false) { | ||
const hash = createHash('sha256') | ||
hash.update(value) | ||
return { key, value: hash.digest('hex') } | ||
} | ||
|
||
if (config.substitute?.includes(key) ?? false) { | ||
// TODO: implement this logic | ||
return { key, value: 'substituted!' } | ||
} | ||
|
||
return { key, value } | ||
}) | ||
|
||
return anonymizedEntries.reduce<Attributes>((prev, cur) => { | ||
prev[cur.key] = cur.value | ||
return prev | ||
}, {}) | ||
} |
This file contains 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 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,53 @@ | ||
/* | ||
* Copyright IBM Corp. 2023, 2023 | ||
* | ||
* This source code is licensed under the Apache-2.0 license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
import path from 'path' | ||
|
||
import { InvalidRootPathError } from '../exceptions/invalid-root-path-error.js' | ||
import { Loggable } from './log/loggable.js' | ||
import { Trace } from './log/trace.js' | ||
|
||
/** | ||
* Class capable of enumerating directories based on a leaf dir, root dir, and predicate function. | ||
*/ | ||
export class DirectoryEnumerator extends Loggable { | ||
/** | ||
* Finds directories between leaf and root (inclusive) which satisfy the predicate. | ||
* | ||
* @param leaf - Leaf-most directory. This must be inside of the root directory. | ||
* @param root - Root-most directory. | ||
* @param predicate - Function to indicate whether or not each enumerated directory should be part | ||
* of the result set. | ||
* @returns A (possibly empty) array of directories. | ||
*/ | ||
@Trace() | ||
public async find( | ||
leaf: string, | ||
root: string, | ||
predicate: (dir: string) => boolean | Promise<boolean> | ||
): Promise<string[]> { | ||
// Ensure the format is normalized | ||
leaf = path.resolve(leaf) | ||
root = path.resolve(root) | ||
|
||
// (if leaf is not a subpath of root, throw an exception) | ||
if (path.relative(root, leaf).startsWith('..')) { | ||
throw new InvalidRootPathError(root, leaf) | ||
} | ||
|
||
const dirs = [] | ||
|
||
for (let cur = leaf; cur !== root; cur = path.resolve(cur, '..')) { | ||
dirs.push(cur) | ||
} | ||
dirs.push(root) | ||
|
||
const checks = await Promise.all(dirs.map(predicate)) | ||
|
||
return dirs.filter((_dir, index) => checks[index] === true) | ||
} | ||
} |
This file contains 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 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 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 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
Oops, something went wrong.