Skip to content

Commit

Permalink
dan/per-10080-node-sdk-local-facts-uploader (#85)
Browse files Browse the repository at this point in the history
* Added support for PDP proxy facts

* Fixed lint

* Added log warning
  • Loading branch information
danyi1212 authored Jun 23, 2024
1 parent 914bf83 commit 2f21d3f
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 15 deletions.
46 changes: 46 additions & 0 deletions src/api/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,49 @@ export abstract class BasePermitApi {
}
}
}

export interface IWaitForSync {
/**
* Wait for the facts to be synchronized with the PDP. Available only when `proxyFactsViaPdp` is set to `true`.
* @param timeout - The maximum number of seconds to wait for the synchronization to complete.
* Set to null to wait indefinitely.
*/
waitForSync(timeout: number | null): this;
}

export abstract class BaseFactsPermitAPI extends BasePermitApi implements IWaitForSync {
constructor(protected config: IPermitConfig, protected logger: Logger) {
super(config, logger);
if (config.proxyFactsViaPdp) {
this.openapiClientConfig = new Configuration({
basePath: `${this.config.pdp}`,
accessToken: this.config.token,
baseOptions: {
headers: {
...this.openapiClientConfig.baseOptions.headers,
'X-Wait-Timeout':
this.config.factsSyncTimeout === null ? '' : this.config.factsSyncTimeout.toString(),
},
},
});
}
}

protected clone(): this {
return new (this.constructor as any)(this.config, this.logger);
}

public waitForSync(timeout: number | null): this {
if (this.config.proxyFactsViaPdp) {
const clone = this.clone();
clone.openapiClientConfig.baseOptions.headers['X-Wait-Timeout'] =
timeout === null ? '' : timeout.toString();
return clone;
} else {
this.logger.warn(
"Attempted to wait for sync, but 'proxyFactsViaPdp' is not enabled. Ignoring.",
);
return this;
}
}
}
6 changes: 3 additions & 3 deletions src/api/relationship-tuples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from '../openapi';
import { BASE_PATH } from '../openapi/base';

import { BasePermitApi, IPagination } from './base';
import { BaseFactsPermitAPI, IPagination, IWaitForSync } from './base';
import { ApiContextLevel, ApiKeyLevel } from './context';

export {
Expand Down Expand Up @@ -49,7 +49,7 @@ export interface IListRelationshipTuples extends IPagination {
/**
* API client for managing role createments.
*/
export interface IRelationshipTuplesApi {
export interface IRelationshipTuplesApi extends IWaitForSync {
/**
* Retrieves a list of role createments based on the specified filters.
*
Expand Down Expand Up @@ -111,7 +111,7 @@ export interface IRelationshipTuplesApi {
/**
* The RelationshipTuplesApi class provides methods for interacting with Role createments.
*/
export class RelationshipTuplesApi extends BasePermitApi implements IRelationshipTuplesApi {
export class RelationshipTuplesApi extends BaseFactsPermitAPI implements IRelationshipTuplesApi {
private relationshipTuples: AutogenRelationshipTuplesApi;

/**
Expand Down
6 changes: 3 additions & 3 deletions src/api/resource-instances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from '../openapi';
import { BASE_PATH } from '../openapi/base';

import { BasePermitApi, IPagination } from './base';
import { BaseFactsPermitAPI, IPagination, IWaitForSync } from './base';
import { ApiContextLevel, ApiKeyLevel } from './context';

export { ResourceInstanceCreate, ResourceInstanceRead, ResourceInstanceUpdate } from '../openapi';
Expand All @@ -18,7 +18,7 @@ export interface IListResourceInstanceUsers extends IPagination {
instanceKey: string;
}

export interface IResourceInstancesApi {
export interface IResourceInstancesApi extends IWaitForSync {
/**
* Retrieves a list of resource instances.
*
Expand Down Expand Up @@ -96,7 +96,7 @@ export interface IResourceInstancesApi {
/**
* The ResourceInstancesApi class provides methods for interacting with Permit ResourceInstances.
*/
export class ResourceInstancesApi extends BasePermitApi implements IResourceInstancesApi {
export class ResourceInstancesApi extends BaseFactsPermitAPI implements IResourceInstancesApi {
private instances: AutogenResourceInstancesApi;

/**
Expand Down
6 changes: 3 additions & 3 deletions src/api/role-assignments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from '../openapi';
import { BASE_PATH } from '../openapi/base';

import { BasePermitApi, IPagination } from './base';
import { BaseFactsPermitAPI, IPagination, IWaitForSync } from './base';
import { ApiContextLevel, ApiKeyLevel } from './context';

export {
Expand Down Expand Up @@ -50,7 +50,7 @@ export interface IListRoleAssignments extends IPagination {
/**
* API client for managing role assignments.
*/
export interface IRoleAssignmentsApi {
export interface IRoleAssignmentsApi extends IWaitForSync {
/**
* Retrieves a list of role assignments based on the specified filters.
*
Expand Down Expand Up @@ -107,7 +107,7 @@ export interface IRoleAssignmentsApi {
/**
* The RoleAssignmentsApi class provides methods for interacting with Role Assignments.
*/
export class RoleAssignmentsApi extends BasePermitApi implements IRoleAssignmentsApi {
export class RoleAssignmentsApi extends BaseFactsPermitAPI implements IRoleAssignmentsApi {
private roleAssignments: AutogenRoleAssignmentsApi;

/**
Expand Down
6 changes: 3 additions & 3 deletions src/api/tenants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from '../openapi';
import { BASE_PATH } from '../openapi/base';

import { BasePermitApi, IPagination } from './base';
import { BaseFactsPermitAPI, IPagination, IWaitForSync } from './base';
import { ApiContextLevel, ApiKeyLevel } from './context';

export { PaginatedResultUserRead, TenantCreate, TenantRead, TenantUpdate } from '../openapi';
Expand All @@ -19,7 +19,7 @@ export interface IListTenantUsers extends IPagination {
tenantKey: string;
}

export interface ITenantsApi {
export interface ITenantsApi extends IWaitForSync {
/**
* Retrieves a list of tenants.
*
Expand Down Expand Up @@ -118,7 +118,7 @@ export interface ITenantsApi {
/**
* The TenantsApi class provides methods for interacting with Permit Tenants.
*/
export class TenantsApi extends BasePermitApi implements ITenantsApi {
export class TenantsApi extends BaseFactsPermitAPI implements ITenantsApi {
private tenants: AutogenTenantsApi;

/**
Expand Down
6 changes: 3 additions & 3 deletions src/api/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { UserCreateBulkOperation } from '../openapi/types/user-create-bulk-opera
import { UserDeleteBulkOperation } from '../openapi/types/user-delete-bulk-operation';
import { UserReplaceBulkOperation } from '../openapi/types/user-replace-bulk-operation';

import { BasePermitApi, IPagination } from './base';
import { BaseFactsPermitAPI, IPagination, IWaitForSync } from './base';
import { ApiContextLevel, ApiKeyLevel } from './context';

export {
Expand Down Expand Up @@ -69,7 +69,7 @@ export interface IGetUserRoles {
readonly perPage?: number;
}

export interface IUsersApi {
export interface IUsersApi extends IWaitForSync {
/**
* Retrieves a list of users.
*
Expand Down Expand Up @@ -220,7 +220,7 @@ export interface IUsersApi {
/**
* The UsersApi class provides methods for interacting with Permit Users.
*/
export class UsersApi extends BasePermitApi implements IUsersApi {
export class UsersApi extends BaseFactsPermitAPI {
private users: AutogenUsersApi;
private roleAssignments: AutogenRoleAssignmentsApi;
private bulkOperationsApi: BulkOperationsApi;
Expand Down
11 changes: 11 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ export interface IPermitConfig {
* @see https://axios-http.com/docs/req_config
*/
axiosInstance: AxiosInstance;
/**
* Create facts via the PDP API instead of using the default Permit REST API.
*/
proxyFactsViaPdp: boolean;
/**
* The amount of time in seconds to wait for facts to be available
* in the PDP cache before returning the response.
*/
factsSyncTimeout: number | null;
}

/**
Expand Down Expand Up @@ -115,6 +124,8 @@ export class ConfigFactory {
throwOnError: true,
apiContext: new ApiContext(),
axiosInstance: globalAxios.create(),
proxyFactsViaPdp: false,
factsSyncTimeout: null,
};
}

Expand Down

0 comments on commit 2f21d3f

Please sign in to comment.