Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent undefined key name when creating Secret #6964

Merged
merged 2 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions ui/src/formkit/inputs/secret/SecretSelect.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { useQueryClient } from "@tanstack/vue-query";
import { onClickOutside } from "@vueuse/core";
import Fuse from "fuse.js";
import { computed, ref, watch, type PropType } from "vue";
import SecretCreationModal from "./components/SecretCreationModal.vue";
import SecretEditModal from "./components/SecretEditModal.vue";
import SecretListModal from "./components/SecretListModal.vue";
import { Q_KEY, useSecretsFetch } from "./composables/use-secrets-fetch";
Expand All @@ -25,6 +26,8 @@ const props = defineProps({
},
});

const requiredKey = computed(() => props.context.requiredKey);

const selectedSecret = ref<Secret>();
const dropdownVisible = ref(false);
const text = ref("");
Expand Down Expand Up @@ -137,10 +140,11 @@ const scrollToSelected = () => {

// Check required key and edit secret
function hasRequiredKey(secret: Secret) {
return !!secret.stringData?.[props.context.requiredKey as string];
return !!secret.stringData?.[requiredKey.value as string];
}
const secretToUpdate = ref<Secret>();
const secretEditModalVisible = ref(false);
const secretCreationModalVisible = ref(false);

const handleSelect = (secret?: Secret) => {
if (!secret || secret.metadata.name === props.context._value) {
Expand All @@ -159,7 +163,7 @@ const handleSelect = (secret?: Secret) => {
if (!hasRequiredKey(secret)) {
const stringDataToUpdate = {
...secret.stringData,
[props.context.requiredKey as string]: "",
[requiredKey.value ? (requiredKey.value as string) : ""]: "",
};
secretToUpdate.value = {
...secret,
Expand All @@ -177,6 +181,11 @@ const secretListModalVisible = ref(false);

// Create new secret
async function handleCreateSecret() {
if (!requiredKey.value) {
secretCreationModalVisible.value = true;
return;
}

const { data: newSecret } = await coreApiClient.secret.createSecret({
secret: {
metadata: {
Expand All @@ -187,7 +196,7 @@ async function handleCreateSecret() {
apiVersion: "v1alpha1",
type: "Opaque",
stringData: {
[props.context.requiredKey as string]: text.value,
[requiredKey.value as string]: text.value,
},
},
});
Expand All @@ -212,6 +221,13 @@ async function handleCreateSecret() {
:secret="secretToUpdate"
@close="secretEditModalVisible = false"
/>
<SecretCreationModal
v-if="secretCreationModalVisible"
:form-state="{
stringDataArray: [{ key: requiredKey ? requiredKey as string : '', value: text || '' }],
}"
@close="secretCreationModalVisible = false"
/>
<div
ref="wrapperRef"
class="flex h-full w-full items-center border border-gray-300 transition-all"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ import SecretForm from "./SecretForm.vue";
const { t } = useI18n();
const queryClient = useQueryClient();

withDefaults(
defineProps<{
formState?: SecretFormState;
}>(),
{ formState: undefined }
);

const emit = defineEmits<{
(event: "close"): void;
}>();
Expand Down Expand Up @@ -61,7 +68,7 @@ function onSubmit(data: SecretFormState) {
:width="600"
@close="emit('close')"
>
<SecretForm @submit="onSubmit" />
<SecretForm :form-state="formState" @submit="onSubmit" />

<template #footer>
<VSpace>
Expand Down
Loading