You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by we...@apache.org on 2024/06/18 10:11:14 UTC

(cloudstack) branch 4.19 updated: UI: Fix Userdata registration from UI (#8791)

This is an automated email from the ASF dual-hosted git repository.

weizhou pushed a commit to branch 4.19
in repository https://gitbox.apache.org/repos/asf/cloudstack.git


The following commit(s) were added to refs/heads/4.19 by this push:
     new 75341963614 UI: Fix Userdata registration from UI (#8791)
75341963614 is described below

commit 7534196361434b8dafbaaa6f2ad0a22efddedab7
Author: Pearl Dsilva <pe...@gmail.com>
AuthorDate: Tue Jun 18 06:11:08 2024 -0400

    UI: Fix Userdata registration from UI (#8791)
    
    * UI: Fix Userdata registration from UI
    
    * add isbase64 checkbox
---
 ui/public/locales/en.json                 |  1 +
 ui/src/utils/plugins.js                   | 12 ++++++++++--
 ui/src/views/compute/RegisterUserData.vue |  9 +++++++--
 3 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/ui/public/locales/en.json b/ui/public/locales/en.json
index e798a798c70..c49fef5e4fe 100644
--- a/ui/public/locales/en.json
+++ b/ui/public/locales/en.json
@@ -1110,6 +1110,7 @@
 "label.ipv6.subnets": "IPv6 Subnets",
 "label.ip.addresses": "IP Addresses",
 "label.iqn": "Target IQN",
+"label.is.base64.encoded": "Base64 encoded",
 "label.is.in.progress": "is in progress",
 "label.is.shared": "Is shared",
 "label.is2faenabled": "Is 2FA enabled",
diff --git a/ui/src/utils/plugins.js b/ui/src/utils/plugins.js
index acaebf45892..e358988fac9 100644
--- a/ui/src/utils/plugins.js
+++ b/ui/src/utils/plugins.js
@@ -486,6 +486,15 @@ export const fileSizeUtilPlugin = {
   }
 }
 
+function isBase64 (str) {
+  try {
+    const decoded = new TextDecoder().decode(Uint8Array.from(atob(str), c => c.charCodeAt(0)))
+    return btoa(decoded) === str
+  } catch (err) {
+    return false
+  }
+}
+
 export const genericUtilPlugin = {
   install (app) {
     app.config.globalProperties.$isValidUuid = function (uuid) {
@@ -494,8 +503,7 @@ export const genericUtilPlugin = {
     }
 
     app.config.globalProperties.$toBase64AndURIEncoded = function (text) {
-      const base64regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/
-      if (base64regex.test(text)) {
+      if (isBase64(text)) {
         return text
       }
       return encodeURIComponent(btoa(unescape(encodeURIComponent(text))))
diff --git a/ui/src/views/compute/RegisterUserData.vue b/ui/src/views/compute/RegisterUserData.vue
index 990e59ff277..8e6311fdf9a 100644
--- a/ui/src/views/compute/RegisterUserData.vue
+++ b/ui/src/views/compute/RegisterUserData.vue
@@ -43,6 +43,9 @@
             v-model:value="form.userdata"
             :placeholder="apiParams.userdata.description"/>
         </a-form-item>
+        <a-form-item name="isbase64" ref="isbase64" :label="$t('label.is.base64.encoded')">
+          <a-checkbox v-model:checked="form.isbase64"></a-checkbox>
+        </a-form-item>
         <a-form-item name="params" ref="params">
           <template #label>
             <tooltip-label :title="$t('label.userdataparams')" :tooltip="apiParams.params.description"/>
@@ -147,7 +150,9 @@ export default {
   methods: {
     initForm () {
       this.formRef = ref()
-      this.form = reactive({})
+      this.form = reactive({
+        isbase64: false
+      })
       this.rules = reactive({
         name: [{ required: true, message: this.$t('message.error.name') }],
         userdata: [{ required: true, message: this.$t('message.error.userdata') }]
@@ -204,7 +209,7 @@ export default {
         if (this.isValidValueForKey(values, 'account') && values.account.length > 0) {
           params.account = values.account
         }
-        params.userdata = this.$toBase64AndURIEncoded(values.userdata)
+        params.userdata = values.isbase64 ? values.userdata : this.$toBase64AndURIEncoded(values.userdata)
 
         if (values.params != null && values.params.length > 0) {
           var userdataparams = values.params.join(',')