def crawl_metadata(self): """Crawl metadata service when available. @returns: Dictionary of crawled metadata content containing the keys: meta-data, user-data and dynamic. """ if not self.wait_for_metadata_service(): return {} api_version = self.get_metadata_api_version() crawled_metadata = {} try: crawled_metadata['user-data'] = ec2.get_instance_userdata( api_version, self.metadata_address) crawled_metadata['meta-data'] = ec2.get_instance_metadata( api_version, self.metadata_address) if self.cloud_name == CloudNames.AWS: identity = ec2.get_instance_identity( api_version, self.metadata_address) crawled_metadata['dynamic'] = {'instance-identity': identity} except Exception: util.logexc( LOG, "Failed reading from metadata address %s", self.metadata_address) return {} crawled_metadata['_metadata_api_version'] = api_version return crawled_metadata
def test_userdata_fetch(self): hp.register_uri(hp.GET, 'http://169.254.169.254/%s/user-data' % (self.VERSION), body='stuff', status=200) userdata = eu.get_instance_userdata(self.VERSION) self.assertEquals('stuff', userdata)
def get_data(self): seed_ret = {} if util.read_optional_seed(seed_ret, base=(self.seed_dir + "/")): self.userdata_raw = seed_ret['user-data'] self.metadata = seed_ret['meta-data'] LOG.debug("Using seeded ec2 data from %s", self.seed_dir) self._cloud_platform = Platforms.SEEDED return True strict_mode, _sleep = read_strict_mode( util.get_cfg_by_path(self.sys_cfg, STRICT_ID_PATH, STRICT_ID_DEFAULT), ("warn", None)) LOG.debug("strict_mode: %s, cloud_platform=%s", strict_mode, self.cloud_platform) if strict_mode == "true" and self.cloud_platform == Platforms.UNKNOWN: return False try: if not self.wait_for_metadata_service(): return False start_time = time.time() self.userdata_raw = \ ec2.get_instance_userdata(self.api_ver, self.metadata_address) self.metadata = ec2.get_instance_metadata(self.api_ver, self.metadata_address) LOG.debug("Crawl of metadata service took %.3f seconds", time.time() - start_time) return True except Exception: util.logexc(LOG, "Failed reading from metadata address %s", self.metadata_address) return False
def test_userdata_fetch(self): hp.register_uri(hp.GET, 'http://169.254.169.254/%s/user-data' % (self.VERSION), body='stuff', status=200) userdata = eu.get_instance_userdata(self.VERSION) self.assertEqual('stuff', userdata.decode('utf-8'))
def test_userdata_fetch_fail_server_not_found(self): hp.register_uri( hp.GET, "http://169.254.169.254/%s/user-data" % (self.VERSION), status=404, ) userdata = eu.get_instance_userdata(self.VERSION) self.assertEqual("", userdata)
def test_userdata_fetch_fail_server_dead(self): hp.register_uri( hp.GET, "http://169.254.169.254/%s/user-data" % (self.VERSION), status=500, ) userdata = eu.get_instance_userdata(self.VERSION, retries=0) self.assertEqual("", userdata)
def test_userdata_fetch(self): hp.register_uri( hp.GET, "http://169.254.169.254/%s/user-data" % (self.VERSION), body="stuff", status=200, ) userdata = eu.get_instance_userdata(self.VERSION) self.assertEqual("stuff", userdata.decode("utf-8"))
def crawl_metadata(self): """Crawl metadata service when available. @returns: Dictionary of crawled metadata content containing the keys: meta-data, user-data and dynamic. """ if not self.wait_for_metadata_service(): return {} api_version = self.get_metadata_api_version() redact = AWS_TOKEN_REDACT crawled_metadata = {} if self.cloud_name == CloudNames.AWS: exc_cb = self._refresh_stale_aws_token_cb exc_cb_ud = self._skip_or_refresh_stale_aws_token_cb else: exc_cb = exc_cb_ud = None try: crawled_metadata["user-data"] = ec2.get_instance_userdata( api_version, self.metadata_address, headers_cb=self._get_headers, headers_redact=redact, exception_cb=exc_cb_ud, ) crawled_metadata["meta-data"] = ec2.get_instance_metadata( api_version, self.metadata_address, headers_cb=self._get_headers, headers_redact=redact, exception_cb=exc_cb, ) if self.cloud_name == CloudNames.AWS: identity = ec2.get_instance_identity( api_version, self.metadata_address, headers_cb=self._get_headers, headers_redact=redact, exception_cb=exc_cb, ) crawled_metadata["dynamic"] = {"instance-identity": identity} except Exception: util.logexc( LOG, "Failed reading from metadata address %s", self.metadata_address, ) return {} crawled_metadata["_metadata_api_version"] = api_version return crawled_metadata
def _get_data(self): seed_ret = {} if util.read_optional_seed(seed_ret, base=(self.seed_dir + "/")): self.userdata_raw = seed_ret["user-data"] self.metadata = seed_ret["meta-data"] LOG.debug("Using seeded cloudstack data from: %s", self.seed_dir) return True try: if not self.wait_for_metadata_service(): return False start_time = time.time() self.userdata_raw = ec2.get_instance_userdata( self.api_ver, self.metadata_address) self.metadata = ec2.get_instance_metadata(self.api_ver, self.metadata_address) LOG.debug( "Crawl of metadata service took %s seconds", int(time.time() - start_time), ) password_client = CloudStackPasswordServerClient(self.vr_addr) try: set_password = password_client.get_password() except Exception: util.logexc( LOG, "Failed to fetch password from virtual router %s", self.vr_addr, ) else: if set_password: self.cfg = { "ssh_pwauth": True, "password": set_password, "chpasswd": { "expire": False, }, } return True except Exception: util.logexc( LOG, "Failed fetching from metadata service %s", self.metadata_address, ) return False
def _crawl_metadata(self): """Crawl metadata service when available. @returns: True on success, False otherwise. """ if not self.wait_for_metadata_service(): return False api_version = self.get_metadata_api_version() try: self.userdata_raw = ec2.get_instance_userdata( api_version, self.metadata_address) self.metadata = ec2.get_instance_metadata( api_version, self.metadata_address) except Exception: util.logexc( LOG, "Failed reading from metadata address %s", self.metadata_address) return False return True
def read_metadata( metadata_url=METADATA_URL, api_version=API_VERSION, password_server_port=PASSWORD_SERVER_PORT, url_timeout=URL_TIMEOUT, url_retries=URL_RETRIES, ): """Query the metadata server and return the retrieved data.""" crawled_metadata = {} crawled_metadata["_metadata_api_version"] = api_version try: crawled_metadata["user-data"] = ec2.get_instance_userdata( api_version, metadata_url, timeout=url_timeout, retries=url_retries ) crawled_metadata["meta-data"] = ec2.get_instance_metadata( api_version, metadata_url, timeout=url_timeout, retries=url_retries ) except Exception as e: util.logexc( LOG, "failed reading from metadata url %s (%s)", metadata_url, e ) return {} try: crawled_metadata["password"] = get_password( api_version=api_version, metadata_url=metadata_url, password_server_port=password_server_port, url_retries=url_retries, url_timeout=url_timeout, ) except Exception as e: util.logexc( LOG, "failed to read from password server url %s:%s (%s)", metadata_url, password_server_port, e, ) return crawled_metadata
def get_data(self): seed_ret = {} if util.read_optional_seed(seed_ret, base=(self.seed_dir + "/")): self.userdata_raw = seed_ret['user-data'] self.metadata = seed_ret['meta-data'] LOG.debug("Using seeded cloudstack data from: %s", self.seed_dir) return True try: if not self.wait_for_metadata_service(): return False start_time = time.time() self.userdata_raw = ec2.get_instance_userdata( self.api_ver, self.metadata_address) self.metadata = ec2.get_instance_metadata(self.api_ver, self.metadata_address) LOG.debug("Crawl of metadata service took %s seconds", int(time.time() - start_time)) return True except Exception: util.logexc(LOG, 'Failed fetching from metadata service %s', self.metadata_address) return False
def get_data(self): seed_ret = {} if util.read_optional_seed(seed_ret, base=(self.seed_dir + "/")): self.userdata_raw = seed_ret['user-data'] self.metadata = seed_ret['meta-data'] LOG.debug("Using seeded cloudstack data from: %s", self.seed_dir) return True try: if not self.wait_for_metadata_service(): return False start_time = time.time() self.userdata_raw = ec2.get_instance_userdata(self.api_ver, self.metadata_address) self.metadata = ec2.get_instance_metadata(self.api_ver, self.metadata_address) LOG.debug("Crawl of metadata service took %s seconds", int(time.time() - start_time)) return True except Exception: util.logexc(LOG, 'Failed fetching from metadata service %s', self.metadata_address) return False
def get_data(self): seed_ret = {} if util.read_optional_seed(seed_ret, base=(self.seed_dir + "/")): self.userdata_raw = seed_ret['user-data'] self.metadata = seed_ret['meta-data'] LOG.debug("Using seeded cloudstack data from: %s", self.seed_dir) return True try: if not self.wait_for_metadata_service(): return False start_time = time.time() self.userdata_raw = ec2.get_instance_userdata( self.api_ver, self.metadata_address) self.metadata = ec2.get_instance_metadata(self.api_ver, self.metadata_address) LOG.debug("Crawl of metadata service took %s seconds", int(time.time() - start_time)) password_client = CloudStackPasswordServerClient(self.vr_addr) try: set_password = password_client.get_password() except Exception: util.logexc(LOG, 'Failed to fetch password from virtual router %s', self.vr_addr) else: if set_password: self.cfg = { 'ssh_pwauth': True, 'password': set_password, 'chpasswd': { 'expire': False, }, } return True except Exception: util.logexc(LOG, 'Failed fetching from metadata service %s', self.metadata_address) return False
def test_userdata_fetch_fail_server_not_found(self): hp.register_uri(hp.GET, 'http://169.254.169.254/%s/user-data' % (self.VERSION), status=404) userdata = eu.get_instance_userdata(self.VERSION) self.assertEqual('', userdata)
def test_userdata_fetch_fail_server_dead(self): hp.register_uri(hp.GET, 'http://169.254.169.254/%s/user-data' % (self.VERSION), status=500) userdata = eu.get_instance_userdata(self.VERSION, retries=0) self.assertEqual('', userdata)
def test_userdata_fetch_fail_not_found(self): hp.register_uri(hp.GET, "http://169.254.169.254/%s/user-data" % (self.VERSION), status=404) userdata = eu.get_instance_userdata(self.VERSION, retries=0) self.assertEquals("", userdata)
def test_userdata_fetch_fail_not_found(self): hp.register_uri(hp.GET, 'http://169.254.169.254/%s/user-data' % (self.VERSION), status=404) userdata = eu.get_instance_userdata(self.VERSION, retries=0) self.assertEquals('', userdata)
def test_userdata_fetch(self): hp.register_uri(hp.GET, "http://169.254.169.254/%s/user-data" % (self.VERSION), body="stuff", status=200) userdata = eu.get_instance_userdata(self.VERSION) self.assertEquals("stuff", userdata)