Skip to content

Commit

Permalink
factor out secureDB form components
Browse files Browse the repository at this point in the history
Signed-off-by: gitdallas <[email protected]>
  • Loading branch information
gitdallas committed Dec 11, 2024
1 parent 9f56b0a commit 748cf46
Show file tree
Hide file tree
Showing 2 changed files with 275 additions and 215 deletions.
260 changes: 260 additions & 0 deletions frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,260 @@
import React, { useState } from 'react';
import { FormGroup, Radio, Alert, MenuItem, MenuGroup } from '@patternfly/react-core';
import SearchSelector from '~/components/searchSelector/SearchSelector';
import { translateDisplayNameForK8s } from '~/concepts/k8s/utils';
import { RecursivePartial } from '~/typeHelpers';
import { PemFileUpload } from './PemFileUpload';

export enum SecureDBRType {
CLUSTER_WIDE = 'cluster-wide',
OPENSHIFT = 'openshift',
EXISTING = 'existing',
NEW = 'new',
}

export interface SecureDBInfo {
type: SecureDBRType;
configMap: string;
key: string;
certificate: string;
nameSpace: string;
isValid: boolean;
}

interface CreateMRSecureDBSectionProps {
secureDBInfo: SecureDBInfo;
modelRegistryNamespace: string;
nameDesc: { name: string };
existingCertKeys: string[];
existingCertConfigMaps: string[];
existingCertSecrets: string[];
setSecureDBInfo: (info: SecureDBInfo) => void;
}

export const CreateMRSecureDBSection: React.FC<CreateMRSecureDBSectionProps> = ({
secureDBInfo,
modelRegistryNamespace,
nameDesc,
existingCertKeys,
existingCertConfigMaps,
existingCertSecrets,
setSecureDBInfo,
}) => {
const [searchValue, setSearchValue] = useState('');

Check warning on line 43 in frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx#L35-L43

Added lines #L35 - L43 were not covered by tests

const hasContent = (value: string): boolean => !!value.trim().length;

const isValid = (info?: RecursivePartial<SecureDBInfo>) => {
const fullInfo: SecureDBInfo = { ...secureDBInfo, ...info };
if ([SecureDBRType.CLUSTER_WIDE, SecureDBRType.OPENSHIFT].includes(fullInfo.type)) {
return true;

Check warning on line 50 in frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx#L47-L50

Added lines #L47 - L50 were not covered by tests
}
if (fullInfo.type === SecureDBRType.EXISTING) {
return hasContent(fullInfo.configMap) && hasContent(fullInfo.key);

Check warning on line 53 in frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx#L52-L53

Added lines #L52 - L53 were not covered by tests
}
if (fullInfo.type === SecureDBRType.NEW) {
return hasContent(fullInfo.certificate);

Check warning on line 56 in frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx#L55-L56

Added lines #L55 - L56 were not covered by tests
}
return false;

Check warning on line 58 in frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx#L58

Added line #L58 was not covered by tests
};

const handleSecureDBTypeChange = (type: SecureDBRType) => {
const newInfo = {

Check warning on line 62 in frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx#L61-L62

Added lines #L61 - L62 were not covered by tests
type,
nameSpace: '',
key: '',
configMap: '',
certificate: '',
};
setSecureDBInfo({

Check warning on line 69 in frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx#L69

Added line #L69 was not covered by tests
...newInfo,
isValid: isValid(newInfo),
});
};

const getFilteredExistingCAResources = () => (
<>

Check warning on line 76 in frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx#L76

Added line #L76 was not covered by tests
<MenuGroup label="ConfigMaps">
{existingCertConfigMaps
.filter((configMap) => configMap.toLowerCase().includes(searchValue.toLowerCase()))
.map((configMap, index) => (
<MenuItem

Check warning on line 81 in frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx#L79-L81

Added lines #L79 - L81 were not covered by tests
key={`configmap-${index}`}
onClick={() => {
setSearchValue('');
const newInfo = {

Check warning on line 85 in frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx#L83-L85

Added lines #L83 - L85 were not covered by tests
...secureDBInfo,
configMap,
key: '',
};
setSecureDBInfo({

Check warning on line 90 in frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx#L90

Added line #L90 was not covered by tests
...newInfo,
isValid: isValid(newInfo),
});
}}
>
{configMap}
</MenuItem>
))}
</MenuGroup>
<MenuGroup label="Secrets">
{existingCertSecrets
.filter((secret) => secret.toLowerCase().includes(searchValue.toLowerCase()))
.map((secret, index) => (
<MenuItem

Check warning on line 104 in frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx#L102-L104

Added lines #L102 - L104 were not covered by tests
key={`secret-${index}`}
onClick={() => {
setSearchValue('');
const newInfo = {

Check warning on line 108 in frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx#L106-L108

Added lines #L106 - L108 were not covered by tests
...secureDBInfo,
configMap: secret,
key: '',
};
setSecureDBInfo({

Check warning on line 113 in frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx#L113

Added line #L113 was not covered by tests
...newInfo,
isValid: isValid(newInfo),
});
}}
>
{secret}
</MenuItem>
))}
</MenuGroup>
</>
);

return (

Check warning on line 126 in frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx#L126

Added line #L126 was not covered by tests
<>
<Radio
isChecked={secureDBInfo.type === SecureDBRType.CLUSTER_WIDE}
name="cluster-wide-ca"
onChange={() => handleSecureDBTypeChange(SecureDBRType.CLUSTER_WIDE)}

Check warning on line 131 in frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx#L131

Added line #L131 was not covered by tests
label="Use cluster-wide CA bundle"
description={
<>
Use the <strong>ca-bundle.crt</strong> bundle in the{' '}
<strong>odh-trusted-ca-bundle</strong> ConfigMap.
</>
}
id="cluster-wide-ca"
/>
<Radio
isChecked={secureDBInfo.type === SecureDBRType.OPENSHIFT}
name="openshift-ca"
onChange={() => handleSecureDBTypeChange(SecureDBRType.OPENSHIFT)}

Check warning on line 144 in frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx#L144

Added line #L144 was not covered by tests
label="Use OpenShift AI CA bundle"
description={
<>
Use the <strong>odh-ca-bundle.crt</strong> bundle in the{' '}
<strong>odh-trusted-ca-bundle</strong> ConfigMap.
</>
}
id="openshift-ca"
/>
<Radio
isChecked={secureDBInfo.type === SecureDBRType.EXISTING}
name="existing-ca"
onChange={() => handleSecureDBTypeChange(SecureDBRType.EXISTING)}

Check warning on line 157 in frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx#L157

Added line #L157 was not covered by tests
label="Choose from existing certificates"
description={
<>
You can select the key of any ConfigMap or Secret in the{' '}
<strong>{modelRegistryNamespace}</strong> namespace.
</>
}
id="existing-ca"
/>
{secureDBInfo.type === SecureDBRType.EXISTING && (
<>

Check warning on line 168 in frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx#L167-L168

Added lines #L167 - L168 were not covered by tests
<FormGroup
label="Resource"
isRequired
fieldId="existing-ca-resource"
style={{ marginLeft: 'var(--pf-t--global--spacer--lg)' }}
>
<SearchSelector
isFullWidth
dataTestId="existing-ca-resource-selector"
onSearchChange={(newValue) => setSearchValue(newValue)}
onSearchClear={() => setSearchValue('')}

Check warning on line 179 in frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx#L178-L179

Added lines #L178 - L179 were not covered by tests
searchValue={searchValue}
toggleText={secureDBInfo.configMap || 'Select a ConfigMap or a Secret'}

Check warning on line 181 in frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx#L181

Added line #L181 was not covered by tests
>
{getFilteredExistingCAResources()}
</SearchSelector>
</FormGroup>
<FormGroup
label="Key"
isRequired
fieldId="existing-ca-key"
style={{ marginLeft: 'var(--pf-t--global--spacer--lg)' }}
>
<SearchSelector
isFullWidth
dataTestId="existing-ca-key-selector"
onSearchChange={(newValue) => setSearchValue(newValue)}
onSearchClear={() => setSearchValue('')}

Check warning on line 196 in frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx#L195-L196

Added lines #L195 - L196 were not covered by tests
searchValue={searchValue}
toggleText={secureDBInfo.key || 'Select a key'}

Check warning on line 198 in frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx#L198

Added line #L198 was not covered by tests
>
{existingCertKeys
.filter((item) => item.toLowerCase().includes(searchValue.toLowerCase()))
.map((item, index) => (
<MenuItem

Check warning on line 203 in frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx#L201-L203

Added lines #L201 - L203 were not covered by tests
key={`key-${index}`}
onClick={() => {
setSearchValue('');
const newInfo = {

Check warning on line 207 in frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx#L205-L207

Added lines #L205 - L207 were not covered by tests
...secureDBInfo,
key: item,
};
setSecureDBInfo({ ...newInfo, isValid: isValid(newInfo) });

Check warning on line 211 in frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx#L211

Added line #L211 was not covered by tests
}}
>
{item}
</MenuItem>
))}
</SearchSelector>
</FormGroup>
</>
)}
<Radio
isChecked={secureDBInfo.type === SecureDBRType.NEW}
name="new-ca"
onChange={() => handleSecureDBTypeChange(SecureDBRType.NEW)}

Check warning on line 224 in frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx#L224

Added line #L224 was not covered by tests
label="Upload new certificate"
id="new-ca"
/>
{secureDBInfo.type === SecureDBRType.NEW && (
<>

Check warning on line 229 in frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx#L228-L229

Added lines #L228 - L229 were not covered by tests
<Alert
isInline
title="Note"
variant="info"
style={{ marginLeft: 'var(--pf-t--global--spacer--lg)' }}
>
Uploading a certificate below creates the{' '}
<strong>{translateDisplayNameForK8s(nameDesc.name)}-db-credential</strong> ConfigMap
with the <strong>ca.crt</strong> key. If you&apos;d like to upload the certificate as a
Secret instead, see the documentation for more details.
</Alert>
<FormGroup
label="Certificate"
required
style={{ marginLeft: 'var(--pf-t--global--spacer--lg)' }}
>
<PemFileUpload
onChange={(value) => {
const newInfo = {

Check warning on line 248 in frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx#L247-L248

Added lines #L247 - L248 were not covered by tests
...secureDBInfo,
certificate: value,
};
setSecureDBInfo({ ...newInfo, isValid: isValid(newInfo) });

Check warning on line 252 in frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx

View check run for this annotation

Codecov / codecov/patch

frontend/src/pages/modelRegistrySettings/CreateMRSecureDBSection.tsx#L252

Added line #L252 was not covered by tests
}}
/>
</FormGroup>
</>
)}
</>
);
};
Loading

0 comments on commit 748cf46

Please sign in to comment.