Skip to content

Commit

Permalink
Merge pull request #76 from aserto-dev/import_improvements
Browse files Browse the repository at this point in the history
Provide ImportRequest message helpers
  • Loading branch information
gimmyxd authored Mar 29, 2024
2 parents 9dfec4f + 4bb5733 commit 8b411eb
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 30 deletions.
30 changes: 15 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -721,42 +721,42 @@ await directoryClient.deleteManifest();
### Import
```ts

import { ImportMsgCase, ImportOpCode, objectPropertiesAsStruct } from "@aserto/aserto-node"
const importRequest = createAsyncIterable([
{
opCode: 1,
opCode: ImportOpCode.SET,
msg: {
case: "object",
case: ImportMsgCase.OBJECT,
value: {
id: "id1",
id: "import-user",
type: "user",
properties: {},
properties: objectPropertiesAsStruct({ foo: "bar" }),
displayName: "name1",
},
},
},
{
opCode: 1,
opCode: ImportOpCode.SET,
msg: {
case: "object",
case: ImportMsgCase.OBJECT,
value: {
id: "id2",
type: "user",
id: "import-group",
type: "group",
properties: {},
displayName: "name2",
},
},
},
{
opCode: 1,
opCode: ImportOpCode.SET,
msg: {
case: "relation",
case: ImportMsgCase.RELATION,
value: {
objectId: "id1",
objectType: "user",
subjectId: "id2",
subjectId: "import-user",
subjectType: "user",
relation: "manager",
objectId: "import-group",
objectType: "group",
relation: "member",
},
},
},
Expand Down
6 changes: 2 additions & 4 deletions __tests__/directory/v3/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
SetObjectResponse,
SetRelationResponse,
} from "@aserto/node-directory/src/gen/cjs/aserto/directory/writer/v3/writer_pb";
import { Struct } from "@bufbuild/protobuf";
import { Code, ConnectError } from "@connectrpc/connect";
import { createAsyncIterable } from "@connectrpc/connect/protocol";
import * as connectNode from "@connectrpc/connect-node";
Expand All @@ -42,6 +41,7 @@ import {
EtagMismatchError,
InvalidArgumentError,
NotFoundError,
objectPropertiesAsStruct,
UnauthenticatedError,
} from "../../../lib/index";
jest.mock("fs");
Expand Down Expand Up @@ -741,9 +741,7 @@ describe("DirectoryV3", () => {
type: "user",
displayName: "test",
etag: "",
properties: Struct.fromJsonString(
JSON.stringify(params.object?.properties || {})
),
properties: objectPropertiesAsStruct(params.object?.properties || {}),
},
});

Expand Down
20 changes: 10 additions & 10 deletions __tests__/integration/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import {
DirectoryServiceV3,
DirectoryV3,
EtagMismatchError,
ImportMsgCase,
ImportOpCode,
NotFoundError,
objectPropertiesAsStruct,
policyContext,
policyInstance,
readAsyncIterable,
Expand Down Expand Up @@ -346,26 +349,23 @@ types:
});

it("imports objects and relationships", async () => {
const objectCase = "object" as const;
const relationCase = "relation" as const;

const importRequest = createAsyncIterable([
{
opCode: 1,
opCode: ImportOpCode.SET,
msg: {
case: objectCase,
case: ImportMsgCase.OBJECT,
value: {
id: "import-user",
type: "user",
properties: {},
properties: objectPropertiesAsStruct({ foo: "bar" }),
displayName: "name1",
},
},
},
{
opCode: 1,
opCode: ImportOpCode.SET,
msg: {
case: objectCase,
case: ImportMsgCase.OBJECT,
value: {
id: "import-group",
type: "group",
Expand All @@ -375,9 +375,9 @@ types:
},
},
{
opCode: 1,
opCode: ImportOpCode.SET,
msg: {
case: relationCase,
case: ImportMsgCase.RELATION,
value: {
subjectId: "import-user",
subjectType: "user",
Expand Down
31 changes: 30 additions & 1 deletion lib/directory/v3/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ import { Reader } from "@aserto/node-directory/src/gen/cjs/aserto/directory/read
import { GetObjectManyRequest } from "@aserto/node-directory/src/gen/cjs/aserto/directory/reader/v3/reader_pb";
import { Writer } from "@aserto/node-directory/src/gen/cjs/aserto/directory/writer/v3/writer_connect";
import { SetObjectRequest as SetObjectRequest$ } from "@aserto/node-directory/src/gen/cjs/aserto/directory/writer/v3/writer_pb";
import { PartialMessage, PlainMessage, Struct } from "@bufbuild/protobuf";
import {
JsonObject,
PartialMessage,
PlainMessage,
Struct,
} from "@bufbuild/protobuf";
import {
createPromiseClient,
Interceptor,
Expand Down Expand Up @@ -63,6 +68,20 @@ import {
*/
type DATA_TYPE_OPTIONS = keyof typeof Option;

/**
* Enum representing the different cases for importing data.
* The cases are "object" and "relation".
*
* OBJECT = "object"
*
* RELATION = "relation"
*
*/
export enum ImportMsgCase {
OBJECT = "object",
RELATION = "relation",
}

export class DirectoryV3 {
ReaderClient: PromiseClient<typeof Reader>;
WriterClient: PromiseClient<typeof Writer>;
Expand Down Expand Up @@ -457,6 +476,16 @@ export async function* createAsyncIterable<T>(items: T[]): AsyncIterable<T> {
yield* items;
}

/**
* Converts a JSON object to a Protobuf Struct.
*
* @param value - The JSON object to convert.
* @returns The converted Protobuf Struct.
*/
export function objectPropertiesAsStruct(value: JsonObject): Struct {
return Struct.fromJson(value);
}

function mergeUint8Arrays(...arrays: Uint8Array[]): Uint8Array {
const totalSize = arrays.reduce((acc, e) => acc + e.length, 0);
const merged = new Uint8Array(totalSize);
Expand Down
7 changes: 7 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Opcode } from "@aserto/node-directory/src/gen/cjs/aserto/directory/importer/v3/importer_pb";

import { Authorizer, authz } from "./authorizer";
import AnonymousIdentityMapper from "./authorizer/mapper/identity/anonymous";
import JWTIdentityMapper from "./authorizer/mapper/identity/jwt";
Expand All @@ -20,6 +22,8 @@ import {
createAsyncIterable,
DirectoryServiceV3,
DirectoryV3,
ImportMsgCase,
objectPropertiesAsStruct,
readAsyncIterable,
} from "./directory/v3";
import { DirectoryV3Config } from "./directory/v3/types";
Expand Down Expand Up @@ -55,6 +59,9 @@ export {
queryOptions,
readAsyncIterable,
SubIdentityMapper,
ImportMsgCase,
Opcode as ImportOpCode,
objectPropertiesAsStruct,
};

export * from "./errors";

0 comments on commit 8b411eb

Please sign in to comment.