Skip to content

cli command line arguments for url and token #960

Closed

Description

@rrauenza

(enhancement request)

What do you think of adding command line arguments to pass the url and token on the command line? This would make integration with gitlab ci easier,

gitlab --url https://$CI_SERVER_HOST --token $CI_JOB_TOKEN ...

Either that, or an option to read them from the environment (or default them from the environment?)

Activity

  1. max-wittig commented on Dec 9, 2019

    @max-wittig
    Member

    Good idea.

  2. chrisoldwood commented on Jan 30, 2020

    @chrisoldwood

    Is this a duplicate of #893?

  3. nejch commented on Feb 25, 2020

    @nejch
    Member

    @rrauenza if you'd be OK with env variables only, this can maybe be closed as a duplicate as Chris suggests and I can try and work on #893.

    Environment variables just make sense as CLI arguments would end up passing environment variables when used in CI systems anyway (which is probably what a lot of people do with this library, like your example).

    There are many issues open for CLI so maybe args can be a topic for when that gets reworked.

  4. rrauenza commented on Jul 9, 2020

    @rrauenza
    Author

    Yeah, that sounds reasonable.

  5. bittner commented on Dec 17, 2020

    @bittner
    Contributor

    I would need this feature now. Where would we implement the API token? As in:

    curl --silent --header "Authorization: Bearer $GITLAB_API_TOKEN" ...

    Aside, for clarification:

    For simplicity, I'd prefer to have it like @rrauenza originally requested: A single --token option for everything, which overrides any value in configurations files. We probably also should cover --username and --password (with an optional value to allow interactive input), which as a side effect will make the container entry point unnecessary, e.g.

    gitlab --url https://$CI_SERVER_HOST --token $GITLAB_API_TOKEN ...
    gitlab --username $CI_DEPLOY_USER --password ...

    I'll try to find out if we can read from the environment from within the argparse setup code via the defaults. To my knowledge, there is not direct way to make argparse consider environment variables (unlike click).

  6. nejch commented on Dec 17, 2020

    @nejch
    Member

    Yeah, click would make a lot more sense for this, which is why this has kind of stalled.
    To make it a global option you would probably want to add them to the base parser here:

    def _get_base_parser(add_help=True):
    parser = argparse.ArgumentParser(
    add_help=add_help, description="GitLab API Command Line Interface"
    )
    parser.add_argument("--version", help="Display the version.", action="store_true")
    parser.add_argument(
    "-v",
    "--verbose",
    "--fancy",
    help="Verbose mode (legacy format only)",
    action="store_true",
    )
    parser.add_argument(
    "-d", "--debug", help="Debug mode (display HTTP requests)", action="store_true"
    )
    parser.add_argument(
    "-c",
    "--config-file",
    action="append",
    help="Configuration file to use. Can be used multiple times.",
    )
    parser.add_argument(
    "-g",
    "--gitlab",
    help=(
    "Which configuration section should "
    "be used. If not defined, the default selection "
    "will be used."
    ),
    required=False,
    )
    parser.add_argument(
    "-o",
    "--output",
    help="Output format (v4 only): json|legacy|yaml",
    required=False,
    choices=["json", "legacy", "yaml"],
    default="legacy",
    )
    parser.add_argument(
    "-f",
    "--fields",
    help=(
    "Fields to display in the output (comma "
    "separated). Not used with legacy output"
    ),
    required=False,
    )
    return parser

    Then if those arguments are provided and valid you would need to override from_config() in CLI and instantiate Gitlab with the args instead:

    gl = gitlab.Gitlab.from_config(gitlab_id, config_files)

    Feel free to give it a shot :)

    Do we technically need the distinction between the 3 types of token?

    Different types of tokens send different headers to the API, so you need to distinguish this, see

    if self.private_token:
    self.headers.pop("Authorization", None)
    self.headers["PRIVATE-TOKEN"] = self.private_token
    self.headers.pop("JOB-TOKEN", None)
    if self.oauth_token:
    self.headers["Authorization"] = "Bearer %s" % self.oauth_token
    self.headers.pop("PRIVATE-TOKEN", None)
    self.headers.pop("JOB-TOKEN", None)
    if self.job_token:
    self.headers.pop("Authorization", None)
    self.headers.pop("PRIVATE-TOKEN", None)
    self.headers["JOB-TOKEN"] = self.job_token
    if self.http_username:
    self._http_auth = requests.auth.HTTPBasicAuth(
    self.http_username, self.http_password
    )

    You can get away with combining oauth and PAT like as you suggested with the bearer header instead, but I don't think it'd work for job tokens, at least according to docs. But maybe it's just not documented, should be tested.

    There is now also a project access token. Which one of the 3 tokens needs to be set in that case?

    This and the upcoming group access tokens are basically just personal access tokens for project/group bot users.

    In the meantime, if you use this in CI, this workaround has worked well for me:

      before_script:
        - pip install --quiet python-gitlab
        - 'echo -e "[global]\ndefault=gitlab\n[gitlab]\nurl=$CI_SERVER_URL\nprivate_token=$API_TOKEN" > ~/.python-gitlab.cfg'
      script:
        - 'gitlab <args...>

    or if you're not in docker:

      before_script:
        - pip install --quiet python-gitlab
        - 'echo -e "[global]\ndefault=gitlab\n[gitlab]\nurl=$CI_SERVER_URL\nprivate_token=$API_TOKEN" > gitlab.cfg'
      script:
        - 'gitlab --config-file gitlab.cfg <args...>
  7. nejch commented on Dec 25, 2020

    @nejch
    Member

    @bittner it does seem like there's some undocumented support for the bearer header with CI job tokens, but I wouldn't rely on it being a stable API, see below. But at least downloading job artifacts with a job token + bearer auth worked. So --token could possibly be a convenience option with no guarantees..

    For more context on the matrix of different auth headers and allowed credentials they come with, you can scroll through this gitlab epic: https://gitlab.com/groups/gitlab-org/-/epics/3807 (specifically this note https://gitlab.com/groups/gitlab-org/-/epics/3807#note_374553902)

  8. locked as resolved and limited conversation to collaborators on Jan 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions