-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Configure axios instance for the devWorkspaceGenerator lib
Signed-off-by: Roman Nikitenko <rnikiten@redhat.com>
- Loading branch information
1 parent
d62f1b3
commit 1e274ba
Showing
4 changed files
with
84 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
code/extensions/che-remote/src/axios-certificate-authority.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/********************************************************************** | ||
* Copyright (c) 2023 Red Hat, Inc. | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
***********************************************************************/ | ||
|
||
/* eslint-disable header/header */ | ||
|
||
import * as axios from 'axios'; | ||
import * as fs from 'fs-extra'; | ||
import https from 'https'; | ||
import path from 'path'; | ||
|
||
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( | ||
CHE_SELF_SIGNED_MOUNT_PATH ? CHE_SELF_SIGNED_MOUNT_PATH : DEFAULT_CHE_SELF_SIGNED_MOUNT_PATH, | ||
); | ||
|
||
export const axiosInstance = certificateAuthority | ||
? axios.default.create({ | ||
httpsAgent: new https.Agent({ | ||
ca: certificateAuthority, | ||
}), | ||
}) | ||
: axios.default; | ||
|
||
function getCertificateAuthority(certPath: string): Buffer[] | undefined { | ||
if (!fs.existsSync(certPath)) { | ||
return undefined; | ||
} | ||
|
||
const certificateAuthority: Buffer[] = []; | ||
searchCertificate(certPath, certificateAuthority); | ||
|
||
return certificateAuthority.length > 0 ? certificateAuthority : undefined; | ||
} | ||
|
||
function searchCertificate( | ||
certPath: string, | ||
certificateAuthority: Buffer[], | ||
subdirLevel = 1, | ||
): void { | ||
const maxSubdirQuantity = 10; | ||
const maxSubdirLevel = 5; | ||
|
||
const tmpPaths: string[] = []; | ||
try { | ||
const publicCertificates = fs.readdirSync(certPath); | ||
for (const publicCertificate of publicCertificates) { | ||
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters