1
0
mirror of https://github.com/oauth2-proxy/oauth2-proxy.git synced 2025-07-13 01:40:48 +02:00
Files
.github
contrib
docs
pkg
apis
ip
middleware
options
util
util.go
util_suite_test.go
util_test.go
alpha_options.go
app.go
common.go
common_test.go
cookie.go
doc.go
header.go
legacy_options.go
legacy_options_test.go
load.go
load_test.go
logging.go
login_url_parameters.go
options.go
options_suite_test.go
providers.go
server.go
sessions.go
upstreams.go
sessions
app
authentication
clock
cookies
encryption
header
http
ip
logger
middleware
providers
requests
sessions
upstream
util
validation
watcher
providers
static
testdata
tools
.dockerignore
.gitignore
.golangci.yml
CHANGELOG.md
CONTRIBUTING.md
Dockerfile
LICENSE
MAINTAINERS
Makefile
README.md
RELEASE.md
SECURITY.md
dist.sh
go.mod
go.sum
main.go
main_suite_test.go
main_test.go
nsswitch.conf
oauthproxy.go
oauthproxy_test.go
validator.go
validator_test.go
version.go
oauth2-proxy/pkg/apis/options/util/util_test.go

79 lines
2.2 KiB
Go
Raw Normal View History

2020-07-26 04:50:18 +01:00
package util
import (
"os"
"path"
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/options"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("GetSecretValue", func() {
var fileDir string
const secretEnvKey = "SECRET_ENV_KEY"
const secretEnvValue = "secret-env-value"
var secretFileValue = []byte("secret-file-value")
BeforeEach(func() {
os.Setenv(secretEnvKey, secretEnvValue)
var err error
fileDir, err = os.MkdirTemp("", "oauth2-proxy-util-get-secret-value")
2020-07-26 04:50:18 +01:00
Expect(err).ToNot(HaveOccurred())
Expect(os.WriteFile(path.Join(fileDir, "secret-file"), secretFileValue, 0600)).To(Succeed())
2020-07-26 04:50:18 +01:00
})
AfterEach(func() {
os.Unsetenv(secretEnvKey)
os.RemoveAll(fileDir)
})
It("returns the correct value from the string value", func() {
2020-07-26 04:50:18 +01:00
value, err := GetSecretValue(&options.SecretSource{
Value: []byte("secret-value-1"),
2020-07-26 04:50:18 +01:00
})
Expect(err).ToNot(HaveOccurred())
Expect(string(value)).To(Equal("secret-value-1"))
2020-07-26 04:50:18 +01:00
})
It("returns the correct value from the environment", func() {
value, err := GetSecretValue(&options.SecretSource{
FromEnv: secretEnvKey,
})
Expect(err).ToNot(HaveOccurred())
Expect(value).To(BeEquivalentTo(secretEnvValue))
})
It("returns the correct value from a file", func() {
value, err := GetSecretValue(&options.SecretSource{
FromFile: path.Join(fileDir, "secret-file"),
})
Expect(err).ToNot(HaveOccurred())
Expect(value).To(Equal(secretFileValue))
})
It("when the file does not exist", func() {
value, err := GetSecretValue(&options.SecretSource{
FromFile: path.Join(fileDir, "not-exist"),
})
Expect(err).To(HaveOccurred())
Expect(value).To(BeEmpty())
})
It("with no source set", func() {
value, err := GetSecretValue(&options.SecretSource{})
Expect(err).To(MatchError("secret source is invalid: exactly one entry required, specify either value, fromEnv or fromFile"))
Expect(value).To(BeEmpty())
})
It("with multiple sources set", func() {
value, err := GetSecretValue(&options.SecretSource{
FromEnv: secretEnvKey,
FromFile: path.Join(fileDir, "secret-file"),
})
Expect(err).To(MatchError("secret source is invalid: exactly one entry required, specify either value, fromEnv or fromFile"))
Expect(value).To(BeEmpty())
})
})