Skip to content
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

Re-add sonobuoy gen plugin. #398

Merged
merged 2 commits into from
Jun 22, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't look like a sub-command of gen, but it's own sub-command of the root? Is that what you want?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I add it to GenCommand, so I think it'll be a subcommand of that, no?

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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh I see, nvmd.

}

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
}