@@ -19,6 +19,7 @@ package configgen
19
19
import (
20
20
"fmt"
21
21
"io/ioutil"
22
+ "log"
22
23
"os"
23
24
"path/filepath"
24
25
"strings"
@@ -41,8 +42,7 @@ func NewCommand() *cobra.Command {
41
42
legacyPlugin := os .Getenv ("KUSTOMIZE_PLUGIN_CONFIG_STRING" )
42
43
err := yaml .Unmarshal ([]byte (legacyPlugin ), kp )
43
44
if err != nil {
44
- fmt .Fprintln (os .Stderr , err .Error ())
45
- return nil
45
+ log .Fatal (err )
46
46
}
47
47
48
48
// Eager check to make sure pkged templates are found.
@@ -51,8 +51,7 @@ func NewCommand() *cobra.Command {
51
51
})
52
52
if err != nil {
53
53
// this shouldn't fail if it was compiled correctly
54
- fmt .Fprintln (os .Stderr , err .Error ())
55
- return nil
54
+ log .Fatal (err )
56
55
}
57
56
58
57
c := framework.TemplateCommand {
@@ -248,44 +247,64 @@ transformers:
248
247
image: my-org/my-project:v0.1.0
249
248
EOF
250
249
251
- # generate configuration from kustomize
250
+ # generate configuration from kustomize > v4.0.0
252
251
kustomize build --enable-alpha-plugins .
253
- ` )
254
252
255
- dir , dirErr := getPluginDir ()
256
- pluginFile := filepath .Join (dir , "KubebuilderConfigGen" )
253
+ # generate configuration from kustomize <= v4.0.0
254
+ kustomize build --enable_alpha_plugins .
255
+ ` )
257
256
258
257
// command for installing the plugin
259
258
install := & cobra.Command {
260
259
Use : "install-as-plugin" ,
261
260
Short : "Install config-gen as a kustomize plugin" ,
262
- Long : strings . TrimSpace ( fmt .Sprintf (`
263
- Write a script to %s for kustomize to locate as a plugin .
264
- ` , pluginFile ) ),
265
- Example : strings . TrimSpace ( `
261
+ Long : fmt .Sprintf (`Write a script to %s for kustomize to locate as a plugin.
262
+ This path will be written to $XDG_CONFIG_HOME if set, otherwise $HOME .
263
+ ` , pluginScriptPath ),
264
+ Example : `
266
265
kubebuilder alpha config-gen install-as-plugin
267
- ` ) ,
266
+ ` ,
268
267
RunE : func (cmd * cobra.Command , args []string ) error {
269
- if dirErr != nil {
270
- return dirErr
271
- }
272
- fmt .Fprintf (cmd .OutOrStdout (), "writing kustomize plugin file at %s\n " , pluginFile )
273
- err = os .MkdirAll (dir , 0700 )
268
+ hd , err := getPluginHomeDir ()
274
269
if err != nil {
270
+ log .Fatal (err )
271
+ }
272
+ fullScriptPath := filepath .Join (hd , pluginScriptPath )
273
+
274
+ // Given the script perms, this command will not be able to overwrite the plugin script file.
275
+ // That's ok, let the user handle removal to maintain security.
276
+ if info , err := os .Stat (fullScriptPath ); err == nil && ! info .IsDir () {
277
+ fmt .Fprintf (cmd .OutOrStdout (), "kustomize plugin configured at %s\n " , fullScriptPath )
278
+ return nil
279
+ }
280
+
281
+ fmt .Fprintf (cmd .OutOrStdout (), "writing kustomize plugin file at %s\n " , fullScriptPath )
282
+
283
+ dir , _ := filepath .Split (fullScriptPath )
284
+ if err = os .MkdirAll (dir , 0700 ); err != nil {
275
285
return err
276
286
}
277
287
278
- return ioutil .WriteFile (pluginFile , []byte (`#!/bin/bash
279
- KUSTOMIZE_FUNCTION=true kubebuilder alpha config-gen
280
- ` ), 0500 )
288
+ // r-x perms to prevent overwrite vulnerability since the script will be executed out-of-tree.
289
+ return ioutil .WriteFile (fullScriptPath , []byte (pluginScript ), 0500 )
281
290
},
282
291
}
283
292
c .AddCommand (install )
284
293
285
294
return c
286
295
}
287
296
288
- func getPluginDir () (string , error ) {
297
+ // Kustomize plugin execution script.
298
+ const pluginScript = `#!/bin/bash
299
+ KUSTOMIZE_FUNCTION=true kubebuilder alpha config-gen
300
+ `
301
+
302
+ // Qualified directory containing the config-gen plugin script. Child of plugin home dir.
303
+ var pluginScriptPath = filepath .Join ("kustomize" , "plugin" ,
304
+ "kubebuilder.sigs.k8s.io" , "v1alpha1" , "kubebuilderconfiggen" , "KubebuilderConfigGen" )
305
+
306
+ // getPluginHomeDir returns $XDG_CONFIG_HOME if set, otherwise $HOME.
307
+ func getPluginHomeDir () (string , error ) {
289
308
xdg := os .Getenv ("XDG_CONFIG_HOME" )
290
309
if xdg == "" {
291
310
dir , err := os .UserHomeDir ()
@@ -294,6 +313,5 @@ func getPluginDir() (string, error) {
294
313
}
295
314
xdg = filepath .Join (dir , ".config" )
296
315
}
297
- dir := filepath .Join (xdg , "kustomize" , "plugin" , "kubebuilder.sigs.k8s.io" , "v1alpha1" , "kubebuilderconfiggen" )
298
- return dir , nil
316
+ return xdg , nil
299
317
}
0 commit comments