Пример #1
0
def main(argv):
    """A script to test Admin SDK Directory APIs."""
    flags = common_flags.ParseFlags(argv, 'List domain users.', AddFlags)
    if flags.json:
        FILE_MANAGER.ExitIfCannotOverwriteFile(FILE_MANAGER.USERS_FILE_NAME,
                                               overwrite_ok=flags.force)

    http = auth_helper.GetAuthorizedHttp(flags)
    api_wrapper = users_api.UsersApiWrapper(http)

    max_results = flags.first_n if flags.first_n > 0 else None
    try:
        if flags.json:
            user_list = api_wrapper.GetDomainUsers(flags.apps_domain,
                                                   max_results=max_results)
        else:
            api_wrapper.PrintDomainUsers(flags.apps_domain,
                                         max_results=max_results)
    except admin_api_tool_errors.AdminAPIToolUserError as e:
        log_utils.LogError(
            'Unable to enumerate users from domain %s.' % flags.apps_domain, e)
        sys.exit(1)

    if flags.json:
        try:
            filename_path = FILE_MANAGER.WriteJsonFile(
                FILE_MANAGER.USERS_FILE_NAME,
                user_list,
                overwrite_ok=flags.force)
        except admin_api_tool_errors.AdminAPIToolFileError as e:
            # This usually means the file already exists and --force not supplied.
            log_utils.LogError('Unable to write the domain users file.', e)
            sys.exit(1)
        print 'Users list written to %s.' % filename_path
def main(argv):
  """Retrieve and print the customer_id for a given apps domain."""
  flags = common_flags.ParseFlags(argv, 'List the Google Apps Customer ID.',
                                  AddFlags)
  http = auth_helper.GetAuthorizedHttp(flags)
  api_wrapper = users_api.UsersApiWrapper(http)
  try:
    api_wrapper.PrintCustomerId(flags.apps_domain)
  except admin_api_tool_errors.AdminAPIToolUserError as e:
    log_utils.LogError('Unable to enumerate one user.', e)
    sys.exit(1)
Пример #3
0
def main(argv):
  """A script to test Admin SDK Directory APIs: delete."""
  flags = common_flags.ParseFlags(argv, 'Remove a domain user.', AddFlags)
  http = auth_helper.GetAuthorizedHttp(flags)
  api_wrapper = users_api.UsersApiWrapper(http)
  try:
    api_wrapper.DeleteDomainUser(flags.user_email)
  except admin_api_tool_errors.AdminAPIToolUserError as e:
    log_utils.LogError('Unable to rm user %s.' % flags.user_email, e)
    sys.exit(1)
  log_utils.LogInfo('User %s successfully removed.' % flags.user_email)
Пример #4
0
def main(argv):
    """A script to test Admin SDK Directory APIs."""
    flags = common_flags.ParseFlags(argv, 'Add a domain user.', AddFlags)
    http = auth_helper.GetAuthorizedHttp(flags)
    api_wrapper = users_api.UsersApiWrapper(http)
    try:
        api_wrapper.AddDomainUser(flags.first_name, flags.last_name,
                                  flags.user_email, flags.password)
    except admin_api_tool_errors.AdminAPIToolUserError as e:
        # Could not add user. Details provided by api wrapper in the e string.
        log_utils.LogError('Unable to add user %s.' % flags.user_email, e)
        sys.exit(1)
    log_utils.LogInfo('User %s added.' % flags.user_email)
Пример #5
0
def main(argv):
    """A script to test Admin SDK Directory APIs: ls (show) user."""
    flags = common_flags.ParseFlags(argv, 'List info about a domain user.',
                                    AddFlags)
    http = auth_helper.GetAuthorizedHttp(flags)
    if flags.plus_domains:
        user_api = people_api.PlusDomains(http)
    else:
        user_api = users_api.UsersApiWrapper(http)
    try:
        user_api.PrintDomainUser(flags.user_email, flags.long_list)
    except admin_api_tool_errors.AdminAPIToolUserError as e:
        # Could not ls user. Details provided by api wrapper in the e string.
        log_utils.LogError('Unable to locate user %s.' % flags.user_email, e)
        sys.exit(1)
Пример #6
0
    def setUp(self, mock_ApiclientDiscoveryBuildFn):
        """Setup mocks to simulate Apiary users service exported by the Admin SDK.

    The UsersApiWrapper() object contains the users service object so that
    is mocked next to simulate the back-end provider.  Because Apiary is
    dynamic we have to do this through the build() method.

    Args:
      mock_ApiclientDiscoveryBuildFn: mock object to stub the build function.
    """
        log_utils.SetupLogging(verbose_flag=_ENABLE_VERBOSE_LOGGING)
        mock_ApiclientDiscoveryBuildFn.return_value = (
            MockDirectoryServiceObject())
        self._api_wrapper = users_api.UsersApiWrapper(http=None)
        self._new_stdout = PrintMocker.MockStdOut()
def main(argv):
    """A script to test Apps Security APIs."""
    flags = common_flags.ParseFlags(
        argv, 'List token info about a user and client.', AddFlags)
    http = auth_helper.GetAuthorizedHttp(flags)
    user_api = users_api.UsersApiWrapper(http)
    if not user_api.IsDomainUser(flags.user_email):
        print 'User %s not found.' % flags.user_email
        sys.exit(1)
    apps_security_api = tokens_api.TokensApiWrapper(http)
    try:
        apps_security_api.PrintTokenForUserClientId(flags.user_email,
                                                    flags.client_id,
                                                    flags.long_list)
    except admin_api_tool_errors.AdminAPIToolTokenRequestError as e:
        log_utils.LogError(
            'Unable to retrieve tokens for user %s.' % flags.user_email, e)
        sys.exit(1)
def _GetUserList(http, flags):
  """Helper to retrieve the user list from local file or request.

  The list may be 10's of thousands of users so we prefer to keep a
  cached copy local for user_id lookups.

  Args:
    http: An authorized http interface object.
    flags: Argparse flags object with apps_domain, resume and first_n.

  Returns:
    A list of user tuples. For example:
      [["*****@*****.**", "000000000298938768732", "George Lasta"],
      ["*****@*****.**", "000000000406809560189", "usertest0
      userlast"], ["*****@*****.**", "000000000766612723480",
      "usertest100 userlast"]]
  """
  # Need a list of users in the domain.
  if FILE_MANAGER.FileExists(FILE_MANAGER.USERS_FILE_NAME):
    log_utils.LogInfo('Using existing users list last modified on %s.' %
                      FILE_MANAGER.FileTime(FILE_MANAGER.USERS_FILE_NAME))
    users_list = FILE_MANAGER.ReadJsonFile(FILE_MANAGER.USERS_FILE_NAME)
    # Verify that the domain has not changed
    if users_list:
      domain = validators.GetEmailParts(users_list[0][0])[1]
      if domain != flags.apps_domain:
        log_utils.LogError(
            'You have requested to use domain %s, but your existing users '
            'file \n(%s) was generated using\n%s. Please remove the file or '
            'specify %s as your apps_domain.' % (
                flags.apps_domain,
                FILE_MANAGER.BuildFullPathToFileName(
                    FILE_MANAGER.USERS_FILE_NAME),
                domain, domain))
        sys.exit(1)
  else:
    log_utils.LogInfo('Retrieving list of users...')
    api_wrapper = users_api.UsersApiWrapper(http)
    users_list = api_wrapper.GetDomainUsers(flags.apps_domain)
    FILE_MANAGER.WriteJsonFile(FILE_MANAGER.USERS_FILE_NAME, users_list)
  user_count = len(users_list)
  log_utils.LogInfo('Found %d users to check.' % user_count)
  return users_list, user_count
def main(argv):
    """A script to test Apps Security APIs."""
    flags = common_flags.ParseFlags(argv,
                                    'List token info about a domain user.',
                                    AddFlags)
    http = auth_helper.GetAuthorizedHttp(flags)
    user_api = users_api.UsersApiWrapper(http)
    if not user_api.IsDomainUser(flags.user_email):
        print 'User %s not found.' % flags.user_email
        sys.exit(1)

    apps_security_api = tokens_api.TokensApiWrapper(http)
    try:
        apps_security_api.PrintTokensForUser(flags.user_email, flags.long_list)
    except admin_api_tool_errors.AdminAPIToolTokenRequestError as e:
        # This suggests an unexpected response from the apps security api.
        # As much detail as possible is provided by the raiser.
        log_utils.LogError(
            'Unable to retrieve tokens for user %s.' % flags.user_email, e)
        sys.exit(1)
Пример #10
0
def main(argv):
    """Produce a user report with contents stipulated by flags."""
    flags = common_flags.ParseFlags(argv,
                                    'Create a report of domain user info.',
                                    AddFlags)
    FILE_MANAGER.ExitIfCannotOverwriteFile(flags.output_file,
                                           overwrite_ok=flags.force)
    http = auth_helper.GetAuthorizedHttp(flags)
    api_wrapper = users_api.UsersApiWrapper(http)

    max_results = flags.first_n if flags.first_n > 0 else None
    try:
        user_list = api_wrapper.GetDomainUsers(flags.apps_domain,
                                               basic=False,
                                               max_results=max_results,
                                               query_filter=flags.query_filter)
    except admin_api_tool_errors.AdminAPIToolUserError as e:
        log_utils.LogError(
            'Unable to enumerate users from domain %s.' % flags.apps_domain, e)
        sys.exit(1)

    ReportDomainUsers(user_list, flags)
Пример #11
0
def main(argv):
    """Save the domain to a file to avoid constantly passing a flag."""
    flags = common_flags.ParseFlags(argv,
                                    'Save default domain for all commands.',
                                    AddFlags)

    # Fail if the defaults file exists. Better than waiting for write to check.
    FILE_MANAGER.ExitIfCannotOverwriteFile(
        FILE_MANAGER.DEFAULT_DOMAIN_FILE_NAME,
        work_dir=False,
        overwrite_ok=flags.force)
    http = auth_helper.GetAuthorizedHttp(flags)
    api_wrapper = users_api.UsersApiWrapper(http)
    try:
        customer_id = api_wrapper.GetCustomerId(flags.apps_domain)
    except admin_api_tool_errors.AdminAPIToolUserError as e:
        log_utils.LogError(
            'Unable to retrieve customer_id for domain %s.' %
            flags.apps_domain, e)
        sys.exit(1)
    filename_path = FILE_MANAGER.WriteDefaults(flags.apps_domain, customer_id,
                                               flags.force)
    print 'Default domain stored in %s.' % filename_path
Пример #12
0
def main(argv):
    """A script to revoke tokens issued to users."""
    flags = common_flags.ParseFlags(
        argv, 'Revoke token issued by a user for a client.', AddFlags)
    http = auth_helper.GetAuthorizedHttp(flags)
    user_api = users_api.UsersApiWrapper(http)
    if not user_api.IsDomainUser(flags.user_email):
        print 'User %s not found.' % flags.user_email
        sys.exit(1)

    apps_security_api = tokens_api.TokensApiWrapper(http)
    try:
        # NOTE: attempting to revoke a non-existent token causes no
        #       discernible output (no failure message or fail status).
        apps_security_api.DeleteToken(flags.user_email, flags.client_id)
    except admin_api_tool_errors.AdminAPIToolTokenRequestError as e:
        log_utils.LogError(
            'Unable to revoke token for user %s and client_id %s.' %
            (flags.user_email, flags.client_id), e)
        sys.exit(1)
    log_utils.LogInfo(
        'Successfully revoked token for user %s for client_id %s.' %
        (flags.user_email, flags.client_id))