Skip to content

Commit

Permalink
Merge pull request #398 from liztio/gen-plugin
Browse files Browse the repository at this point in the history
Re-add `sonobuoy gen plugin`.
  • Loading branch information
timothysc authored Jun 22, 2018
2 parents d8008f6 + a3d51d9 commit 2a39858
Showing 1 changed file with 120 additions and 0 deletions.
120 changes: 120 additions & 0 deletions cmd/sonobuoy/app/gen_plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
Copyright 2018 Heptio Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package app

import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/tls"
"crypto/x509"
"fmt"
"math/big"
"os"

"github.com/heptio/sonobuoy/pkg/config"
"github.com/heptio/sonobuoy/pkg/errlog"
"github.com/heptio/sonobuoy/pkg/plugin"
"github.com/heptio/sonobuoy/pkg/plugin/loader"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

const (
placeholderHostname = "<hostname>"
)

// GenPluginConfig are the input options for running
type GenPluginConfig struct {
Paths []string
PluginName string
}

var genPluginOpts GenPluginConfig

func init() {
cmd := &cobra.Command{
Use: "plugin",
Short: "Generates the manifest Sonobuoy uses to run a worker for the given plugin",
Run: genPluginManifest,
Hidden: true,
Args: cobra.ExactArgs(1),
}

GenCommand.PersistentFlags().StringArrayVarP(
&genPluginOpts.Paths, "paths", "p", []string{".", "./examples/plugins.d/"},
"the paths to search for the plugins in. Defaults to . and ./plugins.d/",
)
// TODO: Other options?
GenCommand.AddCommand(cmd)
}

func genPluginManifest(cmd *cobra.Command, args []string) {
genPluginOpts.PluginName = args[0]
code := 0
manifest, err := generatePluginManifest(genPluginOpts)
if err == nil {
fmt.Printf("%s\n", manifest)
} else {
errlog.LogError(errors.Wrap(err, "error attempting to generate sonobuoy manifest"))
code = 1
}
os.Exit(code)
}

func generatePluginManifest(cfg GenPluginConfig) ([]byte, error) {
plugins, err := loader.LoadAllPlugins(
config.DefaultNamespace,
config.DefaultImage,
"Always",
cfg.Paths,
[]plugin.Selection{{Name: cfg.PluginName}},
)
if err != nil {
return nil, err
}

if len(plugins) != 1 {
return nil, fmt.Errorf("expected 1 plugin, got %v", len(plugins))
}

cert, err := genCert()
if err != nil {
return nil, err
}

return plugins[0].FillTemplate(placeholderHostname, cert)
}

func genCert() (*tls.Certificate, error) {
privKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
return nil, errors.Wrap(err, "couldn't generate private key")
}
tmpl := &x509.Certificate{
SerialNumber: big.NewInt(0),
}
certDER, err := x509.CreateCertificate(rand.Reader, tmpl, tmpl, &privKey.PublicKey, privKey)
if err != nil {
return nil, errors.Wrap(err, "couldn't create certificate")
}

return &tls.Certificate{
Certificate: [][]byte{certDER},
PrivateKey: privKey,
}, nil
}

0 comments on commit 2a39858

Please sign in to comment.