CheckACLs check if the current profile has the right ACLs to execute the command
(cmd *cobra.Command, f *cmdutil.Factory)
| 68 | |
| 69 | // CheckACLs check if the current profile has the right ACLs to execute the command |
| 70 | func CheckACLs(cmd *cobra.Command, f *cmdutil.Factory) error { |
| 71 | if cmd.Annotations == nil { |
| 72 | return nil |
| 73 | } |
| 74 | |
| 75 | aclsAsString, ok := cmd.Annotations["acls"] |
| 76 | if !ok { |
| 77 | return nil |
| 78 | } |
| 79 | neededACLs := strings.Split(aclsAsString, ",") |
| 80 | |
| 81 | client, err := f.SearchClient() |
| 82 | if err != nil { |
| 83 | return err |
| 84 | } |
| 85 | _, err = client.ListApiKeys() |
| 86 | if err == nil { |
| 87 | return nil // Admin API Key, no need to check ACLs |
| 88 | } |
| 89 | |
| 90 | // Command requires an admin API Key |
| 91 | if utils.Contains(neededACLs, "admin") { |
| 92 | return errAdminAPIKeyRequired |
| 93 | } |
| 94 | |
| 95 | // Check the ACLs of the provided API Key |
| 96 | key, err := f.Config.Profile().GetAPIKey() |
| 97 | if err != nil { |
| 98 | return err |
| 99 | } |
| 100 | apiKey, err := client.GetApiKey(client.NewApiGetApiKeyRequest(key)) |
| 101 | if err != nil { |
| 102 | return err |
| 103 | } |
| 104 | |
| 105 | var hasAcls []string |
| 106 | for _, acl := range apiKey.Acl { |
| 107 | hasAcls = append(hasAcls, string(acl)) |
| 108 | } |
| 109 | |
| 110 | missingACLs := utils.Differences(neededACLs, hasAcls) |
| 111 | if len(missingACLs) > 0 { |
| 112 | return errMissingACLs(missingACLs) |
| 113 | } |
| 114 | |
| 115 | return nil |
| 116 | } |
| 117 | |
| 118 | func IsAuthCheckEnabled(cmd *cobra.Command) bool { |
| 119 | switch cmd.Name() { |