Skip to content

Commit

Permalink
Fix remarks
Browse files Browse the repository at this point in the history
Signed-off-by: Anatolii Bazko <[email protected]>
  • Loading branch information
tolusha committed Aug 5, 2024
1 parent 45cab91 commit 8f26e01
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 29 deletions.
2 changes: 1 addition & 1 deletion build/dockerfiles/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ RUN yarn build
RUN yarn workspace @eclipse-che/dashboard-backend install --production

# Prepare air-gapped resources
ARG GITHUB_TOKEN=$GITHUB_TOKEN
# ARG GITHUB_TOKEN=$GITHUB_TOKEN
COPY build/dockerfiles/airgap.sh /dashboard/airgap.sh
RUN /dashboard/airgap.sh -d /dashboard/packages/devfile-registry/air-gap

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ describe('Getting Started Samples API Service', () => {

beforeEach(() => {
jest.resetModules();
airGapSampleApiService = new AirGapSampleApiService();
airGapSampleApiService.getAirGapResourcesDir = jest
.fn()
.mockReturnValue(path.join(__dirname, 'fixtures', 'air-gap'));
airGapSampleApiService = new AirGapSampleApiService(
path.join(__dirname, 'fixtures', 'air-gap'),
);
});

afterEach(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,29 @@ import path from 'path';
import { IAirGapSampleApi } from '@/devworkspaceClient/types';
import { isLocalRun } from '@/localRun';

// See build/dockerfiles/Dockerfile for the path
const airGapResourcesDir = '/public/dashboard/devfile-registry/air-gap';

export class AirGapSampleApiService implements IAirGapSampleApi {
protected readonly airGapResourcesDir: string;
protected readonly airGapIndexFilePath: string;
protected readonly samples: Array<api.IAirGapSample>;
constructor(airGapResourcesDir?: string) {
this.airGapResourcesDir = airGapResourcesDir
? airGapResourcesDir
: isLocalRun()
? path.join(
__dirname,
'../../../dashboard-frontend/lib/public/dashboard/devfile-registry/air-gap',
)
: '/public/dashboard/devfile-registry/air-gap';
this.airGapIndexFilePath = path.join(this.airGapResourcesDir, 'index.json');
this.samples = this.readAirGapIndex();
}

async list(): Promise<Array<api.IAirGapSample>> {
return this.readAirGapIndex();
return this.samples;
}

async downloadProject(name: string): Promise<api.IStreamedFile> {
const sample = this.readAirGapIndex().find(sample => sample.displayName === name);
const sample = this.samples.find(sample => sample.displayName === name);
if (sample) {
return this.download(sample.project?.zip?.filename);
}
Expand All @@ -37,7 +50,7 @@ export class AirGapSampleApiService implements IAirGapSampleApi {
}

async downloadDevfile(name: string): Promise<api.IStreamedFile> {
const sample = this.readAirGapIndex().find(sample => sample.displayName === name);
const sample = this.samples.find(sample => sample.displayName === name);
if (sample) {
return this.download(sample.devfile?.filename);
}
Expand All @@ -46,27 +59,13 @@ export class AirGapSampleApiService implements IAirGapSampleApi {
throw new Error(`Sample not found`);
}

getAirGapResourcesDir(): string {
return isLocalRun()
? path.join(
__dirname,
'../../../dashboard-frontend/lib/public/dashboard/devfile-registry/air-gap',
)
: airGapResourcesDir;
}

private getAirGapIndexFilePath(): string {
return path.join(this.getAirGapResourcesDir(), 'index.json');
}

private readAirGapIndex(): Array<api.IAirGapSample> {
const airGapIndexFilePath = this.getAirGapIndexFilePath();
if (!fs.existsSync(airGapIndexFilePath)) {
if (!fs.existsSync(this.airGapIndexFilePath)) {
return [];
}

try {
const data = fs.readFileSync(airGapIndexFilePath, 'utf8');
const data = fs.readFileSync(this.airGapIndexFilePath, 'utf8');
return JSON.parse(data) as api.IAirGapSample[];
} catch (e) {
console.error(e, 'Failed to read air-gap index.json');
Expand All @@ -80,10 +79,10 @@ export class AirGapSampleApiService implements IAirGapSampleApi {
throw new Error(`filename not defined`);
}

const filepath = path.resolve(this.getAirGapResourcesDir(), filename);
const filepath = path.resolve(this.airGapResourcesDir, filename);

// This is a security check to ensure that the file is within the airGapResourcesDir
if (!filepath.startsWith(this.getAirGapResourcesDir())) {
if (!filepath.startsWith(this.airGapResourcesDir)) {
console.error(`Invalid file path: ${filepath}`);
throw new Error(`Invalid file path`);
}
Expand Down

0 comments on commit 8f26e01

Please sign in to comment.