Skip to content

Commit

Permalink
Cleanup temporary certificate files after build
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobwgillespie committed Aug 14, 2024
1 parent 37c4635 commit 972c379
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cmd/buildctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"

"github.com/depot/cli/internal/build"
"github.com/depot/cli/pkg/cleanup"
"github.com/depot/cli/pkg/cmd/buildctl"
"github.com/getsentry/sentry-go"
)
Expand All @@ -26,6 +27,8 @@ func runMain() int {
}
}

defer cleanup.CleanupTmpfiles()

err := buildctl.NewBuildctl().Execute()
if err != nil {
return 1
Expand Down
3 changes: 3 additions & 0 deletions cmd/depot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/depot/cli/internal/build"
"github.com/depot/cli/internal/update"
"github.com/depot/cli/pkg/api"
"github.com/depot/cli/pkg/cleanup"
"github.com/depot/cli/pkg/cmd/root"
"github.com/depot/cli/pkg/config"
"github.com/depot/cli/pkg/helpers"
Expand Down Expand Up @@ -82,6 +83,8 @@ func runMain() int {
}
}

defer cleanup.CleanupTmpfiles()

buildVersion := build.Version
buildDate := build.Date

Expand Down
15 changes: 15 additions & 0 deletions pkg/cleanup/tmpfiles.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cleanup

import "os"

var tmpfiles = []string{}

func RegisterTmpfile(filename string) {
tmpfiles = append(tmpfiles, filename)
}

func CleanupTmpfiles() {
for _, filename := range tmpfiles {
_ = os.Remove(filename)
}
}
4 changes: 4 additions & 0 deletions pkg/machine/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"connectrpc.com/connect"
"github.com/depot/cli/pkg/api"
"github.com/depot/cli/pkg/cleanup"
cliv1 "github.com/depot/cli/pkg/proto/depot/cli/v1"
"github.com/depot/cli/pkg/proto/depot/cli/v1/cliv1connect"
"github.com/moby/buildkit/client"
Expand Down Expand Up @@ -180,6 +181,7 @@ func (m *Machine) Client(ctx context.Context) (*client.Client, error) {
return nil, errors.Wrap(err, "failed to write cert to temp file")
}
cert := file.Name()
cleanup.RegisterTmpfile(cert)

file, err = os.CreateTemp("", "depot-key")
if err != nil {
Expand All @@ -191,6 +193,7 @@ func (m *Machine) Client(ctx context.Context) (*client.Client, error) {
return nil, errors.Wrap(err, "failed to write key to temp file")
}
key := file.Name()
cleanup.RegisterTmpfile(key)

file, err = os.CreateTemp("", "depot-ca-cert")
if err != nil {
Expand All @@ -202,6 +205,7 @@ func (m *Machine) Client(ctx context.Context) (*client.Client, error) {
return nil, errors.Wrap(err, "failed to write CA cert to temp file")
}
caCert := file.Name()
cleanup.RegisterTmpfile(caCert)

opts = append(opts, client.WithCredentials(m.ServerName, caCert, cert, key))
}
Expand Down

0 comments on commit 972c379

Please sign in to comment.