Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup temporary certificate files after build #297

Merged
merged 1 commit into from
Aug 14, 2024
Merged
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
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
Loading