-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from duplocloud/release/0.4.0
Release v0.4.0
- Loading branch information
Showing
5 changed files
with
126 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io" | ||
"net" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/skratchdot/open-golang/open" | ||
) | ||
|
||
type tokenResult struct { | ||
token string | ||
err error | ||
} | ||
|
||
func tokenViaPost(baseUrl string, timeout time.Duration) (string, error) { | ||
|
||
// Create the listener on a random port. | ||
listener, err := net.Listen("tcp", "127.0.0.1:0") | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
// Get the port being listened to. | ||
localPort := listener.Addr().(*net.TCPAddr).Port | ||
|
||
// Run the HTTP server on localhost. | ||
done := make(chan tokenResult) | ||
go func() { | ||
mux := http.NewServeMux() | ||
|
||
mux.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) { | ||
var bytes []byte | ||
var err error | ||
completed := false | ||
status := "ok" | ||
|
||
// Only allow the specified Duplo to give us creds. | ||
res.Header().Add("Access-Control-Allow-Origin", baseUrl) | ||
|
||
// A POST means we are done, whether good or bad. | ||
if req.Method == "POST" { | ||
defer req.Body.Close() | ||
|
||
completed = true | ||
|
||
// Authorize the origin, and get the POST body. | ||
origin := req.Header.Get("Origin") | ||
if origin != baseUrl { | ||
err = fmt.Errorf("unauthorized origin: %s", origin) | ||
} else { | ||
bytes, err = io.ReadAll(req.Body) | ||
} | ||
} | ||
|
||
// Send the proper response. | ||
if completed { | ||
if err != nil { | ||
res.WriteHeader(500) | ||
status = "failed" | ||
} else { | ||
status = "done" | ||
if len(bytes) == 0 { | ||
err = errors.New("canceled") | ||
} | ||
} | ||
} | ||
_, _ = fmt.Fprintf(res, "\"%s\"\n", status) | ||
|
||
// If we are done, send the result to the channel. | ||
if completed { | ||
done <- tokenResult{token: string(bytes), err: err} | ||
} | ||
}) | ||
_ = http.Serve(listener, mux) | ||
}() | ||
|
||
// Open the browser. | ||
url := fmt.Sprintf("%s/app/user/verify-token?localAppName=duplo-aws-credential-process&localPort=%d", baseUrl, localPort) | ||
err = open.Run(url) | ||
dieIf(err, "failed to open interactive browser session") | ||
|
||
// Wait for the token result, and return it. | ||
select { | ||
case tokenResult := <-done: | ||
return tokenResult.token, tokenResult.err | ||
case <-time.After(timeout): | ||
return "", errors.New("timed out") | ||
} | ||
} | ||
|
||
func mustTokenInteractive(host string) string { | ||
token, err := tokenViaPost(host, 180*time.Second) | ||
dieIf(err, "failed to get token from interactive browser session") | ||
|
||
return token | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
module github.com/duplocloud/duplo-aws-jit | ||
|
||
go 1.16 | ||
|
||
require github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 h1:JIAuq3EEf9cgbU6AtGPK4CTG3Zf6CKMNqf0MHTggAUA= | ||
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966/go.mod h1:sUM3LWHvSMaG192sy56D9F7CNvL7jUJVXoqM1QKLnog= |