Skip to content

Commit

Permalink
Merge pull request #41 from crytic/username-config
Browse files Browse the repository at this point in the history
Username config
  • Loading branch information
bohendo authored Mar 15, 2024
2 parents 9d3aa08 + 801e379 commit 5604787
Show file tree
Hide file tree
Showing 5 changed files with 243 additions and 235 deletions.
17 changes: 14 additions & 3 deletions cmd/cloudexec/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,42 @@ import (
"fmt"
"os"
"os/exec"
"os/user"
"strings"

"github.com/crytic/cloudexec/pkg/config"
"golang.org/x/term"
)

func Configure() error {
user, err := user.Current()
if err != nil {
fmt.Printf("Failed to get current user: %v", err)
os.Exit(1)
}
username, err := promptUserInput("Username", user.Username)
if err != nil {
return err
}
spacesRegion, err := promptUserInput("Digital Ocean Spaces region", "nyc3")
if err != nil {
return err
}
apiKey, err := promptSecretInput("Digital Ocean API key or reference", "op://private/DigitalOcean/api token")
apiKey, err := promptSecretInput("Digital Ocean API key or reference", "op://Private/DigitalOcean/ApiKey")
if err != nil {
return err
}
spacesAccessKey, err := promptSecretInput("Digital Ocean Spaces access key ID or reference", "op://private/DigitalOcean/spaces access key id")
spacesAccessKey, err := promptSecretInput("Digital Ocean Spaces access key ID or reference", "op://Private/DigitalOcean/SpacesKeyID")
if err != nil {
return err
}
spacesSecretKey, err := promptSecretInput("Digital Ocean Spaces secret access key or reference", "op://private/DigitalOcean/spaces secret access key")
spacesSecretKey, err := promptSecretInput("Digital Ocean Spaces secret access key or reference", "op://Private/DigitalOcean/SpacesSecret")
if err != nil {
return err
}

configValues := config.Config{
Username: username,
DigitalOcean: struct {
ApiKey string `toml:"apiKey"`
SpacesAccessKey string `toml:"spacesAccessKey"`
Expand Down
18 changes: 6 additions & 12 deletions cmd/cloudexec/launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package main
import (
"fmt"
"os"
"os/user"
"path/filepath"
"strings"
"time"

"github.com/BurntSushi/toml"
Expand Down Expand Up @@ -85,8 +83,8 @@ func LoadLaunchConfig(launchConfigPath string) (LaunchConfig, error) {
return lc, nil
}

func Launch(user *user.User, config config.Config, dropletSize string, dropletRegion string, lc LaunchConfig) error {
username := user.Username
func Launch(config config.Config, dropletSize string, dropletRegion string, lc LaunchConfig) error {
username := config.Username
bucketName := fmt.Sprintf("cloudexec-%s", username)

// get existing state from bucket
Expand Down Expand Up @@ -134,7 +132,7 @@ func Launch(user *user.User, config config.Config, dropletSize string, dropletRe

// Get or create an SSH key
fmt.Println("Getting or creating SSH key pair...")
publicKey, err := ssh.GetOrCreateSSHKeyPair(user)
publicKey, err := ssh.GetOrCreateSSHKeyPair()
if err != nil {
return fmt.Errorf("Failed to get or creating SSH key pair: %w", err)
}
Expand Down Expand Up @@ -171,23 +169,19 @@ func Launch(user *user.User, config config.Config, dropletSize string, dropletRe

// Add the droplet to the SSH config file
fmt.Println("Deleting old cloudexec instance from SSH config file...")
err = ssh.DeleteSSHConfig(user, "cloudexec")
err = ssh.DeleteSSHConfig("cloudexec")
if err != nil {
return fmt.Errorf("Failed to delete old cloudexec entry from SSH config file: %w", err)
}
fmt.Println("Adding droplet to SSH config file...")
err = ssh.AddSSHConfig(user, droplet.IP)
err = ssh.AddSSHConfig(droplet.IP)
if err != nil {
return fmt.Errorf("Failed to add droplet to SSH config file: %w", err)
}

// Ensure we can SSH into the droplet
fmt.Println("Ensuring we can SSH into the droplet...")
// sshConfigName := fmt.Sprintf("cloudexec-%v", dropletIp)
sshConfigName := "cloudexec"
sshConfigName = strings.ReplaceAll(sshConfigName, ".", "-")
sshConfigPath := filepath.Join(user.HomeDir, ".ssh", "config.d", sshConfigName)
err = ssh.WaitForSSHConnection(sshConfigPath)
err = ssh.WaitForSSHConnection()
if err != nil {
return fmt.Errorf("Failed to SSH into the droplet: %w", err)
}
Expand Down
Loading

0 comments on commit 5604787

Please sign in to comment.