示例#1
0
 def _populate_keys_from_metadata_server(self):
     # get_instance_metadata is imported here because of a circular
     # dependency.
     mssapi.log.debug("Retrieving credentials from metadata server.")
     from mssapi.utils import get_instance_metadata
     timeout = config.getfloat('Mssapi', 'metadata_service_timeout', 1.0)
     attempts = config.getint('Mssapi', 'metadata_service_num_attempts', 1)
     # The num_retries arg is actually the total number of attempts made,
     # so the config options is named *_num_attempts to make this more
     # clear to users.
     metadata = get_instance_metadata(
         timeout=timeout,
         num_retries=attempts,
         data='meta-data/iam/security-credentials/')
     if metadata:
         # I'm assuming there's only one role on the instance profile.
         security = list(metadata.values())[0]
         self._access_key = security['AccessKeyId']
         self._secret_key = self._convert_key_to_str(
             security['SecretAccessKey'])
         self._security_token = security['Token']
         expires_at = security['Expiration']
         self._credential_expiry_time = datetime.strptime(
             expires_at, "%Y-%m-%dT%H:%M:%SZ")
         mssapi.log.debug("Retrieved credentials will expire in %s at: %s",
                          self._credential_expiry_time - datetime.now(),
                          expires_at)
示例#2
0
 def write_metadata(self):
     fp = open(os.path.expanduser(MssapiConfigPath), 'w')
     fp.write('[Instance]\n')
     inst_data = get_instance_metadata()
     for key in inst_data:
         fp.write('%s = %s\n' % (key, inst_data[key]))
     user_data = get_instance_userdata()
     fp.write('\n%s\n' % user_data)
     fp.write('[Pyami]\n')
     fp.write('working_dir = %s\n' % self.working_dir)
     fp.close()
     # This file has the AWS credentials, should we lock it down?
     # os.chmod(MssapiConfigPath, stat.S_IREAD | stat.S_IWRITE)
     # now that we have written the file, read it into a pyami Config object
     mssapi.config = Config()
     mssapi.init_logging()
示例#3
0
 def write_metadata(self):
     fp = open(os.path.expanduser(MssapiConfigPath), 'w')
     fp.write('[Instance]\n')
     inst_data = get_instance_metadata()
     for key in inst_data:
         fp.write('%s = %s\n' % (key, inst_data[key]))
     user_data = get_instance_userdata()
     fp.write('\n%s\n' % user_data)
     fp.write('[Pyami]\n')
     fp.write('working_dir = %s\n' % self.working_dir)
     fp.close()
     # This file has the AWS credentials, should we lock it down?
     # os.chmod(MssapiConfigPath, stat.S_IREAD | stat.S_IWRITE)
     # now that we have written the file, read it into a pyami Config object
     mssapi.config = Config()
     mssapi.init_logging()
示例#4
0
 def _populate_keys_from_metadata_server(self):
     # get_instance_metadata is imported here because of a circular
     # dependency.
     mssapi.log.debug("Retrieving credentials from metadata server.")
     from mssapi.utils import get_instance_metadata
     timeout = config.getfloat('Mssapi', 'metadata_service_timeout', 1.0)
     attempts = config.getint('Mssapi', 'metadata_service_num_attempts', 1)
     # The num_retries arg is actually the total number of attempts made,
     # so the config options is named *_num_attempts to make this more
     # clear to users.
     metadata = get_instance_metadata(
         timeout=timeout, num_retries=attempts,
         data='meta-data/iam/security-credentials/')
     if metadata:
         # I'm assuming there's only one role on the instance profile.
         security = list(metadata.values())[0]
         self._access_key = security['AccessKeyId']
         self._secret_key = self._convert_key_to_str(security['SecretAccessKey'])
         self._security_token = security['Token']
         expires_at = security['Expiration']
         self._credential_expiry_time = datetime.strptime(
             expires_at, "%Y-%m-%dT%H:%M:%SZ")
         mssapi.log.debug("Retrieved credentials will expire in %s at: %s",
                        self._credential_expiry_time - datetime.now(), expires_at)