2023-03-15 16:00:52 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2015-12-14 21:23:04 +00:00
|
|
|
package token
|
|
|
|
|
|
|
|
import (
|
2020-04-27 23:55:13 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2015-12-14 21:23:04 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TestCommand re-uses the existing Test function to ensure proper behavior of
|
|
|
|
// the internal token helper
|
|
|
|
func TestCommand(t *testing.T) {
|
2020-04-27 23:55:13 +00:00
|
|
|
helper, err := NewInternalTokenHelper()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
Test(t, helper)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInternalHelperFilePerms(t *testing.T) {
|
|
|
|
tmpDir, err := ioutil.TempDir("", t.Name())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
|
|
|
helper, err := NewInternalTokenHelper()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
helper.homeDir = tmpDir
|
|
|
|
|
|
|
|
tmpFile := filepath.Join(tmpDir, ".vault-token")
|
|
|
|
f, err := os.Create(tmpFile)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
fi, err := os.Stat(tmpFile)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-04-08 16:43:39 +00:00
|
|
|
if fi.Mode().Perm()&0o04 != 0o04 {
|
2020-04-27 23:55:13 +00:00
|
|
|
t.Fatalf("expected world-readable/writable permission bits, got: %o", fi.Mode().Perm())
|
|
|
|
}
|
|
|
|
|
|
|
|
err = helper.Store("bogus_token")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fi, err = os.Stat(tmpFile)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-04-08 16:43:39 +00:00
|
|
|
if fi.Mode().Perm()&0o04 != 0 {
|
2020-04-27 23:55:13 +00:00
|
|
|
t.Fatalf("expected no world-readable/writable permission bits, got: %o", fi.Mode().Perm())
|
|
|
|
}
|
2015-12-14 21:23:04 +00:00
|
|
|
}
|