-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Creds-init writes to fixed location when HOME override is disabled
When the disable-home-env-overwrite flag is set to "true" each Step in a Task can conceivably have its own HOME directory. The concept of "HOME" is further muddied in systems that randomize the UID of containers. So now creds-init will write to a shared volumeMount, /tekton/creds, when the disable-home-env-overwrite flag is "true". When the flag is "false" creds-init will behave exactly the same as before, writing the credentials to /tekton/home, and no extra volume mount will be needed. This change should be mostly transparent to users: the entrypoint binary in each Step will now try and copy credentials out of /tekton/creds into $HOME/. The net result is the same as before the flag was introduced, it's just that entrypoint does the final copy into $HOME instead of creds-init. To support users who were in some way depending on the location of credentials, the path to where creds-init writes is now exposed for Tasks via the $(credentials.path) variable. This will be replaced with the directory that creds-init writes to: either "/tekton/home" or "/tekton/creds" depending on the state of the disable-home-env-overwrite flag.
- Loading branch information
1 parent
a65caec
commit 1325eea
Showing
15 changed files
with
509 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package credentials | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
) | ||
|
||
const credContents string = "hello, world!" | ||
|
||
func TestTryCopyCredDir(t *testing.T) { | ||
dir, cleanup := createTempDir(t) | ||
defer cleanup() | ||
|
||
fakeCredDir := filepath.Join(dir, ".docker") | ||
err := os.Mkdir(fakeCredDir, 0700) | ||
if err != nil { | ||
t.Fatalf("unexpected error creating fake credential directory: %v", err) | ||
} | ||
credFilename := "important-credential.json" | ||
writeFakeCred(t, fakeCredDir, credFilename, credContents) | ||
destination := filepath.Join(dir, ".docker-copy") | ||
|
||
copiedFile := filepath.Join(destination, credFilename) | ||
if err := tryCopyCred(fakeCredDir, destination); err != nil { | ||
t.Fatalf("error creating copy of credential directory: %v", err) | ||
} | ||
if _, err := os.Lstat(filepath.Join(destination, credFilename)); err != nil { | ||
t.Fatalf("error accessing copied credential: %v", err) | ||
} | ||
b, err := ioutil.ReadFile(copiedFile) | ||
if err != nil { | ||
t.Fatalf("unexpected error opening copied file: %v", err) | ||
} | ||
if string(b) != credContents { | ||
t.Fatalf("mismatching file contents, expected %q received %q", credContents, string(b)) | ||
} | ||
} | ||
|
||
func TestTryCopyCredFile(t *testing.T) { | ||
dir, cleanup := createTempDir(t) | ||
defer cleanup() | ||
fakeCredFile := writeFakeCred(t, dir, ".git-credentials", credContents) | ||
destination := filepath.Join(dir, ".git-credentials-copy") | ||
|
||
if err := tryCopyCred(fakeCredFile, destination); err != nil { | ||
t.Fatalf("error creating copy of credential file: %v", err) | ||
} | ||
if _, err := os.Lstat(destination); err != nil { | ||
t.Fatalf("error accessing copied credential: %v", err) | ||
} | ||
b, err := ioutil.ReadFile(destination) | ||
if err != nil { | ||
t.Fatalf("unexpected error opening copied file: %v", err) | ||
} | ||
if string(b) != credContents { | ||
t.Fatalf("mismatching file contents, expected %q received %q", credContents, string(b)) | ||
} | ||
} | ||
|
||
func TestTryCopyCredFileMissing(t *testing.T) { | ||
dir, cleanup := createTempDir(t) | ||
defer cleanup() | ||
fakeCredFile := filepath.Join(dir, "foo") | ||
destination := filepath.Join(dir, "foo-copy") | ||
|
||
if err := tryCopyCred(fakeCredFile, destination); err != nil { | ||
t.Fatalf("error creating copy of credential file: %v", err) | ||
} | ||
if _, err := os.Lstat(destination); err != nil && !os.IsNotExist(err) { | ||
t.Fatalf("error accessing copied credential: %v", err) | ||
} | ||
_, err := ioutil.ReadFile(destination) | ||
if !os.IsNotExist(err) { | ||
t.Fatalf("destination file exists but should not have been copied: %v", err) | ||
} | ||
} | ||
|
||
func writeFakeCred(t *testing.T, dir, name, contents string) string { | ||
flags := os.O_RDWR | os.O_CREATE | os.O_TRUNC | ||
path := filepath.Join(dir, name) | ||
cred, err := os.OpenFile(path, flags, 0600) | ||
if err != nil { | ||
t.Fatalf("unexpected error writing fake credential: %v", err) | ||
} | ||
_, _ = cred.Write([]byte(credContents)) | ||
_ = cred.Close() | ||
return path | ||
} | ||
|
||
func createTempDir(t *testing.T) (string, func()) { | ||
dir, err := ioutil.TempDir("", "cred-test-fs-") | ||
if err != nil { | ||
t.Fatalf("unexpected error creating temp directory: %v", err) | ||
} | ||
return dir, func() { | ||
if err := os.RemoveAll(dir); err != nil { | ||
t.Errorf("unexpected error cleaning up temp directory: %v", err) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.