Skip to content
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

🌱 deploy.sh to go code to fix gosec related issues #1602

Closed

Conversation

maxrantil
Copy link

@maxrantil maxrantil commented Mar 6, 2024

What this PR does / why we need it:
This PR transitions the functionality of the deploy.sh script into Go, aiming to fix gosec related issues. There is still one exception related to gosec that remains within the pipeCommands function:

OLD

A env var, KUBECTL_ARGS, introduces a potential security risk. The validateCmd function is currently the best approach to ador the option to choose kubeconfigdressing this issue

@metal3-io-bot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign zaneb for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@metal3-io-bot metal3-io-bot added the size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. label Mar 6, 2024
@tuminoid
Copy link
Member

tuminoid commented Mar 6, 2024

A env var, KUBECTL_ARGS, introduces a potential security risk. The validateCmd function is currently the best approach to addressing this

What kind of legit arguments is passed there? Something like --foo --bar=3 ? If so, then you should be validating versus legit patterns like --\w and --\w=\w. It is very hard to try look for unsafe characters, there are so many ways around it, via UTF etc.

@maxrantil
Copy link
Author

This is an example from metal3-dev-env/tests/roles/run_tests/vars/main.yml:
KUBECTL_ARGS: "--kubeconfig=/tmp/kubeconfig-{{ CLUSTER_NAME }}.yaml"

Also in baremetal-operator/docs/deploying.md is says:
- KUBECTL_ARGS : Additional arguments to kubectl apply

From my testing (setting up with dev-env) there has been nothing set to KUBECTL_ARGS

@tuminoid
Copy link
Member

tuminoid commented Mar 6, 2024

This is an example from metal3-dev-env/tests/roles/run_tests/vars/main.yml: KUBECTL_ARGS: "--kubeconfig=/tmp/kubeconfig-{{ CLUSTER_NAME }}.yaml"

Also in baremetal-operator/docs/deploying.md is says: - KUBECTL_ARGS : Additional arguments to kubectl apply

From my testing (setting up with dev-env) there has been nothing set to KUBECTL_ARGS

Then definitely it is worth doing the regex validation the way I suggested. There aren't many legit ways to format legit params.

@dtantsur
Copy link
Member

dtantsur commented Mar 6, 2024

Do you want to remove the old deploy.sh to avoid confusion?

tools/deploy.go Outdated Show resolved Hide resolved
@kashifest
Copy link
Member

Do you want to remove the old deploy.sh to avoid confusion?

Thats the ultimate goal, for the time being they can coexist until we test the go code properly.

@maxrantil maxrantil force-pushed the deploy-script-to-go-lang/max branch 3 times, most recently from ae8e7a2 to be8724d Compare March 7, 2024 08:05
@maxrantil maxrantil changed the title 🌱 deploy.sh to go code to fix gosec related issues WIP: 🌱 deploy.sh to go code to fix gosec related issues Mar 7, 2024
@metal3-io-bot metal3-io-bot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Mar 7, 2024
@maxrantil
Copy link
Author

I'm currently updating the code to incorporate the k8s go-client. However, I'm unsure about the necessary options for KUBECTL_ARGS. Is specifying KUBECTL_ARGS="--kubeconfig=/path/to/custom-kubeconfig.yaml" sufficient, or should we support additional options? For example, should we include options like KUBECTL_ARGS="--namespace=my-namespace" and more? It is not that straight forward to add every possible option with k8s go-client.

@kashifest
Copy link
Member

I'm currently updating the code to incorporate the k8s go-client. However, I'm unsure about the necessary options for KUBECTL_ARGS. Is specifying KUBECTL_ARGS="--kubeconfig=/path/to/custom-kubeconfig.yaml" sufficient, or should we support additional options? For example, should we include options like KUBECTL_ARGS="--namespace=my-namespace" and more? It is not that straight forward to add every possible option with k8s go-client.

Can you elaborate a bit more what do you mean my specifying options? Would you push the code so that we can check what is it that we may need?

@maxrantil maxrantil force-pushed the deploy-script-to-go-lang/max branch from be8724d to 6d418d6 Compare March 13, 2024 09:36
@maxrantil
Copy link
Author

So the solution I came up with for now is to use $KUBECLT_ARGS exclusively for selecting the kubeconfig. This is the priority and function I came up with:

// getKubeconfigPath determines the kubeconfig path from KUBECTL_ARGS, KUBECONFIG_PATH, or defaults to ~/.kube/config.
func getKubeconfigPath() string {
	kubectlArgs := os.Getenv("KUBECTL_ARGS")
	kubeconfigPath := ""

	kubeconfigPrefix := "--kubeconfig="
	if strings.Contains(kubectlArgs, kubeconfigPrefix) {
		if strings.Contains(kubectlArgs, " ") {
			log.Fatalf("Error: $KUBECTL_ARGS accepts only one argument.")
		}
		kubeconfigPath = strings.TrimPrefix(kubectlArgs, kubeconfigPrefix)
	} else if kubectlArgs != "" {
		log.Fatalf("Error: Invalid format in $KUBECTL_ARGS. Expected format: '--kubeconfig=/path/to/kubeconfig'.")
	}

	if kubeconfigPath == "" {
		kubeconfigPath = os.Getenv("KUBECONFIG_PATH")
		if kubeconfigPath == "" {
			homeDir, err := os.UserHomeDir()
			if err != nil {
				log.Fatalf("Failed to get user home directory: %v", err)
			}
			kubeconfigPath = filepath.Join(homeDir, ".kube", "config")
		}
	}

	return kubeconfigPath
}

@maxrantil maxrantil changed the title WIP: 🌱 deploy.sh to go code to fix gosec related issues 🌱 deploy.sh to go code to fix gosec related issues Mar 13, 2024
@metal3-io-bot metal3-io-bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Mar 13, 2024
config/README.md Outdated Show resolved Hide resolved
docs/deploying.md Outdated Show resolved Hide resolved
tools/deploy.go Outdated Show resolved Hide resolved
tools/deploy.go Outdated Show resolved Hide resolved
tools/deploy.go Outdated Show resolved Hide resolved
tools/deploy.go Outdated Show resolved Hide resolved
tools/deploy.go Outdated Show resolved Hide resolved
tools/deploy.go Outdated Show resolved Hide resolved
tools/deploy.go Outdated Show resolved Hide resolved
tools/deploy.go Outdated Show resolved Hide resolved
@tuminoid
Copy link
Member

Please check if you can use built-ins or de facto libraries instead of DIY. Also, logging a fatal and continuing is weird. If you can fall thru, then its not a fatal but a warning, or you error out if its really fatal.

@maxrantil
Copy link
Author

Thanks @tuminoid for the review! Will look into these comments today

@maxrantil maxrantil force-pushed the deploy-script-to-go-lang/max branch 7 times, most recently from 402d90c to 0e73b3b Compare March 22, 2024 07:45
@maxrantil maxrantil force-pushed the deploy-script-to-go-lang/max branch 7 times, most recently from e066a76 to 7d8b23f Compare April 5, 2024 11:44
@metal3-io-bot metal3-io-bot added the needs-rebase Indicates that a PR cannot be merged because it has merge conflicts with HEAD. label Apr 9, 2024
@maxrantil maxrantil force-pushed the deploy-script-to-go-lang/max branch from 7d8b23f to 0dfa2a5 Compare April 9, 2024 11:07
@metal3-io-bot metal3-io-bot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. and removed needs-rebase Indicates that a PR cannot be merged because it has merge conflicts with HEAD. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Apr 9, 2024
@maxrantil maxrantil force-pushed the deploy-script-to-go-lang/max branch from d7cec0b to d4a8751 Compare April 9, 2024 15:27
@metal3-io-bot metal3-io-bot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Apr 9, 2024
@maxrantil maxrantil force-pushed the deploy-script-to-go-lang/max branch 2 times, most recently from e146e7e to b805267 Compare April 10, 2024 05:45
@metal3-io-bot metal3-io-bot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. and removed size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Apr 10, 2024
@maxrantil maxrantil force-pushed the deploy-script-to-go-lang/max branch from b805267 to e34ef0a Compare April 10, 2024 06:29
@metal3-io-bot metal3-io-bot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Apr 10, 2024
@maxrantil
Copy link
Author

/test metal3-bmo-e2e-test-pull
/ test-centos-e2e-integration-main

@metal3-io-bot metal3-io-bot added the needs-rebase Indicates that a PR cannot be merged because it has merge conflicts with HEAD. label Apr 17, 2024
@metal3-io-bot
Copy link
Contributor

PR needs rebase.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@metal3-io-bot
Copy link
Contributor

@maxrantil: The following tests failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
generate e34ef0a link true /test generate
unit e34ef0a link true /test unit
gomod e34ef0a link true /test gomod
manifestlint e34ef0a link true /test manifestlint

Full PR test history. Your PR dashboard.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here.

@lentzi90
Copy link
Member

Closing this in favor of #1669
/close

@metal3-io-bot
Copy link
Contributor

@lentzi90: Closed this PR.

In response to this:

Closing this in favor of #1669
/close

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-rebase Indicates that a PR cannot be merged because it has merge conflicts with HEAD. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants