Skip to content

Commit

Permalink
feat: Add @cerbos/files package to load policies from YAML or JSON …
Browse files Browse the repository at this point in the history
…files (#611)

* Add `@cerbos/files` package to load policies from YAML or JSON files

Signed-off-by: Andrew Haines <[email protected]>

* Work around the exclusion of `@defaultValue` in generated docs

microsoft/rushstack#4249

Signed-off-by: Andrew Haines <[email protected]>

---------

Signed-off-by: Andrew Haines <[email protected]>
  • Loading branch information
haines authored Jul 25, 2023
1 parent ffc4114 commit f98868d
Show file tree
Hide file tree
Showing 80 changed files with 3,089 additions and 91 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ The Cerbos JavaScript SDK makes it easy to interact with the Cerbos PDP from you

## Packages

For server-side Node.js applications, the [@cerbos/grpc](/packages/grpc/README.md) library provides a client ([`GRPC`](/docs/grpc.grpc.md)) for interacting with the Cerbos PDP over gRPC.
For server-side Node.js applications, the [@cerbos/grpc](/packages/grpc/README.md) package provides a client ([`GRPC`](/docs/grpc.grpc.md)) for interacting with the Cerbos PDP over gRPC.

For browser-based applications, the [@cerbos/http](/packages/http/README.md) library provides a client ([`HTTP`](/docs/http.http.md)) for interacting with the Cerbos PDP over HTTP.
For browser-based applications, the [@cerbos/http](/packages/http/README.md) package provides a client ([`HTTP`](/docs/http.http.md)) for interacting with the Cerbos PDP over HTTP.

Both clients extend the base [`Client`](/docs/core.client.md) class from [@cerbos/core](/packages/core/README.md), so they can be used interchangeably in isomorphic applications.

To instrument the clients with [OpenTelemetry](http://opentelemetry.io), use the [@cerbos/opentelemetry](/packages/opentelemetry/README.md) library.
To instrument the clients with [OpenTelemetry](http://opentelemetry.io), use the [@cerbos/opentelemetry](/packages/opentelemetry/README.md) package.

To load Cerbos policies from YAML or JSON files, use the [@cerbos/files](/packages/files/README.md) package.

## Further reading

Expand Down
28 changes: 27 additions & 1 deletion docs/core.client.addorupdatepolicies.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ Requires

- a dynamic [storage backend](https://docs.cerbos.dev/cerbos/latest/configuration/storage.html)<!-- -->.

## Example
## Example 1

Create a policy in code:

```typescript
await cerbos.addOrUpdatePolicies({
Expand All @@ -51,3 +52,28 @@ await cerbos.addOrUpdatePolicies({
});
```

## Example 2

Load a policy from a YAML or JSON file with [readPolicy()](./files.readpolicy.md)<!-- -->:

```typescript
import { readPolicy } from "@cerbos/files";

await cerbos.addOrUpdatePolicies({
policies: [await readPolicy("path/to/policy.yaml")],
});
```

## Example 3

Load policies and schemas from a directory with [readDirectory()](./files.readdirectory.md)<!-- -->:

```typescript
import { readDirectory } from "@cerbos/files";

const { policies, schemas } = await readDirectory("path/to/directory");

await cerbos.addOrUpdateSchemas({ schemas });
await cerbos.addOrUpdatePolicies({ policies });
```

49 changes: 39 additions & 10 deletions docs/core.client.addorupdateschemas.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,47 @@ Requires

- a dynamic [storage backend](https://docs.cerbos.dev/cerbos/latest/configuration/storage.html)<!-- -->.

## Example
## Example 1

Create a schema in code:

```typescript
await cerbos.addOrUpdateSchemas([{
id: "document.json",
definition: `{
"type": "object",
"properties": {
"owner": { "type": "string" }
}
}`,
}]);

await cerbos.addOrUpdateSchemas({
schemas: [{
id: "document.json",
definition: {
type: "object",
properties: {
owner: { type: "string" }
}
},
}],
});
```

## Example 2

Load a schema from a JSON file with [readSchema()](./files.readschema.md)<!-- -->:

```typescript
import { readSchema } from "@cerbos/files";

await cerbos.addOrUpdateSchemas({
schemas: [await readSchema("_schemas/path/to/schema.json")],
});
```

## Example 3

Load policies and schemas from a directory with [readDirectory()](./files.readdirectory.md)<!-- -->:

```typescript
import { readDirectory } from "@cerbos/files";

const { policies, schemas } = await readDirectory("path/to/directory");

await cerbos.addOrUpdateSchemas({ schemas });
await cerbos.addOrUpdatePolicies({ policies });
```

21 changes: 21 additions & 0 deletions docs/files.directorycontents.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@cerbos/files](./files.md) &gt; [DirectoryContents](./files.directorycontents.md)

## DirectoryContents interface

The contents of a directory, returned by [readDirectory()](./files.readdirectory.md)<!-- -->.

**Signature:**

```typescript
export interface DirectoryContents
```

## Properties

| Property | Modifiers | Type | Description |
| --- | --- | --- | --- |
| [policies](./files.directorycontents.policies.md) | | [Policy](./core.policy.md)<!-- -->\[\] | The policies found in the directory. |
| [schemas](./files.directorycontents.schemas.md) | | [Schema](./files.schema.md)<!-- -->\[\] | The schemas found in the directory's <code>_schemas</code> subdirectory. |

13 changes: 13 additions & 0 deletions docs/files.directorycontents.policies.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@cerbos/files](./files.md) &gt; [DirectoryContents](./files.directorycontents.md) &gt; [policies](./files.directorycontents.policies.md)

## DirectoryContents.policies property

The policies found in the directory.

**Signature:**

```typescript
policies: Policy[];
```
13 changes: 13 additions & 0 deletions docs/files.directorycontents.schemas.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@cerbos/files](./files.md) &gt; [DirectoryContents](./files.directorycontents.md) &gt; [schemas](./files.directorycontents.schemas.md)

## DirectoryContents.schemas property

The schemas found in the directory's `_schemas` subdirectory.

**Signature:**

```typescript
schemas: Schema[];
```
25 changes: 25 additions & 0 deletions docs/files.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@cerbos/files](./files.md)

## files package

Load Cerbos policies from YAML or JSON files.

## Functions

| Function | Description |
| --- | --- |
| [parsePolicy(contents)](./files.parsepolicy.md) | Parse a policy from a YAML- or JSON-encoded string. |
| [readDirectory(path)](./files.readdirectory.md) | Read the policy and schema files in a directory and its subdirectories. |
| [readPolicy(path)](./files.readpolicy.md) | Read a policy from a YAML or JSON file. |
| [readSchema(path, options)](./files.readschema.md) | Read a schema from a JSON file. |

## Interfaces

| Interface | Description |
| --- | --- |
| [DirectoryContents](./files.directorycontents.md) | The contents of a directory, returned by [readDirectory()](./files.readdirectory.md)<!-- -->. |
| [ReadSchemaOptions](./files.readschemaoptions.md) | Options for [readSchema()](./files.readschema.md)<!-- -->. |
| [Schema](./files.schema.md) | A JSON schema to be used to validate principal or resource attributes. |

24 changes: 24 additions & 0 deletions docs/files.parsepolicy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@cerbos/files](./files.md) &gt; [parsePolicy](./files.parsepolicy.md)

## parsePolicy() function

Parse a policy from a YAML- or JSON-encoded string.

**Signature:**

```typescript
export declare function parsePolicy(contents: string): Policy;
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| contents | string | the YAML- or JSON-encoded policy definition. |

**Returns:**

[Policy](./core.policy.md)

28 changes: 28 additions & 0 deletions docs/files.readdirectory.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@cerbos/files](./files.md) &gt; [readDirectory](./files.readdirectory.md)

## readDirectory() function

Read the policy and schema files in a directory and its subdirectories.

**Signature:**

```typescript
export declare function readDirectory(path: string): Promise<DirectoryContents>;
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| path | string | the path to the directory. |

**Returns:**

Promise&lt;[DirectoryContents](./files.directorycontents.md)<!-- -->&gt;

## Remarks

This function looks for policies and schemas stored in the [standard Cerbos directory layout](https://docs.cerbos.dev/cerbos/latest/policies/best_practices.html#_policy_repository_layout)<!-- -->.

24 changes: 24 additions & 0 deletions docs/files.readpolicy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@cerbos/files](./files.md) &gt; [readPolicy](./files.readpolicy.md)

## readPolicy() function

Read a policy from a YAML or JSON file.

**Signature:**

```typescript
export declare function readPolicy(path: string): Promise<Policy>;
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| path | string | the path to the policy file. |

**Returns:**

Promise&lt;[Policy](./core.policy.md)<!-- -->&gt;

25 changes: 25 additions & 0 deletions docs/files.readschema.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@cerbos/files](./files.md) &gt; [readSchema](./files.readschema.md)

## readSchema() function

Read a schema from a JSON file.

**Signature:**

```typescript
export declare function readSchema(path: string, options?: ReadSchemaOptions): Promise<Schema>;
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| path | string | the path to the schema file. |
| options | [ReadSchemaOptions](./files.readschemaoptions.md) | _(Optional)_ additional settings. |

**Returns:**

Promise&lt;[Schema](./files.schema.md)<!-- -->&gt;

18 changes: 18 additions & 0 deletions docs/files.readschemaoptions.id.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@cerbos/files](./files.md) &gt; [ReadSchemaOptions](./files.readschemaoptions.md) &gt; [id](./files.readschemaoptions.id.md)

## ReadSchemaOptions.id property

Unique ID for the schema, to be used to reference the schema from policies and from other schemas.

**Signature:**

```typescript
id?: string | undefined;
```

## Remarks

If the schema is nested under a directory called `_schemas`<!-- -->, the default ID will be the file path relative to the `_schemas` directory. Otherwise, the default ID will be the file's basename.

20 changes: 20 additions & 0 deletions docs/files.readschemaoptions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@cerbos/files](./files.md) &gt; [ReadSchemaOptions](./files.readschemaoptions.md)

## ReadSchemaOptions interface

Options for [readSchema()](./files.readschema.md)<!-- -->.

**Signature:**

```typescript
export interface ReadSchemaOptions
```

## Properties

| Property | Modifiers | Type | Description |
| --- | --- | --- | --- |
| [id?](./files.readschemaoptions.id.md) | | string \| undefined | _(Optional)_ Unique ID for the schema, to be used to reference the schema from policies and from other schemas. |

13 changes: 13 additions & 0 deletions docs/files.schema.definition.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@cerbos/files](./files.md) &gt; [Schema](./files.schema.md) &gt; [definition](./files.schema.definition.md)

## Schema.definition property

Definition of the schema.

**Signature:**

```typescript
definition: string;
```
21 changes: 21 additions & 0 deletions docs/files.schema.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@cerbos/files](./files.md) &gt; [Schema](./files.schema.md)

## Schema interface

A JSON schema to be used to validate principal or resource attributes.

**Signature:**

```typescript
export interface Schema extends SchemaInput
```
**Extends:** [SchemaInput](./core.schemainput.md)
## Properties
| Property | Modifiers | Type | Description |
| --- | --- | --- | --- |
| [definition](./files.schema.definition.md) | | string | Definition of the schema. |
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
| Package | Description |
| --- | --- |
| [@cerbos/core](./core.md) | Common types used by the [gRPC](./grpc.md) and [HTTP](./http.md) client libraries. |
| [@cerbos/files](./files.md) | Load Cerbos policies from YAML or JSON files. |
| [@cerbos/grpc](./grpc.md) | Client library for interacting with the Cerbos policy decision point over gRPC from server-side Node.js applications. |
| [@cerbos/http](./http.md) | Client library for interacting with the Cerbos policy decision point over HTTP from browser-based applications. |
| [@cerbos/lite](./lite.md) | Client library for interacting with WebAssembly Cerbos policy bundles from server-side Node.js and browser-based applications. |
Expand Down
Loading

0 comments on commit f98868d

Please sign in to comment.