mirror of
https://github.com/docker/compose.git
synced 2025-07-22 21:24:38 +02:00
Separate azure login bits in LocalServer + Helper (mocked part)
This commit is contained in:
parent
7edc6659a2
commit
7cf2309ca6
@ -4,17 +4,13 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"log"
|
"log"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net"
|
|
||||||
"net/http"
|
|
||||||
"net/url"
|
"net/url"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/docker/api/errdefs"
|
"github.com/docker/api/errdefs"
|
||||||
@ -70,11 +66,6 @@ type AzureLoginService struct {
|
|||||||
|
|
||||||
const tokenStoreFilename = "dockerAccessToken.json"
|
const tokenStoreFilename = "dockerAccessToken.json"
|
||||||
|
|
||||||
func getTokenStorePath() string {
|
|
||||||
cliPath, _ := cli.AccessTokensPath()
|
|
||||||
return filepath.Join(filepath.Dir(cliPath), tokenStoreFilename)
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewAzureLoginService creates a NewAzureLoginService
|
// NewAzureLoginService creates a NewAzureLoginService
|
||||||
func NewAzureLoginService() (AzureLoginService, error) {
|
func NewAzureLoginService() (AzureLoginService, error) {
|
||||||
return newAzureLoginServiceFromPath(getTokenStorePath(), azureAPIHelper{})
|
return newAzureLoginServiceFromPath(getTokenStorePath(), azureAPIHelper{})
|
||||||
@ -91,14 +82,6 @@ func newAzureLoginServiceFromPath(tokenStorePath string, helper apiHelper) (Azur
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type apiHelper interface {
|
|
||||||
queryToken(data url.Values, tenantID string) (azureToken, error)
|
|
||||||
openAzureLoginPage(redirectURL string)
|
|
||||||
queryAuthorizationAPI(authorizationURL string, authorizationHeader string) ([]byte, int, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
type azureAPIHelper struct{}
|
|
||||||
|
|
||||||
//Login perform azure login through browser
|
//Login perform azure login through browser
|
||||||
func (login AzureLoginService) Login(ctx context.Context) error {
|
func (login AzureLoginService) Login(ctx context.Context) error {
|
||||||
queryCh := make(chan url.Values, 1)
|
queryCh := make(chan url.Values, 1)
|
||||||
@ -165,92 +148,9 @@ func (login AzureLoginService) Login(ctx context.Context) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func startLoginServer(queryCh chan url.Values) (int, error) {
|
func getTokenStorePath() string {
|
||||||
mux := http.NewServeMux()
|
cliPath, _ := cli.AccessTokensPath()
|
||||||
mux.HandleFunc("/", queryHandler(queryCh))
|
return filepath.Join(filepath.Dir(cliPath), tokenStoreFilename)
|
||||||
listener, err := net.Listen("tcp", ":0")
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
|
|
||||||
availablePort := listener.Addr().(*net.TCPAddr).Port
|
|
||||||
server := &http.Server{Handler: mux}
|
|
||||||
go func() {
|
|
||||||
if err := server.Serve(listener); err != nil {
|
|
||||||
queryCh <- url.Values{
|
|
||||||
"error": []string{fmt.Sprintf("error starting http server with: %v", err)},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
return availablePort, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (helper azureAPIHelper) openAzureLoginPage(redirectURL string) {
|
|
||||||
state := randomString("", 10)
|
|
||||||
authURL := fmt.Sprintf(authorizeFormat, clientID, redirectURL, state, scopes)
|
|
||||||
openbrowser(authURL)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (helper azureAPIHelper) queryAuthorizationAPI(authorizationURL string, authorizationHeader string) ([]byte, int, error) {
|
|
||||||
req, err := http.NewRequest(http.MethodGet, authorizationURL, nil)
|
|
||||||
if err != nil {
|
|
||||||
return nil, 0, err
|
|
||||||
}
|
|
||||||
req.Header.Add("Authorization", authorizationHeader)
|
|
||||||
res, err := http.DefaultClient.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return nil, 0, err
|
|
||||||
}
|
|
||||||
bits, err := ioutil.ReadAll(res.Body)
|
|
||||||
if err != nil {
|
|
||||||
return nil, 0, err
|
|
||||||
}
|
|
||||||
return bits, res.StatusCode, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func queryHandler(queryCh chan url.Values) func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
queryHandler := func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
_, hasCode := r.URL.Query()["code"]
|
|
||||||
if hasCode {
|
|
||||||
_, err := w.Write([]byte(successfullLoginHTML))
|
|
||||||
if err != nil {
|
|
||||||
queryCh <- url.Values{
|
|
||||||
"error": []string{err.Error()},
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
queryCh <- r.URL.Query()
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
_, err := w.Write([]byte(loginFailedHTML))
|
|
||||||
if err != nil {
|
|
||||||
queryCh <- url.Values{
|
|
||||||
"error": []string{err.Error()},
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
queryCh <- r.URL.Query()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return queryHandler
|
|
||||||
}
|
|
||||||
|
|
||||||
func (helper azureAPIHelper) queryToken(data url.Values, tenantID string) (azureToken, error) {
|
|
||||||
res, err := http.Post(fmt.Sprintf(tokenEndpoint, tenantID), "application/x-www-form-urlencoded", strings.NewReader(data.Encode()))
|
|
||||||
if err != nil {
|
|
||||||
return azureToken{}, err
|
|
||||||
}
|
|
||||||
if res.StatusCode != 200 {
|
|
||||||
return azureToken{}, errors.Errorf("error while renewing access token, status : %s", res.Status)
|
|
||||||
}
|
|
||||||
bits, err := ioutil.ReadAll(res.Body)
|
|
||||||
if err != nil {
|
|
||||||
return azureToken{}, err
|
|
||||||
}
|
|
||||||
token := azureToken{}
|
|
||||||
if err := json.Unmarshal(bits, &token); err != nil {
|
|
||||||
return azureToken{}, err
|
|
||||||
}
|
|
||||||
return token, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func toOAuthToken(token azureToken) oauth2.Token {
|
func toOAuthToken(token azureToken) oauth2.Token {
|
||||||
@ -341,44 +241,3 @@ func openbrowser(url string) {
|
|||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
|
||||||
letterRunes = []rune("abcdefghijklmnopqrstuvwxyz123456789")
|
|
||||||
)
|
|
||||||
|
|
||||||
func randomString(prefix string, length int) string {
|
|
||||||
b := make([]rune, length)
|
|
||||||
for i := range b {
|
|
||||||
b[i] = letterRunes[rand.Intn(len(letterRunes))]
|
|
||||||
}
|
|
||||||
return prefix + string(b)
|
|
||||||
}
|
|
||||||
|
|
||||||
const loginFailedHTML = `
|
|
||||||
<!DOCTYPE html>
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<meta charset="utf-8" />
|
|
||||||
<title>Login failed</title>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<h4>Some failures occurred during the authentication</h4>
|
|
||||||
<p>You can log an issue at <a href="https://github.com/azure/azure-cli/issues">Azure CLI GitHub Repository</a> and we will assist you in resolving it.</p>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
`
|
|
||||||
|
|
||||||
const successfullLoginHTML = `
|
|
||||||
<!DOCTYPE html>
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<meta charset="utf-8" />
|
|
||||||
<meta http-equiv="refresh" content="10;url=https://docs.microsoft.com/cli/azure/">
|
|
||||||
<title>Login successfully</title>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<h4>You have logged into Microsoft Azure!</h4>
|
|
||||||
<p>You can close this window, or we will redirect you to the <a href="https://docs.microsoft.com/cli/azure/">Azure CLI documents</a> in 10 seconds.</p>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
`
|
|
||||||
|
75
azure/login/loginHelper.go
Normal file
75
azure/login/loginHelper.go
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
package login
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"math/rand"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
type apiHelper interface {
|
||||||
|
queryToken(data url.Values, tenantID string) (azureToken, error)
|
||||||
|
openAzureLoginPage(redirectURL string)
|
||||||
|
queryAuthorizationAPI(authorizationURL string, authorizationHeader string) ([]byte, int, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type azureAPIHelper struct{}
|
||||||
|
|
||||||
|
func (helper azureAPIHelper) openAzureLoginPage(redirectURL string) {
|
||||||
|
state := randomString("", 10)
|
||||||
|
authURL := fmt.Sprintf(authorizeFormat, clientID, redirectURL, state, scopes)
|
||||||
|
openbrowser(authURL)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (helper azureAPIHelper) queryAuthorizationAPI(authorizationURL string, authorizationHeader string) ([]byte, int, error) {
|
||||||
|
req, err := http.NewRequest(http.MethodGet, authorizationURL, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
|
req.Header.Add("Authorization", authorizationHeader)
|
||||||
|
res, err := http.DefaultClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
|
bits, err := ioutil.ReadAll(res.Body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
|
return bits, res.StatusCode, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (helper azureAPIHelper) queryToken(data url.Values, tenantID string) (azureToken, error) {
|
||||||
|
res, err := http.Post(fmt.Sprintf(tokenEndpoint, tenantID), "application/x-www-form-urlencoded", strings.NewReader(data.Encode()))
|
||||||
|
if err != nil {
|
||||||
|
return azureToken{}, err
|
||||||
|
}
|
||||||
|
if res.StatusCode != 200 {
|
||||||
|
return azureToken{}, errors.Errorf("error while renewing access token, status : %s", res.Status)
|
||||||
|
}
|
||||||
|
bits, err := ioutil.ReadAll(res.Body)
|
||||||
|
if err != nil {
|
||||||
|
return azureToken{}, err
|
||||||
|
}
|
||||||
|
token := azureToken{}
|
||||||
|
if err := json.Unmarshal(bits, &token); err != nil {
|
||||||
|
return azureToken{}, err
|
||||||
|
}
|
||||||
|
return token, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
letterRunes = []rune("abcdefghijklmnopqrstuvwxyz123456789")
|
||||||
|
)
|
||||||
|
|
||||||
|
func randomString(prefix string, length int) string {
|
||||||
|
b := make([]rune, length)
|
||||||
|
for i := range b {
|
||||||
|
b[i] = letterRunes[rand.Intn(len(letterRunes))]
|
||||||
|
}
|
||||||
|
return prefix + string(b)
|
||||||
|
}
|
83
azure/login/logingLocalServer.go
Normal file
83
azure/login/logingLocalServer.go
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
package login
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
)
|
||||||
|
|
||||||
|
const loginFailedHTML = `
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8" />
|
||||||
|
<title>Login failed</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h4>Some failures occurred during the authentication</h4>
|
||||||
|
<p>You can log an issue at <a href="https://github.com/azure/azure-cli/issues">Azure CLI GitHub Repository</a> and we will assist you in resolving it.</p>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
`
|
||||||
|
|
||||||
|
const successfullLoginHTML = `
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8" />
|
||||||
|
<meta http-equiv="refresh" content="10;url=https://docs.microsoft.com/cli/azure/">
|
||||||
|
<title>Login successfully</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h4>You have logged into Microsoft Azure!</h4>
|
||||||
|
<p>You can close this window, or we will redirect you to the <a href="https://docs.microsoft.com/cli/azure/">Azure CLI documents</a> in 10 seconds.</p>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
`
|
||||||
|
|
||||||
|
func startLoginServer(queryCh chan url.Values) (int, error) {
|
||||||
|
mux := http.NewServeMux()
|
||||||
|
mux.HandleFunc("/", queryHandler(queryCh))
|
||||||
|
listener, err := net.Listen("tcp", ":0")
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
availablePort := listener.Addr().(*net.TCPAddr).Port
|
||||||
|
server := &http.Server{Handler: mux}
|
||||||
|
go func() {
|
||||||
|
if err := server.Serve(listener); err != nil {
|
||||||
|
queryCh <- url.Values{
|
||||||
|
"error": []string{fmt.Sprintf("error starting http server with: %v", err)},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
return availablePort, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func queryHandler(queryCh chan url.Values) func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
queryHandler := func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
_, hasCode := r.URL.Query()["code"]
|
||||||
|
if hasCode {
|
||||||
|
_, err := w.Write([]byte(successfullLoginHTML))
|
||||||
|
if err != nil {
|
||||||
|
queryCh <- url.Values{
|
||||||
|
"error": []string{err.Error()},
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
queryCh <- r.URL.Query()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
_, err := w.Write([]byte(loginFailedHTML))
|
||||||
|
if err != nil {
|
||||||
|
queryCh <- url.Values{
|
||||||
|
"error": []string{err.Error()},
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
queryCh <- r.URL.Query()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return queryHandler
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user