mirror of
				https://github.com/optim-enterprises-bv/vault.git
				synced 2025-10-31 02:28:09 +00:00 
			
		
		
		
	Move config validation code to it's own, non ENT specific file (#11579)
* Move config validation code to it's own, non ENT specific file * Fix imports * import order
This commit is contained in:
		| @@ -4,7 +4,6 @@ import ( | |||||||
| 	"encoding/json" | 	"encoding/json" | ||||||
| 	"errors" | 	"errors" | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"github.com/hashicorp/hcl/hcl/token" |  | ||||||
| 	"io" | 	"io" | ||||||
| 	"io/ioutil" | 	"io/ioutil" | ||||||
| 	"os" | 	"os" | ||||||
| @@ -23,7 +22,7 @@ import ( | |||||||
|  |  | ||||||
| // Config is the configuration for the vault server. | // Config is the configuration for the vault server. | ||||||
| type Config struct { | type Config struct { | ||||||
| 	UnusedKeys map[string][]token.Pos `hcl:",unusedKeyPositions"` | 	UnusedKeys configutil.UnusedKeyMap `hcl:",unusedKeyPositions"` | ||||||
| 	entConfig | 	entConfig | ||||||
|  |  | ||||||
| 	*configutil.SharedConfig `hcl:"-"` | 	*configutil.SharedConfig `hcl:"-"` | ||||||
|   | |||||||
| @@ -3,30 +3,11 @@ | |||||||
| package configutil | package configutil | ||||||
|  |  | ||||||
| import ( | import ( | ||||||
| 	"fmt" |  | ||||||
| 	"github.com/asaskevich/govalidator" |  | ||||||
| 	"github.com/hashicorp/hcl/hcl/ast" | 	"github.com/hashicorp/hcl/hcl/ast" | ||||||
| 	"github.com/hashicorp/hcl/hcl/token" |  | ||||||
| 	"github.com/hashicorp/vault/sdk/helper/strutil" |  | ||||||
| ) | ) | ||||||
|  |  | ||||||
| type EntSharedConfig struct{} | type EntSharedConfig struct{} | ||||||
|  |  | ||||||
| type UnusedKeyMap map[string][]token.Pos |  | ||||||
|  |  | ||||||
| type ConfigError struct { |  | ||||||
| 	Problem  string |  | ||||||
| 	Position token.Pos |  | ||||||
| } |  | ||||||
|  |  | ||||||
| func (c *ConfigError) String() string { |  | ||||||
| 	return fmt.Sprintf("%s at %s", c.Problem, c.Position.String()) |  | ||||||
| } |  | ||||||
|  |  | ||||||
| type ValidatableConfig interface { |  | ||||||
| 	Validate() []ConfigError |  | ||||||
| } |  | ||||||
|  |  | ||||||
| func (ec *EntSharedConfig) ParseConfig(list *ast.ObjectList) error { | func (ec *EntSharedConfig) ParseConfig(list *ast.ObjectList) error { | ||||||
| 	return nil | 	return nil | ||||||
| } | } | ||||||
| @@ -34,41 +15,3 @@ func (ec *EntSharedConfig) ParseConfig(list *ast.ObjectList) error { | |||||||
| func ParseEntropy(result *SharedConfig, list *ast.ObjectList, blockName string) error { | func ParseEntropy(result *SharedConfig, list *ast.ObjectList, blockName string) error { | ||||||
| 	return nil | 	return nil | ||||||
| } | } | ||||||
|  |  | ||||||
| // Creates the ConfigErrors for unused fields, which occur in various structs |  | ||||||
| func ValidateUnusedFields(unusedKeyPositions UnusedKeyMap, sourceFilePath string) []ConfigError { |  | ||||||
| 	if unusedKeyPositions == nil { |  | ||||||
| 		return nil |  | ||||||
| 	} |  | ||||||
| 	var errors []ConfigError |  | ||||||
| 	for field, positions := range unusedKeyPositions { |  | ||||||
| 		problem := fmt.Sprintf("unknown field %s found in configuration", field) |  | ||||||
| 		for _, pos := range positions { |  | ||||||
| 			if pos.Filename == "" && sourceFilePath != "" { |  | ||||||
| 				pos.Filename = sourceFilePath |  | ||||||
| 			} |  | ||||||
| 			errors = append(errors, ConfigError{ |  | ||||||
| 				Problem:  problem, |  | ||||||
| 				Position: pos, |  | ||||||
| 			}) |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
| 	return errors |  | ||||||
| } |  | ||||||
|  |  | ||||||
| // UnusedFieldDifference returns all the keys in map a that are not present in map b, and also not present in foundKeys. |  | ||||||
| func UnusedFieldDifference(a, b UnusedKeyMap, foundKeys []string) UnusedKeyMap { |  | ||||||
| 	if a == nil { |  | ||||||
| 		return nil |  | ||||||
| 	} |  | ||||||
| 	if b == nil { |  | ||||||
| 		return a |  | ||||||
| 	} |  | ||||||
| 	res := make(UnusedKeyMap) |  | ||||||
| 	for k, v := range a { |  | ||||||
| 		if _, ok := b[k]; !ok && !strutil.StrListContainsCaseInsensitive(foundKeys, govalidator.UnderscoreToCamelCase(k)) { |  | ||||||
| 			res[k] = v |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
| 	return res |  | ||||||
| } |  | ||||||
|   | |||||||
							
								
								
									
										61
									
								
								internalshared/configutil/lint.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										61
									
								
								internalshared/configutil/lint.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,61 @@ | |||||||
|  | package configutil | ||||||
|  |  | ||||||
|  | import ( | ||||||
|  | 	"fmt" | ||||||
|  | 	"github.com/asaskevich/govalidator" | ||||||
|  | 	"github.com/hashicorp/hcl/hcl/token" | ||||||
|  | 	"github.com/hashicorp/vault/sdk/helper/strutil" | ||||||
|  | ) | ||||||
|  |  | ||||||
|  | type UnusedKeyMap map[string][]token.Pos | ||||||
|  |  | ||||||
|  | type ConfigError struct { | ||||||
|  | 	Problem  string | ||||||
|  | 	Position token.Pos | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func (c *ConfigError) String() string { | ||||||
|  | 	return fmt.Sprintf("%s at %s", c.Problem, c.Position.String()) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | type ValidatableConfig interface { | ||||||
|  | 	Validate() []ConfigError | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // Creates the ConfigErrors for unused fields, which occur in various structs | ||||||
|  | func ValidateUnusedFields(unusedKeyPositions UnusedKeyMap, sourceFilePath string) []ConfigError { | ||||||
|  | 	if unusedKeyPositions == nil { | ||||||
|  | 		return nil | ||||||
|  | 	} | ||||||
|  | 	var errors []ConfigError | ||||||
|  | 	for field, positions := range unusedKeyPositions { | ||||||
|  | 		problem := fmt.Sprintf("unknown field %s found in configuration", field) | ||||||
|  | 		for _, pos := range positions { | ||||||
|  | 			if pos.Filename == "" && sourceFilePath != "" { | ||||||
|  | 				pos.Filename = sourceFilePath | ||||||
|  | 			} | ||||||
|  | 			errors = append(errors, ConfigError{ | ||||||
|  | 				Problem:  problem, | ||||||
|  | 				Position: pos, | ||||||
|  | 			}) | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	return errors | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // UnusedFieldDifference returns all the keys in map a that are not present in map b, and also not present in foundKeys. | ||||||
|  | func UnusedFieldDifference(a, b UnusedKeyMap, foundKeys []string) UnusedKeyMap { | ||||||
|  | 	if a == nil { | ||||||
|  | 		return nil | ||||||
|  | 	} | ||||||
|  | 	if b == nil { | ||||||
|  | 		return a | ||||||
|  | 	} | ||||||
|  | 	res := make(UnusedKeyMap) | ||||||
|  | 	for k, v := range a { | ||||||
|  | 		if _, ok := b[k]; !ok && !strutil.StrListContainsCaseInsensitive(foundKeys, govalidator.UnderscoreToCamelCase(k)) { | ||||||
|  | 			res[k] = v | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	return res | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user
	 Scott Miller
					Scott Miller