Skip to content

Commit e1613c5

Browse files
committed
Support reading secrets from systemd credentials
Allow reading OauthClientId and OauthClientSecret from systemd LoadCredential directives. Add support for reading the secret key and the session key from the following files: $CREDENTIALS_DIRECTORY/OAUTH_CLIENT_ID $CREDENTIALS_DIRECTORY/OAUTH_CLIENT_SECRET
1 parent 2d3ea12 commit e1613c5

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
Coming soon! Please document any work in progress here as part of your PR. It will be moved to the next tag when released.
66

7+
- [Support reading secrets from systemd credentials](https://github.com/vouch/vouch-proxy/pull/487)
8+
79
## v0.37.0
810

911
- [allow configurable Write, Read and Idle timeouts for the http server](https://github.com/vouch/vouch-proxy/pull/468)

pkg/cfg/cfg.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,13 @@ func configureFromEnv() bool {
229229
if err != nil {
230230
log.Fatal(err.Error())
231231
}
232+
233+
// try to read secrets provided by systemd
234+
if credDir := os.Getenv("CREDENTIALS_DIRECTORY"); credDir != "" {
235+
readSystemdSecret(credDir, "OAUTH_CLIENT_ID", &GenOAuth.ClientID)
236+
readSystemdSecret(credDir, "OAUTH_CLIENT_SECRET", &GenOAuth.ClientSecret)
237+
}
238+
232239
// did anything change?
233240
if !reflect.DeepEqual(preEnvConfig, *Cfg) ||
234241
!reflect.DeepEqual(preEnvGenOAuth, *GenOAuth) {
@@ -244,6 +251,23 @@ func configureFromEnv() bool {
244251
return false
245252
}
246253

254+
// try to read a secret from systemd LoadCredential directive, if it hasn't been set yet
255+
func readSystemdSecret(credDir, name string, outVal *string) {
256+
if *outVal != "" {
257+
return
258+
}
259+
credPath := path.Join(credDir, name)
260+
val, err := os.ReadFile(credPath)
261+
if err != nil {
262+
if !os.IsNotExist(err) {
263+
log.Info(fmt.Errorf("read systemd secret %s: %w", credPath, err))
264+
}
265+
return
266+
}
267+
log.Infof("%s secret read from systemd credential", name)
268+
*outVal = string(val)
269+
}
270+
247271
// ValidateConfiguration confirm the Configuration is valid
248272
func ValidateConfiguration() error {
249273
if Cfg.Testing {

0 commit comments

Comments
 (0)