Skip to content

cli command line arguments for url and token #960

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
rrauenza opened this issue Dec 7, 2019 · 7 comments · Fixed by #1296
Closed

cli command line arguments for url and token #960

rrauenza opened this issue Dec 7, 2019 · 7 comments · Fixed by #1296

Comments

@rrauenza
Copy link

rrauenza commented Dec 7, 2019

(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?)

@max-wittig
Copy link
Member

Good idea.

@chrisoldwood
Copy link

Is this a duplicate of #893?

@nejch
Copy link
Member

nejch commented Feb 25, 2020

@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.

@rrauenza
Copy link
Author

rrauenza commented Jul 9, 2020

Yeah, that sounds reasonable.

@bittner
Copy link
Contributor

bittner commented Dec 17, 2020

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).

@nejch
Copy link
Member

nejch commented Dec 17, 2020

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...>

@nejch
Copy link
Member

nejch commented Dec 25, 2020

@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)

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 9, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants