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 an error when passing the editor's Devfile link #751

Merged
merged 6 commits into from
Mar 20, 2023
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 @@ -15,19 +15,12 @@ import path from 'path';
import * as axios from 'axios';
import https from 'https';

export interface ICrtConfig {
ssCrtPath?: string;
publicCrtPath?: string;
}

const DEFAULT_CHE_SELF_SIGNED_MOUNT_PATH = '/public-certs/che-self-signed';
const DEFAULT_CHE_SELF_SIGNED_MOUNT_PATH = '/public-certs';
const CHE_SELF_SIGNED_MOUNT_PATH = process.env.CHE_SELF_SIGNED_MOUNT_PATH;

const certificateAuthority = getCertificateAuthority({
publicCrtPath: CHE_SELF_SIGNED_MOUNT_PATH
? CHE_SELF_SIGNED_MOUNT_PATH
: DEFAULT_CHE_SELF_SIGNED_MOUNT_PATH,
});
const certificateAuthority = getCertificateAuthority(
CHE_SELF_SIGNED_MOUNT_PATH ? CHE_SELF_SIGNED_MOUNT_PATH : DEFAULT_CHE_SELF_SIGNED_MOUNT_PATH,
);

export const axiosInstance = certificateAuthority
? axios.default.create({
Expand All @@ -37,21 +30,46 @@ export const axiosInstance = certificateAuthority
})
: axios.default;

function getCertificateAuthority(config: ICrtConfig): Buffer[] | undefined {
const certificateAuthority: Buffer[] = [];
if (config.ssCrtPath && fs.existsSync(config.ssCrtPath)) {
certificateAuthority.push(fs.readFileSync(config.ssCrtPath));
}
function searchCertificate(
certPath: string,
certificateAuthority: Buffer[],
subdirLevel = 1,
): void {
const maxSubdirQuantity = 10;
const maxSubdirLevel = 5;

if (config.publicCrtPath && fs.existsSync(config.publicCrtPath)) {
const publicCertificates = fs.readdirSync(config.publicCrtPath);
const tmpPaths: string[] = [];
try {
const publicCertificates = fs.readdirSync(certPath);
for (const publicCertificate of publicCertificates) {
if (publicCertificate.endsWith('.crt')) {
const certPath = path.join(config.publicCrtPath, publicCertificate);
certificateAuthority.push(fs.readFileSync(certPath));
const newPath = path.join(certPath, publicCertificate);
if (fs.lstatSync(newPath).isDirectory()) {
if (tmpPaths.length < maxSubdirQuantity) {
tmpPaths.push(newPath);
}
} else {
const fullPath = path.join(certPath, publicCertificate);
certificateAuthority.push(fs.readFileSync(fullPath));
}
}
} catch (e) {
// no-op
}

if (subdirLevel < maxSubdirLevel) {
for (const path of tmpPaths) {
searchCertificate(path, certificateAuthority, ++subdirLevel);
}
}
}

function getCertificateAuthority(certPath: string): Buffer[] | undefined {
if (!fs.existsSync(certPath)) {
return undefined;
}

const certificateAuthority: Buffer[] = [];
searchCertificate(certPath, certificateAuthority);

return certificateAuthority.length > 0 ? certificateAuthority : undefined;
}
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ describe('Workspace Loader, step CHECK_RUNNING_WORKSPACES_LIMIT', () => {
await waitFor(() => expect(hasError.textContent).toEqual('true'));

const closeRunningWorkspaceButton = screen.queryByText(
`Close running workspace (${runningDevworkspace.metadata.name}) and restart ${targetDevworkspace.metadata.name}`,
`Close running workspace (${runningDevworkspace.metadata.name}) and restart`,
);
expect(closeRunningWorkspaceButton).not.toBeNull();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ class StepCheckRunningWorkspacesLimit extends AbstractLoaderStep<Props, State> {
return findTargetWorkspace(props.allWorkspaces, props.matchParams);
}

private getAlertItem(error: unknown, workspace: Workspace | undefined): AlertItem | undefined {
private getAlertItem(error: unknown): AlertItem | undefined {
const { runningWorkspaces } = this.props;

if (error instanceof RunningWorkspacesExceededError) {
Expand All @@ -277,7 +277,7 @@ class StepCheckRunningWorkspacesLimit extends AbstractLoaderStep<Props, State> {
const runningWorkspace = runningWorkspaces[0];
runningWorkspacesAlertItem.actionCallbacks = [
{
title: `Close running workspace (${runningWorkspace.name}) and restart ${workspace?.name}`,
title: `Close running workspace (${runningWorkspace.name}) and restart`,
callback: () => this.handleStopRedundantWorkspace(runningWorkspace),
},
{
Expand Down Expand Up @@ -317,7 +317,7 @@ class StepCheckRunningWorkspacesLimit extends AbstractLoaderStep<Props, State> {
const steps = loaderSteps.values;
const currentStepId = loaderSteps.get(currentStepIndex).value.id;

const alertItem = this.getAlertItem(lastError, workspace);
const alertItem = this.getAlertItem(lastError);

return (
<LoaderPage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,12 @@ class StepApplyDevfile extends AbstractLoaderStep<Props, State> {
}

private async createWorkspaceFromDevfile(devfile: devfileApi.Devfile): Promise<void> {
const params = Object.fromEntries(this.props.searchParams);
const optionalFilesContent = this.props.factoryResolver?.optionalFilesContent || {};
await this.props.createWorkspaceFromDevfile(devfile, params, optionalFilesContent);
await this.props.createWorkspaceFromDevfile(
devfile,
this.state.factoryParams,
optionalFilesContent,
);
}

private handleCreateWorkspaceError(): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { SamplesListTab } from '..';
import { selectWorkspacesSettings } from '../../../../store/Workspaces/Settings/selectors';
import { BrandingData } from '../../../../services/bootstrap/branding.constant';
import { selectPvcStrategy } from '../../../../store/ServerConfig/selectors';
import { api as dashboardBackendApi } from '@eclipse-che/common';
import { api } from '@eclipse-che/common';
import devfileApi from '../../../../services/devfileApi';

const onDevfileMock: (
Expand Down Expand Up @@ -70,7 +70,7 @@ describe('Samples list tab', () => {
defaults: {
pvcStrategy: preferredStorageType,
},
} as dashboardBackendApi.IServerConfig)
} as api.IServerConfig)
.withDevWorkspaces({
workspaces: [],
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ describe('Quick Add page', () => {
devfileButton.click();

expect(createWorkspaceFromDevfileMock).toHaveBeenCalledWith(dummyDevfile, {
stackName: 'dummyStackName',
factoryId: 'dummyStackName',
});
});

Expand Down
7 changes: 4 additions & 3 deletions packages/dashboard-frontend/src/pages/GetStarted/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import { selectWorkspaceByQualifiedName } from '../../store/Workspaces/selectors
import { selectDefaultNamespace } from '../../store/InfrastructureNamespaces/selectors';
import getRandomString from '../../services/helpers/random';
import devfileApi from '../../services/devfileApi';
import { FactoryParams } from '../../containers/Loader/buildFactoryParams';

const SamplesListTab = React.lazy(() => import('./GetStartedTab'));

Expand Down Expand Up @@ -116,9 +117,9 @@ export class GetStarted extends React.PureComponent<Props, State> {
[fileName: string]: string;
},
): Promise<void> {
const attr: { [key: string]: string } = {};
const attr: Partial<FactoryParams> = {};
if (stackName) {
attr.stackName = stackName;
attr.factoryId = stackName;
}
if (isCheDevfile(devfile) && !devfile.metadata.name && devfile.metadata.generateName) {
const name = devfile.metadata.generateName + getRandomString(4).toLowerCase();
Expand All @@ -131,7 +132,7 @@ export class GetStarted extends React.PureComponent<Props, State> {
let workspace: Workspace | undefined;
try {
await this.props.createWorkspaceFromDevfile(devfile, attr, optionalFilesContent);
this.props.setWorkspaceQualifiedName(namespace, devfile.metadata.name as string);
this.props.setWorkspaceQualifiedName(namespace, devfile.metadata.name);
workspace = this.props.activeWorkspace;
} catch (e) {
const errorMessage = common.helpers.errors.getMessage(e);
Expand Down
Loading