Skip to content
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
1 change: 1 addition & 0 deletions __tests__/directory/v3/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1179,6 +1179,7 @@ describe("DirectoryV3", () => {
const result = await directory.getManifest();
expect(result).toEqual({
body: "test",
model: {},
etag: "",
updatedAt: undefined,
});
Expand Down
56 changes: 48 additions & 8 deletions __tests__/integration/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import {
DirectoryServiceV3,
DirectoryV3,
displayStateMap,
HEADER_ASERTO_MANIFEST_REQUEST,
ImportMsgCase,
ImportOpCode,
MANIFEST_REQUEST_DEFAULT,
NotFoundError,
policyContext,
policyInstance,
Expand Down Expand Up @@ -71,6 +73,7 @@ describe("Integration", () => {
expect(res.status).toBe(200);
expect(res.body).toEqual({
check: true,
context: {},
trace: [],
});
});
Expand Down Expand Up @@ -601,11 +604,39 @@ describe("Integration", () => {

expect(res.status).toBe(200);
expect(res.body).toEqual([
{},
{},
{
object: { recv: "2", set: "2", delete: "0", error: "0" },
relation: { recv: "1", set: "1", delete: "0", error: "0" },
counter: {
delete: "0",
error: "0",
recv: "2",
set: "2",
type: "object",
},
},
{
counter: {
delete: "0",
error: "0",
recv: "1",
set: "1",
type: "relation",
},
},
{
object: {
recv: "2",
set: "2",
delete: "0",
error: "0",
type: "object",
},
relation: {
recv: "1",
set: "1",
delete: "0",
error: "0",
type: "relation",
},
},
]);
});
Expand Down Expand Up @@ -948,7 +979,14 @@ describe("Integration", () => {
it("get manifest serializes to json", async () => {
const GetManifest = async (_req: Request, res: Response) => {
try {
const manifest = await directoryClient.getManifest();
const manifest = await directoryClient.getManifest(
{},
{
headers: {
[HEADER_ASERTO_MANIFEST_REQUEST]: MANIFEST_REQUEST_DEFAULT,
},
},
);
res.status(200).send(manifest);
} catch (error) {
console.error(error);
Expand All @@ -971,6 +1009,7 @@ model:

# object type definitions
types:
### display_name: User ###
# user represents a user that can be granted role(s)
user:
relations:
Expand All @@ -980,19 +1019,19 @@ types:
### display_name: user#in_management_chain ###
in_management_chain: manager | manager->in_management_chain


### display_name: Group ###
# group represents a collection of users and/or (nested) groups
group:
relations:
member: user | group#member


### display_name: Identity ###
# identity represents a collection of identities for users
identity:
relations:
identifier: user


### display_name: Resource Creator ###
# resource creator represents a user type that can create new resources
resource-creator:
relations:
Expand All @@ -1017,6 +1056,7 @@ types:

expect(res.body).toEqual({
body: expectedBody,
model: {},
updatedAt: expect.any(String),
etag: expect.any(String),
});
Expand Down
93 changes: 74 additions & 19 deletions lib/directory/v3/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@ import {
ImportRequestSchema,
} from "@aserto/node-directory/src/gen/cjs/aserto/directory/importer/v3/importer_pb";
import {
MetadataSchema,
Model,
SetManifestRequestSchema,
} from "@aserto/node-directory/src/gen/cjs/aserto/directory/model/v3/model_pb";
import {
Body,
DeleteManifestRequest,
GetManifestRequest,
Metadata,
} from "@aserto/node-directory/src/gen/cjs/aserto/directory/model/v3/model_pb";
import {
CheckRequestSchema,
ChecksRequestSchema,
GetGraphRequestSchema,
GetObjectManyRequestSchema,
GetObjectRequestSchema,
Expand Down Expand Up @@ -63,6 +64,8 @@ import { DsRegistry } from "./serializer";
import {
CheckRequest,
CheckResponse,
ChecksRequest,
ChecksResponse,
DeleteManifestResponse,
DeleteObjectRequest,
DeleteObjectResponse,
Expand All @@ -73,6 +76,7 @@ import {
ExportResponse,
GetGraphRequest,
GetGraphResponse,
GetManifestRequest,
GetManifestResponse,
GetObjectManyRequest,
GetObjectManyResponse,
Expand Down Expand Up @@ -129,6 +133,18 @@ export enum ImportMsgCase {

const ADDRESS_REGEX = /https?:\/\//;

export const HEADER_ASERTO_MANIFEST_REQUEST =
"Aserto-Manifest-Request" as const;

// Return the manifest metadata and body.
export const MANIFEST_REQUEST_DEFAULT = "" as const;
// Only return the manifest metadata.
export const MANIFEST_REQUEST_METADATA_ONLY = "metadata-only" as const;
// Only return the manifest metadata and model.
export const MANIFEST_REQUEST_MODEL_ONLY = "model-only" as const;
// Return the manifest metadata, body, and model.
export const MANIFEST_REQUEST_WITH_MODEL = "with-model" as const;

export class DirectoryV3 {
ReaderClient: Client<typeof Reader>;
WriterClient: Client<typeof Writer>;
Expand Down Expand Up @@ -313,6 +329,22 @@ export class DirectoryV3 {
}
}

async checks(
params: ChecksRequest,
options?: CallOptions,
): Promise<ChecksResponse> {
try {
const response = await this.ReaderClient.checks(
create(ChecksRequestSchema, params),
options,
);

return this.registry.serializeResponse(response);
} catch (error) {
throw handleError(error, "checks");
}
}

async object(
params: GetObjectRequest,
options?: CallOptions,
Expand Down Expand Up @@ -526,28 +558,51 @@ export class DirectoryV3 {
};
});

const bodyData = data
return this.buildManifestResponse(data);
} catch (error) {
throw handleError(error, "getManifest");
}
}

private buildManifestResponse(
data: { [x: string]: JsonObject | Metadata | Body | undefined }[],
) {
let bodyData: Uint8Array[] = [new Uint8Array()];
let modelData: JsonObject = {};
let metadata: Metadata = create(MetadataSchema, {});
let body: string = "";

metadata = data[0]?.metadata as Metadata;

bodyData = data
.map((el) => {
return el["body"];
})
.filter((el) => el !== undefined)
.map((el) => {
return (el as Body)?.data;
});

body = new TextDecoder().decode(mergeUint8Arrays(...bodyData));

modelData =
data
.map((el) => {
return el["body"];
return el["model"];
})
.filter((el) => el !== undefined)
.map((el) => {
return (el as Body)?.data;
});

const body = new TextDecoder().decode(mergeUint8Arrays(...bodyData));
const metadata = data[0]?.metadata as Metadata;

return {
body,
updatedAt: metadata?.updatedAt
? this.registry.serializeResponse(metadata?.updatedAt)
: undefined,
etag: metadata?.etag,
};
} catch (error) {
throw handleError(error, "getManifest");
}
return el as JsonObject;
})?.[0] || {};

return {
body,
model: modelData,
updatedAt: metadata?.updatedAt
? this.registry.serializeResponse(metadata?.updatedAt)
: undefined,
etag: metadata?.etag,
};
}

async setManifest(
Expand Down
24 changes: 24 additions & 0 deletions lib/directory/v3/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ import {
DeleteManifestResponse as DeleteManifestResponse$,
SetManifestResponse as SetManifestResponse$,
} from "@aserto/node-directory/src/gen/cjs/aserto/directory/model/v3/model_pb";
import { GetManifestRequest as GetManifestRequest$ } from "@aserto/node-directory/src/gen/cjs/aserto/directory/model/v3/model_pb";
import {
CheckRequest as CheckRequest$,
CheckResponse as CheckResponse$,
ChecksRequest as ChecksRequest$,
ChecksResponse as ChecksResponse$,
GetGraphRequest as GetGraphRequest$,
GetGraphResponse as GetGraphResponse$,
GetObjectManyRequest as GetObjectManyRequest$,
Expand Down Expand Up @@ -49,6 +52,7 @@ import {
DescFile,
DescMessage,
DescService,
JsonObject,
Registry,
} from "@bufbuild/protobuf";
import { Timestamp } from "@bufbuild/protobuf/wkt";
Expand Down Expand Up @@ -170,6 +174,13 @@ export type DeleteRelationRequest = Optional<
>;

export type CheckRequest = Optional<Omit<CheckRequest$, "$typeName">, "trace">;
export type ChecksRequest = Omit<
ChecksRequest$,
"$typeName" | "default" | "checks"
> & {
default?: CheckRequest;
checks: CheckRequest[];
};

export type GetGraphRequest = Optional<
Omit<GetGraphRequest$, "$typeName">,
Expand Down Expand Up @@ -205,8 +216,14 @@ export type ImportRequest = Omit<
"$typeName"
>;

export type GetManifestRequest = Omit<
GetManifestRequest$,
"$typeName" | "$unknown"
>;

export type GetManifestResponse = {
body: string;
model: JsonObject;
updatedAt: Timestamp | undefined;
etag: string;
};
Expand All @@ -217,6 +234,13 @@ export type PaginationResponse = Omit<
>;

export type CheckResponse = Omit<CheckResponse$, "$typeName" | "$unknown">;
export type ChecksResponse = Omit<
ChecksResponse$,
"$typeName" | "$unknown" | "checks"
> & {
checks: CheckResponse[];
};

export type GetGraphResponse = Omit<
GetGraphResponse$,
"$typeName" | "$unknown"
Expand Down
10 changes: 10 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ import {
createImportRequest,
DirectoryServiceV3,
DirectoryV3,
HEADER_ASERTO_MANIFEST_REQUEST,
ImportMsgCase,
MANIFEST_REQUEST_DEFAULT,
MANIFEST_REQUEST_METADATA_ONLY,
MANIFEST_REQUEST_MODEL_ONLY,
MANIFEST_REQUEST_WITH_MODEL,
objectPropertiesAsStruct,
readAsyncIterable,
serializeAsyncIterable,
Expand All @@ -39,11 +44,16 @@ export {
DirectoryV3,
DirectoryV3Config,
displayStateMap,
HEADER_ASERTO_MANIFEST_REQUEST,
identityContext,
ImportMsgCase,
is,
jwtAuthz,
JWTIdentityMapper,
MANIFEST_REQUEST_DEFAULT,
MANIFEST_REQUEST_METADATA_ONLY,
MANIFEST_REQUEST_MODEL_ONLY,
MANIFEST_REQUEST_WITH_MODEL,
ManualIdentityMapper,
Middleware,
ObjectIDFromVar,
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@
"homepage": "https://github.com/aserto-dev/aserto-node#readme",
"dependencies": {
"@aserto/node-authorizer": "^0.21.0",
"@aserto/node-directory": "^0.32.0",
"@aserto/node-directory": "^0.33.0",
"@bufbuild/protobuf": "^2.2.3",
"@connectrpc/connect": "^2.0.0",
"@connectrpc/connect-node": "^2.0.0",
"@connectrpc/connect": "^2.0.1",
"@connectrpc/connect-node": "^2.0.1",
"express": "^4.21.2",
"jwt-decode": "^4.0.0"
},
Expand Down
Loading