Skip to content

Commit 8aa6e40

Browse files
committed
Filter by regex
1 parent f1b7cdb commit 8aa6e40

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
lines changed

gcp.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"fmt"
66
"path"
7-
"strings"
87

98
secretmanager "cloud.google.com/go/secretmanager/apiv1"
109
"google.golang.org/api/iterator"
@@ -13,6 +12,7 @@ import (
1312

1413
// GCPSecretManager represents the Google Cloud Platform Secret Manager
1514
type GCPSecretManager struct {
15+
GenericProvider
1616
client *secretmanager.Client
1717
}
1818

@@ -55,7 +55,8 @@ func (s *GCPSecretManager) ListSecrets(project string, prefix string) ([]*Secret
5555
}
5656

5757
name := path.Base(resp.GetName())
58-
if strings.HasPrefix(name, prefix) {
58+
59+
if s.Filter(name, prefix) {
5960
content, err := s.client.AccessSecretVersion(context.Background(), &secretmanagerpb.AccessSecretVersionRequest{Name: resp.GetName() + "/versions/latest"})
6061
if err != nil {
6162
return nil, err

provider.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
package main
22

3+
import "regexp"
4+
35
// SecretProvider defines the behaviors for a secret provider
46
type SecretProvider interface {
57
Init() error
8+
Filter(string, string) bool
69
ListSecrets(string, string) ([]*SecretData, error)
710
}
11+
12+
type GenericProvider struct{}
13+
14+
// Filter the secrets by regex
15+
func (s *GenericProvider) Filter(name string, exp string) bool {
16+
re := regexp.MustCompile(exp)
17+
return re.MatchString(name)
18+
}

provider_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestFilter(t *testing.T) {
10+
11+
expected := []struct {
12+
value string
13+
exp string
14+
r bool
15+
}{
16+
{
17+
"myapp_password",
18+
`^myapp*`,
19+
true,
20+
},
21+
{
22+
"xxx_password",
23+
`^myapp*|^xxx*`,
24+
true,
25+
},
26+
{
27+
"app_password",
28+
`^myapp*|^xxx*`,
29+
false,
30+
},
31+
}
32+
33+
gcp := &GCPSecretManager{}
34+
35+
for _, e := range expected {
36+
r := gcp.Filter(e.value, e.exp)
37+
assert.Equal(t, e.r, r)
38+
}
39+
}

runner_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
)
99

1010
type MockProvider struct {
11+
GenericProvider
1112
}
1213

1314
func (m *MockProvider) Init() error {

0 commit comments

Comments
 (0)