Skip to content
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

Add support for additional git configs #240

Closed
wants to merge 1 commit into from

Conversation

onesolpark
Copy link

Fixes #235
Add support for adding additional git configs through
args --git-configs=k1:v1,k2:v2 or through env variable GIT_CONFIGS

All key/values are added as global values to gitconfig

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Mar 24, 2020
@k8s-ci-robot
Copy link
Contributor

Welcome @onesolpark!

It looks like this is your first PR to kubernetes/git-sync 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes/git-sync has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Mar 24, 2020
@k8s-ci-robot k8s-ci-robot requested review from stp-ip and thockin March 24, 2020 05:44
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
To complete the pull request process, please assign stp-ip
You can assign the PR to them by writing /assign @stp-ip in a comment when ready.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@onesolpark
Copy link
Author

/assign @stp-ip

Copy link
Member

@thockin thockin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this. Some comments here.

Is there any e2e test we can craft that proves that this works?

@@ -109,5 +109,6 @@ docker run -d \
| GIT_SYNC_HTTP_BIND | `--http-bind` | the bind address (including port) for git-sync's HTTP endpoint | "" |
| GIT_SYNC_HTTP_METRICS | `--http-metrics` | enable metrics on git-sync's HTTP endpoint | true |
| GIT_SYNC_HTTP_PPROF | `--http-pprof` | enable the pprof debug endpoints on git-sync's HTTP endpoint | false |
| GIT_CONFIGS | `--git-configs` | add additional git config in 'key1:value1,key2:value2' format (ex - http.sslCAInfo:/path/to/cert/file,http.version:HTTP/2) | "" |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be GIT_SYNC_GIT_CONFIGS

@@ -114,6 +114,9 @@ var flHTTPMetrics = flag.Bool("http-metrics", envBool("GIT_SYNC_HTTP_METRICS", t
var flHTTPprof = flag.Bool("http-pprof", envBool("GIT_SYNC_HTTP_PPROF", false),
"enable the pprof debug endpoints on git-sync's HTTP endpoint")

var flGitConfigs = flag.String("git-configs", envString("GIT_CONFIGS", ""),
"add additional git configs in 'key1:value1,key2:value2' format (ex - http.sslCAInfo:/path/to/cert/file,http.version:HTTP/2)")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: change "ex - " to "e.g. "

@@ -283,6 +286,13 @@ func main() {
}
}

if *flGitConfigs != "" {
if err := setupGitConfigs(ctx); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please pass the flag in here so that only main() deals with flags (mostly)

func setupGitConfigs(ctx context.Context) error {
log.V(1).Info("adding git configs from flag")

for _, config := range strings.Split(*flGitConfigs, ",") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any git-config keys or values that embed commas? I know it's weird, but if a path had a comma in it, this would break.

This also applies to colons. Simple string split probably is not enough.

A more robust approach would be a function that walks through the input string, one rune at a time and produces a []struct{k, v string}, which you can then iterate over safely. As you walk the string, any time you find a literal backslash, replace it with whatever comes next.

"\a\b\c" => "abc"
"foo\n" => "foon"
"hello\there" => "hello\there"
"/weird/path/with/,comma" => "/weird/path/with/,comma"

Or are we SURE that we never have to deal with this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then we want a unit test :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you pointed out, I believe I can't be sure that there won't be any commas and semicolons in these variables.

I'll change the function to go over rune at a time and escape based on backslashes.

The thing that concerns me is that you can actually put backslashes in gitconfig with double backslash \\

And if users want to put double backslash it would mean that they have to put \\\\ with the function logic.

"\a\b\c" => "abc"
"foo\n" => "foon"
"hello\there" => "hellothere"
"/weird/path/with/\,comma" => "/weird/path/with/,comma"

"weird\\\\backslashid" => "weird\\backslashid"

Does this sound fine?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll definitely add a unit test to check this logic :)

Copy link
Member

@thockin thockin Mar 25, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This got long. I spent a lot of time playing with git-config, finding edge-case behaviors.
PLEASE double check me.

Looking at the git-config docs:

Section names are case-insensitive. Only alphanumeric characters,
- and . are allowed in section names.

and

The variable names are case-insensitive, allow only alphanumeric
characters and -, and must start with an alphabetic character.

So keys can not hold comma or colon. if you walk the string and parse the flag-string into a []string, delimited by "true" commas, you can take everything up to the first : as key (stripping leading/trailing whitespace) and everything after that (stripping leading/trailing whitespace) as value. No need to escape : at all.

Regarding whitespace on values:

Leading whitespaces after name =, ... and trailing whitespaces
of the line are discarded unless they are enclosed in double
quotes. Internal whitespaces within the value are retained
verbatim.

To break the flag-string, though, you need to be smart.

Let's consider a sort of pathological case. A key with a value that is a list, and another key with a simple value.

key1 = "k1"
value = "a:b, b:c, c:d"
key2 = "k2"
value = "v2"

Naive answer: k1:a:b, b:c, c:d, k2:v2

Obviously that doesn't work - it's ambiguous.

Option 1, escape commas: k1:a:b\, b:c\, c:d, k2:v2

Option 2 embed quotes: k1:"a:b, b:c, c:d", k2:v2

So I THINK we can say something like:

  • The value is a series of comma-delimited key-value pairs.
  • Each key must be a valid git-config compatible "[section].[variable]" (see git-config docs)
  • Each value must be a either a quoted string or an unquoted string
  • Within all values, the following escapes are supported:
    • \n => [newline]
    • \t => [tab]
    • \" => "
    • \, => ,
    • \\ => \
  • Within unquoted values, commas MUST be escaped. Within quoted values, commas MAY be escaped, but are not required to be.
  • Any other escape sequence is an error

When we build the string to pass to git config we DO NOT reformat it (e.g. no "%q"). Git is weird here, and (empirically) it seems correct to pass literal newlines, which git will internally escape.

If there's demand for more escapes we can consider expanding it later (e.g. git-config supports \b, but I don't see why we should).

Now, keep in mind that to get these INTO git-sync, we need to pass them through a shell or YAML or something, so they may get escaped again. That seems unavoidable. In truth, I hope the vast vast majority of people just use simple tokens, buy we should be precise and code defensively. :)

We'll need a good test to cover the various ways one could try to break this.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've gone through the documentation as well.
I'll go and work on the codes for this and a solid test code as well.
btw thanks for the thorough review :)

@@ -283,6 +286,13 @@ func main() {
}
}

if *flGitConfigs != "" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment here that this should be the very last config section, so it overrides all others.

@thockin
Copy link
Member

thockin commented Apr 24, 2020

Any updates on this?

@fejta-bot
Copy link

Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.

If this issue is safe to close now please do so with /close.

Send feedback to sig-testing, kubernetes/test-infra and/or fejta.
/lifecycle stale

@k8s-ci-robot k8s-ci-robot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Jul 23, 2020
@thockin
Copy link
Member

thockin commented Jul 24, 2020

ping?

@thockin thockin removed the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Jul 24, 2020
@ntdef
Copy link

ntdef commented Aug 11, 2020

I'd really like to see this feature added if possible. I've never written any Go, otherwise I'd give it a crack myself.

Is there much that needs to be done to finalize this PR? I might be open to try completing the work.

@thockin
Copy link
Member

thockin commented Aug 11, 2020

Go is easy. Getting all the edge-cases in parsing right is the only hard part here :)

@thockin thockin added the lifecycle/frozen Indicates that an issue or PR should not be auto-closed due to staleness. label Sep 4, 2020
@thockin thockin mentioned this pull request Mar 11, 2021
@thockin
Copy link
Member

thockin commented Mar 11, 2021

Hi, I have proposed #341 as a way to pass arbitrary args. That subsumes this abandoned PR. Thanks for the idea.

@thockin thockin closed this Mar 11, 2021
@thockin thockin mentioned this pull request Mar 11, 2021
@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Mar 11, 2021
@k8s-ci-robot
Copy link
Contributor

@onesolpark: PR needs rebase.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lifecycle/frozen Indicates that an issue or PR should not be auto-closed due to staleness. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support using custom ca(self-signed) for interacting with git
6 participants