Read encoded with base64 user data
This allows users of CloudSigma's VM to encode their user data with base64. In order to do that thet have to add the ``cloudinit-user-data`` field to the ``base64_fields``. The latter is a comma-separated field with all the meta fields whit base64 encoded values.
This commit is contained in:
parent
eb6ab3fd3d
commit
f385a85879
|
@ -15,6 +15,7 @@
|
||||||
#
|
#
|
||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
from base64 import b64decode
|
||||||
import re
|
import re
|
||||||
|
|
||||||
from cloudinit import log as logging
|
from cloudinit import log as logging
|
||||||
|
@ -60,7 +61,11 @@ class DataSourceCloudSigma(sources.DataSource):
|
||||||
if dsmode == "disabled" or dsmode != self.dsmode:
|
if dsmode == "disabled" or dsmode != self.dsmode:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
base64_fields = server_meta.get('base64_fields', '').split(',')
|
||||||
self.userdata_raw = server_meta.get('cloudinit-user-data', "")
|
self.userdata_raw = server_meta.get('cloudinit-user-data', "")
|
||||||
|
if 'cloudinit-user-data' in base64_fields:
|
||||||
|
self.userdata_raw = b64decode(self.userdata_raw)
|
||||||
|
|
||||||
self.metadata = server_context
|
self.metadata = server_context
|
||||||
self.ssh_public_key = server_meta['ssh_public_key']
|
self.ssh_public_key = server_meta['ssh_public_key']
|
||||||
|
|
||||||
|
|
|
@ -23,6 +23,10 @@ You can provide user-data to the VM using the dedicated `meta field`_ in the `se
|
||||||
header could be omitted. However since this is a raw-text field you could provide any of the valid
|
header could be omitted. However since this is a raw-text field you could provide any of the valid
|
||||||
`config formats`_.
|
`config formats`_.
|
||||||
|
|
||||||
|
You have the option to encode your user-data using Base64. In order to do that you have to add the
|
||||||
|
``cloudinit-user-data`` field to the ``base64_fields``. The latter is a comma-separated field with
|
||||||
|
all the meta fields whit base64 encoded values.
|
||||||
|
|
||||||
If your user-data does not need an internet connection you can create a
|
If your user-data does not need an internet connection you can create a
|
||||||
`meta field`_ in the `server context`_ ``cloudinit-dsmode`` and set "local" as value.
|
`meta field`_ in the `server context`_ ``cloudinit-dsmode`` and set "local" as value.
|
||||||
If this field does not exist the default value is "net".
|
If this field does not exist the default value is "net".
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
# coding: utf-8
|
# coding: utf-8
|
||||||
|
import copy
|
||||||
from unittest import TestCase
|
from unittest import TestCase
|
||||||
|
|
||||||
from cloudinit.cs_utils import Cepko
|
from cloudinit.cs_utils import Cepko
|
||||||
|
@ -24,7 +25,8 @@ SERVER_CONTEXT = {
|
||||||
|
|
||||||
|
|
||||||
class CepkoMock(Cepko):
|
class CepkoMock(Cepko):
|
||||||
result = SERVER_CONTEXT
|
def __init__(self, mocked_context):
|
||||||
|
self.result = mocked_context
|
||||||
|
|
||||||
def all(self):
|
def all(self):
|
||||||
return self
|
return self
|
||||||
|
@ -33,7 +35,7 @@ class CepkoMock(Cepko):
|
||||||
class DataSourceCloudSigmaTest(TestCase):
|
class DataSourceCloudSigmaTest(TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.datasource = DataSourceCloudSigma.DataSourceCloudSigma("", "", "")
|
self.datasource = DataSourceCloudSigma.DataSourceCloudSigma("", "", "")
|
||||||
self.datasource.cepko = CepkoMock()
|
self.datasource.cepko = CepkoMock(SERVER_CONTEXT)
|
||||||
self.datasource.get_data()
|
self.datasource.get_data()
|
||||||
|
|
||||||
def test_get_hostname(self):
|
def test_get_hostname(self):
|
||||||
|
@ -57,3 +59,12 @@ class DataSourceCloudSigmaTest(TestCase):
|
||||||
def test_user_data(self):
|
def test_user_data(self):
|
||||||
self.assertEqual(self.datasource.userdata_raw,
|
self.assertEqual(self.datasource.userdata_raw,
|
||||||
SERVER_CONTEXT['meta']['cloudinit-user-data'])
|
SERVER_CONTEXT['meta']['cloudinit-user-data'])
|
||||||
|
|
||||||
|
def test_encoded_user_data(self):
|
||||||
|
encoded_context = copy.deepcopy(SERVER_CONTEXT)
|
||||||
|
encoded_context['meta']['base64_fields'] = 'cloudinit-user-data'
|
||||||
|
encoded_context['meta']['cloudinit-user-data'] = 'aGkgd29ybGQK'
|
||||||
|
self.datasource.cepko = CepkoMock(encoded_context)
|
||||||
|
self.datasource.get_data()
|
||||||
|
|
||||||
|
self.assertEqual(self.datasource.userdata_raw, b'hi world\n')
|
||||||
|
|
Loading…
Reference in New Issue