mirror of
https://github.com/hashicorp/vault.git
synced 2025-08-06 14:47:01 +02:00
* upgrade hcl dependency on api pkg
This upgrades the hcl dependency for the API pkg,
and adapts its usage so users of our API pkg are
not affected. There's no good way of communicating
a warning via a library call so we don't.
The tokenHelper which is used by all Vault CLI
commands in order to create the Vault client, as
well as directly used by the login and server
commands, is implemented on the api pkg, so this
upgrade also affects all of those commands. Seems
like this was only moved to the api pkg because
the Terraform provider uses it, and I thought
creating a full copy of all those files back under
command would be too much spaghetti.
Also leaving some TODOs to make next deprecation
steps easier.
* upgrade hcl dependency in vault and sdk pkgs
* upgrade hcl dependency in vault and sdk pkgs
* add CLI warnings to commands that take a config
- vault agent (unit test on CMD warning)
- vault proxy (unit test on CMD warning)
- vault server (no test for the warning)
- vault operator diagnose (no tests at all, uses the
same function as vault server
* ignore duplicates on ParseKMSes function
* Extend policy parsing functions and warn on policy store
* Add warning on policy fmt with duplicate attributes
* Add warnings when creating/updating policy with duplicate HCL attrs
* Add log warning when switchedGetPolicy finds duplicate attrs
Following operations can trigger this warning when they run into a policy
with duplicate attributes:
* replication filtered path namespaces invalidation
* policy read API
* building an ACL (for many different purposes like most authZ operations)
* looking up DR token policies
* creating a token with named policies
* when caching the policies for all namespaces during unseal
* Print log warnings when token inline policy has duplicate attrs
No unit tests on these as new test infra would have to be built on all.
Operations affected, which will now print a log warning when the retrieved
token has an inline policy with duplicate attributes:
* capabilities endpoints in sys mount
* handing events under a subscription with a token with duplicate
attrs in inline policies
* token used to create another token has duplicate attrs in inline
policies (sudo check)
* all uses of fetchACLTokenEntryAndEntity when the request uses a
token with inline policies with duplicate attrs. Almost all reqs
are subject to this
* when tokens are created with inline policies (unclear exactly how that
can happen)
* add changelog and deprecation notice
* add missing copywrite notice
* fix copy-paste mistake
good thing it was covered by unit tests
* Fix manual parsing of telemetry field in SharedConfig
This commit in the hcl library was not in the
v1.0.1-vault-5 version we're using but is
included in v1.0.1-vault-7:
e80118accb
This thing of reusing when parsing means that
our approach of manually re-parsing fields
on top of fields that have already been parsed
by the hcl annotation causes strings (maybe
more?) to concatenate.
Fix that by removing annotation. There's
actually more occurrences of this thing of
automatically parsing something that is also
manually parsing. In some places we could
just remove the boilerplate manual parsing, in
others we better remove the auto parsing, but
I don't wanna pull at that thread right now. I
just checked that all places at least fully
overwrite the automatically parsed field
instead of reusing it as the target of the
decode call. The only exception is the AOP
field on ent but that doesn't have maps or
slices, so I think it's fine.
An alternative approach would be to ensure
that the auto-parsed value is discarded,
like the current parseCache function does
note how it's template not templates
* Fix linter complaints
* Update command/base_predict.go
Co-authored-by: Mike Palmiotto <mike.palmiotto@hashicorp.com>
* address review
* remove copywrite headers
* re-add copywrite headers
* make fmt
* Update website/content/partials/deprecation/duplicate-hcl-attributes.mdx
Co-authored-by: Sarah Chavis <62406755+schavis@users.noreply.github.com>
* Update website/content/partials/deprecation/duplicate-hcl-attributes.mdx
Co-authored-by: Sarah Chavis <62406755+schavis@users.noreply.github.com>
* Update website/content/partials/deprecation/duplicate-hcl-attributes.mdx
Co-authored-by: Sarah Chavis <62406755+schavis@users.noreply.github.com>
* undo changes to deprecation.mdx
* remove deprecation doc
* fix conflict with changes from main
---------
Co-authored-by: Mike Palmiotto <mike.palmiotto@hashicorp.com>
Co-authored-by: Sarah Chavis <62406755+schavis@users.noreply.github.com>
136 lines
3.5 KiB
Go
136 lines
3.5 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package command
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"strings"
|
|
|
|
"github.com/hashicorp/cli"
|
|
"github.com/hashicorp/hcl/hcl/printer"
|
|
"github.com/hashicorp/vault/helper/namespace"
|
|
"github.com/hashicorp/vault/helper/random"
|
|
"github.com/hashicorp/vault/vault"
|
|
homedir "github.com/mitchellh/go-homedir"
|
|
"github.com/posener/complete"
|
|
)
|
|
|
|
var (
|
|
_ cli.Command = (*PolicyFmtCommand)(nil)
|
|
_ cli.CommandAutocomplete = (*PolicyFmtCommand)(nil)
|
|
)
|
|
|
|
type PolicyFmtCommand struct {
|
|
*BaseCommand
|
|
}
|
|
|
|
func (c *PolicyFmtCommand) Synopsis() string {
|
|
return "Formats a policy on disk"
|
|
}
|
|
|
|
func (c *PolicyFmtCommand) Help() string {
|
|
helpText := `
|
|
Usage: vault policy fmt [options] PATH
|
|
|
|
Formats a local policy file to the policy specification. This command will
|
|
overwrite the file at the given PATH with the properly-formatted policy
|
|
file contents.
|
|
|
|
Format the local file "my-policy.hcl" as a policy file:
|
|
|
|
$ vault policy fmt my-policy.hcl
|
|
|
|
` + c.Flags().Help()
|
|
|
|
return strings.TrimSpace(helpText)
|
|
}
|
|
|
|
func (c *PolicyFmtCommand) Flags() *FlagSets {
|
|
return c.flagSet(FlagSetNone)
|
|
}
|
|
|
|
func (c *PolicyFmtCommand) AutocompleteArgs() complete.Predictor {
|
|
return complete.PredictFiles("*.hcl")
|
|
}
|
|
|
|
func (c *PolicyFmtCommand) AutocompleteFlags() complete.Flags {
|
|
return c.Flags().Completions()
|
|
}
|
|
|
|
func (c *PolicyFmtCommand) Run(args []string) int {
|
|
f := c.Flags()
|
|
|
|
if err := f.Parse(args); err != nil {
|
|
c.UI.Error(err.Error())
|
|
return 1
|
|
}
|
|
|
|
args = f.Args()
|
|
switch {
|
|
case len(args) < 1:
|
|
c.UI.Error(fmt.Sprintf("Not enough arguments (expected 1, got %d)", len(args)))
|
|
return 1
|
|
case len(args) > 1:
|
|
c.UI.Error(fmt.Sprintf("Too many arguments (expected 1, got %d)", len(args)))
|
|
return 1
|
|
}
|
|
|
|
// Get the filepath, accounting for ~ and stuff
|
|
path, err := homedir.Expand(strings.TrimSpace(args[0]))
|
|
if err != nil {
|
|
c.UI.Error(fmt.Sprintf("Failed to expand path: %s", err))
|
|
return 1
|
|
}
|
|
|
|
// Read the entire contents into memory - it would be nice if we could use
|
|
// a buffer, but hcl wants the full contents.
|
|
b, err := ioutil.ReadFile(path)
|
|
if err != nil {
|
|
c.UI.Error(fmt.Sprintf("Error reading source file: %s", err))
|
|
return 1
|
|
}
|
|
|
|
// Actually parse the policy. We always use the root namespace here because
|
|
// we don't want to modify the results.
|
|
// TODO (HCL_DUP_KEYS_DEPRECATION): Revert back to ParseACLPolicy once the deprecation is done
|
|
_, duplicate, err := vault.ParseACLPolicyCheckDuplicates(namespace.RootNamespace, string(b))
|
|
if err != nil {
|
|
c.UI.Error(err.Error())
|
|
return 1
|
|
}
|
|
if duplicate {
|
|
c.UI.Warn("WARNING: Duplicate keys found in the provided policy, duplicate keys in HCL files are deprecated and will be forbidden in a future release.")
|
|
}
|
|
|
|
// TODO (HCL_DUP_KEYS_DEPRECATION): Restore commented code and remove section below once deprecation is done
|
|
//result, err := printer.Format(b)
|
|
//if err != nil {
|
|
// c.UI.Error(fmt.Sprintf("Error printing result: %s", err))
|
|
// return 1
|
|
//}
|
|
ast, _, err := random.ParseAndCheckForDuplicateHclAttributes(string(b))
|
|
if err != nil {
|
|
c.UI.Error(err.Error())
|
|
return 1
|
|
}
|
|
var buf bytes.Buffer
|
|
if err := printer.DefaultConfig.Fprint(&buf, ast); err != nil {
|
|
c.UI.Error(fmt.Sprintf("Error printing result: %s", err))
|
|
return 1
|
|
}
|
|
buf.WriteString("\n")
|
|
result := buf.Bytes()
|
|
|
|
// Write them back out
|
|
if err := ioutil.WriteFile(path, result, 0o644); err != nil {
|
|
c.UI.Error(fmt.Sprintf("Error writing result: %s", err))
|
|
return 1
|
|
}
|
|
|
|
c.UI.Output(fmt.Sprintf("Success! Formatted policy: %s", path))
|
|
return 0
|
|
}
|