From 8322cbe3b608c6f3997c9fac6dc0af03fedfedb1 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Mon, 15 Jun 2020 11:16:50 -0300 Subject: [PATCH 1/4] feat: accept GIT_TOKEN Signed-off-by: Carlos Alexandro Becker --- README.md | 16 +++++++++------- pkg/buildcontext/git.go | 6 ++++++ pkg/buildcontext/git_test.go | 22 ++++++++++++++++++++++ 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 5e0b7bdb7..5238d6802 100644 --- a/README.md +++ b/README.md @@ -176,7 +176,9 @@ If you are using Azure Blob Storage for context file, you will need to pass [Azu You can use `Personal Access Tokens` for Build Contexts from Private Repositories from [GitHub](https://blog.github.com/2012-09-21-easier-builds-and-deployments-using-git-over-https-and-oauth/). You can either pass this in as part of the git URL (e.g., `git://TOKEN@github.com/acme/myproject.git#refs/heads/mybranch`) -or using the environment variable `GIT_USERNAME`. +or using the environment variable `GIT_TOKEN`. + +You can also pass `GIT_USERNAME` and `GIT_PASSWORD` (password being the token) if you want to be explicit about the username. ### Using Standard Input If running kaniko and using Standard Input build context, you will need to add the docker or kubernetes `-i, --interactive` flag. @@ -197,7 +199,7 @@ Complete example of how to interactively run kaniko with `.tar.gz` Standard Inpu echo -e 'FROM alpine \nRUN echo "created from standard input"' > Dockerfile | tar -cf - Dockerfile | gzip -9 | kubectl run kaniko \ --rm --stdin=true \ --image=gcr.io/kaniko-project/executor:latest --restart=Never \ ---overrides='{ +--overrides='{ "apiVersion": "v1", "spec": { "containers": [ @@ -211,12 +213,12 @@ echo -e 'FROM alpine \nRUN echo "created from standard input"' > Dockerfile | ta "--context=tar://stdin", "--destination=gcr.io/my-repo/my-image" ], "volumeMounts": [ - { + { "name": "cabundle", "mountPath": "/kaniko/ssl/certs/" }, - { - "name": "docker-config", + { + "name": "docker-config", "mountPath": "/kaniko/.docker/" }] }], @@ -225,9 +227,9 @@ echo -e 'FROM alpine \nRUN echo "created from standard input"' > Dockerfile | ta "name": "cabundle", "configMap": { "name": "cabundle"}}, - { + { "name": "docker-config", - "configMap": { + "configMap": { "name": "docker-config" }} ] } diff --git a/pkg/buildcontext/git.go b/pkg/buildcontext/git.go index 18d6e2ded..61d71342e 100644 --- a/pkg/buildcontext/git.go +++ b/pkg/buildcontext/git.go @@ -34,6 +34,7 @@ const ( gitAuthUsernameEnvKey = "GIT_USERNAME" gitAuthPasswordEnvKey = "GIT_PASSWORD" + gitAuthTokenEnvKey = "GIT_TOKEN" ) var ( @@ -64,6 +65,11 @@ func (g *Git) UnpackTarFromBuildContext() (string, error) { func getGitAuth() transport.AuthMethod { username := os.Getenv(gitAuthUsernameEnvKey) password := os.Getenv(gitAuthPasswordEnvKey) + token := os.Getenv(gitAuthTokenEnvKey) + if token != "" { + username = token + password = "" + } if username != "" || password != "" { return &http.BasicAuth{ Username: username, diff --git a/pkg/buildcontext/git_test.go b/pkg/buildcontext/git_test.go index aaa487c92..cc23eefd8 100644 --- a/pkg/buildcontext/git_test.go +++ b/pkg/buildcontext/git_test.go @@ -149,6 +149,28 @@ func TestGetGitAuth(t *testing.T) { return }, }, + { + testName: "withToken", + setEnv: func() (expectedValue transport.AuthMethod) { + token := "super-secret-password-1234" + _ = os.Setenv(gitAuthTokenEnvKey, token) + expectedValue = &http.BasicAuth{Username: token} + return + }, + }, + { + testName: "withTokenUsernamePassword", + setEnv: func() (expectedValue transport.AuthMethod) { + token := "super-secret-password-1234" + username := "foo" + pass := "super-secret-password-1234" + _ = os.Setenv(gitAuthUsernameEnvKey, username) + _ = os.Setenv(gitAuthPasswordEnvKey, pass) + _ = os.Setenv(gitAuthTokenEnvKey, token) + expectedValue = &http.BasicAuth{Username: token} + return + }, + }, } for _, tt := range tests { From 13e6bdf533256c18d7715bb24266ee7f064c4bef Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Mon, 15 Jun 2020 11:20:27 -0300 Subject: [PATCH 2/4] fix: improve tests Signed-off-by: Carlos Alexandro Becker --- pkg/buildcontext/git_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/buildcontext/git_test.go b/pkg/buildcontext/git_test.go index cc23eefd8..ff7a3a9c7 100644 --- a/pkg/buildcontext/git_test.go +++ b/pkg/buildcontext/git_test.go @@ -152,7 +152,7 @@ func TestGetGitAuth(t *testing.T) { { testName: "withToken", setEnv: func() (expectedValue transport.AuthMethod) { - token := "super-secret-password-1234" + token := "super-secret-token-1234" _ = os.Setenv(gitAuthTokenEnvKey, token) expectedValue = &http.BasicAuth{Username: token} return @@ -161,9 +161,9 @@ func TestGetGitAuth(t *testing.T) { { testName: "withTokenUsernamePassword", setEnv: func() (expectedValue transport.AuthMethod) { - token := "super-secret-password-1234" username := "foo" - pass := "super-secret-password-1234" + token := "super-secret-password-1234" + pass := "super-secret-token-1234" _ = os.Setenv(gitAuthUsernameEnvKey, username) _ = os.Setenv(gitAuthPasswordEnvKey, pass) _ = os.Setenv(gitAuthTokenEnvKey, token) From fdcc40683e1e432b049c3ad0729cb318b98d22e4 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Wed, 29 Jul 2020 23:26:08 -0300 Subject: [PATCH 3/4] fix: lint issues Signed-off-by: Carlos Alexandro Becker --- pkg/buildcontext/git_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/buildcontext/git_test.go b/pkg/buildcontext/git_test.go index bed3cb469..3b3a56558 100644 --- a/pkg/buildcontext/git_test.go +++ b/pkg/buildcontext/git_test.go @@ -152,7 +152,7 @@ func TestGetGitAuth(t *testing.T) { { testName: "withToken", setEnv: func() (expectedValue transport.AuthMethod) { - token := "super-secret-token-1234" + token := "some-other-token" _ = os.Setenv(gitAuthTokenEnvKey, token) expectedValue = &http.BasicAuth{Username: token} return @@ -162,8 +162,8 @@ func TestGetGitAuth(t *testing.T) { testName: "withTokenUsernamePassword", setEnv: func() (expectedValue transport.AuthMethod) { username := "foo" - token := "super-secret-password-1234" - pass := "super-secret-token-1234" + token := "some-token-45678" + pass := "some-password-12345" _ = os.Setenv(gitAuthUsernameEnvKey, username) _ = os.Setenv(gitAuthPasswordEnvKey, pass) _ = os.Setenv(gitAuthTokenEnvKey, token) From 008f8e890ca4ce8438ec86de40dd66651fd1d159 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Wed, 29 Jul 2020 23:32:40 -0300 Subject: [PATCH 4/4] fix: lint issues Signed-off-by: Carlos Alexandro Becker --- pkg/buildcontext/git_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/buildcontext/git_test.go b/pkg/buildcontext/git_test.go index 3b3a56558..0f86ca166 100644 --- a/pkg/buildcontext/git_test.go +++ b/pkg/buildcontext/git_test.go @@ -161,7 +161,7 @@ func TestGetGitAuth(t *testing.T) { { testName: "withTokenUsernamePassword", setEnv: func() (expectedValue transport.AuthMethod) { - username := "foo" + username := "foo-user" token := "some-token-45678" pass := "some-password-12345" _ = os.Setenv(gitAuthUsernameEnvKey, username)