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

Allow setting default SMB credentials in my user Vagrantfile #11413

Closed
AnthonyMastrean opened this issue Feb 25, 2020 · 10 comments
Closed

Allow setting default SMB credentials in my user Vagrantfile #11413

AnthonyMastrean opened this issue Feb 25, 2020 · 10 comments

Comments

@AnthonyMastrean
Copy link

AnthonyMastrean commented Feb 25, 2020

Vagrant version

Vagrant 2.2.7

Host operating system

Windows 10

Guest operating system

Any

Vagrantfile

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
  config.vm.box = "generic/debian10"
  config.vm.synced_folder ".", "/vagrant"
end

Debug output

Expected behavior

I should be able to setup default SMB credentials for any subsequent SMB synced folder in my user Vagrantfile.

That's this one, per the docs:

~/.vagrant.d/Vagrantfile

https://www.vagrantup.com/docs/vagrantfile/#load-order-and-merging

Actual behavior

Since the smb_username and smb_password and mount_options are method parameters on synced_folder, I cannot easily set defaults in a way that still allows me to share this Vagrantfile with coworkers.

Note: my janky workaround doesn't actually work, so I'm not sure I have a workaround... will continue exploring.

I have to do something janky like...

~/.vagrant.d/Vagrantfile

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
  default_smb_username = "..."
  default_smb_password = "..."
  default_mount_options = "..."
end

And then in my ultimate Vagrantfile

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
  config.vm.box = "generic/debian10"
  config.vm.synced_folder ".", "/vagrant", smb_username: default_smb_username, smb_password: default_smb_password
end

Steps to reproduce

References

@AnthonyMastrean
Copy link
Author

Oh, well, my janky workaround doesn't actually work... I guess the Vagrantfile merging method is more about building and merging Vagrant conf and less about like merging Ruby source files.

So, that's even worse? I don't even have a bad workaround 😆

@briancain
Copy link
Member

Hi @AnthonyMastrean - You should be able to potentially set these values as variables as you did, do you have a debug log of the result of running that Vagrantfile?

@briancain
Copy link
Member

Also please be aware that I think the smb password is usually your system password 🙈 Vagrant will typically scrub the logs with that, but always good to double check before sharing anything! Thanks.

@briancain
Copy link
Member

Also additionally, I should mention, since it seems like you are configuring the default share, you shouldn't actually need to do any of this. You should simply be able to put this line in your global Vagrantfile, and it should apply to all guests:

~/.vagrant.d/Vagrantfile

Vagrant.configure("2") do |config|
  config.vm.synced_folder ".", "/vagrant", smb_username: "username here", smb_password: "password here"
end

config is a top scope option, and Vagrant will apply it to each guest that has that specific synced folder (which by default, is every guest because /vagrant will always be synced unless disabled).

@AnthonyMastrean
Copy link
Author

AnthonyMastrean commented Mar 4, 2020

Variables inside the configure block scope didn’t seem to survive merging. Global variables outside the block scope seemed to work.

You’re right about overriding the default synced folder in my user Vagrantfile. I think that’ll work in the short term. But, it will only work easily for the default synced folder. I don’t think I’m going to like adding every SMB synced folder I come across to my user Vagrantfile!

And, actually wouldn’t synced folders in the folder/project scope override the same in the user scope? So, maybe I couldn’t use it that way.

The other problem is, I really want to share these Vagrantfiles in source control with team members that may or may not end up setting up defaults. It would be nice if the default credentials solution is hidden from anyone not using it (unlike, adding globals to my user Vagrantfile and referencing them in the project Vagrantfile).

@briancain
Copy link
Member

To your last point, I imagine each of your team members probably have different smb_username and smb_passwords? Those values are your host system username and password, not the guest username and password that you're bringing up with Vagrant. So unless all of you have the same username and password, there isn't much value here in setting a default value to share in git if you're trying to get a usable default across multiple different machines for different people. Leaving them blank will just prompt your teammates for their host password. Which could be annoying, but at least it won't be a wrong default value.

There isn't really a great way to set these values across a team, but mostly because of what they are. Each person should have a unique username and password, which makes it difficult to share. You could potentially abstract out these values and read them from a config file (like a json or yaml structure), and expect your team to fill out those values in the config file to prevent their Vagrantfile from adding unstaged changes or being wrong against the master branch with their local username/password. Then at least there won't be any extra changes to ignore with git, but the downside is now you have your system password in a json or yaml file in your project directory.

@AnthonyMastrean
Copy link
Author

AnthonyMastrean commented Mar 5, 2020

@briancain I'm definitely talking about per-user host credentials. That's why I'm asking for per-user Vagrantfile configuration. I am, in fact, already checking into source control Vagrantfiles in several projects. And I'm defining more synced folder than just the default.

I was trying to keep the convo generic, but we were getting a little lost without details. 🤗

I feel like it's a reasonable "feature request" and in line with some of your other configuration options (whether per-user Vagrantfile conf or user-scoped env vars).

@briancain
Copy link
Member

@AnthonyMastrean I guess I'm not 100% clear as to what the feature request is... you should be able to define these defaults like:

~/.vagrant.d/Vagrantfile

DEFAULT_SMB_USERNAME="username"
DEFAULT_SMB_PASSWORD="password"

Vagrant.configure("2") do |config|
    # setting this synced folder is not required, btw. The variables above 
    # will be accessible anywhere as long as it gets loaded by Vagrant
    config.vm.synced_folder ".", "/vagrant", smb_username: DEFAULT_SMB_USERNAME,
        smb_password: DEFAULT_SMB_PASSWORD
end

Or those could also exist in your local Vagrantfile in your project directory too. That seems like it should cover the use case you are looking for, or am I missing something? 🤔

@chrisroberts
Copy link
Member

The other problem is, I really want to share these Vagrantfiles in source control with team members that may or may not end up setting up defaults. It would be nice if the default credentials solution is hidden from anyone not using it (unlike, adding globals to my user Vagrantfile and referencing them in the project Vagrantfile).

An easy solution to this is to reference environment variables for the values within the configuration. Then users can opt-in to setting them if they like, and if they don't they will be prompted by Vagrant when setting up the synced directories:

Vagrant.configure("2") do |config|
  config.vm.synced_folder '.', '/vagrant', smb_username: ENV["SMB_USERNAME"], smb_password: ENV["SMB_PASSWORD"]
end

Cheers!

@ghost
Copy link

ghost commented May 23, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators May 23, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants