Skip to content
This repository has been archived by the owner on May 7, 2021. It is now read-only.

cmd/kola: teach spawn to put local SSH keys into userdata #796

Merged
merged 5 commits into from
Feb 1, 2018
Merged

cmd/kola: teach spawn to put local SSH keys into userdata #796

merged 5 commits into from
Feb 1, 2018

Conversation

bgilbert
Copy link
Contributor

@bgilbert bgilbert commented Jan 27, 2018

To use, run kola spawn -k.

Also add kola spawn {-t,--detach}, which is sugar for starting a machine, injecting local SSH keys, printing its ID and IP, and exiting.

@ajeddeloh
Copy link
Contributor

Yes yes yes!

I haven't looked into what the go library support for this would be, but it would be better to connect to the ssh-agent or maybe do that and read the files in .ssh? Just reading the files wont support users (like me) with gpg-derived ssh keys.

@bgilbert
Copy link
Contributor Author

You could copy your public key to ~/.ssh/id_rsa.pub (I do) but it's a good point. Let me think about that.

@euank
Copy link
Contributor

euank commented Jan 29, 2018

I think using the ssh-agent would be nice; if you don't want to implement it, I will as a followup.

I just checked whether it's easy to talk to the agent in golang, and it seems pretty simple; here's the sample program I wrote which prints public keys.

Copy link
Contributor

@euank euank left a comment

Choose a reason for hiding this comment

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

Some comments, I do think ssh-agent support would be quite nice! Overall this looks good to me

@@ -405,6 +421,13 @@ func (c *Conf) copyKeysIgnitionV21(keys []*agent.Key) {
for _, key := range keys {
keyObjs = append(keyObjs, v21types.SSHAuthorizedKey(key.String()))
}
for i := range c.ignitionV21.Passwd.Users {
Copy link
Contributor

Choose a reason for hiding this comment

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

note, in all these loops we can use for _, user := ... The usual iter-value-reused thing doesn't apply to us here because of the early return.

I think it is a bit more readable to do that in all these loops

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's what I had first and it was broken. Those are slices of structs, not slices of references to structs, and we're modifying one of them in place.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, whoops. Missed that '&' there, you're right.

cmdSpawn.Flags().IntVarP(&spawnNodeCount, "nodecount", "c", 1, "number of nodes to spawn")
cmdSpawn.Flags().StringVarP(&spawnUserData, "userdata", "u", "", "file containing userdata to pass to the instances")
cmdSpawn.Flags().BoolVarP(&spawnDetach, "detach", "t", false, "-v --shell=false --remove=false, plus -k if -u not set")
Copy link
Contributor

Choose a reason for hiding this comment

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

I can see how this flag is useful, but the -k conditionally defaulting is kinda weird. Maybe -k should always default true, and have the restriction of conflicting with -u removed?

I'm not going to argue particularly strongly since it only seems a little odd

if spawnNodeCount <= 0 {
return fmt.Errorf("Cluster Failed: nodecount must be one or more")
}

var userdata *conf.UserData
if spawnUserData != "" {
if spawnSetSSHKeys && spawnUserData != "" {
return fmt.Errorf("Cannot set SSH keys if userdata is specified")
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems like we could support -k -u... by merging in the user's ssh keys. Is there a reason not to do that? Just simplicity?

It is true that if -u is used it generally should already have ssh keys, but it doesn't seem like they have to conflict

Copy link
Contributor

@arithx arithx left a comment

Choose a reason for hiding this comment

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

LGTM

When adding the agent's SSH keys to an Ignition config that already
has some, add them to the existing "core" user object rather than
creating another object that overrides the existing one.
Add them to the Conf during UserData.Render().
@bgilbert
Copy link
Contributor Author

bgilbert commented Jan 30, 2018

Updated. Now supports keys from SSH agent and doesn't conflict with custom userdata.

Copy link
Contributor

@ajeddeloh ajeddeloh left a comment

Choose a reason for hiding this comment

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

LGTM.

Copy link
Contributor

@euank euank left a comment

Choose a reason for hiding this comment

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

lgtm other than one last thing

}
for _, name := range []string{"id_rsa.pub", "id_dsa.pub", "id_ecdsa.pub", "id_ed25519.pub"} {
path := filepath.Join(userInfo.HomeDir, ".ssh", name)
if _, err := os.Stat(path); !os.IsNotExist(err) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we want err == nil here; as is this check would pass if there were a different error (such as permission denied)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In that case, the current code causes an error an when we try to read the file later on, which I think is the right thing? The alternative is silently ignoring key files that are broken in some way.

Copy link
Contributor

Choose a reason for hiding this comment

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

Since this is in "guess the keys" code anyways, it seems to me like silently ignoring fs errors fits, but my opinion isn't all that strong

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, updated.

bgilbert and others added 3 commits January 31, 2018 14:21
Disable the functionality by default.

Co-authored-by: Euan Kemp <euan.kemp@coreos.com>
If --remove=false, the ID is useful for terminating the machine
afterward.
Sugar for launching a machine with local SSH keys, printing its ID and
IP, and exiting.
@bgilbert bgilbert merged commit 582f151 into coreos:master Feb 1, 2018
@bgilbert bgilbert deleted the spawn branch February 1, 2018 04:45
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants