Remove key_file_path field (#3959)

* Remove key_file_path field

* fix

* Update comparison
main
Maria Khrustaleva 4 years ago committed by GitHub
parent 5bbdbaf6fa
commit 439c6d5fb9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -34,7 +34,6 @@
key: undefined, key: undefined,
secret_key: undefined, secret_key: undefined,
session_token: undefined, session_token: undefined,
key_file_path: undefined,
key_file: undefined, key_file: undefined,
specific_attributes: undefined, specific_attributes: undefined,
owner: undefined, owner: undefined,
@ -155,21 +154,6 @@
data.session_token = value; data.session_token = value;
}, },
}, },
/**
* Key file path
* @name keyFilePath
* @type {string}
* @memberof module:API.cvat.classes.CloudStorage
* @instance
* @throws {module:API.cvat.exceptions.ArgumentError}
*/
keyFilePath: {
get: () => data.key_file_path,
set: (value) => {
validateNotEmptyString(value);
data.key_file_path = value;
},
},
/** /**
* Key file * Key file
* @name keyFile * @name keyFile
@ -433,10 +417,6 @@
data.session_token = cloudStorageInstance.token; data.session_token = cloudStorageInstance.token;
} }
if (cloudStorageInstance.keyFilePath) {
data.key_file_path = cloudStorageInstance.keyFilePath;
}
if (cloudStorageInstance.keyFile) { if (cloudStorageInstance.keyFile) {
data.key_file = cloudStorageInstance.keyFile; data.key_file = cloudStorageInstance.keyFile;
} }

@ -19,7 +19,7 @@ import Tooltip from 'antd/lib/tooltip';
import { CombinedState, CloudStorage } from 'reducers/interfaces'; import { CombinedState, CloudStorage } from 'reducers/interfaces';
import { createCloudStorageAsync, updateCloudStorageAsync } from 'actions/cloud-storage-actions'; import { createCloudStorageAsync, updateCloudStorageAsync } from 'actions/cloud-storage-actions';
import { ProviderType, CredentialsType } from 'utils/enums'; import { ProviderType, CredentialsType } from 'utils/enums';
import { DeleteOutlined, UploadOutlined } from '@ant-design/icons'; import { QuestionCircleOutlined, UploadOutlined } from '@ant-design/icons';
import Upload, { RcFile } from 'antd/lib/upload'; import Upload, { RcFile } from 'antd/lib/upload';
import { Space } from 'antd'; import { Space } from 'antd';
import { AzureProvider, S3Provider, GoogleCloudProvider } from '../../icons'; import { AzureProvider, S3Provider, GoogleCloudProvider } from '../../icons';
@ -31,8 +31,8 @@ export interface Props {
cloudStorage?: CloudStorage; cloudStorage?: CloudStorage;
} }
type CredentialsFormNames = 'key' | 'secret_key' | 'account_name' | 'session_token' | 'key_file_path'; type CredentialsFormNames = 'key' | 'secret_key' | 'account_name' | 'session_token';
type CredentialsCamelCaseNames = 'key' | 'secretKey' | 'accountName' | 'sessionToken' | 'keyFilePath'; type CredentialsCamelCaseNames = 'key' | 'secretKey' | 'accountName' | 'sessionToken';
interface CloudStorageForm { interface CloudStorageForm {
credentials_type: CredentialsType; credentials_type: CredentialsType;
@ -44,7 +44,6 @@ interface CloudStorageForm {
key?: string; key?: string;
secret_key?: string; secret_key?: string;
SAS_token?: string; SAS_token?: string;
key_file_path?: string;
key_file?: File; key_file?: File;
description?: string; description?: string;
region?: string; region?: string;
@ -53,6 +52,8 @@ interface CloudStorageForm {
manifests: string[]; manifests: string[];
} }
const { Dragger } = Upload;
export default function CreateCloudStorageForm(props: Props): JSX.Element { export default function CreateCloudStorageForm(props: Props): JSX.Element {
const { cloudStorage } = props; const { cloudStorage } = props;
const cloudStorageId = cloudStorage ? cloudStorage.id : null; const cloudStorageId = cloudStorage ? cloudStorage.id : null;
@ -76,21 +77,18 @@ export default function CreateCloudStorageForm(props: Props): JSX.Element {
sessionToken: 'X'.repeat(300), sessionToken: 'X'.repeat(300),
key: 'X'.repeat(20), key: 'X'.repeat(20),
secretKey: 'X'.repeat(40), secretKey: 'X'.repeat(40),
keyFilePath: 'X'.repeat(10), keyFile: new File([], 'fakeKey.json'),
}; };
const [keyVisibility, setKeyVisibility] = useState(false); const [keyVisibility, setKeyVisibility] = useState(false);
const [secretKeyVisibility, setSecretKeyVisibility] = useState(false); const [secretKeyVisibility, setSecretKeyVisibility] = useState(false);
const [sessionTokenVisibility, setSessionTokenVisibility] = useState(false); const [sessionTokenVisibility, setSessionTokenVisibility] = useState(false);
const [accountNameVisibility, setAccountNameVisibility] = useState(false); const [accountNameVisibility, setAccountNameVisibility] = useState(false);
const [keyFilePathVisibility, setKeyFilePathVisibility] = useState(false);
const [manifestNames, setManifestNames] = useState<string[]>([]); const [manifestNames, setManifestNames] = useState<string[]>([]);
const [keyFilePathIsDisabled, setKeyFilePathIsDisabled] = useState(false);
const [keyFileIsDisabled, setKeyFileIsDisabled] = useState(false);
const [uploadedKeyFile, setUploadedKeyFile] = useState<File | null>(null); const [uploadedKeyFile, setUploadedKeyFile] = useState<File | null>(null);
const [isFakeKeyFileAttached, setIsFakeKeyFileAttached] = useState(!!cloudStorage);
function initializeFields(): void { function initializeFields(): void {
setManifestNames(cloudStorage.manifests); setManifestNames(cloudStorage.manifests);
@ -113,7 +111,7 @@ export default function CreateCloudStorageForm(props: Props): JSX.Element {
fieldsValue.key = fakeCredentialsData.key; fieldsValue.key = fakeCredentialsData.key;
fieldsValue.secret_key = fakeCredentialsData.secretKey; fieldsValue.secret_key = fakeCredentialsData.secretKey;
} else if (cloudStorage.credentialsType === CredentialsType.KEY_FILE_PATH) { } else if (cloudStorage.credentialsType === CredentialsType.KEY_FILE_PATH) {
fieldsValue.key_file_path = fakeCredentialsData.keyFilePath; setUploadedKeyFile(fakeCredentialsData.keyFile);
} }
if (cloudStorage.specificAttributes) { if (cloudStorage.specificAttributes) {
@ -142,6 +140,7 @@ export default function CreateCloudStorageForm(props: Props): JSX.Element {
} else { } else {
setManifestNames([]); setManifestNames([]);
setSelectedRegion(undefined); setSelectedRegion(undefined);
setUploadedKeyFile(null);
form.resetFields(); form.resetFields();
} }
} }
@ -228,7 +227,7 @@ export default function CreateCloudStorageForm(props: Props): JSX.Element {
cloudStorageData.specific_attributes = specificAttributes.toString(); cloudStorageData.specific_attributes = specificAttributes.toString();
if (uploadedKeyFile) { if (uploadedKeyFile && !isFakeKeyFileAttached) {
cloudStorageData.key_file = uploadedKeyFile; cloudStorageData.key_file = uploadedKeyFile;
} }
@ -259,9 +258,6 @@ export default function CreateCloudStorageForm(props: Props): JSX.Element {
if (cloudStorageData.session_token === fakeCredentialsData.sessionToken) { if (cloudStorageData.session_token === fakeCredentialsData.sessionToken) {
delete cloudStorageData.session_token; delete cloudStorageData.session_token;
} }
if (cloudStorageData.key_file_path === fakeCredentialsData.keyFilePath) {
delete cloudStorageData.key_file_path;
}
dispatch(updateCloudStorageAsync(cloudStorageData)); dispatch(updateCloudStorageAsync(cloudStorageData));
} else { } else {
dispatch(createCloudStorageAsync(cloudStorageData)); dispatch(createCloudStorageAsync(cloudStorageData));
@ -274,8 +270,8 @@ export default function CreateCloudStorageForm(props: Props): JSX.Element {
secret_key: undefined, secret_key: undefined,
session_token: undefined, session_token: undefined,
account_name: undefined, account_name: undefined,
key_file_path: undefined,
}); });
setUploadedKeyFile(null);
}; };
const onFocusCredentialsItem = (credential: CredentialsCamelCaseNames, key: CredentialsFormNames): void => { const onFocusCredentialsItem = (credential: CredentialsCamelCaseNames, key: CredentialsFormNames): void => {
@ -419,64 +415,46 @@ export default function CreateCloudStorageForm(props: Props): JSX.Element {
if (providerType === ProviderType.GOOGLE_CLOUD_STORAGE && credentialsType === CredentialsType.KEY_FILE_PATH) { if (providerType === ProviderType.GOOGLE_CLOUD_STORAGE && credentialsType === CredentialsType.KEY_FILE_PATH) {
return ( return (
<Form.Item <Form.Item
name='key_file'
{...internalCommonProps} {...internalCommonProps}
label={( label={(
<Tooltip title='You can specify path to key file or upload key file. <Tooltip title='You can upload a key file.
If you leave these fields blank, the environment variable will be used.' If you leave this field blank, the environment variable
GOOGLE_APPLICATION_CREDENTIALS will be used.'
> >
Key file Key file
<Button
href='https://cloud.google.com/docs/authentication/getting-started#setting_the_environment_variable'
target='_blank'
type='link'
className='cvat-cloud-storage-help-button'
>
<QuestionCircleOutlined />
</Button>
</Tooltip> </Tooltip>
)} )}
> >
<Space align='start' className='cvat-cloud-storage-form-item-key-file'> <Space align='start' className='cvat-cloud-storage-form-item-key-file'>
<Form.Item <Dragger
name='key_file_path' accept='.json, application/json'
noStyle multiple={false}
maxCount={1}
fileList={
uploadedKeyFile ? [{ uid: '1', name: uploadedKeyFile.name }] : []
}
beforeUpload={(file: RcFile): boolean => {
setIsFakeKeyFileAttached(false);
setUploadedKeyFile(file);
return false;
}}
onRemove={() => setUploadedKeyFile(null)}
> >
<Input.Password <Space>
visibilityToggle={keyFilePathVisibility} Attach a file
onChange={(e) => { <UploadOutlined />
setKeyFilePathVisibility(true); </Space>
const isDisabled = !!(e.target.value); </Dragger>
setKeyFileIsDisabled(isDisabled);
}}
onFocus={() => onFocusCredentialsItem('keyFilePath', 'key_file_path')}
onBlur={() => onBlurCredentialsItem('keyFilePath', 'key_file_path', setKeyFilePathVisibility)}
disabled={keyFilePathIsDisabled}
/>
</Form.Item>
<Tooltip title='Attach a file'>
<Upload
accept='.json, application/json'
multiple={false}
maxCount={1}
showUploadList={false}
beforeUpload={(file: RcFile): boolean => {
if (form.getFieldValue('key_file_path')) {
form.setFieldsValue({
key_file_path: undefined,
});
}
setKeyFilePathIsDisabled(true);
setUploadedKeyFile(file);
return false;
}}
>
<Button icon={<UploadOutlined />} disabled={keyFileIsDisabled} />
</Upload>
</Tooltip>
<Tooltip title='Delete an uploaded file'>
<Button
icon={<DeleteOutlined />}
disabled={keyFileIsDisabled}
onClick={() => {
setKeyFilePathIsDisabled(false);
setUploadedKeyFile(null);
}}
/>
</Tooltip>
</Space> </Space>
</Form.Item> </Form.Item>
); );

@ -41,6 +41,10 @@
:nth-child(1) { :nth-child(1) {
flex-grow: 1; flex-grow: 1;
} }
:nth-child(2) {
max-width: 37 * $grid-unit-size;
}
} }
> div:not(first-child) { > div:not(first-child) {

@ -799,7 +799,6 @@ class CloudStorageSerializer(serializers.ModelSerializer):
session_token = serializers.CharField(max_length=440, allow_blank=True, required=False) session_token = serializers.CharField(max_length=440, allow_blank=True, required=False)
key = serializers.CharField(max_length=20, allow_blank=True, required=False) key = serializers.CharField(max_length=20, allow_blank=True, required=False)
secret_key = serializers.CharField(max_length=40, allow_blank=True, required=False) secret_key = serializers.CharField(max_length=40, allow_blank=True, required=False)
key_file_path = serializers.CharField(max_length=64, allow_blank=True, required=False)
key_file = serializers.FileField(required=False) key_file = serializers.FileField(required=False)
account_name = serializers.CharField(max_length=24, allow_blank=True, required=False) account_name = serializers.CharField(max_length=24, allow_blank=True, required=False)
manifests = ManifestSerializer(many=True, default=[]) manifests = ManifestSerializer(many=True, default=[])
@ -809,8 +808,7 @@ class CloudStorageSerializer(serializers.ModelSerializer):
fields = ( fields = (
'provider_type', 'resource', 'display_name', 'owner', 'credentials_type', 'provider_type', 'resource', 'display_name', 'owner', 'credentials_type',
'created_date', 'updated_date', 'session_token', 'account_name', 'key', 'created_date', 'updated_date', 'session_token', 'account_name', 'key',
'secret_key', 'key_file_path', 'key_file', 'specific_attributes', 'secret_key', 'key_file', 'specific_attributes', 'description', 'id', 'manifests',
'description', 'id', 'manifests',
) )
read_only_fields = ('created_date', 'updated_date', 'owner') read_only_fields = ('created_date', 'updated_date', 'owner')
@ -828,8 +826,6 @@ class CloudStorageSerializer(serializers.ModelSerializer):
if provider_type == models.CloudProviderChoice.AZURE_CONTAINER: if provider_type == models.CloudProviderChoice.AZURE_CONTAINER:
if not attrs.get('account_name', ''): if not attrs.get('account_name', ''):
raise serializers.ValidationError('Account name for Azure container was not specified') raise serializers.ValidationError('Account name for Azure container was not specified')
if attrs.get('key_file', '') and attrs.get('key_file_path', ''):
raise serializers.ValidationError('Should be specified key file or key file path')
return attrs return attrs
def create(self, validated_data): def create(self, validated_data):
@ -850,7 +846,7 @@ class CloudStorageSerializer(serializers.ModelSerializer):
key=validated_data.pop('key', ''), key=validated_data.pop('key', ''),
secret_key=validated_data.pop('secret_key', ''), secret_key=validated_data.pop('secret_key', ''),
session_token=validated_data.pop('session_token', ''), session_token=validated_data.pop('session_token', ''),
key_file_path=validated_data.pop('key_file_path', '') or temporary_file, key_file_path=temporary_file,
credentials_type = validated_data.get('credentials_type') credentials_type = validated_data.get('credentials_type')
) )
details = { details = {
@ -936,7 +932,6 @@ class CloudStorageSerializer(serializers.ModelSerializer):
with NamedTemporaryFile(mode='wb', prefix='cvat', delete=False) as temp_key: with NamedTemporaryFile(mode='wb', prefix='cvat', delete=False) as temp_key:
temp_key.write(key_file.read()) temp_key.write(key_file.read())
temporary_file = temp_key.name temporary_file = temp_key.name
# pair (key_file, key_file_path) isn't supported by server, so only one value may be specified
credentials_dict['key_file_path'] = temporary_file credentials_dict['key_file_path'] = temporary_file
key_file.close() key_file.close()
del key_file del key_file

Loading…
Cancel
Save