Files
releases/cmd/flags.go
T
Ashutosh Narkar 338583c18a Add support for OPA bundle signatures
These changes add support for digital signatures for policy bundles which
can be used to verify their authenticity.

Bundle signature verification involves the following steps:

* Verify the JWT signature
* Verify the files in the JWT payload exist in the bundle
* Verify the file content of the files in bundle match with those in the payload

This commit adds a new `sign` command to generate a digital signature for policy bundles.

For more details, run "opa sign --help"

The signatures generated by the 'sign' command can be verified by the
'build' command. The 'build' command can also sign the bundle it generates.

The 'run' command can verify a signed bundle or skip verification altogether.

OPA 'sign', 'build' and 'run' can be used to
sign/verify bundles in bundle mode (--bundle) mode only. Verification
can be also be performed when bundle downloading is enabled.

Fixes: #1757

Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
2020-07-14 09:49:59 -04:00

134 lines
5.0 KiB
Go

// Copyright 2017 The OPA Authors. All rights reserved.
// Use of this source code is governed by an Apache2
// license that can be found in the LICENSE file.
package cmd
import (
"fmt"
"github.com/spf13/pflag"
"github.com/open-policy-agent/opa/ast"
"github.com/open-policy-agent/opa/util"
)
func addConfigFileFlag(fs *pflag.FlagSet, file *string) {
fs.StringVarP(file, "config-file", "c", "", "set path of configuration file")
}
func addConfigOverrides(fs *pflag.FlagSet, overrides *[]string) {
fs.StringArrayVar(overrides, "set", []string{}, "override config values on the command line (use commas to specify multiple values)")
}
func addConfigOverrideFiles(fs *pflag.FlagSet, overrides *[]string) {
fs.StringArrayVar(overrides, "set-file", []string{}, "override config values with files on the command line (use commas to specify multiple values)")
}
func addFailFlag(fs *pflag.FlagSet, fail *bool, value bool) {
fs.BoolVarP(fail, "fail", "", value, "exits with non-zero exit code on undefined/empty result and errors")
}
func addDataFlag(fs *pflag.FlagSet, paths *repeatedStringFlag) {
fs.VarP(paths, "data", "d", "set data file(s) or directory path(s)")
}
func addBundleFlag(fs *pflag.FlagSet, paths *repeatedStringFlag) {
fs.VarP(paths, "bundle", "b", "set bundle file(s) or directory path(s)")
}
func addBundleModeFlag(fs *pflag.FlagSet, bundle *bool, value bool) {
fs.BoolVarP(bundle, "bundle", "b", value, "load paths as bundle files or root directories")
}
func addInputFlag(fs *pflag.FlagSet, inputPath *string) {
fs.StringVarP(inputPath, "input", "i", "", "set input file path")
}
func addImportFlag(fs *pflag.FlagSet, imports *repeatedStringFlag) {
fs.VarP(imports, "import", "", "set query import(s)")
}
func addPackageFlag(fs *pflag.FlagSet, pkg *string) {
fs.StringVarP(pkg, "package", "", "", "set query package")
}
func addQueryStdinFlag(fs *pflag.FlagSet, stdin *bool) {
fs.BoolVarP(stdin, "stdin", "", false, "read query from stdin")
}
func addInputStdinFlag(fs *pflag.FlagSet, stdinInput *bool) {
fs.BoolVarP(stdinInput, "stdin-input", "I", false, "read input document from stdin")
}
func addMetricsFlag(fs *pflag.FlagSet, metrics *bool, value bool) {
fs.BoolVarP(metrics, "metrics", "", value, "report query performance metrics")
}
func addOutputFormat(fs *pflag.FlagSet, outputFormat *util.EnumFlag) {
fs.VarP(outputFormat, "format", "f", "set output format")
}
func addBenchmemFlag(fs *pflag.FlagSet, benchMem *bool, value bool) {
fs.BoolVar(benchMem, "benchmem", value, "report memory allocations with benchmark results")
}
func addCountFlag(fs *pflag.FlagSet, count *int, cmdType string) {
fs.IntVar(count, "count", 1, fmt.Sprintf("number of times to repeat each %s (default 1)", cmdType))
}
func addMaxErrorsFlag(fs *pflag.FlagSet, errLimit *int) {
fs.IntVarP(errLimit, "max-errors", "m", ast.CompileErrorLimitDefault, "set the number of errors to allow before compilation fails early")
}
func addIgnoreFlag(fs *pflag.FlagSet, ignoreNames *[]string) {
fs.StringSliceVarP(ignoreNames, "ignore", "", []string{}, "set file and directory names to ignore during loading (e.g., '.*' excludes hidden files)")
}
func addSigningAlgFlag(fs *pflag.FlagSet, alg *string, value string) {
fs.StringVarP(alg, "signing-alg", "", value, "name of the signing algorithm")
}
func addClaimsFileFlag(fs *pflag.FlagSet, file *string) {
fs.StringVarP(file, "claims-file", "", "", "set path of JSON file containing optional claims (see: https://openpolicyagent.org/docs/latest/management/#bundle-signature-format)")
}
func addSigningKeyFlag(fs *pflag.FlagSet, key *string) {
fs.StringVarP(key, "signing-key", "", "", "set the secret (HMAC) or path of the PEM file containing the private key (RSA and ECDSA)")
}
func addVerificationKeyFlag(fs *pflag.FlagSet, key *string) {
fs.StringVarP(key, "verification-key", "", "", "set the secret (HMAC) or path of the PEM file containing the public key (RSA and ECDSA)")
}
func addVerificationKeyIDFlag(fs *pflag.FlagSet, keyID *string, value string) {
fs.StringVarP(keyID, "verification-key-id", "", value, "name assigned to the verification key used for bundle verification")
}
func addBundleVerificationScopeFlag(fs *pflag.FlagSet, scope *string) {
fs.StringVarP(scope, "scope", "", "", "scope to use for bundle signature verification")
}
func addBundleVerificationSkipFlag(fs *pflag.FlagSet, skip *bool, value bool) {
fs.BoolVarP(skip, "skip-verify", "", value, "disables bundle signature verification")
}
func addBundleVerificationExcludeFilesFlag(fs *pflag.FlagSet, excludeNames *[]string) {
fs.StringSliceVarP(excludeNames, "exclude-files-verify", "", []string{}, "set file names to exclude during bundle verification")
}
const (
explainModeOff = "off"
explainModeFull = "full"
explainModeNotes = "notes"
explainModeFails = "fails"
)
func newExplainFlag(modes []string) *util.EnumFlag {
return util.NewEnumFlag(modes[0], modes)
}
func setExplainFlag(fs *pflag.FlagSet, explain *util.EnumFlag) {
fs.VarP(explain, "explain", "", "enable query explanations")
}