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

[Fix][WRS-2026]: Prevent changing (update/delete) of "Hub-Based" connectors via CLI #68

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
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@
"type": "string"
}
},
"required": ["name"],
"additionalProperties": false
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,6 @@
"$ref": "#/$defs/SpecCustomization"
}
},
"required": [
"name",
"clientId",
"clientSecret",
"authorizationServerMetadata"
],
"required": ["clientId", "clientSecret", "authorizationServerMetadata"],
"additionalProperties": false
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@
"type": "string"
}
},
"required": ["name", "clientId", "clientSecret", "tokenEndpoint"],
"required": ["clientId", "clientSecret", "tokenEndpoint"],
"additionalProperties": false
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
}
},
"required": [
"name",
"clientId",
"clientSecret",
"tokenEndpoint",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
"type": "string"
}
},
"required": ["name", "key", "value"],
"required": ["key", "value"],
"additionalProperties": false
}
23 changes: 12 additions & 11 deletions src/connector-cli/src/commands/delete/steps/remove-connector.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import { selectAvailableConnector } from '../../../common/select-available-connector';
import { httpErrorHandler, info, success, verbose, warn } from '../../../core';
import { getConnectorById } from '../../../common/get-connector';
import { httpErrorHandler, info, success, verbose } from '../../../core';

export async function removeConnector(
connectorEndpointBaseUrl: string,
connectorId: string,
token: string
): Promise<void> {
info('Retrieving connector to remove...');
const { id, name } = await getConnectorById({
baseUrl: connectorEndpointBaseUrl,
connectorId,
token,
});

info('Removing connector...');
const deleteConnectorEnpdoint = `${connectorEndpointBaseUrl}/${connectorId}`;
const deleteConnectorEnpdoint = `${connectorEndpointBaseUrl}/${id}`;

verbose('Removing connector via -> ' + deleteConnectorEnpdoint);

Expand All @@ -19,15 +26,9 @@ export async function removeConnector(
},
});

if (!res.ok && res.status === 404) {
warn(
`Connector with id ${connectorId} doesn't exist or you don't have permission to remove it`
);
const id = await selectAvailableConnector(connectorEndpointBaseUrl, token);
return removeConnector(connectorEndpointBaseUrl, id, token);
} else if (!res.ok) {
if (!res.ok) {
await httpErrorHandler(res);
}

success(`Connector "${connectorId}" is removed`);
success(`Connector "${name}" is removed`, { id, name });
}
1 change: 1 addition & 0 deletions src/connector-cli/src/commands/init/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const getPackageJson = (projectName: string, type: Type) => ({
type: type,
options: {},
mappings: {},
supportedAuth: [],
},
license: 'MIT',
main: `${outputDirectory}/${outputFilename}`,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { selectAvailableConnector } from '../../../common/select-available-connector';
import { httpErrorHandler, info, success, verbose, warn } from '../../../core';
import { getConnectorById } from '../../../common/get-connector';
import { httpErrorHandler, info, success, verbose } from '../../../core';
import { UpdateConnectorPayload } from '../types';

export async function updateExistingConnector(
Expand All @@ -8,38 +8,19 @@ export async function updateExistingConnector(
token: string,
payload: UpdateConnectorPayload
): Promise<void> {
info('Updating connector...');
const getConnectorEnpdoint = `${connectorEndpointBaseUrl}/${connectorId}`;

verbose(
`Checking connector's existing with id ${connectorId} -> ${getConnectorEnpdoint}`
);
const existingConnectorRes = await fetch(getConnectorEnpdoint, {
headers: {
Authorization: token,
},
info('Retrieving connector to remove...');
const existingConnector = await getConnectorById({
baseUrl: connectorEndpointBaseUrl,
connectorId,
token,
});
if (existingConnectorRes.status !== 200) {
warn(
`Connector with id ${connectorId} doesn't exist or you don't have permission to update it`
);
// When connector is not available we request the list and ask user to select connector for update
const id = await selectAvailableConnector(connectorEndpointBaseUrl, token);
return updateExistingConnector(
connectorEndpointBaseUrl,
id,
token,
payload
);
}

const existingConnector = await existingConnectorRes.json();
const updatePayload = {
...existingConnector,
...payload,
};

const updateConnectorEnpdoint = getConnectorEnpdoint;
info('Updating connector...');
const updateConnectorEnpdoint = `${connectorEndpointBaseUrl}/${existingConnector.id}`;

verbose(
`Deploying connector with a payload\n ${JSON.stringify(
Expand Down
1 change: 1 addition & 0 deletions src/connector-cli/src/commands/publish/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ interface ConnectorPayload {
iconUrl?: string;
script: string;
apiVersion: string;
options?: Record<string, unknown>;
allowedDomains: Array<string>;
proxyOptions: {
forwardedHeaders: boolean;
Expand Down
25 changes: 21 additions & 4 deletions src/connector-cli/src/commands/set-auth/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { buildRequestUrl } from '../../common/build-request-url';
import { getConnectorById } from '../../common/get-connector';
import { info, readConnectorConfig, startCommand, success } from '../../core';
import { readAccessToken } from '../../core/read-access-token';
import {
Expand Down Expand Up @@ -40,9 +42,9 @@ export async function runSetAuth(

const connectorConfig = readConnectorConfig(packageJson);

if (!connectorConfig.supportedAuth) {
if (connectorConfig.supportedAuth.length === 0) {
throw new ExecutionError(
'There is no information about supported authentication for this connector. Specify "config.supportedAuth" in connecotr\'s package.json'
'There is no information about supported authentication for this connector. Specify "config.supportedAuth" in connector\'s package.json'
);
}

Expand All @@ -56,8 +58,19 @@ export async function runSetAuth(
connectorConfig.supportedAuth
);

info('Retrieving connector to update...');
const { id, name } = await getConnectorById({
baseUrl: buildRequestUrl(baseUrl, environment),
connectorId,
token: accessToken,
});

if (!authData.name) {
authData.name = `${id}-${usage}-${type}`;
}

info('Build full request URL...');
const requestUrl = getRequestUrl(baseUrl, environment, connectorId, type);
const requestUrl = getRequestUrl(baseUrl, environment, id, type);

info('Set authentication...');
await setAuthentication(
Expand All @@ -69,5 +82,9 @@ export async function runSetAuth(
accessToken
);

success(`"${type}" authentication is applied.`, { id: connectorId });
success(`"${type}" authentication is applied for "${name}" connector`, {
id,
name,
type,
});
}
52 changes: 52 additions & 0 deletions src/connector-cli/src/common/get-connector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { httpErrorHandler, verbose, warn } from '../core';
import { ExecutionError } from '../core/types';
import { selectAvailableConnector } from './select-available-connector';

interface GetConnectorById {
baseUrl: string;
connectorId: string;
token: string;
}

interface EnvironmentConnector {
id: string;
name: string;
ownerType: 'grafx';
externalSourceId?: string;
}

export async function getConnectorById(
request: GetConnectorById
): Promise<EnvironmentConnector> {
const getConnectorEnpdoint = `${request.baseUrl}/${request.connectorId}`;

verbose(
`Checking connector's existing with id ${request.connectorId} -> ${getConnectorEnpdoint}`
);
const res = await fetch(getConnectorEnpdoint, {
headers: {
Authorization: request.token,
},
});

if (!res.ok && res.status === 404) {
warn(
`Connector with id ${request.connectorId} doesn't exist or you don't have permission to view it`
);
// When particular connector is not available we request the full list and ask user to select one available to make an action
const id = await selectAvailableConnector(request.baseUrl, request.token);
return getConnectorById({ ...request, connectorId: id });
} else if (!res.ok) {
await httpErrorHandler(res);
}

const connector: EnvironmentConnector = await res.json();

verbose('Checking belongings to "Hub-Based" connectors');
if (connector.ownerType === 'grafx' && !!connector.externalSourceId) {
throw new ExecutionError(
`You're trying to change a connector created using Connector Hub. It can not be done via Connector CLI. Consider to use GraFx Platform connectors configuration page instead`
);
}
return connector;
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ export async function selectAvailableConnector(
while (true) {
// Use the question method to get the user input
const index = Number(
await rl.question('Select the index of the connector to make an action')
await rl.question(
'Select the index of the connector to make an action:\xa0'
)
);
// Use the validation function to check the input
if (isNaN(index) || !connectors[index]) {
Expand Down
20 changes: 10 additions & 10 deletions src/connector-cli/src/core/types/gen-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
// match the expected interface, even if the JSON is valid.

export interface ChiliToken {
name: string;
name?: string;
}

export interface Oauth2AuthorizationCode {
authorizationServerMetadata: AuthorizationServerMetadata;
clientId: string;
clientSecret: string;
name: string;
name?: string;
scope?: string;
specCustomization?: SpecCustomization;
}
Expand Down Expand Up @@ -49,7 +49,7 @@ export enum RequestContentType {
export interface Oauth2ClientCredentials {
clientId: string;
clientSecret: string;
name: string;
name?: string;
scope?: string;
tokenEndpoint: string;
}
Expand All @@ -58,7 +58,7 @@ export interface Oauth2ResourceOwnerPassword {
bodyFormat?: RequestContentType;
clientId: string;
clientSecret: string;
name: string;
name?: string;
password: string;
scope?: string;
tokenEndpoint: string;
Expand All @@ -67,7 +67,7 @@ export interface Oauth2ResourceOwnerPassword {

export interface StaticKey {
key: string;
name: string;
name?: string;
value: string;
}

Expand Down Expand Up @@ -298,13 +298,13 @@ function r(name: string) {

const typeMap: any = {
"ChiliToken": o([
{ json: "name", js: "name", typ: "" },
{ json: "name", js: "name", typ: u(undefined, "") },
], false),
"Oauth2AuthorizationCode": o([
{ json: "authorizationServerMetadata", js: "authorizationServerMetadata", typ: r("AuthorizationServerMetadata") },
{ json: "clientId", js: "clientId", typ: "" },
{ json: "clientSecret", js: "clientSecret", typ: "" },
{ json: "name", js: "name", typ: "" },
{ json: "name", js: "name", typ: u(undefined, "") },
{ json: "scope", js: "scope", typ: u(undefined, "") },
{ json: "specCustomization", js: "specCustomization", typ: u(undefined, r("SpecCustomization")) },
], false),
Expand All @@ -320,23 +320,23 @@ const typeMap: any = {
"Oauth2ClientCredentials": o([
{ json: "clientId", js: "clientId", typ: "" },
{ json: "clientSecret", js: "clientSecret", typ: "" },
{ json: "name", js: "name", typ: "" },
{ json: "name", js: "name", typ: u(undefined, "") },
{ json: "scope", js: "scope", typ: u(undefined, "") },
{ json: "tokenEndpoint", js: "tokenEndpoint", typ: "" },
], false),
"Oauth2ResourceOwnerPassword": o([
{ json: "bodyFormat", js: "bodyFormat", typ: u(undefined, r("RequestContentType")) },
{ json: "clientId", js: "clientId", typ: "" },
{ json: "clientSecret", js: "clientSecret", typ: "" },
{ json: "name", js: "name", typ: "" },
{ json: "name", js: "name", typ: u(undefined, "") },
{ json: "password", js: "password", typ: "" },
{ json: "scope", js: "scope", typ: u(undefined, "") },
{ json: "tokenEndpoint", js: "tokenEndpoint", typ: "" },
{ json: "username", js: "username", typ: "" },
], false),
"StaticKey": o([
{ json: "key", js: "key", typ: "" },
{ json: "name", js: "name", typ: "" },
{ json: "name", js: "name", typ: u(undefined, "") },
{ json: "value", js: "value", typ: "" },
], false),
"ConnectorConfig": o([
Expand Down
5 changes: 4 additions & 1 deletion src/connector-cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ function main() {
' You can use "*" to denote dynamic parts of the URL.' +
' Example usage: --proxyOption.allowedDomains "main-domain.com", --proxyOption.allowedDomains "*.sub-domain.com"'
)
.option('--proxyOption.forwardedHeaders')
.option(
'--proxyOption.forwardedHeaders',
'If enabled, any request to connector via proxy ednpoint will include X-Forwarded-* headers'
)
.action(withErrorHandlerAction(runPublish));

program
Expand Down
Loading