Skip to content

Commit

Permalink
chocolatey support
Browse files Browse the repository at this point in the history
  • Loading branch information
unknown committed Nov 19, 2023
1 parent 785a631 commit 4625788
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 2 deletions.
12 changes: 11 additions & 1 deletion artemis-docs/docs/API/Artifacts/windows.md
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ Parse the Windows Security.evtx and try to correlate Logon and Logoff events.
| ----- | -------- | ---------------------------------- |
| path | `string` | Path to Windows Security.evtx file |

### lookupSecurityKey(path, offset) -> `SecurityKey`
### lookupSecurityKey(path, offset) -> `SecurityKey | WindowsError`

Parse Security Key data from Registry at provided Security Key offset. The
offset must be a postive number greater than 0. You can use `getRegistry(path)`
Expand Down Expand Up @@ -433,3 +433,13 @@ Sample output for one table (SmTbleSmp) that has two rows:
| ------ | ---------- | ---------------------------- |
| path | `string` | Path to Windows ESE database |
| tables | `string[]` | One or more tables to parse |

### getChocolateyInfo(alt_base) -> `ChocolateyInfo[] | WindowsError`

Return a list of installed Chocolatey packages. Will use the `ChocolateyInstall` ENV value by default (C:\ProgramData\chocolatey).

An optional alternative base path can also be provided

| Param | Type | Description |
| -------- | -------- | --------------------------------- |
| alt_base | `string` | Optional base path for Chocolatey |
64 changes: 64 additions & 0 deletions src/windows/chocolatey.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { ChocolateyInfo } from "../../types/windows/chocolatey.ts";
import { Nuspec } from "../../types/windows/nuspec.ts";
import { EncodingError } from "../encoding/errors.ts";
import { readXml } from "../encoding/xml.ts";
import { getEnvValue } from "../environment/env.ts";
import { FileError } from "../filesystem/errors.ts";
import { glob } from "../filesystem/mod.ts";
import { WindowsError } from "./errors.ts";

/**
* Function to get installed Chocolatey packages.
* By default will use the `ChocolateyInstall` ENV value to determine base path
* @param alt_base Optional alternative Chocolatey installation base_path
* @returns Array of `ChocolateyInfo` or `WindowsError`
*/
export function getChocolateyInfo(
alt_base?: string,
): ChocolateyInfo[] | WindowsError {
let base_path = getEnvValue("ChocolateyInstall");
if (base_path === "" && alt_base === undefined) {
return [];
} else if (alt_base != undefined) {
base_path = alt_base;
}

const glob_path = `${base_path}\\lib\\*\\*.nuspec`;
const globs = glob(glob_path);
if (globs instanceof FileError) {
return new WindowsError(
"CHOCOLATEYINFO",
`failed to glob ${glob_path}: ${globs.message}`,
);
}

const packages = [];
for (const path of globs) {
// Read the Nuspec XML file
const xml_data = readXml(path.full_path) as unknown as Nuspec;
if (xml_data instanceof EncodingError) {
console.warn(`failed to parse ${path.full_path}: ${xml_data.message}`);
continue;
}

// Try to grab some info
const chocolate: ChocolateyInfo = {
name: xml_data.package.metadata[0].id[0],
version: xml_data.package.metadata[0].version[0],
summary: typeof xml_data.package.metadata[0].summary === "undefined"
? ""
: xml_data.package.metadata[0].summary[0],
author: xml_data.package.metadata[0].authors[0],
license: typeof xml_data.package.metadata[0].licenseUrl === "undefined"
? ""
: xml_data.package.metadata[0].licenseUrl[0],
tags: typeof xml_data.package.metadata[0].tags === "undefined"
? []
: xml_data.package.metadata[0].tags[0].split(" "),
path: path.full_path,
};
packages.push(chocolate);
}

return packages;
}
3 changes: 2 additions & 1 deletion src/windows/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export type ErrorName =
| "USERASSIST"
| "USERS"
| "USNJRNL"
| "LOGONCORRELATION";
| "LOGONCORRELATION"
| "CHOCOLATEYINFO";

export class WindowsError extends ErrorBase<ErrorName> {}
9 changes: 9 additions & 0 deletions types/windows/chocolatey.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export interface ChocolateyInfo {
name: string;
version: string;
summary: string;
author: string;
license: string;
tags: string[];
path: string;
}
107 changes: 107 additions & 0 deletions types/windows/nuspec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/**
* Partial implementation of nuspec. Mainly for Chocolatey packages.
*
* https://learn.microsoft.com/en-us/nuget/reference/nuspec
*
* Sample output:
* ```json
* {
"package": {
"$": {
"xmlns": "http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd"
},
"metadata": [
{
"id": [
"7zip"
],
"version": [
"23.1.0"
],
"title": [
"7-Zip"
],
"authors": [
"Igor Pavlov"
],
"owners": [
"chocolatey-community,Rob Reynolds"
],
"licenseUrl": [
"http://www.7-zip.org/license.txt"
],
"projectUrl": [
"http://www.7-zip.org/"
],
"iconUrl": [
"https://cdn.jsdelivr.net/gh/chocolatey-community/chocolatey-packages@68b91a851cee97e55c748521aa6da6211dd37c98/icons/7zip.svg"
],
"requireLicenseAcceptance": [
"false"
],
"description": [
"7-Zip is a file archiver with a high compression ratio.\n\n## Features\n- High compression ratio in [7z format](http://www.7-zip.org/7z.html) with **LZMA** and **LZMA2** compression\n- Supported formats:\n- Packing / unpacking: 7z, XZ, BZIP2, GZIP, TAR, ZIP and WIM\n- Unpacking only: AR, ARJ, CAB, CHM, CPIO, CramFS, DMG, EXT, FAT, GPT, HFS, IHEX, ISO, LZH, LZMA, MBR, MSI, NSIS, NTFS, QCOW2, RAR, RPM, SquashFS, UDF, UEFI, VDI, VHD, VMDK, WIM, XAR and Z.\n- For ZIP and GZIP formats, **7-Zip** provides a compression ratio that is 2-10 % better than the ratio provided by PKZip and WinZip\n- Strong AES-256 encryption in 7z and ZIP formats\n- Self-extracting capability for 7z format\n- Integration with Windows Shell\n- Powerful File Manager\n- Powerful command line version\n- Plugin for FAR Manager\n- Localizations for 87 languages\n\n## Notes\n- The installer for 7-Zip is known to close the Explorer process. This means you may lose current work. If it doesn't automatically restart explorer, type `explorer` on the command shell to restart it.\n- **If the package is out of date please check [Version History](#versionhistory) for the latest submitted version. If you have a question, please ask it in [Chocolatey Community Package Discussions](https://github.com/chocolatey-community/chocolatey-packages/discussions) or raise an issue on the [Chocolatey Community Packages Repository](https://github.com/chocolatey-community/chocolatey-packages/issues) if you have problems with the package. Disqus comments will generally not be responded to.**"
],
"summary": [
"7-Zip is a file archiver with a high compression ratio."
],
"releaseNotes": [
"http://www.7-zip.org/history.txt"
],
"tags": [
"7zip zip archiver admin foss"
],
"packageSourceUrl": [
"https://github.com/chocolatey-community/chocolatey-packages/tree/master/automatic/7zip"
],
"docsUrl": [
"http://www.7-zip.org/faq.html"
],
"mailingListUrl": [
"https://sourceforge.net/p/sevenzip/discussion/45797/"
],
"bugTrackerUrl": [
"https://sourceforge.net/p/sevenzip/_list/tickets?source=navbar"
],
"dependencies": [
{
"dependency": [
{
"$": {
"id": "7zip.install",
"version": "[23.1.0]"
}
}
]
}
]
}
]
}
}
* ```
*/
export interface Nuspec {
package: {
$: {
xmlns: string;
};
metadata: {
id: string[];
version: string[];
title: string[];
authors: string[];
description: string[];
/**NUSPEC states this is deprecated */
owners?: string[];
licenseUrl?: string[];
projectUrl?: string[];
license?: string[];
iconUrl?: string[];
requireLicenseAcceptance?: string[];
summary?: string[];
tags?: string[];
copyright?: string[];
}[];
};
}

0 comments on commit 4625788

Please sign in to comment.