From b0fc5d919fa10697c4d32c709824e836defaf85a Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Mon, 3 Jun 2024 10:31:31 -0400 Subject: [PATCH] Makefile: replace enumer upstream with dmarkham's Since the enumer implementation we used hadn't been updated for 5+ years, this didn't work with recent linux/go versions, and enumer crashed while attempting to parse/analyse the source files. There's another alternative on Github, forked from the one we used, which seems more maintained now, and does produce the expected files in the SDK. --- Makefile | 2 +- communicator/sshkey/algorithm_enumer.go | 45 ++++++++++++++++++++++--- 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 2e86ca800..47ca7ee29 100644 --- a/Makefile +++ b/Makefile @@ -11,7 +11,7 @@ ci: testrace ## Test in continuous integration install-gen-deps: ## Install dependencies for code generation @go install github.com/mna/pigeon@v1.1.0 # Pinning enumer at master branch; the latest tagged release is out of date. - @go install github.com/alvaroloes/enumer@master + @go install github.com/dmarkham/enumer@master @go install github.com/hashicorp/packer-plugin-sdk/cmd/packer-sdc diff --git a/communicator/sshkey/algorithm_enumer.go b/communicator/sshkey/algorithm_enumer.go index 2d95b10a1..3fd7285bb 100644 --- a/communicator/sshkey/algorithm_enumer.go +++ b/communicator/sshkey/algorithm_enumer.go @@ -4,12 +4,15 @@ package sshkey import ( "fmt" + "strings" ) const _AlgorithmName = "rsadsaecdsaed25519" var _AlgorithmIndex = [...]uint8{0, 3, 6, 11, 18} +const _AlgorithmLowerName = "rsadsaecdsaed25519" + func (i Algorithm) String() string { if i < 0 || i >= Algorithm(len(_AlgorithmIndex)-1) { return fmt.Sprintf("Algorithm(%d)", i) @@ -17,13 +20,34 @@ func (i Algorithm) String() string { return _AlgorithmName[_AlgorithmIndex[i]:_AlgorithmIndex[i+1]] } -var _AlgorithmValues = []Algorithm{0, 1, 2, 3} +// An "invalid array index" compiler error signifies that the constant values have changed. +// Re-run the stringer command to generate them again. +func _AlgorithmNoOp() { + var x [1]struct{} + _ = x[RSA-(0)] + _ = x[DSA-(1)] + _ = x[ECDSA-(2)] + _ = x[ED25519-(3)] +} + +var _AlgorithmValues = []Algorithm{RSA, DSA, ECDSA, ED25519} var _AlgorithmNameToValueMap = map[string]Algorithm{ - _AlgorithmName[0:3]: 0, - _AlgorithmName[3:6]: 1, - _AlgorithmName[6:11]: 2, - _AlgorithmName[11:18]: 3, + _AlgorithmName[0:3]: RSA, + _AlgorithmLowerName[0:3]: RSA, + _AlgorithmName[3:6]: DSA, + _AlgorithmLowerName[3:6]: DSA, + _AlgorithmName[6:11]: ECDSA, + _AlgorithmLowerName[6:11]: ECDSA, + _AlgorithmName[11:18]: ED25519, + _AlgorithmLowerName[11:18]: ED25519, +} + +var _AlgorithmNames = []string{ + _AlgorithmName[0:3], + _AlgorithmName[3:6], + _AlgorithmName[6:11], + _AlgorithmName[11:18], } // AlgorithmString retrieves an enum value from the enum constants string name. @@ -32,6 +56,10 @@ func AlgorithmString(s string) (Algorithm, error) { if val, ok := _AlgorithmNameToValueMap[s]; ok { return val, nil } + + if val, ok := _AlgorithmNameToValueMap[strings.ToLower(s)]; ok { + return val, nil + } return 0, fmt.Errorf("%s does not belong to Algorithm values", s) } @@ -40,6 +68,13 @@ func AlgorithmValues() []Algorithm { return _AlgorithmValues } +// AlgorithmStrings returns a slice of all String values of the enum +func AlgorithmStrings() []string { + strs := make([]string, len(_AlgorithmNames)) + copy(strs, _AlgorithmNames) + return strs +} + // IsAAlgorithm returns "true" if the value is listed in the enum definition. "false" otherwise func (i Algorithm) IsAAlgorithm() bool { for _, v := range _AlgorithmValues {