Skip to content

Commit

Permalink
Merge branch 'yeet-own-refs' into 'main'
Browse files Browse the repository at this point in the history
Use CLI-generated secret references

Closes #17 and #21

See merge request dev/b5/opvs!11
  • Loading branch information
jodyheavener committed May 9, 2022
2 parents cb469ad + d33a36b commit 92fcd3a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 95 deletions.
45 changes: 3 additions & 42 deletions src/items.test.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,8 @@
import { createItem, createItemField, createItemFile } from "../test/factories";
import { createItem, createItemField } from "../test/factories";
import { generateUUID } from "../test/utils";
import { commands, configGet, env, window } from "../test/vscode-mock";
import { COMMANDS, REGEXP } from "./constants";
import {
createSecretReference,
generatePasswordArg,
Items,
safeReferenceValue,
} from "./items";

describe("safeReferenceValue", () => {
it("returns the label if it doesn't contain illegal characters", () => {
const label = "label";
const result = safeReferenceValue(label, generateUUID());
expect(result).toBe(label);
});

it("returns the id if the label contains illegal characters", () => {
const id = generateUUID();
const result = safeReferenceValue("$money-yall$", id);
expect(result).toBe(id);
});
});

describe("createSecretReference", () => {
it("returns a formatted op secret reference for a field", () => {
const value = "some value";
const item = createItem();
const field = createItemField();
const result = createSecretReference(value, item, field);
expect(result).toBe(`op://${value}/${item.title}/${field.label}`);
expect(REGEXP.REFERENCE.test(result)).toBe(true);
});

it("returns a formatted op secret reference for a file", () => {
const value = "some value";
const item = createItem();
const file = createItemFile();
const result = createSecretReference(value, item, file);
expect(result).toBe(`op://${value}/${item.title}/${file.name}`);
expect(REGEXP.REFERENCE.test(result)).toBe(true);
});
});
import { COMMANDS } from "./constants";
import { generatePasswordArg, Items } from "./items";

describe("Items", () => {
let items: Items;
Expand Down
64 changes: 12 additions & 52 deletions src/items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type {
Field,
FieldAssignment,
FieldAssignmentType,
File,
Item,
OutputCategory,
} from "@1password/1password-js";
Expand All @@ -28,30 +27,6 @@ export interface SaveItemInput {
location: Range | Selection;
}

export interface GetItemResult {
vaultItem: Item;
field: Field;
}

export const safeReferenceValue = (label: string, id: string): string =>
REGEXP.REFERENCE_PERMITTED.test(label) ? label : id;

export const createSecretReference = (
vaultValue: string,
vaultItem: Item,
fieldOrFile: Field | File,
): string => {
const fieldValue =
"label" in fieldOrFile
? safeReferenceValue(fieldOrFile.label, fieldOrFile.id)
: safeReferenceValue(fieldOrFile.name, fieldOrFile.id);

return `op://${vaultValue}/${safeReferenceValue(
vaultItem.title,
vaultItem.id,
)}/${fieldValue}`;
};

export const generatePasswordArg = "generate-pasword";

export class Items {
Expand All @@ -72,7 +47,7 @@ export class Items {
);
}

public async getItem(): Promise<GetItemResult | void> {
public async getItem(): Promise<Field | void> {
if (!this.core.cli.valid) {
return this.getItemCallback();
}
Expand Down Expand Up @@ -126,7 +101,7 @@ export class Items {
}

const field = vaultItem.fields.find((f) => f.label === fieldValue);
return this.getItemCallback({ vaultItem, field });
return this.getItemCallback(field);
}

public async getReferenceMetadata(
Expand Down Expand Up @@ -205,7 +180,7 @@ export class Items {
}
}

const vaultItem = await this.core.cli.execute<Item>(() =>
let vaultItem = await this.core.cli.execute<Item>(() =>
item.create(fields, {
title: itemTitle,
category: "Login",
Expand All @@ -216,6 +191,10 @@ export class Items {
}),
);

// TODO: Should be able to remove this once the CLI
// returns references during create operations
vaultItem = await this.core.cli.execute<Item>(() => item.get(vaultItem.id));

if (!vaultItem) {
return;
}
Expand All @@ -227,13 +206,11 @@ export class Items {
);
}

private async getItemCallback(result?: GetItemResult): Promise<void> {
if (!result) {
private async getItemCallback(field?: Field): Promise<void> {
if (!field) {
return;
}

const { vaultItem, field } = result;

const editor = window.activeTextEditor;
const selections = editor?.selections;
if (!editor || selections.length === 0) {
Expand All @@ -245,11 +222,6 @@ export class Items {
return;
}

const vaultValue = safeReferenceValue(
this.core.vaultName,
this.core.vaultId,
);

if (editor && !editor.document.isClosed) {
const useReference = config.get<boolean>(
ConfigKey.ItemsUseSecretReferences,
Expand All @@ -259,9 +231,7 @@ export class Items {
for (const selection of selections) {
editBuilder.replace(
selection,
useReference
? createSecretReference(vaultValue, vaultItem, field)
: field.value,
useReference ? field.reference : field.value,
);
}
});
Expand Down Expand Up @@ -342,11 +312,6 @@ export class Items {
ConfigKey.ItemsUseSecretReferences,
);

const vaultValue = safeReferenceValue(
this.core.vaultName,
this.core.vaultId,
);

if (input === generatePasswordArg) {
const selections = editor?.selections;
if (selections.length === 1) {
Expand All @@ -356,9 +321,7 @@ export class Items {
await editor.edit((editBuilder) =>
editBuilder.insert(
selections[0].active,
useReference
? createSecretReference(vaultValue, vaultItem, field)
: field.value,
useReference ? field.reference : field.value,
),
);
}
Expand All @@ -375,10 +338,7 @@ export class Items {
(field) => field.value === itemValue,
);
await editor.edit((editBuilder) =>
editBuilder.replace(
location,
createSecretReference(vaultValue, vaultItem, field),
),
editBuilder.replace(location, field.reference),
);
}
}
Expand Down
1 change: 1 addition & 0 deletions test/factories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const createItem = (overrides: Partial<Item> = {}): Item => {
title: `Vault Item ${id}`,
vault: {
id: `vault-123`,
name: "My Vault",
},
category: "LOGIN",
last_edited_by: "user-123",
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

"@1password/1password-js@https://github.com/1Password/1password-js":
version "0.1.0"
resolved "https://github.com/1Password/1password-js#55e730a605ef93d3a4f9f665b4d6ce21e0f818e0"
resolved "https://github.com/1Password/1password-js#c24ab603fa8b0bc327e401fee8abb3fbecaceec7"
dependencies:
lookpath "^1.2.2"
semver "^7.3.6"
Expand Down

0 comments on commit 92fcd3a

Please sign in to comment.