-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
feat(terraform): Add hyphen and non-ASCII support for domain names in credential extraction #6108
Conversation
…s-in-credential-extraction
func getPrivateRegistryTokenFromEnvVars(hostname string) (string, error) { | ||
token := "" | ||
asciiHostname, err := idna.ToASCII(hostname) | ||
if err != nil { | ||
return "", fmt.Errorf("Could not convert hostname %s to a punycode encoded ASCII string so cannot find token for this registry", hostname) | ||
} else { | ||
envVar := fmt.Sprintf("TF_TOKEN_%s", strings.ReplaceAll(asciiHostname, ".", "_")) | ||
token = os.Getenv(envVar) | ||
|
||
// Dashes in the hostname can optionally be converted to double underscores | ||
if token == "" { | ||
envVar = strings.ReplaceAll(envVar, "-", "__") | ||
token = os.Getenv(envVar) | ||
} | ||
|
||
if token == "" { | ||
return "", fmt.Errorf("No token was found for the registry at %s", hostname) | ||
} else { | ||
return token, nil | ||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about if we drop the trailing else?
func getPrivateRegistryTokenFromEnvVars(hostname string) (string, error) { | |
token := "" | |
asciiHostname, err := idna.ToASCII(hostname) | |
if err != nil { | |
return "", fmt.Errorf("Could not convert hostname %s to a punycode encoded ASCII string so cannot find token for this registry", hostname) | |
} else { | |
envVar := fmt.Sprintf("TF_TOKEN_%s", strings.ReplaceAll(asciiHostname, ".", "_")) | |
token = os.Getenv(envVar) | |
// Dashes in the hostname can optionally be converted to double underscores | |
if token == "" { | |
envVar = strings.ReplaceAll(envVar, "-", "__") | |
token = os.Getenv(envVar) | |
} | |
if token == "" { | |
return "", fmt.Errorf("No token was found for the registry at %s", hostname) | |
} else { | |
return token, nil | |
} | |
} | |
} | |
func getPrivateRegistryTokenFromEnvVars(hostname string) (string, error) { | |
token := "" | |
asciiHostname, err := idna.ToASCII(hostname) | |
if err != nil { | |
return "", fmt.Errorf("Could not convert hostname %s to a punycode encoded ASCII string so cannot find token for this registry", hostname) | |
} | |
envVar := fmt.Sprintf("TF_TOKEN_%s", strings.ReplaceAll(asciiHostname, ".", "_")) | |
token = os.Getenv(envVar) | |
// Dashes in the hostname can optionally be converted to double underscores | |
if token == "" { | |
envVar = strings.ReplaceAll(envVar, "-", "__") | |
token = os.Getenv(envVar) | |
} | |
if token == "" { | |
return "", fmt.Errorf("No token was found for the registry at %s", hostname) | |
} | |
return token, nil | |
} | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree, added
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for recreating the PR. It lgtm, just left one small comment to improve readability.
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_getPrivateRegistryTokenFromEnvVars(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about using a table test here? Here's an example https://github.com/aquasecurity/trivy/blob/main/pkg/licensing/classifier_test.go#L15
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, see what you think
…tial-extraction' of https://github.com/adam-carruthers/trivy into hyphen-and-non-ascii-support-for-domain-names-in-credential-extraction
…s-in-credential-extraction
Sorry, was on holiday! Sorry it took me a little bit to get back to it. |
Description
Related issues
Related PRs
TF_TOKEN
: Optionally transform-
character in hostname for TF_TOKEN to__
trivy-iac#95 - This PR was open and almost accepted in trivy-iac, but that repo was merged into the main trivy repo very recentlyChecklist