Skip to content

feat: Add "coder projects create" command #246

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 21 commits into from
Feb 12, 2022
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
Fix linting errors
  • Loading branch information
kylecarbs committed Feb 11, 2022
commit c493bf9da0a80112ca4a79a3991c97b9c95624aa
1 change: 1 addition & 0 deletions cli/clitest/clitest.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func extractTar(data []byte, directory string) error {
if err != nil {
return xerrors.Errorf("read project source archive: %w", err)
}
// #nosec
path := filepath.Join(directory, header.Name)
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if we should have a check here to sanitize the header.Name - making sure we can't specify paths like .. that might move up the filesystem. There might be a vulnerability where you could construct a tar that has a header with improper paths (like ../../) - and let you write a file over an existing file.

This would be bad, for example, if a malicious actor could write over a critical config file, a binary (ie, they overwrite our provisionerd with a malicious version that uploads secrets).

Kind of a similar to the example called out here: securego/gosec#439 (comment)

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, and we handle this from provisionerd. But I didn't feel it was necessary for clitest.

We should abstract the tar/untar logic out so it's in one place, then add tests for that specific functionality.

mode := header.FileInfo().Mode()
if mode == 0 {
Expand Down
3 changes: 2 additions & 1 deletion cli/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package cli_test
import (
"testing"

"github.com/stretchr/testify/require"

"github.com/coder/coder/cli/clitest"
"github.com/coder/coder/coderd/coderdtest"
"github.com/stretchr/testify/require"

"github.com/ActiveState/termtest/expect"
)
Expand Down
16 changes: 10 additions & 6 deletions cli/projectcreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,25 +112,29 @@ func projectCreate() *cobra.Command {
cmd.Flags().StringVarP(&directory, "directory", "d", currentDirectory, "Specify the directory to create from")
cmd.Flags().StringVarP(&provisioner, "provisioner", "p", "terraform", "Customize the provisioner backend")
// This is for testing! There's only 1 provisioner type right now.
cmd.Flags().MarkHidden("provisioner")

err := cmd.Flags().MarkHidden("provisioner")
if err != nil {
panic(err)
}
return cmd
}

func validateProjectVersionSource(cmd *cobra.Command, client *codersdk.Client, organization coderd.Organization, provisioner database.ProvisionerType, directory string, parameters ...coderd.CreateParameterValueRequest) (*coderd.ProvisionerJob, error) {
spin := spinner.New(spinner.CharSets[5], 100*time.Millisecond)
spin.Writer = cmd.OutOrStdout()
spin.Suffix = " Uploading current directory..."
spin.Color("fgHiGreen")
err := spin.Color("fgHiGreen")
if err != nil {
return nil, err
}
spin.Start()
defer spin.Stop()

bytes, err := tarDirectory(directory)
tarData, err := tarDirectory(directory)
if err != nil {
return nil, err
}

resp, err := client.UploadFile(cmd.Context(), codersdk.ContentTypeTar, bytes)
resp, err := client.UploadFile(cmd.Context(), codersdk.ContentTypeTar, tarData)
if err != nil {
return nil, err
}
Expand Down
7 changes: 4 additions & 3 deletions cli/projectcreate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"testing"

"github.com/ActiveState/termtest/expect"
"github.com/stretchr/testify/require"

"github.com/coder/coder/cli/clitest"
"github.com/coder/coder/coderd/coderdtest"
"github.com/coder/coder/database"
"github.com/coder/coder/provisioner/echo"
"github.com/coder/coder/provisionersdk/proto"
"github.com/stretchr/testify/require"
)

func TestProjectCreate(t *testing.T) {
Expand All @@ -19,13 +20,13 @@ func TestProjectCreate(t *testing.T) {
console, err := expect.NewConsole(expect.WithStdout(clitest.StdoutLogs(t)))
require.NoError(t, err)
client := coderdtest.New(t)
_ = coderdtest.NewProvisionerDaemon(t, client)
source := clitest.CreateProjectVersionSource(t, &echo.Responses{
Parse: echo.ParseComplete,
Provision: echo.ProvisionComplete,
})
cmd, root := clitest.New(t, "projects", "create", "--directory", source, "--provisioner", string(database.ProvisionerTypeEcho))
_ = clitest.CreateInitialUser(t, client, root)
_ = coderdtest.NewProvisionerDaemon(t, client)
cmd.SetIn(console.Tty())
cmd.SetOut(console.Tty())
closeChan := make(chan struct{})
Expand Down Expand Up @@ -57,7 +58,6 @@ func TestProjectCreate(t *testing.T) {
console, err := expect.NewConsole(expect.WithStdout(clitest.StdoutLogs(t)))
require.NoError(t, err)
client := coderdtest.New(t)
_ = coderdtest.NewProvisionerDaemon(t, client)
source := clitest.CreateProjectVersionSource(t, &echo.Responses{
Parse: []*proto.Parse_Response{{
Type: &proto.Parse_Response_Complete{
Expand All @@ -75,6 +75,7 @@ func TestProjectCreate(t *testing.T) {
})
cmd, root := clitest.New(t, "projects", "create", "--directory", source, "--provisioner", string(database.ProvisionerTypeEcho))
_ = clitest.CreateInitialUser(t, client, root)
_ = coderdtest.NewProvisionerDaemon(t, client)
cmd.SetIn(console.Tty())
cmd.SetOut(console.Tty())
closeChan := make(chan struct{})
Expand Down
5 changes: 3 additions & 2 deletions cli/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"fmt"
"strings"

"github.com/coder/coder/coderd"
"github.com/coder/coder/database"
"github.com/fatih/color"
"github.com/spf13/cobra"
"github.com/xlab/treeprint"
"golang.org/x/xerrors"

"github.com/coder/coder/coderd"
"github.com/coder/coder/database"
)

func projects() *cobra.Command {
Expand Down
13 changes: 7 additions & 6 deletions cli/workspacecreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import (
"fmt"
"time"

"github.com/coder/coder/coderd"
"github.com/coder/coder/database"
"github.com/fatih/color"
"github.com/google/uuid"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
"golang.org/x/xerrors"

"github.com/coder/coder/coderd"
"github.com/coder/coder/database"
)

func workspaceCreate() *cobra.Command {
Expand Down Expand Up @@ -108,17 +109,17 @@ func workspaceCreate() *cobra.Command {
if err != nil {
return err
}
logBuffer := make([]coderd.ProvisionerJobLog, 0, 64)
// logBuffer := make([]coderd.ProvisionerJobLog, 0, 64)
for {
log, ok := <-logs
if !ok {
break
}
fmt.Printf("Logging: %s\n", log.Output)
logBuffer = append(logBuffer, log)
_, _ = fmt.Printf("Logging: %s\n", log.Output)
// logBuffer = append(logBuffer, log)
}

fmt.Printf("Create workspace! %s\n", name)
_, _ = fmt.Printf("Create workspace! %s\n", name)

return nil
},
Expand Down
10 changes: 5 additions & 5 deletions coderd/provisionerdaemons.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,8 @@ func (server *provisionerdServer) AcquireJob(ctx context.Context, _ *proto.Empty
}
// Convert parameters to the protobuf type.
protoParameters := make([]*sdkproto.ParameterValue, 0, len(parameters))
for _, parameter := range parameters {
converted, err := convertComputedParameterValue(parameter)
for _, computedParameter := range parameters {
converted, err := convertComputedParameterValue(computedParameter)
if err != nil {
return nil, failJob(fmt.Sprintf("convert parameter: %s", err))
}
Expand Down Expand Up @@ -371,8 +371,8 @@ func (server *provisionerdServer) UpdateJob(ctx context.Context, request *proto.
}
// Convert parameters to the protobuf type.
protoParameters := make([]*sdkproto.ParameterValue, 0, len(parameters))
for _, parameter := range parameters {
converted, err := convertComputedParameterValue(parameter)
for _, computedParameter := range parameters {
converted, err := convertComputedParameterValue(computedParameter)
if err != nil {
return nil, xerrors.Errorf("convert parameter: %s", err)
}
Expand Down Expand Up @@ -597,7 +597,7 @@ func convertLogSource(logSource proto.LogSource) (database.LogSource, error) {
}

func convertComputedParameterValue(param parameter.ComputedValue) (*sdkproto.ParameterValue, error) {
scheme := sdkproto.ParameterDestination_ENVIRONMENT_VARIABLE
var scheme sdkproto.ParameterDestination_Scheme
switch param.DestinationScheme {
case database.ParameterDestinationSchemeEnvironmentVariable:
scheme = sdkproto.ParameterDestination_ENVIRONMENT_VARIABLE
Expand Down
7 changes: 0 additions & 7 deletions codersdk/provisioners_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,3 @@ func TestFollowProvisionerJobLogsAfter(t *testing.T) {
require.False(t, ok)
})
}

func TestProvisionerJobParameterSchemas(t *testing.T) {
t.Parallel()
t.Run("Error", func(t *testing.T) {

})
}
4 changes: 2 additions & 2 deletions database/databasefake/databasefake.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ func (q *fakeQuerier) GetProjectByOrganizationAndName(_ context.Context, arg dat
return database.Project{}, sql.ErrNoRows
}

func (q *fakeQuerier) GetProjectImportJobResourcesByJobID(ctx context.Context, jobID uuid.UUID) ([]database.ProjectImportJobResource, error) {
func (q *fakeQuerier) GetProjectImportJobResourcesByJobID(_ context.Context, jobID uuid.UUID) ([]database.ProjectImportJobResource, error) {
q.mutex.Lock()
defer q.mutex.Unlock()

Expand Down Expand Up @@ -685,7 +685,7 @@ func (q *fakeQuerier) InsertProject(_ context.Context, arg database.InsertProjec
return project, nil
}

func (q *fakeQuerier) InsertProjectImportJobResource(ctx context.Context, arg database.InsertProjectImportJobResourceParams) (database.ProjectImportJobResource, error) {
func (q *fakeQuerier) InsertProjectImportJobResource(_ context.Context, arg database.InsertProjectImportJobResourceParams) (database.ProjectImportJobResource, error) {
q.mutex.Lock()
defer q.mutex.Unlock()

Expand Down