Skip to content

Commit 46f8efe

Browse files
authored
feat: added skip-defaults for konnect vaults (#354)
1 parent 8ff7380 commit 46f8efe

File tree

2 files changed

+93
-3
lines changed

2 files changed

+93
-3
lines changed

pkg/dump/schemas.go

Lines changed: 92 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ func NewSchemaFetcher(ctx context.Context, client *kong.Client, isKonnect bool)
4343
vaultSchemaCache: types.NewSchemaCache(func(ctx context.Context,
4444
vaultType string,
4545
) (map[string]interface{}, error) {
46-
// works only for gateway, not konnect
47-
return getVaultSchema(ctx, client, vaultType)
46+
return getVaultSchema(ctx, client, vaultType, isKonnect)
4847
}),
4948
}
5049
}
@@ -127,9 +126,13 @@ func getKonnectEntitySchema(ctx context.Context, client *kong.Client, entityType
127126
return schema, nil
128127
}
129128

130-
func getVaultSchema(ctx context.Context, client *kong.Client, vaultType string) (kong.Schema, error) {
129+
func getVaultSchema(ctx context.Context, client *kong.Client, vaultType string, isKonnect bool) (kong.Schema, error) {
131130
var schema map[string]interface{}
132131

132+
if isKonnect {
133+
return getKonnectVaultSchema(ctx, client, vaultType)
134+
}
135+
133136
endpoint := fmt.Sprintf("/schemas/vaults/%s", vaultType)
134137
req, err := client.NewRequest(http.MethodGet, endpoint, nil, nil)
135138
if err != nil {
@@ -145,3 +148,89 @@ func getVaultSchema(ctx context.Context, client *kong.Client, vaultType string)
145148

146149
return schema, nil
147150
}
151+
152+
func getKonnectVaultSchema(ctx context.Context, client *kong.Client, vaultType string) (kong.Schema, error) {
153+
var schema map[string]interface{}
154+
155+
fullSchema, err := getKonnectEntitySchema(ctx, client, "vaults")
156+
if err != nil {
157+
return schema, fmt.Errorf("failed to fetch schema: %w", err)
158+
}
159+
160+
// Start with the base schema from fullSchema
161+
schema = make(map[string]interface{})
162+
for key, value := range fullSchema {
163+
if key != "allOf" {
164+
schema[key] = value
165+
}
166+
}
167+
168+
// Extract the specific vault type schema from the full schema
169+
// The full schema contains conditional logic based on vault name
170+
// We need to find the matching condition for the given vaultType
171+
allOf, ok := fullSchema["allOf"].([]interface{})
172+
if !ok {
173+
return schema, fmt.Errorf("invalid schema format: allOf not found or not an array")
174+
}
175+
176+
const (
177+
ifKey = "if"
178+
thenKey = "then"
179+
propertiesKey = "properties"
180+
nameKey = "name"
181+
constKey = "const"
182+
configKey = "config"
183+
)
184+
185+
// Look for the matching vault type in the conditional schemas
186+
for _, condition := range allOf {
187+
conditionMap, ok := condition.(map[string]interface{})
188+
if !ok {
189+
continue
190+
}
191+
192+
// Check if this condition matches our vault type
193+
if ifClause, exists := conditionMap[ifKey]; exists {
194+
if ifMap, ok := ifClause.(map[string]interface{}); ok {
195+
if properties, exists := ifMap[propertiesKey]; exists {
196+
if propsMap, ok := properties.(map[string]interface{}); ok {
197+
if nameClause, exists := propsMap[nameKey]; exists {
198+
if nameMap, ok := nameClause.(map[string]interface{}); ok {
199+
if constValue, exists := nameMap[constKey]; exists {
200+
if constStr, ok := constValue.(string); ok && constStr == vaultType {
201+
// Found the matching condition, extract the config from "then" clause
202+
if thenClause, exists := conditionMap[thenKey]; exists {
203+
if thenMap, ok := thenClause.(map[string]interface{}); ok {
204+
if thenProps, exists := thenMap[propertiesKey]; exists {
205+
if thenPropsMap, ok := thenProps.(map[string]interface{}); ok {
206+
if configSchema, exists := thenPropsMap[configKey]; exists {
207+
if configSchemaMap, ok := configSchema.(map[string]interface{}); ok {
208+
if configProps, exists := configSchemaMap[propertiesKey]; exists {
209+
if configPropsMap, ok := configProps.(map[string]interface{}); ok {
210+
vaultConfigSchema := configPropsMap[vaultType]
211+
if schemaProps, exists := schema[propertiesKey]; exists {
212+
if schemaPropsMap, ok := schemaProps.(map[string]interface{}); ok {
213+
schemaPropsMap[configKey] = vaultConfigSchema
214+
}
215+
}
216+
return schema, nil
217+
}
218+
}
219+
}
220+
}
221+
}
222+
}
223+
}
224+
}
225+
}
226+
}
227+
}
228+
}
229+
}
230+
}
231+
}
232+
}
233+
}
234+
235+
return schema, nil
236+
}

pkg/utils/kongToKonnectEntities.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ var KongToKonnectEntitiesMap = map[string]string{
1717
"basicauth_credentials": "basic-auth",
1818
"mtls_auth_credentials": "mtls-auth",
1919
"snis": "sni",
20+
"vaults": "vault",
2021
}

0 commit comments

Comments
 (0)