Skip to content

Commit 4b5e053

Browse files
committed
fix: metadata extract and token
1 parent 9e753cd commit 4b5e053

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
lines changed

pkg/generator/config.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,13 @@ func (c *GenVarsConfig) KeySeparator() string {
7676
//
7777
// Further processing down the line will remove other elements of the token.
7878
func ParseMetadata[T comparable](token string, typ T) string {
79-
metadataStr, startIndex, found := extractMetadataStr(token)
79+
metadataStr, metaLessToken, found := extractMetadataStr(token)
8080
if !found {
8181
return token
8282
}
83-
if startIndex > 0 {
84-
token = token[0:startIndex]
85-
}
83+
84+
token = metaLessToken
85+
8686
// crude json like builder from key/val tags
8787
// since we are only ever dealing with a string input
8888
// extracted from the token there is little chance panic would occur here
@@ -94,6 +94,7 @@ func ParseMetadata[T comparable](token string, typ T) string {
9494
metaMap = append(metaMap, fmt.Sprintf(`"%s":"%s"`, mapKeyVal[0], mapKeyVal[1]))
9595
}
9696
}
97+
9798
// empty map will be parsed as `{}` still resulting in a valid json
9899
// and successful unmarshalling but default value pointer struct
99100
b := []byte(fmt.Sprintf(`{%s}`, strings.Join(metaMap, ",")))
@@ -111,18 +112,26 @@ const endMetaStr string = `]`
111112

112113
// extractMetadataStr returns anything between the start and end
113114
// metadata markers in the token string itself
114-
func extractMetadataStr(str string) (metaString string, startIndex int, found bool) {
115+
func extractMetadataStr(token string) (metaString string, tokenWithoutMeta string, found bool) {
115116

116-
startIndex = strings.Index(str, startMetaStr)
117+
startIndex := strings.Index(token, startMetaStr)
117118
// token has no startMetaStr
118119
if startIndex == -1 {
119-
return metaString, startIndex, false
120+
return metaString, token, false
120121
}
121-
newS := str[startIndex+len(startMetaStr):]
122+
newS := token[startIndex+len(startMetaStr):]
123+
122124
endIndex := strings.Index(newS, endMetaStr)
125+
// token has no meta end
123126
if endIndex == -1 {
124-
return metaString, -1, false
127+
return metaString, token, false
125128
}
129+
// metastring extracted
126130
metaString = newS[:endIndex]
127-
return metaString, startIndex, true
131+
132+
// complete [key=value] has been found
133+
// Remove from the token
134+
metaLessToken := strings.Replace(token, startMetaStr+metaString+endMetaStr, "", -1)
135+
136+
return metaString, metaLessToken, true
128137
}

pkg/generator/config_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ func Test_MarshalMetadata_with_label_struct_succeeds(t *testing.T) {
6060
"",
6161
"FOO://basjh/dskjuds/123]asdas=bar]",
6262
},
63+
"metadata is in the middle of path lookup": {
64+
generator.NewConfig(),
65+
`FOO://basjh/dskjuds/123[label=bar]|lookup`,
66+
"bar",
67+
"FOO://basjh/dskjuds/123|lookup",
68+
},
6369
}
6470
for name, tt := range ttests {
6571
t.Run(name, func(t *testing.T) {

pkg/generator/generator.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,12 @@ func (c *GenVars) RawMap() ParsedMap {
148148
func (c *GenVars) AddRawMap(key, val string) {
149149
c.rawMap.mu.Lock()
150150
defer c.rawMap.mu.Unlock()
151-
c.rawMap.tokenMap[key] = c.keySeparatorLookup(key, val)
151+
// strip the metadata from token
152+
strippedToken := ParseMetadata(key, &struct{}{})
153+
// still use the metadata in the key
154+
// there could be different versions / labels for the same token and hence different values
155+
// However the JSONpath look up
156+
c.rawMap.tokenMap[key] = c.keySeparatorLookup(strippedToken, val)
152157
}
153158

154159
// Generate generates a k/v map of the tokens with their corresponding secret/paramstore values

0 commit comments

Comments
 (0)