diff options
| author | Scott Moser <smoser@brickies.net> | 2017-09-15 22:50:21 -0400 | 
|---|---|---|
| committer | Scott Moser <smoser@brickies.net> | 2017-09-18 14:13:11 -0400 | 
| commit | 10f067d87a2d48092c593862e686c517c57b987c (patch) | |
| tree | 144e1a12fe3d9dcbc2ee210a61e6069dcfcc6fe6 | |
| parent | e626966ee7d339b53d2c8b14a8f2ff8e3fe892ee (diff) | |
| download | vyos-cloud-init-10f067d87a2d48092c593862e686c517c57b987c.tar.gz vyos-cloud-init-10f067d87a2d48092c593862e686c517c57b987c.zip | |
GCE: Fix usage of user-data.
This regressed in the rework of GCE datasource to have a main.
The fix really just stores the user-data that was read in
self.userdata_raw, rather than self.userdata.  That is consistent
with other datasources and ulitimately how it was before the refactor.
The main is updated to address the fact that user-data is binary data
and may not be able to be printed.
LP: #1717598
| -rw-r--r-- | cloudinit/sources/DataSourceGCE.py | 26 | ||||
| -rw-r--r-- | tests/unittests/test_datasource/test_gce.py | 3 | 
2 files changed, 21 insertions, 8 deletions
| diff --git a/cloudinit/sources/DataSourceGCE.py b/cloudinit/sources/DataSourceGCE.py index 94484d60..ccae4200 100644 --- a/cloudinit/sources/DataSourceGCE.py +++ b/cloudinit/sources/DataSourceGCE.py @@ -62,7 +62,7 @@ class DataSourceGCE(sources.DataSource):                  LOG.debug(ret['reason'])              return False          self.metadata = ret['meta-data'] -        self.userdata = ret['user-data'] +        self.userdata_raw = ret['user-data']          return True      @property @@ -80,9 +80,6 @@ class DataSourceGCE(sources.DataSource):          # GCE has long FDQN's and has asked for short hostnames          return self.metadata['local-hostname'].split('.')[0] -    def get_userdata_raw(self): -        return self.userdata -      @property      def availability_zone(self):          return self.metadata['availability-zone'] @@ -202,6 +199,9 @@ def get_datasource_list(depends):  if __name__ == "__main__":      import argparse      import json +    import sys + +    from base64 import b64encode      parser = argparse.ArgumentParser(description='Query GCE Metadata Service')      parser.add_argument("--endpoint", metavar="URL", @@ -211,8 +211,20 @@ if __name__ == "__main__":                          help="Ignore smbios platform check",                          action='store_false', default=True)      args = parser.parse_args() -    print(json.dumps( -        read_md(address=args.endpoint, platform_check=args.platform_check), -        indent=1, sort_keys=True, separators=(',', ': '))) +    data = read_md(address=args.endpoint, platform_check=args.platform_check) +    if 'user-data' in data: +        # user-data is bytes not string like other things. Handle it specially. +        # if it can be represented as utf-8 then do so.  Otherwise print base64 +        # encoded value in the key user-data-b64. +        try: +            data['user-data'] = data['user-data'].decode() +        except UnicodeDecodeError: +            sys.stderr.write("User-data cannot be decoded. " +                             "Writing as base64\n") +            del data['user-data'] +            # b64encode returns a bytes value. decode to get the string. +            data['user-data-b64'] = b64encode(data['user-data']).decode() + +    print(json.dumps(data, indent=1, sort_keys=True, separators=(',', ': ')))  # vi: ts=4 expandtab diff --git a/tests/unittests/test_datasource/test_gce.py b/tests/unittests/test_datasource/test_gce.py index 50e49a10..d399ae7a 100644 --- a/tests/unittests/test_datasource/test_gce.py +++ b/tests/unittests/test_datasource/test_gce.py @@ -23,7 +23,8 @@ GCE_META = {      'instance/zone': 'foo/bar',      'project/attributes/sshKeys': 'user:ssh-rsa AA2..+aRD0fyVw== root@server',      'instance/hostname': 'server.project-foo.local', -    'instance/attributes/user-data': b'/bin/echo foo\n', +    # UnicodeDecodeError below if set to ds.userdata instead of userdata_raw +    'instance/attributes/user-data': b'/bin/echo \xff\n',  }  GCE_META_PARTIAL = { | 
