Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ca/certificates.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
"github.com/moby/swarmkit/v2/api"
"github.com/moby/swarmkit/v2/ca/pkcs8"
"github.com/moby/swarmkit/v2/connectionbroker"
"github.com/moby/swarmkit/v2/ioutils"
"github.com/moby/sys/atomicwriter"
"github.com/opencontainers/go-digest"
"github.com/pkg/errors"
"google.golang.org/grpc"
Expand Down Expand Up @@ -906,7 +906,7 @@ func SaveRootCA(rootCA RootCA, paths CertPaths) error {
}

// If the root certificate got returned successfully, save the rootCA to disk.
return ioutils.AtomicWriteFile(paths.Cert, rootCA.Certs, 0o644)
return atomicwriter.WriteFile(paths.Cert, rootCA.Certs, 0o644)
}

// GenerateNewCSR returns a newly generated key and CSR signed with said key
Expand Down
15 changes: 7 additions & 8 deletions ca/keyreadwriter.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ca

import (
"crypto/tls"
"crypto/x509"
"encoding/pem"
"os"
Expand All @@ -9,11 +10,9 @@ import (
"strings"
"sync"

"crypto/tls"

"github.com/moby/swarmkit/v2/ca/keyutils"
"github.com/moby/swarmkit/v2/ca/pkcs8"
"github.com/moby/swarmkit/v2/ioutils"
"github.com/moby/sys/atomicwriter"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -208,7 +207,7 @@ func (k *KeyReadWriter) Read() ([]byte, []byte, error) {
switch {
case err == nil:
_, err = tls.X509KeyPair(cert, keyBytes)
case os.IsNotExist(err): //continue to try temp location
case os.IsNotExist(err): // continue to try temp location
break
default:
return nil, nil, err
Expand Down Expand Up @@ -284,7 +283,7 @@ func (k *KeyReadWriter) ViewAndUpdateHeaders(cb func(PEMKeyHeaders) (PEMKeyHeade
headers[versionHeader] = strconv.FormatUint(k.kekData.Version, 10)
keyBlock.Headers = headers

if err = ioutils.AtomicWriteFile(k.paths.Key, pem.EncodeToMemory(keyBlock), keyPerms); err != nil {
if err = atomicwriter.WriteFile(k.paths.Key, pem.EncodeToMemory(keyBlock), keyPerms); err != nil {
return err
}
k.headersObj = pkh
Expand Down Expand Up @@ -315,7 +314,7 @@ func (k *KeyReadWriter) Write(certBytes, plaintextKeyBytes []byte, kekData *KEKD
// temp path first. This is because we want to have only a single copy of the key
// for rotation and header modification.
tmpPaths := k.genTempPaths()
if err := ioutils.AtomicWriteFile(tmpPaths.Cert, certBytes, certPerms); err != nil {
if err := atomicwriter.WriteFile(tmpPaths.Cert, certBytes, certPerms); err != nil {
return err
}

Expand Down Expand Up @@ -430,7 +429,7 @@ func (k *KeyReadWriter) writeKey(keyBlock *pem.Block, kekData KEKData, pkh PEMKe
}
keyBlock.Headers[versionHeader] = strconv.FormatUint(kekData.Version, 10)

if err := ioutils.AtomicWriteFile(k.paths.Key, pem.EncodeToMemory(keyBlock), keyPerms); err != nil {
if err := atomicwriter.WriteFile(k.paths.Key, pem.EncodeToMemory(keyBlock), keyPerms); err != nil {
return err
}
k.kekData = kekData
Expand Down Expand Up @@ -477,7 +476,7 @@ func (k *KeyReadWriter) DowngradeKey() error {
mergePEMHeaders(newBlock.Headers, oldBlock.Headers)

// do not use krw.Write as it will convert the key to pkcs8
return ioutils.AtomicWriteFile(k.paths.Key, pem.EncodeToMemory(newBlock), keyPerms)
return atomicwriter.WriteFile(k.paths.Key, pem.EncodeToMemory(newBlock), keyPerms)
}

// merges one set of PEM headers onto another, excepting for key encryption value
Expand Down
4 changes: 2 additions & 2 deletions ca/testutils/cautils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ import (
"github.com/moby/swarmkit/v2/ca/pkcs8"
"github.com/moby/swarmkit/v2/connectionbroker"
"github.com/moby/swarmkit/v2/identity"
"github.com/moby/swarmkit/v2/ioutils"
"github.com/moby/swarmkit/v2/log"
"github.com/moby/swarmkit/v2/manager/state/store"
stateutils "github.com/moby/swarmkit/v2/manager/state/testutils"
"github.com/moby/swarmkit/v2/remotes"
"github.com/moby/sys/atomicwriter"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
Expand Down Expand Up @@ -173,7 +173,7 @@ func newTestCA(t *testing.T, tempBaseDir string, apiRootCA api.RootCA, krwGenera
}

// Write the root certificate to disk, using decent permissions
err = ioutils.AtomicWriteFile(paths.RootCA.Cert, apiRootCA.CACert, 0o644)
err = atomicwriter.WriteFile(paths.RootCA.Cert, apiRootCA.CACert, 0o644)
if t != nil {
require.NoError(t, err)
}
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/gogo/protobuf v1.3.2
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
github.com/hashicorp/go-memdb v1.3.2
github.com/moby/sys/atomicwriter v0.1.0
github.com/onsi/ginkgo v1.16.5
github.com/onsi/gomega v1.27.6
github.com/opencontainers/go-digest v1.0.0
Expand Down Expand Up @@ -68,6 +69,7 @@ require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jmoiron/sqlx v1.3.3 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/moby/sys/sequential v0.7.0 // indirect

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This dependency can be dropped once we stop supporting < go1.26

github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/nxadm/tail v1.4.8 // indirect
github.com/pelletier/go-toml v1.8.1 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/moby/sys/atomicwriter v0.1.0 h1:kw5D/EqkBwsBFi0ss9v1VG3wIkVhzGvLklJ+w3A14Sw=
github.com/moby/sys/atomicwriter v0.1.0/go.mod h1:Ul8oqv2ZMNHOceF643P6FKPXeCmYtlQMvpizfsSoaWs=
github.com/moby/sys/sequential v0.7.0 h1:ASQNGNROJSuOO6LL6bPHbKvuZu6NU8P4ldPWk31zj/8=
github.com/moby/sys/sequential v0.7.0/go.mod h1:NfSTAp6V3fw4tmkD62PEcOKeZKquXT8VKCkf7aVR79o=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
Expand Down
37 changes: 7 additions & 30 deletions ioutils/ioutils.go
Original file line number Diff line number Diff line change
@@ -1,39 +1,16 @@
package ioutils

import (
"io"
"os"
"path/filepath"
)

// todo: split docker/pkg/ioutils into a separate repo
"github.com/moby/sys/atomicwriter"
)

// AtomicWriteFile atomically writes data to a file specified by filename.
//
// Deprecated: use [atomicwriter.WriteFile].
//
//go:fix inline
func AtomicWriteFile(filename string, data []byte, perm os.FileMode) error {
f, err := os.CreateTemp(filepath.Dir(filename), ".tmp-"+filepath.Base(filename))
if err != nil {
return err
}
err = os.Chmod(f.Name(), perm)
if err != nil {
f.Close()
return err
}
n, err := f.Write(data)
if err == nil && n < len(data) {
f.Close()
return io.ErrShortWrite
}
if err != nil {
f.Close()
return err
}
if err := f.Sync(); err != nil {
f.Close()
return err
}
if err := f.Close(); err != nil {
return err
}
return os.Rename(f.Name(), filename)
return atomicwriter.WriteFile(filename, data, perm)
}
25 changes: 0 additions & 25 deletions ioutils/ioutils_test.go

This file was deleted.

4 changes: 2 additions & 2 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ import (
"github.com/moby/swarmkit/v2/ca/keyutils"
"github.com/moby/swarmkit/v2/connectionbroker"
"github.com/moby/swarmkit/v2/identity"
"github.com/moby/swarmkit/v2/ioutils"
"github.com/moby/swarmkit/v2/log"
"github.com/moby/swarmkit/v2/manager"
"github.com/moby/swarmkit/v2/manager/allocator/networkallocator"
"github.com/moby/swarmkit/v2/manager/encryption"
"github.com/moby/swarmkit/v2/node/plugin"
"github.com/moby/swarmkit/v2/remotes"
"github.com/moby/swarmkit/v2/xnet"
"github.com/moby/sys/atomicwriter"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
bolt "go.etcd.io/bbolt"
Expand Down Expand Up @@ -1246,7 +1246,7 @@ func (s *persistentRemotes) save() error {
return err
}
s.lastSavedState = remotes
return ioutils.AtomicWriteFile(s.storePath, dt, 0o600)
return atomicwriter.WriteFile(s.storePath, dt, 0o600)
}

// WaitSelect waits until at least one remote becomes available and then selects one.
Expand Down
Loading