-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #924 from rszwajko/fetchCert
🐾 Fetch and verify certificate from tls-certificate endpoint
- Loading branch information
Showing
14 changed files
with
369 additions
and
17 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
19 changes: 19 additions & 0 deletions
19
...es/forklift-console-plugin/src/modules/Providers/hooks/__tests__/useTlsCertifcate.test.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,19 @@ | ||
import { toColonSeparatedHex } from '../useTlsCertificate'; | ||
|
||
describe('valid hex to colon separated', () => { | ||
it('works for empty string', () => { | ||
expect(toColonSeparatedHex('')).toBe(''); | ||
}); | ||
it('works for single letter', () => { | ||
expect(toColonSeparatedHex('a')).toBe('A'); | ||
}); | ||
it('works for n=2', () => { | ||
expect(toColonSeparatedHex('ab')).toBe('AB'); | ||
}); | ||
it('works for n=3', () => { | ||
expect(toColonSeparatedHex('abc')).toBe('AB:C'); | ||
}); | ||
it('works for n=4', () => { | ||
expect(toColonSeparatedHex('abcd')).toBe('AB:CD'); | ||
}); | ||
}); |
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
85 changes: 85 additions & 0 deletions
85
packages/forklift-console-plugin/src/modules/Providers/hooks/useTlsCertificate.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,85 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { KJUR, pemtohex, X509, zulutodate } from 'jsrsasign'; | ||
|
||
import { consoleFetch } from '@openshift-console/dynamic-plugin-sdk'; | ||
|
||
import { getServicesApiUrl } from '../utils'; | ||
|
||
/** | ||
* @param value PEM encoded certificate | ||
* @returns parsed certificate or undefined if parsing failed | ||
*/ | ||
const parseToX509 = (value: string) => { | ||
if (!value) return undefined; | ||
try { | ||
const cert = new X509(); | ||
cert.readCertPEM(value); | ||
return cert; | ||
} catch (e) { | ||
return undefined; | ||
} | ||
}; | ||
|
||
export const toColonSeparatedHex = (hexString: string) => | ||
Array.from(hexString.toUpperCase()) | ||
// [a,b,c,d] => [[c,d][a,b]] | ||
.reduce( | ||
([last = [], ...rest]: string[][], char: string) => | ||
last.length != 2 ? [[...last, char], ...rest] : [[char], last, ...rest], | ||
[], | ||
) | ||
// [[c,d][a,b]] => [[a,b], [c,d]] | ||
.reverse() | ||
.map((tuples) => tuples.join('')) | ||
.join(':'); | ||
|
||
/** | ||
* @param pemEncodedCert valid PEM encoded certificate | ||
* @returns SHA1 thumbprint | ||
*/ | ||
export const calculateThumbprint = (pemEncodedCert: string) => | ||
toColonSeparatedHex(KJUR.crypto.Util.hashHex(pemtohex(pemEncodedCert), 'sha1')); | ||
|
||
/** | ||
* @param url URL param for the tls-certificate endpoint | ||
* @returns certificate and props calculated/parsed based on the certificate | ||
*/ | ||
export const useTlsCertificate = (url: string) => { | ||
const [certificate, setCertificate] = useState(''); | ||
const [fetchError, setFetchError] = useState(false); | ||
const [loading, setLoading] = useState<boolean>(true); | ||
|
||
useEffect(() => { | ||
consoleFetch(getServicesApiUrl(`tls-certificate?URL=${url}`), { | ||
method: 'GET', | ||
}) | ||
.then((response: Response) => response.text()) | ||
.then((certificate) => setCertificate(certificate)) | ||
.catch((e) => setFetchError(e)) | ||
.then(() => setLoading(false)); | ||
}, [url]); | ||
|
||
const x509Cert: X509 = parseToX509(certificate); | ||
const certError = !x509Cert && !loading && !fetchError; | ||
const { | ||
thumbprint = '', | ||
issuer = '', | ||
validTo = undefined, | ||
} = x509Cert | ||
? { | ||
thumbprint: calculateThumbprint(certificate), | ||
issuer: KJUR.asn1.x509.X500Name.onelineToLDAP(x509Cert.getIssuerString()), | ||
validTo: zulutodate(x509Cert.getNotAfter()), | ||
} | ||
: {}; | ||
|
||
return { | ||
loading, | ||
fetchError, | ||
certError, | ||
thumbprint, | ||
issuer, | ||
validTo, | ||
certificate, | ||
}; | ||
}; |
3 changes: 3 additions & 0 deletions
3
...ugin/src/modules/Providers/utils/components/CertificateUpload/CertificateUpload.style.css
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,3 @@ | ||
.forklift-certificate-upload-margin { | ||
margin-top: 0.5rem; | ||
} |
76 changes: 76 additions & 0 deletions
76
...ole-plugin/src/modules/Providers/utils/components/CertificateUpload/CertificateUpload.tsx
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,76 @@ | ||
import React, { FC } from 'react'; | ||
import { useModal } from 'src/modules/Providers/modals'; | ||
import { useForkliftTranslation } from 'src/utils/i18n'; | ||
|
||
import { Button, FileUpload, FileUploadProps, Flex, FlexItem } from '@patternfly/react-core'; | ||
|
||
import { FetchCertificateModal } from './FetchCertificateModal'; | ||
|
||
import './CertificateUpload.style.css'; | ||
|
||
export interface CertificateUploadProps extends FileUploadProps { | ||
url?: string; | ||
} | ||
|
||
/** | ||
* Provide the certificate using following paths: | ||
* 1. manual copy-paste (provided by FileUpload widget) | ||
* 2. uploading a file (provided by FileUpload widget) | ||
* 3. fetch from the specified URL (via tls-certificate endpoint) end verify | ||
*/ | ||
export const CertificateUpload: FC<CertificateUploadProps> = ({ | ||
id, | ||
url, | ||
value, | ||
filenamePlaceholder, | ||
browseButtonText, | ||
validated, | ||
onDataChange, | ||
onTextChange, | ||
onClearClick, | ||
isDisabled, | ||
type, | ||
}) => { | ||
const { showModal } = useModal(); | ||
const { t } = useForkliftTranslation(); | ||
const isText = !type || type === 'text'; | ||
return ( | ||
<> | ||
<FileUpload | ||
id={id} | ||
type={type || 'text'} | ||
filenamePlaceholder={filenamePlaceholder || t('Drag and drop a file or upload one')} | ||
value={value} | ||
validated={validated} | ||
onDataChange={onDataChange} | ||
onTextChange={onTextChange} | ||
onClearClick={onClearClick} | ||
browseButtonText={browseButtonText || t('Upload')} | ||
isDisabled={isDisabled} | ||
> | ||
{url && isText && ( | ||
<Flex> | ||
<FlexItem> | ||
<Button | ||
className="forklift-certificate-upload-margin" | ||
isDisabled={isDisabled} | ||
variant="secondary" | ||
onClick={() => | ||
showModal( | ||
<FetchCertificateModal | ||
url={url} | ||
handleSave={onTextChange} | ||
existingCert={String(value)} | ||
/>, | ||
) | ||
} | ||
> | ||
{t('Fetch certificate from URL')} | ||
</Button> | ||
</FlexItem> | ||
</Flex> | ||
)} | ||
</FileUpload> | ||
</> | ||
); | ||
}; |
Oops, something went wrong.