mirror of
https://github.com/GoogleContainerTools/kaniko
synced 2026-10-04 01:54:47 +02:00
Currently the only way to set the git auth is by including it in the context git URL. This can be problematic for certain environments such as ECS where the command args for are stored in logs. Introduces updates to allow setting the authentication info using the environment variables `GIT_USERNAME` and `GIT_PASSWORD`.
170 lines
4.0 KiB
Go
170 lines
4.0 KiB
Go
/*
|
|
Copyright 2020 Google LLC
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package buildcontext
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/GoogleContainerTools/kaniko/testutil"
|
|
"gopkg.in/src-d/go-git.v4/plumbing/transport"
|
|
"gopkg.in/src-d/go-git.v4/plumbing/transport/http"
|
|
)
|
|
|
|
func TestGetGitPullMethod(t *testing.T) {
|
|
tests := []struct {
|
|
testName string
|
|
setEnv func() (expectedValue string)
|
|
}{
|
|
{
|
|
testName: "noEnv",
|
|
setEnv: func() (expectedValue string) {
|
|
expectedValue = gitPullMethodHTTPS
|
|
return
|
|
},
|
|
},
|
|
{
|
|
testName: "emptyEnv",
|
|
setEnv: func() (expectedValue string) {
|
|
_ = os.Setenv(gitPullMethodEnvKey, "")
|
|
expectedValue = gitPullMethodHTTPS
|
|
return
|
|
},
|
|
},
|
|
{
|
|
testName: "httpEnv",
|
|
setEnv: func() (expectedValue string) {
|
|
err := os.Setenv(gitPullMethodEnvKey, gitPullMethodHTTP)
|
|
if nil != err {
|
|
expectedValue = gitPullMethodHTTPS
|
|
} else {
|
|
expectedValue = gitPullMethodHTTP
|
|
}
|
|
return
|
|
},
|
|
},
|
|
{
|
|
testName: "httpsEnv",
|
|
setEnv: func() (expectedValue string) {
|
|
_ = os.Setenv(gitPullMethodEnvKey, gitPullMethodHTTPS)
|
|
expectedValue = gitPullMethodHTTPS
|
|
return
|
|
},
|
|
},
|
|
{
|
|
testName: "unknownEnv",
|
|
setEnv: func() (expectedValue string) {
|
|
_ = os.Setenv(gitPullMethodEnvKey, "unknown")
|
|
expectedValue = gitPullMethodHTTPS
|
|
return
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.testName, func(t *testing.T) {
|
|
expectedValue := tt.setEnv()
|
|
testutil.CheckDeepEqual(t, expectedValue, getGitPullMethod())
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestGetGitAuth(t *testing.T) {
|
|
tests := []struct {
|
|
testName string
|
|
setEnv func() (expectedValue transport.AuthMethod)
|
|
}{
|
|
{
|
|
testName: "noEnv",
|
|
setEnv: func() (expectedValue transport.AuthMethod) {
|
|
expectedValue = nil
|
|
return
|
|
},
|
|
},
|
|
{
|
|
testName: "emptyUsernameEnv",
|
|
setEnv: func() (expectedValue transport.AuthMethod) {
|
|
_ = os.Setenv(gitAuthUsernameEnvKey, "")
|
|
expectedValue = nil
|
|
return
|
|
},
|
|
},
|
|
{
|
|
testName: "emptyPasswordEnv",
|
|
setEnv: func() (expectedValue transport.AuthMethod) {
|
|
_ = os.Setenv(gitAuthPasswordEnvKey, "")
|
|
expectedValue = nil
|
|
return
|
|
},
|
|
},
|
|
{
|
|
testName: "emptyEnv",
|
|
setEnv: func() (expectedValue transport.AuthMethod) {
|
|
_ = os.Setenv(gitAuthUsernameEnvKey, "")
|
|
_ = os.Setenv(gitAuthPasswordEnvKey, "")
|
|
expectedValue = nil
|
|
return
|
|
},
|
|
},
|
|
{
|
|
testName: "withUsername",
|
|
setEnv: func() (expectedValue transport.AuthMethod) {
|
|
username := "foo"
|
|
_ = os.Setenv(gitAuthUsernameEnvKey, username)
|
|
expectedValue = &http.BasicAuth{Username: username}
|
|
return
|
|
},
|
|
},
|
|
{
|
|
testName: "withPassword",
|
|
setEnv: func() (expectedValue transport.AuthMethod) {
|
|
pass := "super-secret-password-1234"
|
|
_ = os.Setenv(gitAuthPasswordEnvKey, pass)
|
|
expectedValue = &http.BasicAuth{Password: pass}
|
|
return
|
|
},
|
|
},
|
|
{
|
|
testName: "withUsernamePassword",
|
|
setEnv: func() (expectedValue transport.AuthMethod) {
|
|
username := "foo"
|
|
pass := "super-secret-password-1234"
|
|
_ = os.Setenv(gitAuthUsernameEnvKey, username)
|
|
_ = os.Setenv(gitAuthPasswordEnvKey, pass)
|
|
expectedValue = &http.BasicAuth{Username: username, Password: pass}
|
|
return
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.testName, func(t *testing.T) {
|
|
// Make sure to unset environment vars to get a clean test each time
|
|
defer clearTestAuthEnv()
|
|
|
|
expectedValue := tt.setEnv()
|
|
testutil.CheckDeepEqual(t, expectedValue, getGitAuth())
|
|
})
|
|
}
|
|
|
|
}
|
|
|
|
func clearTestAuthEnv() {
|
|
_ = os.Unsetenv(gitAuthUsernameEnvKey)
|
|
_ = os.Unsetenv(gitAuthPasswordEnvKey)
|
|
}
|