2018-05-06 07:15:57 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
2018-05-28 14:40:33 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2018-07-03 18:31:02 +00:00
|
|
|
"io/ioutil"
|
2018-05-06 07:15:57 +00:00
|
|
|
"net/http"
|
2018-05-28 14:40:33 +00:00
|
|
|
"net/url"
|
2018-05-06 07:15:57 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/portainer/portainer"
|
|
|
|
)
|
|
|
|
|
2018-08-21 18:40:42 +00:00
|
|
|
const (
|
|
|
|
errInvalidResponseStatus = portainer.Error("Invalid response status (expecting 200)")
|
|
|
|
)
|
|
|
|
|
2018-05-28 14:40:33 +00:00
|
|
|
// HTTPClient represents a client to send HTTP requests.
|
|
|
|
type HTTPClient struct {
|
|
|
|
*http.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewHTTPClient is used to build a new HTTPClient.
|
|
|
|
func NewHTTPClient() *HTTPClient {
|
|
|
|
return &HTTPClient{
|
|
|
|
&http.Client{
|
|
|
|
Timeout: time.Second * 5,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AzureAuthenticationResponse represents an Azure API authentication response.
|
|
|
|
type AzureAuthenticationResponse struct {
|
|
|
|
AccessToken string `json:"access_token"`
|
|
|
|
ExpiresOn string `json:"expires_on"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExecuteAzureAuthenticationRequest is used to execute an authentication request
|
|
|
|
// against the Azure API. It re-uses the same http.Client.
|
|
|
|
func (client *HTTPClient) ExecuteAzureAuthenticationRequest(credentials *portainer.AzureCredentials) (*AzureAuthenticationResponse, error) {
|
|
|
|
loginURL := fmt.Sprintf("https://login.microsoftonline.com/%s/oauth2/token", credentials.TenantID)
|
|
|
|
params := url.Values{
|
|
|
|
"grant_type": {"client_credentials"},
|
|
|
|
"client_id": {credentials.ApplicationID},
|
|
|
|
"client_secret": {credentials.AuthenticationKey},
|
|
|
|
"resource": {"https://management.azure.com/"},
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := client.PostForm(loginURL, params)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if response.StatusCode != http.StatusOK {
|
|
|
|
return nil, portainer.ErrAzureInvalidCredentials
|
|
|
|
}
|
|
|
|
|
|
|
|
var token AzureAuthenticationResponse
|
|
|
|
err = json.NewDecoder(response.Body).Decode(&token)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &token, nil
|
|
|
|
}
|
|
|
|
|
2018-07-03 18:31:02 +00:00
|
|
|
// Get executes a simple HTTP GET to the specified URL and returns
|
|
|
|
// the content of the response body.
|
|
|
|
func Get(url string) ([]byte, error) {
|
|
|
|
client := &http.Client{
|
|
|
|
Timeout: time.Second * 3,
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := client.Get(url)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
2018-08-21 18:40:42 +00:00
|
|
|
if response.StatusCode != http.StatusOK {
|
|
|
|
return nil, errInvalidResponseStatus
|
|
|
|
}
|
|
|
|
|
2018-07-03 18:31:02 +00:00
|
|
|
body, err := ioutil.ReadAll(response.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return body, nil
|
|
|
|
}
|
|
|
|
|
2018-05-06 07:15:57 +00:00
|
|
|
// ExecutePingOperation will send a SystemPing operation HTTP request to a Docker environment
|
|
|
|
// using the specified host and optional TLS configuration.
|
2018-05-28 14:40:33 +00:00
|
|
|
// It uses a new Http.Client for each operation.
|
2018-05-06 07:15:57 +00:00
|
|
|
func ExecutePingOperation(host string, tlsConfig *tls.Config) (bool, error) {
|
|
|
|
transport := &http.Transport{}
|
|
|
|
|
|
|
|
scheme := "http"
|
|
|
|
if tlsConfig != nil {
|
|
|
|
transport.TLSClientConfig = tlsConfig
|
|
|
|
scheme = "https"
|
|
|
|
}
|
|
|
|
|
|
|
|
client := &http.Client{
|
|
|
|
Timeout: time.Second * 3,
|
|
|
|
Transport: transport,
|
|
|
|
}
|
|
|
|
|
|
|
|
target := strings.Replace(host, "tcp://", scheme+"://", 1)
|
|
|
|
return pingOperation(client, target)
|
|
|
|
}
|
|
|
|
|
|
|
|
func pingOperation(client *http.Client, target string) (bool, error) {
|
|
|
|
pingOperationURL := target + "/_ping"
|
|
|
|
|
|
|
|
response, err := client.Get(pingOperationURL)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
agentOnDockerEnvironment := false
|
|
|
|
if response.Header.Get(portainer.PortainerAgentHeader) != "" {
|
|
|
|
agentOnDockerEnvironment = true
|
|
|
|
}
|
|
|
|
|
|
|
|
return agentOnDockerEnvironment, nil
|
|
|
|
}
|