def UploadPublicKey(self, user, public_key): """Uploads a public key to a user.""" description = 'Added by gcloud compute from {0}'.format( self.GetHostName()) default_expiration = '1d' parser = arg_parsers.Duration() expiration_rfc3339_str = time_utils.CalculateExpiration( parser(default_expiration)) request = self.PublicKeyGenerateUploadRequest( user, public_key, expiration_time=expiration_rfc3339_str, description=description) requests = [request] errors = [] res = list( request_helper.MakeRequests(requests=requests, http=self.http, batch_url=self.batch_url, errors=errors, custom_get_requests=None)) if errors: utils.RaiseException(errors, UserException, error_message='Could not upload public key:') return res
def _ConstructWindowsKeyEntry(self, user, modulus, exponent, email): """Return a JSON formatted entry for 'windows-keys'.""" expire_str = time_utils.CalculateExpiration(RSA_KEY_EXPIRATION_TIME_SEC) windows_key_data = {'userName': user, 'modulus': modulus, 'exponent': exponent, 'email': email, 'expireOn': expire_str} windows_key_entry = json.dumps(windows_key_data, sort_keys=True) return windows_key_entry
def CreateRequests(self, args): name = args.name if not name: name = gaia_utils.GetDefaultAccountName(self.http) user_ref = self.CreateAccountsReference( name, resource_type='users') valid_key_types = [ 'ssh-rsa', 'ssh-dss', 'ecdsa-sha2-nistp256', 'ssh-ed25519'] public_keys = [] for key_file in args.public_key_files: key_text = file_utils.ReadFile(key_file, 'public-key') if key_text.split(' ', 1)[0] not in valid_key_types: raise exceptions.ToolException( 'You must specify a public key file that contains a key of a ' 'supported form. Supported forms are {0}.' .format(', '.join(valid_key_types)) ) public_keys.append(key_text) formatted_expiration = time_utils.CalculateExpiration(args.expire) requests = [] for key in public_keys: public_key_message = self.messages.PublicKey( description=args.description, expirationTimestamp=formatted_expiration, key=key) request = self.messages.ComputeaccountsUsersAddPublicKeyRequest( project=self.project, publicKey=public_key_message, user=user_ref.Name()) requests.append(request) return requests