Skip to content

refactor: change template archive extraction to be on provisioner #9264

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

Merged
merged 17 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
new gen; use uuid for session workdir
Signed-off-by: Spike Curtis <spike@coder.com>
  • Loading branch information
spikecurtis committed Aug 24, 2023
commit ef1820fee440296d530aaa10c3ababb5c27d47e6
4 changes: 2 additions & 2 deletions provisionerd/proto/provisionerd.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion provisionerd/proto/provisionerd_drpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 16 additions & 16 deletions provisionersdk/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
"strings"
"time"

"cdr.dev/slog"

"github.com/google/uuid"
"golang.org/x/xerrors"

"cdr.dev/slog"
"github.com/coder/coder/v2/provisionersdk/proto"
)

Expand All @@ -29,22 +29,15 @@ type protoServer struct {
}

func (p *protoServer) Session(stream proto.DRPCProvisioner_SessionStream) error {
sessID := uuid.New().String()
s := &Session{
Logger: p.opts.Logger,
Logger: p.opts.Logger.With(slog.F("session_id", sessID)),
stream: stream,
server: p.server,
}
sessDir := fmt.Sprintf("Session%p", s)
sessDir := fmt.Sprintf("Session%s", sessID)
s.WorkDirectory = filepath.Join(p.opts.WorkDirectory, sessDir)
// the WorkDirectory shouldn't exist, but it's possible it does from a restarted process or failed cleanup
err := os.RemoveAll(s.WorkDirectory)
if err != nil {
// RemoveAll returns nil if (as expected) the path doesn't exist. So, if we hit an error, the WorkDirectory
// exists AND we failed to clean it up.
s.Logger.Error(s.Context(), "failed to pre-clean work directory", slog.Error(err))
return xerrors.Errorf("failed to pre-clean work directory: %w", err)
}
err = os.MkdirAll(s.WorkDirectory, 0o700)
err := os.MkdirAll(s.WorkDirectory, 0o700)
Copy link
Member

Choose a reason for hiding this comment

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

Thinking loud: what are the chances that s.WorkDirectory exists, and contains a malicious/old/buggy .tf file, which will be included in the terraform provisioning process?

I'm asking as os.MkdirAll doesn't complain that the directory exists.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I guess there is some chance of this from a restarted process, or failed cleanup. I'll add a call to os.RemoveAll() before.

if err != nil {
return xerrors.Errorf("create work directory %q: %w", s.WorkDirectory, err)
}
Expand Down Expand Up @@ -88,7 +81,7 @@ func (p *protoServer) Session(stream proto.DRPCProvisioner_SessionStream) error
return s.handleRequests()
}

func (s *Session) requestReader() <-chan *proto.Request {
func (s *Session) requestReader(done <-chan struct{}) <-chan *proto.Request {
ch := make(chan *proto.Request)
go func() {
defer close(ch)
Expand All @@ -98,14 +91,21 @@ func (s *Session) requestReader() <-chan *proto.Request {
s.Logger.Info(s.Context(), "recv done on Session", slog.Error(err))
return
}
ch <- req
select {
case ch <- req:
continue
case <-done:
return
}
}
}()
return ch
}

func (s *Session) handleRequests() error {
requests := s.requestReader()
done := make(chan struct{})
defer close(done)
requests := s.requestReader(done)
planned := false
for req := range requests {
if req.GetCancel() != nil {
Expand Down