github.com/mattermosttest/mattermost-server/v5@v5.0.0-20200917143240-9dfa12e121f9/cmd/mattermost/commands/plugin.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package commands 5 6 import ( 7 "net/http" 8 "os" 9 "path/filepath" 10 11 "github.com/mattermost/mattermost-server/v5/audit" 12 "github.com/mattermost/mattermost-server/v5/model" 13 "github.com/pkg/errors" 14 "github.com/spf13/cobra" 15 ) 16 17 var PluginCmd = &cobra.Command{ 18 Use: "plugin", 19 Short: "Management of plugins", 20 } 21 22 var PluginAddCmd = &cobra.Command{ 23 Use: "add [plugins]", 24 Short: "Add plugins", 25 Long: "Add plugins to your Mattermost server.", 26 Example: ` plugin add hovercardexample.tar.gz pluginexample.tar.gz`, 27 RunE: pluginAddCmdF, 28 } 29 30 var PluginDeleteCmd = &cobra.Command{ 31 Use: "delete [plugins]", 32 Short: "Delete plugins", 33 Long: "Delete previously uploaded plugins from your Mattermost server.", 34 Example: ` plugin delete hovercardexample pluginexample`, 35 RunE: pluginDeleteCmdF, 36 } 37 38 var PluginEnableCmd = &cobra.Command{ 39 Use: "enable [plugins]", 40 Short: "Enable plugins", 41 Long: "Enable plugins for use on your Mattermost server.", 42 Example: ` plugin enable hovercardexample pluginexample`, 43 RunE: pluginEnableCmdF, 44 } 45 46 var PluginDisableCmd = &cobra.Command{ 47 Use: "disable [plugins]", 48 Short: "Disable plugins", 49 Long: "Disable plugins. Disabled plugins are immediately removed from the user interface and logged out of all sessions.", 50 Example: ` plugin disable hovercardexample pluginexample`, 51 RunE: pluginDisableCmdF, 52 } 53 54 var PluginListCmd = &cobra.Command{ 55 Use: "list", 56 Short: "List plugins", 57 Long: "List all enabled and disabled plugins installed on your Mattermost server.", 58 Example: ` plugin list`, 59 RunE: pluginListCmdF, 60 } 61 62 var PluginPublicKeysCmd = &cobra.Command{ 63 Use: "keys", 64 Short: "List public keys", 65 Long: "List names of all public keys installed on your Mattermost server.", 66 Example: ` plugin keys 67 plugin keys --verbose`, 68 RunE: pluginPublicKeysCmdF, 69 } 70 71 var PluginAddPublicKeyCmd = &cobra.Command{ 72 Use: "add [keys]", 73 Short: "Adds public key(s)", 74 Long: "Adds public key(s) for plugins on your Mattermost server.", 75 Example: ` plugin keys add my-pk-file1 my-pk-file2`, 76 RunE: pluginAddPublicKeyCmdF, 77 } 78 79 var PluginDeletePublicKeyCmd = &cobra.Command{ 80 Use: "delete [keys]", 81 Short: "Deletes public key(s)", 82 Long: "Deletes public key(s) for plugins on your Mattermost server.", 83 Example: ` plugin keys delete my-pk-file1 my-pk-file2`, 84 RunE: pluginDeletePublicKeyCmdF, 85 } 86 87 func init() { 88 PluginPublicKeysCmd.Flags().Bool("verbose", false, "List names and details of all public keys installed on your Mattermost server.") 89 PluginPublicKeysCmd.AddCommand( 90 PluginAddPublicKeyCmd, 91 PluginDeletePublicKeyCmd, 92 ) 93 PluginCmd.AddCommand( 94 PluginAddCmd, 95 PluginDeleteCmd, 96 PluginEnableCmd, 97 PluginDisableCmd, 98 PluginListCmd, 99 PluginPublicKeysCmd, 100 ) 101 102 RootCmd.AddCommand(PluginCmd) 103 } 104 105 func pluginAddCmdF(command *cobra.Command, args []string) error { 106 a, err := InitDBCommandContextCobra(command) 107 if err != nil { 108 return err 109 } 110 defer a.Srv().Shutdown() 111 112 if len(args) < 1 { 113 return errors.New("Expected at least one argument. See help text for details.") 114 } 115 116 for i, plugin := range args { 117 fileReader, err := os.Open(plugin) 118 if err != nil { 119 return err 120 } 121 122 if _, err := a.InstallPlugin(fileReader, false); err != nil { 123 CommandPrintErrorln("Unable to add plugin: " + args[i] + ". Error: " + err.Error()) 124 } else { 125 CommandPrettyPrintln("Added plugin: " + plugin) 126 auditRec := a.MakeAuditRecord("pluginAdd", audit.Success) 127 auditRec.AddMeta("plugin", plugin) 128 a.LogAuditRec(auditRec, nil) 129 } 130 fileReader.Close() 131 } 132 return nil 133 } 134 135 func pluginDeleteCmdF(command *cobra.Command, args []string) error { 136 a, err := InitDBCommandContextCobra(command) 137 if err != nil { 138 return err 139 } 140 defer a.Srv().Shutdown() 141 142 if len(args) < 1 { 143 return errors.New("Expected at least one argument. See help text for details.") 144 } 145 146 for _, plugin := range args { 147 if err := a.RemovePlugin(plugin); err != nil { 148 CommandPrintErrorln("Unable to delete plugin: " + plugin + ". Error: " + err.Error()) 149 } else { 150 CommandPrettyPrintln("Deleted plugin: " + plugin) 151 auditRec := a.MakeAuditRecord("pluginDelete", audit.Success) 152 auditRec.AddMeta("plugin", plugin) 153 a.LogAuditRec(auditRec, nil) 154 } 155 } 156 return nil 157 } 158 159 func pluginEnableCmdF(command *cobra.Command, args []string) error { 160 a, err := InitDBCommandContextCobra(command) 161 if err != nil { 162 return err 163 } 164 defer a.Srv().Shutdown() 165 166 if len(args) < 1 { 167 return errors.New("Expected at least one argument. See help text for details.") 168 } 169 170 for _, plugin := range args { 171 if err := a.EnablePlugin(plugin); err != nil { 172 CommandPrintErrorln("Unable to enable plugin: " + plugin + ". Error: " + err.Error()) 173 } else { 174 CommandPrettyPrintln("Enabled plugin: " + plugin) 175 auditRec := a.MakeAuditRecord("pluginEnable", audit.Success) 176 auditRec.AddMeta("plugin", plugin) 177 a.LogAuditRec(auditRec, nil) 178 } 179 } 180 return nil 181 } 182 183 func pluginDisableCmdF(command *cobra.Command, args []string) error { 184 a, err := InitDBCommandContextCobra(command) 185 if err != nil { 186 return err 187 } 188 defer a.Srv().Shutdown() 189 190 if len(args) < 1 { 191 return errors.New("Expected at least one argument. See help text for details.") 192 } 193 194 for _, plugin := range args { 195 if err := a.DisablePlugin(plugin); err != nil { 196 CommandPrintErrorln("Unable to disable plugin: " + plugin + ". Error: " + err.Error()) 197 } else { 198 CommandPrettyPrintln("Disabled plugin: " + plugin) 199 auditRec := a.MakeAuditRecord("pluginDisable", audit.Success) 200 auditRec.AddMeta("plugin", plugin) 201 a.LogAuditRec(auditRec, nil) 202 } 203 } 204 return nil 205 } 206 207 func pluginListCmdF(command *cobra.Command, args []string) error { 208 a, err := InitDBCommandContextCobra(command) 209 if err != nil { 210 return err 211 } 212 defer a.Srv().Shutdown() 213 214 pluginsResp, appErr := a.GetPlugins() 215 if appErr != nil { 216 return errors.Wrap(appErr, "Unable to list plugins.") 217 } 218 219 CommandPrettyPrintln("Listing enabled plugins") 220 for _, plugin := range pluginsResp.Active { 221 CommandPrettyPrintln(plugin.Manifest.Name + ", Version: " + plugin.Manifest.Version) 222 } 223 224 CommandPrettyPrintln("Listing disabled plugins") 225 for _, plugin := range pluginsResp.Inactive { 226 CommandPrettyPrintln(plugin.Manifest.Name + ", Version: " + plugin.Manifest.Version) 227 } 228 229 return nil 230 } 231 232 func pluginPublicKeysCmdF(command *cobra.Command, args []string) error { 233 a, err := InitDBCommandContextCobra(command) 234 if err != nil { 235 return err 236 } 237 defer a.Srv().Shutdown() 238 239 verbose, err := command.Flags().GetBool("verbose") 240 if err != nil { 241 return errors.Wrap(err, "Failed reading verbose flag.") 242 } 243 244 pluginPublicKeysResp, appErr := a.GetPluginPublicKeyFiles() 245 if appErr != nil { 246 return errors.Wrap(appErr, "Unable to list public keys.") 247 } 248 249 if verbose { 250 for _, publicKey := range pluginPublicKeysResp { 251 key, err := a.GetPublicKey(publicKey) 252 if err != nil { 253 CommandPrintErrorln("Unable to get plugin public key: " + publicKey + ". Error: " + err.Error()) 254 } 255 CommandPrettyPrintln("Plugin name: " + publicKey + ". \nPublic key: \n" + string(key) + "\n") 256 } 257 } else { 258 for _, publicKey := range pluginPublicKeysResp { 259 CommandPrettyPrintln(publicKey) 260 } 261 } 262 263 return nil 264 } 265 266 func pluginAddPublicKeyCmdF(command *cobra.Command, args []string) error { 267 a, err := InitDBCommandContextCobra(command) 268 if err != nil { 269 return err 270 } 271 defer a.Srv().Shutdown() 272 273 if len(args) < 1 { 274 return errors.New("Expected at least one argument. See help text for details.") 275 } 276 277 for _, pkFile := range args { 278 filename := filepath.Base(pkFile) 279 fileReader, err := os.Open(pkFile) 280 if err != nil { 281 return model.NewAppError("AddPublicKey", "api.plugin.add_public_key.open.app_error", nil, err.Error(), http.StatusInternalServerError) 282 } 283 defer fileReader.Close() 284 285 if err := a.AddPublicKey(filename, fileReader); err != nil { 286 CommandPrintErrorln("Unable to add public key: " + pkFile + ". Error: " + err.Error()) 287 } else { 288 CommandPrettyPrintln("Added public key: " + pkFile) 289 auditRec := a.MakeAuditRecord("pluginAddPublicKey", audit.Success) 290 auditRec.AddMeta("file", pkFile) 291 a.LogAuditRec(auditRec, nil) 292 } 293 } 294 return nil 295 } 296 297 func pluginDeletePublicKeyCmdF(command *cobra.Command, args []string) error { 298 a, err := InitDBCommandContextCobra(command) 299 if err != nil { 300 return err 301 } 302 defer a.Srv().Shutdown() 303 304 if len(args) < 1 { 305 return errors.New("Expected at least one argument. See help text for details.") 306 } 307 308 for _, pkFile := range args { 309 if err := a.DeletePublicKey(pkFile); err != nil { 310 CommandPrintErrorln("Unable to delete public key: " + pkFile + ". Error: " + err.Error()) 311 } else { 312 CommandPrettyPrintln("Deleted public key: " + pkFile) 313 auditRec := a.MakeAuditRecord("pluginDeletePublicKey", audit.Success) 314 auditRec.AddMeta("file", pkFile) 315 a.LogAuditRec(auditRec, nil) 316 } 317 } 318 return nil 319 }