Skip to content

Commit

Permalink
sshutil: lock ${LIMA_HOME}/_config
Browse files Browse the repository at this point in the history
Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
  • Loading branch information
AkihiroSuda committed Jun 30, 2021
1 parent c1a0fb7 commit 2e4f8cf
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 6 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ require (
github.com/sirupsen/logrus v1.8.1
github.com/urfave/cli/v2 v2.3.0
github.com/yalue/native_endian v1.0.1
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c
gopkg.in/yaml.v2 v2.4.0
gotest.tools/v3 v3.0.3
)
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -920,8 +920,9 @@ golang.org/x/sys v0.0.0-20201202213521-69691e467435/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210303074136-134d130e1a04/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210324051608-47abb6519492 h1:Paq34FxTluEPvVyayQqMPgHm+vTOrIifmcYxFBx9TLg=
golang.org/x/sys v0.0.0-20210324051608-47abb6519492/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210503060354-a79de5458b56 h1:b8jxX3zqjpqb2LklXPzKSGJhzyxCOZSz8ncv8Nv+y7w=
golang.org/x/term v0.0.0-20210503060354-a79de5458b56/go.mod h1:tfny5GFUkzUvx4ps4ajbZsCe5lw1metzhBm9T3x7oIY=
Expand Down
53 changes: 53 additions & 0 deletions pkg/lockutil/lockutil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// From https://github.com/containerd/nerdctl/blob/v0.9.0/pkg/lockutil/lockutil_linux.go
/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package lockutil

import (
"os"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"golang.org/x/sys/unix"
)

func WithDirLock(dir string, fn func() error) error {
dirFile, err := os.Open(dir)
if err != nil {
return err
}
defer dirFile.Close()
if err := Flock(dirFile, unix.LOCK_EX); err != nil {
return errors.Wrapf(err, "failed to lock %q", dir)
}
defer func() {
if err := Flock(dirFile, unix.LOCK_UN); err != nil {
logrus.WithError(err).Errorf("failed to unlock %q", dir)
}
}()
return fn()
}

func Flock(f *os.File, flags int) error {
fd := int(f.Fd())
for {
err := unix.Flock(fd, flags)
if err == nil || err != unix.EINTR {
return err
}
}
}
16 changes: 11 additions & 5 deletions pkg/sshutil/sshutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"path/filepath"
"strings"

"github.com/AkihiroSuda/lima/pkg/lockutil"
"github.com/AkihiroSuda/lima/pkg/osutil"
"github.com/AkihiroSuda/lima/pkg/store"
"github.com/AkihiroSuda/lima/pkg/store/filenames"
Expand Down Expand Up @@ -51,11 +52,16 @@ func DefaultPubKeys() ([]PubKey, error) {
if err := os.MkdirAll(configDir, 0700); err != nil {
return nil, errors.Wrapf(err, "could not create %q directory", configDir)
}
keygenCmd := exec.Command("ssh-keygen", "-t", "ed25519", "-q", "-N", "", "-f",
filepath.Join(configDir, filenames.UserPrivateKey))
logrus.Debugf("executing %v", keygenCmd.Args)
if out, err := keygenCmd.CombinedOutput(); err != nil {
return nil, errors.Wrapf(err, "failed to run %v: %q", keygenCmd.Args, string(out))
if err := lockutil.WithDirLock(configDir, func() error {
keygenCmd := exec.Command("ssh-keygen", "-t", "ed25519", "-q", "-N", "", "-f",
filepath.Join(configDir, filenames.UserPrivateKey))
logrus.Debugf("executing %v", keygenCmd.Args)
if out, err := keygenCmd.CombinedOutput(); err != nil {
return errors.Wrapf(err, "failed to run %v: %q", keygenCmd.Args, string(out))
}
return nil
}); err != nil {
return nil, err
}
}
entry, err := readPublicKey(filepath.Join(configDir, filenames.UserPublicKey))
Expand Down

0 comments on commit 2e4f8cf

Please sign in to comment.