Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api key): add lifetimeDuration parameter and split interface #880

Merged
merged 1 commit into from
Oct 28, 2024
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
7 changes: 6 additions & 1 deletion renovate.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
{
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
"enabled": true,
"extends": ["github>coveo/renovate-presets", ":semanticPrefixFixDepsChoreOthers", "helpers:pinGitHubActionDigests", "schedule:earlyMondays"],
"extends": [
"github>coveo/renovate-presets",
":semanticPrefixFixDepsChoreOthers",
"helpers:pinGitHubActionDigests",
"schedule:earlyMondays"
],
"packageRules": [
{
"matchPackagePatterns": ["*"],
Expand Down
5 changes: 2 additions & 3 deletions src/resources/ApiKeys/ApiKeys.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import API from '../../APICore.js';
import {New} from '../BaseInterfaces.js';
import Resource from '../Resource.js';
import {ApiKeyModel, CreateApiKeyOptions, DuplicateApiKeyOptions} from './ApiKeysInterfaces.js';
import {ApiKeyModel, CreateApiKeyModel, CreateApiKeyOptions, DuplicateApiKeyOptions} from './ApiKeysInterfaces.js';

export default class ApiKey extends Resource {
static baseUrl = `/rest/organizations/${API.orgPlaceholder}/apikeys`;
Expand All @@ -10,7 +9,7 @@ export default class ApiKey extends Resource {
return this.api.get<ApiKeyModel[]>(ApiKey.baseUrl);
}

create(apiKey: New<ApiKeyModel, 'resourceId' | 'id'>, options?: CreateApiKeyOptions) {
create(apiKey: CreateApiKeyModel, options?: CreateApiKeyOptions) {
return this.api.post<ApiKeyModel>(this.buildPath(ApiKey.baseUrl, options), apiKey);
}

Expand Down
109 changes: 58 additions & 51 deletions src/resources/ApiKeys/ApiKeysInterfaces.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,53 @@
import {GranularResource, PrivilegeModel} from '../BaseInterfaces.js';
import {ApiKeyStatus} from '../Enums.js';
export interface ApiKeyModel extends GranularResource {

export interface ApiKeyBaseModel extends GranularResource {
/**
* The display name for the API key.
* @example PushApiKey
*/
displayName?: string;
/**
* A brief description of the API key.
* @example API key used for managing sources.
*/
description?: string;
/**
* A set of IP addresses allowed to use the API key.
*
* Notes:
* - IP ranges using CIDR notation are also supported.
* - If an IP address is included in both the `allowedIps` and the `deniedIps`, the IP address will be denied.
* @example ["192.168.0.0/16", "29.186.225.13"]
*/
allowedIps?: string[];
/**
* A set of IP addresses that will be denied access when attempting to use the API key.
*
* Notes:
* - IP ranges using CIDR notation are also supported.
* - If an IP address is included in both the `allowedIps` as well as the `deniedIps`, the IP address will be denied.
* @example [`"192.168.0.0/16"`, `"29.186.225.13"`]
*/
deniedIps?: string[];
/**
* A set of privileges.
*/
privileges?: PrivilegeModel[];
/**
* Additional configuration to be included in an API key. [to be revised]
*/
additionalConfiguration?: AdditionalConfigurationModel;
}

export interface ApiKeyModel extends ApiKeyBaseModel {
/**
* The unique identifier of the [organization](https://docs.coveo.com/en/222/) the API key was created for.
*/
organizationId?: string;
/**
* The unique identifier of the API key.
*
* **Example:** t4hk287bfj5sg6wskg64ckk5a
* @example t4hk287bfj5sg6wskg64ckk5a
*/
id: string;
/**
Expand All @@ -17,73 +56,29 @@ export interface ApiKeyModel extends GranularResource {
enabled?: boolean;
/**
* The value of the API key.
*
* **Example:** xx65151ec3-7b30-4772-a99a-09b4c0f71343
* @example xx65151ec3-7b30-4772-a99a-09b4c0f71343
*/
value?: string;
/**
* The display name for the API key.
*
* **Example:** PushApiKey
*/
displayName?: string;
/**
* A brief description of the API key.
*
* **Example:** API key used for managing sources.
*/
description?: string;
/**
* The username or the email address that was used to create this API key.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
createdBy?: any;
/**
* The API key creation date in Unix timestamp in milliseconds.
*
* **Example:** 1614969486000
* @example 1614969486000
*/
createdDate?: number;
/**
* The approximate API key last used date in Unix timestamp in milliseconds.
*
* **Example:** 1624575600000
* @example 1624575600000
*/
lastUsedDate?: number;
/**
* A set of IP addresses allowed to use the API key.
*
* **Notes:**
* - IP ranges using CIDR notation are also supported.
* - If an IP address is included in both the `allowedIps` as well as the `deniedIps`, the IP address will be denied.
*
* **Example:** [`"192.168.0.0/16"`, `"29.186.225.13"`]
*/
allowedIps?: string[];
/**
* A set of IP addresses that will be denied access when attempting to use the API key.
*
* **Notes:**
* - IP ranges using CIDR notation are also supported.
* - If an IP address is included in both the `allowedIps` as well as the `deniedIps`, the IP address will be denied.
*
* **Example:** [`"192.168.0.0/16"`, `"29.186.225.13"`]
*/
deniedIps?: string[];
/**
* A set of privileges.
*/
privileges?: PrivilegeModel[];
/**
* The unique identifier of the API key.
*
* **Example:** t4hk287bfj5sg6wskg64ckk5a
* @example t4hk287bfj5sg6wskg64ckk5a
*/
resourceId?: string;
/**
* Additional configuration to be included in an API key. [to be revised]
*/
additionalConfiguration?: AdditionalConfigurationModel;
/**
* The expiration date of the API key.
*/
Expand All @@ -106,6 +101,18 @@ export interface ApiKeyModel extends GranularResource {
disabledDate?: number;
}

export interface CreateApiKeyModel extends ApiKeyBaseModel {
/**
* The duration of the API key in ISO-8601 format. Once the duration is reached the key expires and cannot be used anymore.
* @example
* 'P1Y' for 1 year
* 'P14D' for 14 days
* 'P1M' for 1 month.
* 'P1Y3M14D' for 1 year, 3 months, and 14 days.
*/
lifetimeDuration?: string;
}

export interface CreateApiKeyOptions {
/**
* The unique identifier of the template on which to base the API key.
Expand Down
11 changes: 6 additions & 5 deletions src/resources/ApiKeys/test/ApiKeys.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import API from '../../../APICore.js';
import {New} from '../../BaseInterfaces.js';
import ApiKey from '../ApiKeys.js';
import {ApiKeyModel} from '../ApiKeysInterfaces.js';
import {ApiKeyModel, CreateApiKeyModel} from '../ApiKeysInterfaces.js';

jest.mock('../../../APICore.js');

Expand All @@ -25,9 +24,11 @@ describe('ApiKey', () => {

describe('create', () => {
it('should make a POST call to the ApiKeys base url', async () => {
const apiKeyModel: New<ApiKeyModel> = {
organizationId: 'a-smol-org',
value: '',
const apiKeyModel: CreateApiKeyModel = {
displayName: 'Cool key',
description: 'My super cool API key',
lifetimeDuration: 'P1M',
allowedIps: ['192.168.0.0/24'],
};

await apiKey.create(apiKeyModel);
Expand Down