-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
github-events: fix secrets parsing (#671)
1. os.Environ returns a list of `key=value`, not a list of keys 2. Use append instead of overwriting secrets list each time.
- Loading branch information
Showing
3 changed files
with
60 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
Copyright 2024 Chainguard, Inc. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package secrets | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"strings" | ||
|
||
"github.com/chainguard-dev/clog" | ||
) | ||
|
||
func LoadFromEnv(ctx context.Context) [][]byte { | ||
var secrets [][]byte | ||
for _, e := range os.Environ() { | ||
k, v, ok := strings.Cut(e, "=") | ||
if !ok { | ||
continue | ||
} | ||
|
||
if strings.HasPrefix(k, "WEBHOOK_SECRET") { | ||
clog.InfoContextf(ctx, "loading secret: %q", k) | ||
secrets = append(secrets, []byte(v)) | ||
} | ||
} | ||
return secrets | ||
} |
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,28 @@ | ||
/* | ||
Copyright 2024 Chainguard, Inc. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package secrets | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/chainguard-dev/clog/slogtest" | ||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
func TestLoadFromEnv(t *testing.T) { | ||
// Set up the environment variables for testing. | ||
t.Setenv("WEBHOOK_SECRET", "foo") | ||
t.Setenv("WEBHOOK_SECRET_2", "bar") | ||
|
||
ctx := slogtest.Context(t) | ||
got := LoadFromEnv(ctx) | ||
|
||
want := [][]byte{[]byte("foo"), []byte("bar")} | ||
|
||
if diff := cmp.Diff(want, got); diff != "" { | ||
t.Errorf("LoadFromEnv() mismatch (-want +got):\n%s", diff) | ||
} | ||
} |