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

Add support for provisioning without logging in as root #49

Merged
merged 2 commits into from
Apr 28, 2021
Merged

Add support for provisioning without logging in as root #49

merged 2 commits into from
Apr 28, 2021

Conversation

caleb-devops
Copy link
Contributor

The k8s_ca_certificates_install provisioner now uses sudo to create the certificate. This enables a non-root user (with sudo permissions) to create this resource.

In addition to this change, a non-root user must set the following server flags to make the kube config file world readable:

flags = ["--write-kubeconfig-mode '0644'"]

This assumes that the non-root user can sudo with no password.

Relates to: #42

@xunleii
Copy link
Owner

xunleii commented Mar 1, 2021

Hi @caleb-devops,

Thanks for your contribution !
Unfortunately, I don't like using sudo for a module because it is not available by default on all OSes. However, in order to implement your workaround, we can create a new null_resources k8s_ca_certificates_install_noroot that will use sudo and should be enabled through a new variable. What do you think about that ?

@caleb-devops
Copy link
Contributor Author

What do you think of something like this for the k8s_ca_certificates_install provisioner?

 provisioner "remote-exec" {
    inline = [
      <<-EOT
      # --- use sudo if we are not already root ---
      [ $(id -u) -eq 0 ] || exec sh -c "echo ${var.sudo_pass} | sudo -S $0 $@"

      mkdir -p /var/lib/rancher/k3s/server/tls/
      echo '${local.certificates_files[count.index].file_content}' > /var/lib/rancher/k3s/server/tls/${local.certificates_files[count.index].file_name}
      EOT
    ]
  }

The sudo check could be added to every remote-exec provisioner to enable provisioning without root and it would also support a sudo password. Sudo would not run if the user is already root.

@xunleii
Copy link
Owner

xunleii commented Mar 2, 2021

I think it's better than before, but I prefer to avoid the password as a variable (even if in sensitive mode). Passing the password is it mandatory ?

@caleb-devops
Copy link
Contributor Author

I updated my pull request to use sudo only if you are not already root. I removed the sudo password check, but this does now require that the user can sudo with no password.

@caleb-devops
Copy link
Contributor Author

caleb-devops commented Mar 13, 2021

Let me know if you need anything else for this. As this currently is, it meets my needs as I do not require a sudo pass in my homelab environment. If you do want to support sudo passwords, I think we would have to include that password as a Terraform variable as shown above.

@xunleii
Copy link
Owner

xunleii commented Apr 18, 2021

Hi @caleb-devops, I will read your work this week and sorry for the latency.

Copy link
Owner

@xunleii xunleii left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems good to me :)
Thanks a lot for your contribution

@xunleii xunleii merged commit 2d7819c into xunleii:master Apr 28, 2021
@fredleger
Copy link

we tested it a lot yesterday and it seems to work like a charm ! thanks @caleb-devops !
let's make a release to avoid pointing to git master ;-)

@xunleii
Copy link
Owner

xunleii commented Apr 30, 2021

Yep, I'll do that this noon !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants