-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from 0xsequence/fix_walking_slice
Support walking slices, recursive types & prepare for fetching secrets in batches
- Loading branch information
Showing
4 changed files
with
183 additions
and
64 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package cloudsecrets | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"reflect" | ||
"strings" | ||
) | ||
|
||
type secretField struct { | ||
value reflect.Value | ||
fieldPath string | ||
secretName string | ||
} | ||
|
||
type collector struct { | ||
fields []*secretField | ||
err error | ||
} | ||
|
||
// Walks given reflect value recursively and collects any string fields with $SECRET: prefix. | ||
func (g *collector) collectSecretFields(v reflect.Value, path string) { | ||
switch v.Kind() { | ||
case reflect.Ptr: | ||
if v.IsNil() { | ||
return | ||
} | ||
|
||
// Dereference pointer | ||
g.collectSecretFields(v.Elem(), path) | ||
|
||
case reflect.Struct: | ||
for i := 0; i < v.NumField(); i++ { | ||
field := v.Field(i) | ||
g.collectSecretFields(field, fmt.Sprintf("%v.%v", path, v.Type().Field(i).Name)) | ||
} | ||
|
||
case reflect.Slice, reflect.Array: | ||
for i := 0; i < v.Len(); i++ { | ||
item := v.Index(i) | ||
g.collectSecretFields(item, fmt.Sprintf("%v[%v]", path, i)) | ||
} | ||
|
||
case reflect.String: | ||
secretName, found := strings.CutPrefix(v.String(), "$SECRET:") | ||
if !found { | ||
return | ||
} | ||
|
||
if !v.CanSet() { | ||
g.err = errors.Join(g.err, fmt.Errorf("can't set field %v", path)) | ||
return | ||
} | ||
|
||
g.fields = append(g.fields, &secretField{ | ||
value: v, | ||
fieldPath: path, | ||
secretName: secretName, | ||
}) | ||
|
||
default: | ||
return | ||
} | ||
} |
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,87 @@ | ||
package cloudsecrets | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
type dbConfig struct { | ||
User string | ||
Password string | ||
} | ||
|
||
type jwtSecret string | ||
|
||
type config1 struct { | ||
DB dbConfig | ||
JWTSecrets []jwtSecret | ||
unexported dbConfig | ||
} | ||
|
||
func TestCollectFields(t *testing.T) { | ||
tt := []struct { | ||
Input any | ||
Out []string // field paths | ||
Error bool | ||
}{ | ||
{ | ||
Input: &config1{ | ||
DB: dbConfig{ | ||
User: "db-user", | ||
Password: "db-password", | ||
}, | ||
}, | ||
Out: []string{}, | ||
}, | ||
{ | ||
Input: &config1{ | ||
DB: dbConfig{ | ||
User: "db-user", | ||
Password: "$SECRET:secretName", | ||
}, | ||
JWTSecrets: []jwtSecret{"$SECRET:jwtSecret1", "$SECRET:jwtSecret2", "nope"}, | ||
}, | ||
Out: []string{"secretName", "jwtSecret1", "jwtSecret2"}, | ||
}, | ||
{ | ||
Input: &config1{ | ||
unexported: dbConfig{ // unexported fields can't be updated via reflect pkg | ||
User: "db-user", | ||
Password: "$SECRET:secretName", // match inside unexported field | ||
}, | ||
}, | ||
Out: []string{}, | ||
Error: true, // expect error | ||
}, | ||
} | ||
|
||
for i, tc := range tt { | ||
i, tc := i, tc | ||
t.Run(fmt.Sprintf("tt[%v]", i), func(t *testing.T) { | ||
v := reflect.ValueOf(tc.Input) | ||
|
||
c := &collector{} | ||
c.collectSecretFields(v, fmt.Sprintf("tt[%v].input", i)) | ||
|
||
if tc.Error { | ||
if c.err == nil { | ||
t.Error("expected error, got nil") | ||
} | ||
} else { | ||
if c.err != nil { | ||
t.Errorf("unexpected error: %v", c.err) | ||
} | ||
} | ||
|
||
if len(c.fields) != len(tc.Out) { | ||
t.Errorf("expected %v secrets, got %v", len(tc.Out), len(c.fields)) | ||
} | ||
for i := 0; i < len(c.fields); i++ { | ||
if c.fields[i].secretName != tc.Out[i] { | ||
t.Errorf("collected field[%v].secretName=%v doesn't match tc.Out[%v]=%v", i, c.fields[i].secretName, i, tc.Out[i]) | ||
} | ||
} | ||
}) | ||
} | ||
} |
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