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

Metadata server running on windows #174

Merged
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
18 changes: 16 additions & 2 deletions server/alias_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,22 @@

package server

import "fmt"
import (
"fmt"
"os/exec"
"strings"
)

func installNetworkAlias() ([]byte, error) {
return make([]byte, 0), fmt.Errorf("Server mode is unsupported on windows.")
out, err := exec.Command("netsh", "interface", "ipv4", "add", "address", "Loopback Pseudo-Interface 1", "169.254.169.254", "255.255.0.0").CombinedOutput()

if err == nil || strings.Contains(string(out), "The object already exists") {
return []byte{}, nil
}

if strings.Contains(string(out), "Run as administrator") {
fmt.Println("Creation of network alias for server mode requires elevated permissions (Run as administrator).")
}

return out, err
}
41 changes: 35 additions & 6 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package server

import (
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net"
"net/http"
"os"
"os/exec"
"runtime"
"time"

"github.com/99designs/aws-vault/vault"
Expand Down Expand Up @@ -74,14 +76,41 @@ func checkServerRunning(bind string) bool {
return err == nil
}

func StartCredentialProxyOnWindows() error {
log.Printf("Starting `aws-vault server` in the background")
cmd := exec.Command(os.Args[0], "server")
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Start(); err != nil {
return err
}
time.Sleep(time.Second * 1)
if !checkServerRunning(metadataBind) {
return errors.New("The credential proxy server isn't running. Run aws-vault server as Administrator in the background and then try this command again")
}
return nil
}

func StartCredentialProxyWithSudo() error {
log.Printf("Starting `aws-vault server` as root in the background")
cmd := exec.Command("sudo", "-b", os.Args[0], "server")
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}

func StartCredentialProxy() error {
if runtime.GOOS == "windows" {
return StartCredentialProxyOnWindows()
}
return StartCredentialProxyWithSudo()
Copy link
Member

Choose a reason for hiding this comment

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

@williamdenton Actually another way to do this is put the arch specific functions into conditional build files, rather than using the runtime check. What's preferred @lox ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

It gets complicated, let's stick to this switch. LGTM.

}

func StartCredentialsServer(creds *vault.VaultCredentials) error {
if !checkServerRunning(metadataBind) {
log.Printf("Starting `aws-vault server` as root in the background")
cmd := exec.Command("sudo", "-b", os.Args[0], "server")
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
if err := StartCredentialProxy(); err != nil {
return err
}
}
Expand Down