-
Notifications
You must be signed in to change notification settings - Fork 25
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 #204 from jetstack/fieldfilter
Implement more generic means of filtering uploaded objects
- Loading branch information
Showing
6 changed files
with
283 additions
and
53 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
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
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,102 @@ | ||
package k8s | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/Jeffail/gabs/v2" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
) | ||
|
||
// SecretSelectedFields is the list of fields sent from Secret objects to the | ||
// backend | ||
var SecretSelectedFields = []string{ | ||
"kind", | ||
"apiVersion", | ||
"metadata.name", | ||
"metadata.namespace", | ||
"type", | ||
"/data/tls.crt", | ||
"/data/ca.crt", | ||
} | ||
|
||
// Select removes all but the supplied fields from the resource | ||
func Select(fields []string, resource *unstructured.Unstructured) error { | ||
// convert the object to JSON for field filtering | ||
asJSON, err := json.Marshal(resource) | ||
if err != nil { | ||
return fmt.Errorf("failed to marshal json for resource: %s", err) | ||
} | ||
|
||
// parse the JSON for processing in gabs | ||
jsonParsed, err := gabs.ParseJSON(asJSON) | ||
if err != nil { | ||
return fmt.Errorf("failed to parse generated json for resource: %s", err) | ||
} | ||
|
||
// craft a new object containing only selected fields | ||
filteredObject := gabs.New() | ||
for _, v := range fields { | ||
// also support JSONPointers for keys containing '.' chars | ||
if strings.HasPrefix(v, "/") { | ||
gObject, err := jsonParsed.JSONPointer(v) | ||
if err != nil { | ||
// fail to select field if missing, just continue | ||
continue | ||
} | ||
pathComponents := strings.Split(v, "/") | ||
filteredObject.Set(gObject.Data(), pathComponents[1:]...) | ||
} else { | ||
if jsonParsed.ExistsP(v) { | ||
filteredObject.SetP(jsonParsed.Path(v).Data(), v) | ||
} | ||
} | ||
} | ||
|
||
// load the filtered JSON back into the resource | ||
err = json.Unmarshal(filteredObject.Bytes(), resource) | ||
if err != nil { | ||
return fmt.Errorf("failed to update resource: %s", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Redact removes the supplied fields from the resource | ||
func Redact(fields []string, resource *unstructured.Unstructured) error { | ||
// convert the object to JSON for field filtering | ||
asJSON, err := json.Marshal(resource) | ||
if err != nil { | ||
return fmt.Errorf("failed to marshal json for resource: %s", err) | ||
} | ||
|
||
// parse the JSON for processing in gabs | ||
jsonParsed, err := gabs.ParseJSON(asJSON) | ||
if err != nil { | ||
return fmt.Errorf("failed to parse generated json for resource: %s", err) | ||
} | ||
|
||
// craft a new object excluding redacted fields | ||
for _, v := range fields { | ||
// also support JSONPointers for keys containing '.' chars | ||
if strings.HasPrefix(v, "/") { | ||
pathComponents := strings.Split(v, "/")[1:] | ||
if jsonParsed.Exists(pathComponents...) { | ||
jsonParsed.Delete(pathComponents...) | ||
} | ||
} else { | ||
if jsonParsed.ExistsP(v) { | ||
jsonParsed.DeleteP(v) | ||
} | ||
} | ||
} | ||
|
||
// load the filtered JSON back into the resource | ||
err = json.Unmarshal(jsonParsed.Bytes(), resource) | ||
if err != nil { | ||
return fmt.Errorf("failed to update resource: %s", err) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.