-
Notifications
You must be signed in to change notification settings - Fork 6
feat(resource-encodings): add resource encoding CRUD for CLI #56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4759fbd
feat(resource-encodings): add resource encoding CRUD for CLI
jrschumacher e557a6a
Merge branch 'main' of github.com:opentdf/tructl into jrschumacher/is…
jrschumacher 9e4ab55
Update cmds
jrschumacher e025ea7
Add CLI resource mappings CRUD commands
jrschumacher 002b585
feat(policy): add resource-mappings CLI CRUD
jrschumacher File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,176 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| _ "embed" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/opentdf/tructl/docs/man" | ||
| "github.com/opentdf/tructl/pkg/cli" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| var ( | ||
| policy_resourceMappingsTerms []string | ||
|
|
||
| policy_resourceMappingsCmd = &cobra.Command{ | ||
| Use: man.PolicyResourceMappings["en"].Command, | ||
| Aliases: man.PolicyResourceMappings["en"].Aliases, | ||
| Short: man.PolicyResourceMappings["en"].ShortWithSubCommands([]string{ | ||
| policy_resourceMappingsCreateCmd.Use, | ||
| policy_resourceMappingsGetCmd.Use, | ||
| policy_resourceMappingsListCmd.Use, | ||
| policy_resourceMappingsUpdateCmd.Use, | ||
| policy_resourceMappingsDeleteCmd.Use, | ||
| }), | ||
| Long: man.PolicyResourceMappings["en"].Long, | ||
| } | ||
|
|
||
| policy_resourceMappingsCreateCmd = &cobra.Command{ | ||
| Use: "create", | ||
| Short: "Create resource mappings", | ||
| Run: func(cmd *cobra.Command, args []string) { | ||
| h := cli.NewHandler(cmd) | ||
| defer h.Close() | ||
|
|
||
| flagHelper := cli.NewFlagHelper(cmd) | ||
| attrId := flagHelper.GetRequiredString("attribute-value-id") | ||
| terms := flagHelper.GetStringSlice("terms", policy_resourceMappingsTerms, cli.FlagHelperStringSliceOptions{ | ||
| Min: 1, | ||
| }) | ||
|
|
||
| resourceMapping, err := h.CreateResourceMapping(attrId, terms) | ||
| if err != nil { | ||
| cli.ExitWithError("Failed to create resource mapping", err) | ||
| } | ||
|
|
||
| fmt.Println(cli.SuccessMessage("Resource mapping created")) | ||
| fmt.Println(cli.NewTabular().Rows([][]string{ | ||
| {"Id", resourceMapping.Id}, | ||
| {"Attribute Id", resourceMapping.AttributeValue.AttributeId}, | ||
| {"Attribute Value", resourceMapping.AttributeValue.Value}, | ||
| {"Terms", strings.Join(resourceMapping.Terms, ", ")}, | ||
| }...).Render()) | ||
| }, | ||
| } | ||
|
|
||
| policy_resourceMappingsGetCmd = &cobra.Command{ | ||
| Use: "get", | ||
| Short: "Get resource mappings", | ||
| Run: func(cmd *cobra.Command, args []string) { | ||
| h := cli.NewHandler(cmd) | ||
| defer h.Close() | ||
|
|
||
| flagHelper := cli.NewFlagHelper(cmd) | ||
| id := flagHelper.GetRequiredString("id") | ||
|
|
||
| resourceMapping, err := h.GetResourceMapping(id) | ||
| if err != nil { | ||
| cli.ExitWithError("Failed to get resource mapping", err) | ||
| } | ||
|
|
||
| fmt.Println(cli.NewTabular().Rows([][]string{ | ||
| {"Id", resourceMapping.Id}, | ||
| {"Attribute Id", resourceMapping.AttributeValue.AttributeId}, | ||
| {"Attribute Value", resourceMapping.AttributeValue.Value}, | ||
| {"Terms", strings.Join(resourceMapping.Terms, ", ")}, | ||
| }...).Render()) | ||
| }, | ||
| } | ||
|
|
||
| policy_resourceMappingsListCmd = &cobra.Command{ | ||
| Use: "list", | ||
| Short: "List resource mappings", | ||
| Run: func(cmd *cobra.Command, args []string) { | ||
| h := cli.NewHandler(cmd) | ||
| defer h.Close() | ||
|
|
||
| r, err := h.ListResourceMappings() | ||
| if err != nil { | ||
| cli.ExitWithError("Failed to list resource mappings", err) | ||
| } | ||
|
|
||
| t := cli.NewTable() | ||
| t.Headers("Id", "Attribute Id", "Attribute Value", "Terms") | ||
| for _, resourceMapping := range r { | ||
| t.Row(resourceMapping.Id, resourceMapping.AttributeValue.AttributeId, resourceMapping.AttributeValue.Value, strings.Join(resourceMapping.Terms, ", ")) | ||
| } | ||
| fmt.Println(t.Render()) | ||
| }, | ||
| } | ||
|
|
||
| policy_resourceMappingsUpdateCmd = &cobra.Command{ | ||
| Use: "update", | ||
| Short: "Update resource mappings", | ||
| Run: func(cmd *cobra.Command, args []string) { | ||
| h := cli.NewHandler(cmd) | ||
| defer h.Close() | ||
|
|
||
| flagHelper := cli.NewFlagHelper(cmd) | ||
| id := flagHelper.GetRequiredString("id") | ||
| attrValueId := flagHelper.GetOptionalString("attribute-value-id") | ||
| terms := flagHelper.GetStringSlice("terms", policy_resourceMappingsTerms, cli.FlagHelperStringSliceOptions{}) | ||
|
|
||
| resourceMapping, err := h.UpdateResourceMapping(id, attrValueId, terms) | ||
| if err != nil { | ||
| cli.ExitWithError("Failed to update resource mapping", err) | ||
| } | ||
|
|
||
| fmt.Println(cli.SuccessMessage("Resource mapping updated")) | ||
| fmt.Println(cli.NewTabular().Rows([][]string{ | ||
| {"Id", resourceMapping.Id}, | ||
| {"Attribute Id", resourceMapping.AttributeValue.AttributeId}, | ||
| {"Attribute Value", resourceMapping.AttributeValue.Value}, | ||
| {"Terms", strings.Join(resourceMapping.Terms, ", ")}, | ||
| }...).Render()) | ||
| }, | ||
| } | ||
|
|
||
| policy_resourceMappingsDeleteCmd = &cobra.Command{ | ||
| Use: "delete", | ||
| Short: "Delete resource mappings", | ||
| Run: func(cmd *cobra.Command, args []string) { | ||
| h := cli.NewHandler(cmd) | ||
| defer h.Close() | ||
|
|
||
| flagHelper := cli.NewFlagHelper(cmd) | ||
| id := flagHelper.GetRequiredString("id") | ||
|
|
||
| cli.ConfirmDelete("resource-mapping", id) | ||
|
|
||
| resourceMapping, err := h.DeleteResourceMapping(id) | ||
| if err != nil { | ||
| cli.ExitWithError("Failed to delete resource mapping", err) | ||
| } | ||
|
|
||
| fmt.Println(cli.SuccessMessage("Resource mapping deleted")) | ||
| fmt.Println(cli.NewTabular().Rows([][]string{ | ||
| {"Id", resourceMapping.Id}, | ||
| {"Attribute Id", resourceMapping.AttributeValue.AttributeId}, | ||
| {"Attribute Value", resourceMapping.AttributeValue.Value}, | ||
| {"Terms", strings.Join(resourceMapping.Terms, ", ")}, | ||
| }...).Render()) | ||
| }, | ||
| } | ||
| ) | ||
|
|
||
| func init() { | ||
| policyCmd.AddCommand(policy_resourceMappingsCmd) | ||
|
|
||
| policy_resourceMappingsCmd.AddCommand(policy_resourceMappingsCreateCmd) | ||
| policy_resourceMappingsCreateCmd.Flags().String("attribute-value-id", "", "Attribute Value ID") | ||
| policy_resourceMappingsCreateCmd.Flags().StringSliceVar(&policy_resourceMappingsTerms, "terms", []string{}, "Synonym terms") | ||
|
|
||
| policy_resourceMappingsCmd.AddCommand(policy_resourceMappingsGetCmd) | ||
| policy_resourceMappingsGetCmd.Flags().String("id", "", "Resource Mapping ID") | ||
|
|
||
| policy_resourceMappingsCmd.AddCommand(policy_resourceMappingsListCmd) | ||
|
|
||
| policy_resourceMappingsCmd.AddCommand(policy_resourceMappingsUpdateCmd) | ||
| policy_resourceMappingsUpdateCmd.Flags().String("id", "", "Resource Mapping ID") | ||
| policy_resourceMappingsUpdateCmd.Flags().String("attribute-value-id", "", "Attribute Value ID") | ||
| policy_resourceMappingsUpdateCmd.Flags().StringSliceVar(&policy_resourceMappingsTerms, "terms", []string{}, "Synonym terms") | ||
|
|
||
| policy_resourceMappingsCmd.AddCommand(policy_resourceMappingsDeleteCmd) | ||
| policy_resourceMappingsDeleteCmd.Flags().String("id", "", "Resource Mapping ID") | ||
| } |
This file contains hidden or 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,47 @@ | ||
| package man | ||
|
|
||
| import ( | ||
| _ "embed" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/adrg/frontmatter" | ||
| ) | ||
|
|
||
| //go:embed policy-resourceMappings.md | ||
| var policyResourceMappingsEn string | ||
|
|
||
| //go:embed policy-resourceMappings.fr.md | ||
| var policyResourceMappingsFr string | ||
|
|
||
| var PolicyResourceMappings = map[string]Doc{} | ||
|
|
||
| type Doc struct { | ||
| Command string `yaml:"command"` | ||
| Aliases []string `yaml:"aliases"` | ||
| Short string `yaml:"short"` | ||
| Long string | ||
| } | ||
|
|
||
| func (m Doc) ShortWithSubCommands(subcommands []string) string { | ||
| return m.Short + " [" + strings.Join(subcommands, ", ") + "]" | ||
| } | ||
|
|
||
| func init() { | ||
| PolicyResourceMappings["en"] = ProcessDoc(policyResourceMappingsEn) | ||
| PolicyResourceMappings["fr"] = ProcessDoc(policyResourceMappingsFr) | ||
| } | ||
|
|
||
| func ProcessDoc(doc string) Doc { | ||
| if len(doc) <= 0 { | ||
| return Doc{} | ||
| } | ||
| var matter Doc | ||
| rest, err := frontmatter.Parse(strings.NewReader(doc), &matter) | ||
| if err != nil { | ||
| fmt.Print(err) | ||
| return Doc{} | ||
| } | ||
| matter.Long = string(rest) | ||
| return matter | ||
| } |
This file contains hidden or 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,11 @@ | ||
| --- | ||
| command: "resource-mappings" | ||
| aliases: ["resource-mapping", "resource-encodings", "resource-encoding"] | ||
| short: "Gérer les mappages de ressources" | ||
| --- | ||
|
|
||
| # Gérer les mappages de ressources | ||
|
|
||
| Les mappages de ressources sont utilisés pour mapper les ressources à leurs valeurs d'attribut respectives en fonction des termes qui sont liés aux données. Seul, ce service n'est pas très utile, mais lorsqu'il est combiné avec un PEP ou un PDP qui peut utiliser les mappages de ressources, il devient un outil puissant pour automatiser le contrôle d'accès. | ||
|
|
||
| Par exemple, le PDP de marquage utilise des mappages de ressources pour mapper les ressources en fonction des termes trouvés dans les métadonnées et les documents qui lui sont envoyés. Combiné avec les mappages de ressources, il peut alors déterminer quels attributs doivent être appliqués au TDF et renvoyer ces attributs au PEP. |
This file contains hidden or 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,15 @@ | ||
| --- | ||
| command: "resource-mappings" | ||
| aliases: ["resource-mapping", "resource-encodings", "resource-encoding"] | ||
| short: "Manage resource mappings" | ||
| --- | ||
|
|
||
| # Manage Resource Mappings | ||
|
|
||
| Resource mappings are used to map resources to their respective attribute values based on the terms | ||
| that are related to the data. Alone, this service is not very useful, but when combined with a PEP | ||
| or PDP that can use the resource mappings it becomes a powerful tool for automating access control. | ||
|
|
||
| As an example, Tagging PDP uses resource mappings to map resources based on the terms found within | ||
| the metadata and documents which are sent to it. Combined with the resource mappings it can then | ||
| determine which attributes should be applied to the TDF and return those attributes to the PEP. |
This file contains hidden or 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 hidden or 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 hidden or 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,74 @@ | ||
| package handlers | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/opentdf/opentdf-v2-poc/sdk/resourcemapping" | ||
| ) | ||
|
|
||
| type ResourceMapping struct { | ||
| Id string | ||
| AttributeId string | ||
| Terms []string | ||
| } | ||
|
|
||
| func (h *Handler) CreateResourceMapping(attributeId string, terms []string) (*resourcemapping.ResourceMapping, error) { | ||
| res, err := h.sdk.ResourceMapping.CreateResourceMapping(context.Background(), &resourcemapping.CreateResourceMappingRequest{ | ||
| ResourceMapping: &resourcemapping.ResourceMappingCreateUpdate{ | ||
| AttributeValueId: attributeId, | ||
| Terms: terms, | ||
| }, | ||
| }) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return res.ResourceMapping, nil | ||
| } | ||
|
|
||
| func (h *Handler) GetResourceMapping(id string) (*resourcemapping.ResourceMapping, error) { | ||
| res, err := h.sdk.ResourceMapping.GetResourceMapping(context.Background(), &resourcemapping.GetResourceMappingRequest{ | ||
| Id: id, | ||
| }) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return res.ResourceMapping, nil | ||
| } | ||
|
|
||
| func (h *Handler) ListResourceMappings() ([]*resourcemapping.ResourceMapping, error) { | ||
| res, err := h.sdk.ResourceMapping.ListResourceMappings(context.Background(), &resourcemapping.ListResourceMappingsRequest{}) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return res.ResourceMappings, nil | ||
| } | ||
|
|
||
| func (h *Handler) UpdateResourceMapping(id string, attrValueId string, terms []string) (*resourcemapping.ResourceMapping, error) { | ||
| res, err := h.sdk.ResourceMapping.UpdateResourceMapping(context.Background(), &resourcemapping.UpdateResourceMappingRequest{ | ||
| Id: id, | ||
| ResourceMapping: &resourcemapping.ResourceMappingCreateUpdate{ | ||
| AttributeValueId: attrValueId, | ||
| Terms: terms, | ||
| }, | ||
| }) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return res.ResourceMapping, nil | ||
|
|
||
| } | ||
|
|
||
| func (h *Handler) DeleteResourceMapping(id string) (*resourcemapping.ResourceMapping, error) { | ||
| resp, err := h.sdk.ResourceMapping.DeleteResourceMapping(context.Background(), &resourcemapping.DeleteResourceMappingRequest{ | ||
| Id: id, | ||
| }) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return resp.ResourceMapping, nil | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.