Skip to content

Commit

Permalink
copy enclaveKey from tmp Dir without using os.Rename
Browse files Browse the repository at this point in the history
Signed-off-by: Toshihiko Okubo <[email protected]>
  • Loading branch information
toshihiko-okubo committed Oct 5, 2023
1 parent f908d1d commit 9d59649
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
29 changes: 20 additions & 9 deletions relay/lcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"log"
"os"
"path/filepath"
Expand All @@ -13,13 +14,14 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
ibcexported "github.com/cosmos/ibc-go/v7/modules/core/exported"
mapset "github.com/deckarep/golang-set/v2"
"github.com/hyperledger-labs/yui-relayer/core"
oias "github.com/oasisprotocol/oasis-core/go/common/sgx/ias"

lcptypes "github.com/datachainlab/lcp-go/light-clients/lcp/types"
"github.com/datachainlab/lcp-go/relay/elc"
"github.com/datachainlab/lcp-go/relay/enclave"
"github.com/datachainlab/lcp-go/sgx/ias"
mapset "github.com/deckarep/golang-set/v2"
"github.com/hyperledger-labs/yui-relayer/core"
oias "github.com/oasisprotocol/oasis-core/go/common/sgx/ias"
)

const lastEnclaveKeyInfoFile = "last_eki"
Expand All @@ -46,26 +48,35 @@ func (pr *Prover) loadLastEnclaveKey(ctx context.Context) (*enclave.EnclaveKeyIn
}

func (pr *Prover) saveLastEnclaveKey(ctx context.Context, eki *enclave.EnclaveKeyInfo) error {
path, err := pr.lastEnclaveKeyInfoFilePath()
src, err := os.CreateTemp(os.TempDir(), lastEnclaveKeyInfoFile)
if err != nil {
return err
}
f, err := os.CreateTemp(os.TempDir(), lastEnclaveKeyInfoFile)
defer src.Close()

bz, err := json.Marshal(eki)
if err != nil {
return err
}
defer f.Close()

bz, err := json.Marshal(eki)
if err = os.WriteFile(src.Name(), bz, 0600); err != nil {
return err
}

path, err := pr.lastEnclaveKeyInfoFilePath()
if err != nil {
return err
}
if _, err := f.Write(bz); err != nil {
dst, err := os.Create(path)
if err != nil {
return err
}
if err := os.Rename(f.Name(), path); err != nil {
defer dst.Close()

if _, err = io.Copy(dst, src); err != nil {
return err
}

return nil
}

Expand Down
4 changes: 2 additions & 2 deletions simapp/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ require (
github.com/petermattis/goid v0.0.0-20230317030725-371a4b8eda08 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.14.0 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/client_golang v1.15.1 // indirect
github.com/prometheus/client_model v0.4.0 // indirect
github.com/prometheus/common v0.42.0 // indirect
github.com/prometheus/procfs v0.9.0 // indirect
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
Expand Down
2 changes: 2 additions & 0 deletions simapp/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,7 @@ github.com/prometheus/client_golang v1.4.0/go.mod h1:e9GMxYsXl05ICDXkRhurwBS4Q3O
github.com/prometheus/client_golang v1.7.1/go.mod h1:PY5Wy2awLA44sXw4AOSfFBetzPP4j5+D6mVACh+pe2M=
github.com/prometheus/client_golang v1.14.0 h1:nJdhIvne2eSX/XRAFV9PcvFFRbrjbcTUj0VP62TMhnw=
github.com/prometheus/client_golang v1.14.0/go.mod h1:8vpkKitgIVNcqrRBWh1C4TIUQgYNtG/XQE4E/Zae36Y=
github.com/prometheus/client_golang v1.15.1/go.mod h1:e9yaBhRPU2pPNsZwE+JdQl0KEt1N9XgF6zxWmaC0xOk=
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
github.com/prometheus/client_model v0.0.0-20190115171406-56726106282f/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
Expand All @@ -834,6 +835,7 @@ github.com/prometheus/client_model v0.1.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6T
github.com/prometheus/client_model v0.2.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/client_model v0.3.0 h1:UBgGFHqYdG/TPFD1B1ogZywDqEkwp3fBMvqdiQ7Xew4=
github.com/prometheus/client_model v0.3.0/go.mod h1:LDGWKZIo7rky3hgvBe+caln+Dr3dPggB5dvjtD7w9+w=
github.com/prometheus/client_model v0.4.0/go.mod h1:oMQmHW1/JoDwqLtg57MGgP/Fb1CJEYF2imWWhWtMkYU=
github.com/prometheus/common v0.2.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4=
github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4=
github.com/prometheus/common v0.7.0/go.mod h1:DjGbpBbp5NYNiECxcL/VnbXCCaQpKd3tt26CguLLsqA=
Expand Down

0 comments on commit 9d59649

Please sign in to comment.